blob: 683613e7355bca8bebd87f4b1ed3e67a253a9933 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
15/*
16 * Changes:
17 *
18 * Janos Farkas : delete timer on ifdown
19 * <chexum@bankinf.banki.hu>
20 * Andi Kleen : kill double kfree on module
21 * unload.
22 * Maciej W. Rozycki : FDDI support
23 * sekiya@USAGI : Don't send too many RS
24 * packets.
25 * yoshfuji@USAGI : Fixed interval between DAD
26 * packets.
27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
28 * address validation timer.
29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
30 * support.
31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
32 * address on a same interface.
33 * YOSHIFUJI Hideaki @USAGI : ARCnet support
34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
35 * seq_file.
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -080036 * YOSHIFUJI Hideaki @USAGI : improved source address
37 * selection; consider scope,
38 * status etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
40
Joe Perchesf3213832012-05-15 14:11:53 +000041#define pr_fmt(fmt) "IPv6: " fmt
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/errno.h>
44#include <linux/types.h>
Ilpo Järvinena0bffff2009-03-21 13:36:17 -070045#include <linux/kernel.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010046#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/socket.h>
48#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/net.h>
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +010050#include <linux/inet.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/in6.h>
52#include <linux/netdevice.h>
Thomas Graf18237302006-08-04 23:04:54 -070053#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/if_arp.h>
55#include <linux/if_arcnet.h>
56#include <linux/if_infiniband.h>
57#include <linux/route.h>
58#include <linux/inetdevice.h>
59#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090060#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#ifdef CONFIG_SYSCTL
62#include <linux/sysctl.h>
63#endif
Randy Dunlap4fc268d2006-01-11 12:17:47 -080064#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/delay.h>
66#include <linux/notifier.h>
Paulo Marques543537b2005-06-23 00:09:02 -070067#include <linux/string.h>
Eric Dumazetddbe5032012-07-18 08:11:12 +000068#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020070#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <net/sock.h>
72#include <net/snmp.h>
73
Alexander Aring5241c2d2015-12-14 20:55:22 +010074#include <net/6lowpan.h>
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +000075#include <net/firewire.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <net/ipv6.h>
77#include <net/protocol.h>
78#include <net/ndisc.h>
79#include <net/ip6_route.h>
80#include <net/addrconf.h>
81#include <net/tcp.h>
82#include <net/ip.h>
Thomas Graf5d620262006-08-15 00:35:02 -070083#include <net/netlink.h>
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -070084#include <net/pkt_sched.h>
David Ahernca254492015-10-12 11:47:10 -070085#include <net/l3mdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include <linux/if_tunnel.h>
87#include <linux/rtnetlink.h>
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +000088#include <linux/netconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <linux/random.h>
Stephen Hemmingere21e8462010-03-20 16:09:01 -070090#include <linux/uaccess.h>
Herbert Xu7f7d9a62007-04-24 21:54:09 -070091#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93#include <linux/proc_fs.h>
94#include <linux/seq_file.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040095#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#define INFINITY_LIFE_TIME 0xFFFFFFFF
Thomas Graf18a31e12010-11-17 04:12:02 +000098
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +010099#define IPV6_MAX_STRLEN \
100 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
101
Thomas Graf18a31e12010-11-17 04:12:02 +0000102static inline u32 cstamp_delta(unsigned long cstamp)
103{
104 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
105}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700107static inline s32 rfc3315_s14_backoff_init(s32 irt)
108{
109 /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
110 u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
111 do_div(tmp, 1000000);
112 return (s32)tmp;
113}
114
115static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
116{
117 /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
118 u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
119 do_div(tmp, 1000000);
120 if ((s32)tmp > mrt) {
121 /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
122 tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
123 do_div(tmp, 1000000);
124 }
125 return (s32)tmp;
126}
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#ifdef CONFIG_SYSCTL
WANG Conga317a2f2014-07-25 15:25:09 -0700129static int addrconf_sysctl_register(struct inet6_dev *idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800130static void addrconf_sysctl_unregister(struct inet6_dev *idev);
131#else
WANG Conga317a2f2014-07-25 15:25:09 -0700132static inline int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800133{
WANG Conga317a2f2014-07-25 15:25:09 -0700134 return 0;
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800135}
136
137static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
138{
139}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#endif
141
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200142static void ipv6_regen_rndid(struct inet6_dev *idev);
143static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900145static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
Eric Dumazetd9bf82c2017-10-07 19:30:24 -0700146static int ipv6_count_addresses(const struct inet6_dev *idev);
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +0100147static int ipv6_generate_stable_address(struct in6_addr *addr,
148 u8 dad_count,
149 const struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Eric Dumazet27c565a2017-11-04 08:53:27 -0700151#define IN6_ADDR_HSIZE_SHIFT 8
152#define IN6_ADDR_HSIZE (1 << IN6_ADDR_HSIZE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
154 * Configured unicast address hash table
155 */
stephen hemmingerc2e21292010-03-17 20:31:10 +0000156static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
stephen hemminger5c578aed2010-03-17 20:31:11 +0000157static DEFINE_SPINLOCK(addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100159static void addrconf_verify(void);
160static void addrconf_verify_rtnl(void);
161static void addrconf_verify_work(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100163static struct workqueue_struct *addrconf_wq;
164static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
167static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
168
Jiri Pirko93d9b7d2010-03-10 10:28:56 +0000169static void addrconf_type_change(struct net_device *dev,
170 unsigned long event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171static int addrconf_ifdown(struct net_device *dev, int how);
172
David Ahern8d1c8022018-04-17 17:33:26 -0700173static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
Romain Kuntz21caa662013-01-09 21:06:03 +0000174 int plen,
175 const struct net_device *dev,
David Ahern2b2450c2019-03-27 20:53:52 -0700176 u32 flags, u32 noflags,
177 bool no_gw);
Romain Kuntz21caa662013-01-09 21:06:03 +0000178
David S. Millercf22f9a2012-04-14 21:37:40 -0400179static void addrconf_dad_start(struct inet6_ifaddr *ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100180static void addrconf_dad_work(struct work_struct *w);
David Ahernc76fe2d2018-01-25 20:16:29 -0800181static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
182 bool send_na);
Hangbin Liu896585d2018-11-21 21:52:33 +0800183static void addrconf_dad_run(struct inet6_dev *idev, bool restart);
Kees Cooke99e88a2017-10-16 14:43:17 -0700184static void addrconf_rs_timer(struct timer_list *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
186static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
187
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900188static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 struct prefix_info *pinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Adrian Bunk888c8482008-07-22 14:21:58 -0700191static struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 .forwarding = 0,
193 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
194 .mtu6 = IPV6_MIN_MTU,
195 .accept_ra = 1,
196 .accept_redirects = 1,
197 .autoconf = 1,
198 .force_mld_version = 0,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200199 .mldv1_unsolicited_report_interval = 10 * HZ,
200 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 .dad_transmits = 1,
202 .rtr_solicits = MAX_RTR_SOLICITATIONS,
203 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700204 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Ian Morris67ba4152014-08-24 21:53:10 +0100206 .use_tempaddr = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 .temp_valid_lft = TEMP_VALID_LIFETIME,
208 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
209 .regen_max_retry = REGEN_MAX_RETRY,
210 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800212 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700213 .accept_ra_from_local = 0,
Hangbin Liu8013d1d2015-07-30 14:28:42 +0800214 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800215 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800216#ifdef CONFIG_IPV6_ROUTER_PREF
217 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800218 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800219#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzbbea1242017-03-22 18:19:04 +0900220 .accept_ra_rt_info_min_plen = 0,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800221 .accept_ra_rt_info_max_plen = 0,
222#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800223#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700224 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700225 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900226 .disable_ipv6 = 0,
Nicolas Dichtel09400952017-11-14 14:21:32 +0100227 .accept_dad = 0,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200228 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700229 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100230 .stable_secret = {
231 .initialized = false,
Erik Kline3985e8a2015-07-22 16:38:25 +0900232 },
233 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400234 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800235 .keep_addr_on_down = 0,
David Lebrun1ababeb2016-11-08 14:57:39 +0100236 .seg6_enabled = 0,
David Lebrunbf355b82016-11-08 14:57:42 +0100237#ifdef CONFIG_IPV6_SEG6_HMAC
238 .seg6_require_hmac = 0,
239#endif
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800240 .enhanced_dad = 1,
Felix Jiad35a00b2017-01-26 16:59:17 +1300241 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
David Forsterdf789fe2017-02-23 16:27:18 +0000242 .disable_policy = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243};
244
Brian Haleyab32ea52006-09-22 14:15:41 -0700245static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 .forwarding = 0,
247 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
248 .mtu6 = IPV6_MIN_MTU,
249 .accept_ra = 1,
250 .accept_redirects = 1,
251 .autoconf = 1,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200252 .force_mld_version = 0,
253 .mldv1_unsolicited_report_interval = 10 * HZ,
254 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 .dad_transmits = 1,
256 .rtr_solicits = MAX_RTR_SOLICITATIONS,
257 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700258 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 .use_tempaddr = 0,
261 .temp_valid_lft = TEMP_VALID_LIFETIME,
262 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
263 .regen_max_retry = REGEN_MAX_RETRY,
264 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800266 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700267 .accept_ra_from_local = 0,
Hangbin Liu8013d1d2015-07-30 14:28:42 +0800268 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800269 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800270#ifdef CONFIG_IPV6_ROUTER_PREF
271 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800272 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800273#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzbbea1242017-03-22 18:19:04 +0900274 .accept_ra_rt_info_min_plen = 0,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800275 .accept_ra_rt_info_max_plen = 0,
276#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800277#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700278 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700279 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900280 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900281 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200282 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700283 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100284 .stable_secret = {
285 .initialized = false,
286 },
Erik Kline3985e8a2015-07-22 16:38:25 +0900287 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400288 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800289 .keep_addr_on_down = 0,
David Lebrun1ababeb2016-11-08 14:57:39 +0100290 .seg6_enabled = 0,
David Lebrunbf355b82016-11-08 14:57:42 +0100291#ifdef CONFIG_IPV6_SEG6_HMAC
292 .seg6_require_hmac = 0,
293#endif
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800294 .enhanced_dad = 1,
Felix Jiad35a00b2017-01-26 16:59:17 +1300295 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
David Forsterdf789fe2017-02-23 16:27:18 +0000296 .disable_policy = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297};
298
Mike Manning1f372c72017-09-25 22:01:36 +0100299/* Check if link is ready: is it up and is a valid qdisc available */
300static inline bool addrconf_link_ready(const struct net_device *dev)
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700301{
Mike Manning1f372c72017-09-25 22:01:36 +0100302 return netif_oper_up(dev) && !qdisc_tx_is_noop(dev);
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700303}
304
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200305static void addrconf_del_rs_timer(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200307 if (del_timer(&idev->rs_timer))
308 __in6_dev_put(idev);
309}
310
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100311static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200312{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100313 if (cancel_delayed_work(&ifp->dad_work))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 __in6_ifa_put(ifp);
315}
316
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200317static void addrconf_mod_rs_timer(struct inet6_dev *idev,
318 unsigned long when)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200320 if (!timer_pending(&idev->rs_timer))
321 in6_dev_hold(idev);
322 mod_timer(&idev->rs_timer, jiffies + when);
323}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100325static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
326 unsigned long delay)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200327{
Xin Longf8a894b2017-06-15 16:33:58 +0800328 in6_ifa_hold(ifp);
329 if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay))
330 in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700333static int snmp6_alloc_dev(struct inet6_dev *idev)
334{
John Stultz827da442013-10-07 15:51:58 -0700335 int i;
336
WANG Cong698365f2014-05-05 15:55:55 -0700337 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
338 if (!idev->stats.ipv6)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700339 goto err_ip;
John Stultz827da442013-10-07 15:51:58 -0700340
341 for_each_possible_cpu(i) {
342 struct ipstats_mib *addrconf_stats;
WANG Cong698365f2014-05-05 15:55:55 -0700343 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
John Stultz827da442013-10-07 15:51:58 -0700344 u64_stats_init(&addrconf_stats->syncp);
John Stultz827da442013-10-07 15:51:58 -0700345 }
346
347
Eric Dumazetbe281e52011-05-19 01:14:23 +0000348 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
349 GFP_KERNEL);
350 if (!idev->stats.icmpv6dev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700351 goto err_icmp;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000352 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
353 GFP_KERNEL);
354 if (!idev->stats.icmpv6msgdev)
David L Stevens14878f72007-09-16 16:52:35 -0700355 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700356
357 return 0;
358
David L Stevens14878f72007-09-16 16:52:35 -0700359err_icmpmsg:
Eric Dumazetbe281e52011-05-19 01:14:23 +0000360 kfree(idev->stats.icmpv6dev);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700361err_icmp:
WANG Cong698365f2014-05-05 15:55:55 -0700362 free_percpu(idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700363err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700364 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700365}
366
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000367static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 struct inet6_dev *ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700370 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 ASSERT_RTNL();
373
374 if (dev->mtu < IPV6_MIN_MTU)
WANG Conga317a2f2014-07-25 15:25:09 -0700375 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900377 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100378 if (!ndev)
WANG Conga317a2f2014-07-25 15:25:09 -0700379 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Ingo Oeser322f74a2006-03-20 23:01:47 -0800381 rwlock_init(&ndev->lock);
382 ndev->dev = dev;
stephen hemminger502a2ff2010-03-17 20:31:13 +0000383 INIT_LIST_HEAD(&ndev->addr_list);
Kees Cooke99e88a2017-10-16 14:43:17 -0700384 timer_setup(&ndev->rs_timer, addrconf_rs_timer, 0);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900385 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100386
387 if (ndev->cnf.stable_secret.initialized)
Felix Jiad35a00b2017-01-26 16:59:17 +1300388 ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100389
Ingo Oeser322f74a2006-03-20 23:01:47 -0800390 ndev->cnf.mtu6 = dev->mtu;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800391 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
Ian Morris63159f22015-03-29 14:00:04 +0100392 if (!ndev->nd_parms) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800393 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700394 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800395 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700396 if (ndev->cnf.forwarding)
397 dev_disable_lro(dev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800398 /* We refer to the device */
399 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Ingo Oeser322f74a2006-03-20 23:01:47 -0800401 if (snmp6_alloc_dev(ndev) < 0) {
Joe Perchese32ac252018-03-26 08:35:01 -0700402 netdev_dbg(dev, "%s: cannot allocate memory for statistics\n",
403 __func__);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800404 neigh_parms_release(&nd_tbl, ndev->nd_parms);
Roy Li8603e332011-09-20 15:10:16 -0400405 dev_put(dev);
406 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700407 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Ingo Oeser322f74a2006-03-20 23:01:47 -0800410 if (snmp6_register_dev(ndev) < 0) {
Joe Perchese32ac252018-03-26 08:35:01 -0700411 netdev_dbg(dev, "%s: cannot create /proc/net/dev_snmp6/%s\n",
412 __func__, dev->name);
WANG Conga317a2f2014-07-25 15:25:09 -0700413 goto err_release;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200416 /* One reference from device. */
Reshetova, Elena1be92462017-07-04 09:34:55 +0300417 refcount_set(&ndev->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900419 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
420 ndev->cnf.accept_dad = -1;
421
Amerigo Wang07a93622012-10-29 16:23:10 +0000422#if IS_ENABLED(CONFIG_IPV6_SIT)
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700423 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
Joe Perchesf3213832012-05-15 14:11:53 +0000424 pr_info("%s: Disabled Multicast RS\n", dev->name);
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700425 ndev->cnf.rtr_solicits = 0;
426 }
427#endif
428
stephen hemminger372e6c82010-03-17 20:31:09 +0000429 INIT_LIST_HEAD(&ndev->tempaddr_list);
Jiri Bohac76506a92016-10-13 18:52:15 +0200430 ndev->desync_factor = U32_MAX;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800431 if ((dev->flags&IFF_LOOPBACK) ||
432 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700433 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700434 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700435 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800436 ndev->cnf.use_tempaddr = -1;
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200437 } else
438 ipv6_regen_rndid(ndev);
David S. Miller5d9efa72013-10-28 20:07:50 -0400439
Daniel Borkmann914faa12013-04-09 03:47:14 +0000440 ndev->token = in6addr_any;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800441
Mike Manning1f372c72017-09-25 22:01:36 +0100442 if (netif_running(dev) && addrconf_link_ready(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700443 ndev->if_flags |= IF_READY;
444
Ingo Oeser322f74a2006-03-20 23:01:47 -0800445 ipv6_mc_init_dev(ndev);
446 ndev->tstamp = jiffies;
WANG Conga317a2f2014-07-25 15:25:09 -0700447 err = addrconf_sysctl_register(ndev);
448 if (err) {
449 ipv6_mc_destroy_dev(ndev);
Sabrina Dubroca2a189f9e2015-11-04 14:47:53 +0100450 snmp6_unregister_dev(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700451 goto err_release;
452 }
David L Stevens30c4cf52007-01-04 12:31:14 -0800453 /* protected by rtnl_lock */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000454 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800455
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000456 /* Join interface-local all-node multicast group */
457 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
458
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800459 /* Join all-node multicast group */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900460 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800461
Li Weid6ddef92012-03-05 14:45:17 +0000462 /* Join all-router multicast group if forwarding is set */
Li Wei8b2aaed2012-03-07 14:58:07 +0000463 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
Li Weid6ddef92012-03-05 14:45:17 +0000464 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700467
468err_release:
469 neigh_parms_release(&nd_tbl, ndev->nd_parms);
470 ndev->dead = 1;
471 in6_dev_finish_destroy(ndev);
472 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000475static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 struct inet6_dev *idev;
478
479 ASSERT_RTNL();
480
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700481 idev = __in6_dev_get(dev);
482 if (!idev) {
483 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -0700484 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return NULL;
486 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (dev->flags&IFF_UP)
489 ipv6_mc_up(idev);
490 return idev;
491}
492
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000493static int inet6_netconf_msgsize_devconf(int type)
494{
495 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
496 + nla_total_size(4); /* NETCONFA_IFINDEX */
Zhang Shengju136ba622016-03-10 08:55:50 +0000497 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000498
Zhang Shengju136ba622016-03-10 08:55:50 +0000499 if (type == NETCONFA_ALL)
500 all = true;
501
502 if (all || type == NETCONFA_FORWARDING)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000503 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500504#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000505 if (all || type == NETCONFA_MC_FORWARDING)
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000506 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500507#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000508 if (all || type == NETCONFA_PROXY_NEIGH)
stephen hemmingerc92d5492013-12-17 22:37:14 -0800509 size += nla_total_size(4);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000510
Zhang Shengju136ba622016-03-10 08:55:50 +0000511 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
Andy Gospodarek35103d12015-08-13 10:39:01 -0400512 size += nla_total_size(4);
513
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000514 return size;
515}
516
517static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
518 struct ipv6_devconf *devconf, u32 portid,
519 u32 seq, int event, unsigned int flags,
520 int type)
521{
522 struct nlmsghdr *nlh;
523 struct netconfmsg *ncm;
Zhang Shengju136ba622016-03-10 08:55:50 +0000524 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000525
526 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
527 flags);
Ian Morris63159f22015-03-29 14:00:04 +0100528 if (!nlh)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000529 return -EMSGSIZE;
530
Zhang Shengju136ba622016-03-10 08:55:50 +0000531 if (type == NETCONFA_ALL)
532 all = true;
533
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000534 ncm = nlmsg_data(nlh);
535 ncm->ncm_family = AF_INET6;
536
537 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
538 goto nla_put_failure;
539
David Ahern23452172017-03-28 14:28:05 -0700540 if (!devconf)
541 goto out;
542
Zhang Shengju136ba622016-03-10 08:55:50 +0000543 if ((all || type == NETCONFA_FORWARDING) &&
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000544 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
545 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500546#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000547 if ((all || type == NETCONFA_MC_FORWARDING) &&
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000548 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
549 devconf->mc_forwarding) < 0)
550 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500551#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000552 if ((all || type == NETCONFA_PROXY_NEIGH) &&
stephen hemmingerc92d5492013-12-17 22:37:14 -0800553 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
554 goto nla_put_failure;
555
Zhang Shengju136ba622016-03-10 08:55:50 +0000556 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
Andy Gospodarek35103d12015-08-13 10:39:01 -0400557 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
558 devconf->ignore_routes_with_linkdown) < 0)
559 goto nla_put_failure;
560
David Ahern23452172017-03-28 14:28:05 -0700561out:
Johannes Berg053c0952015-01-16 22:09:00 +0100562 nlmsg_end(skb, nlh);
563 return 0;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000564
565nla_put_failure:
566 nlmsg_cancel(skb, nlh);
567 return -EMSGSIZE;
568}
569
David Ahern85b3daa2017-03-28 14:28:04 -0700570void inet6_netconf_notify_devconf(struct net *net, int event, int type,
571 int ifindex, struct ipv6_devconf *devconf)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000572{
573 struct sk_buff *skb;
574 int err = -ENOBUFS;
575
Eric Dumazet927265b2016-07-08 05:46:04 +0200576 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100577 if (!skb)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000578 goto errout;
579
580 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
David Ahern85b3daa2017-03-28 14:28:04 -0700581 event, 0, type);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000582 if (err < 0) {
583 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
584 WARN_ON(err == -EMSGSIZE);
585 kfree_skb(skb);
586 goto errout;
587 }
Eric Dumazet927265b2016-07-08 05:46:04 +0200588 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000589 return;
590errout:
Cong Dingbd779022012-12-18 12:08:56 +0000591 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000592}
593
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000594static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
595 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
596 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
stephen hemmingerc92d5492013-12-17 22:37:14 -0800597 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
Andy Gospodarek35103d12015-08-13 10:39:01 -0400598 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000599};
600
Jakub Kicinski38d51812019-01-18 10:46:22 -0800601static int inet6_netconf_valid_get_req(struct sk_buff *skb,
602 const struct nlmsghdr *nlh,
603 struct nlattr **tb,
604 struct netlink_ext_ack *extack)
605{
606 int i, err;
607
608 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(struct netconfmsg))) {
609 NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf get request");
610 return -EINVAL;
611 }
612
613 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +0200614 return nlmsg_parse_deprecated(nlh, sizeof(struct netconfmsg),
615 tb, NETCONFA_MAX,
616 devconf_ipv6_policy, extack);
Jakub Kicinski38d51812019-01-18 10:46:22 -0800617
Johannes Berg8cb08172019-04-26 14:07:28 +0200618 err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct netconfmsg),
619 tb, NETCONFA_MAX,
620 devconf_ipv6_policy, extack);
Jakub Kicinski38d51812019-01-18 10:46:22 -0800621 if (err)
622 return err;
623
624 for (i = 0; i <= NETCONFA_MAX; i++) {
625 if (!tb[i])
626 continue;
627
628 switch (i) {
629 case NETCONFA_IFINDEX:
630 break;
631 default:
632 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in netconf get request");
633 return -EINVAL;
634 }
635 }
636
637 return 0;
638}
639
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000640static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
David Ahernc21ef3e2017-04-16 09:48:24 -0700641 struct nlmsghdr *nlh,
642 struct netlink_ext_ack *extack)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000643{
644 struct net *net = sock_net(in_skb->sk);
645 struct nlattr *tb[NETCONFA_MAX+1];
Florian Westphal4ea26072017-10-11 10:28:00 +0200646 struct inet6_dev *in6_dev = NULL;
647 struct net_device *dev = NULL;
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000648 struct sk_buff *skb;
649 struct ipv6_devconf *devconf;
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000650 int ifindex;
651 int err;
652
Jakub Kicinski38d51812019-01-18 10:46:22 -0800653 err = inet6_netconf_valid_get_req(in_skb, nlh, tb, extack);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000654 if (err < 0)
Florian Westphal4ea26072017-10-11 10:28:00 +0200655 return err;
656
657 if (!tb[NETCONFA_IFINDEX])
658 return -EINVAL;
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000659
Anton Protopopova97eb332016-02-16 21:43:16 -0500660 err = -EINVAL;
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000661 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
662 switch (ifindex) {
663 case NETCONFA_IFINDEX_ALL:
664 devconf = net->ipv6.devconf_all;
665 break;
666 case NETCONFA_IFINDEX_DEFAULT:
667 devconf = net->ipv6.devconf_dflt;
668 break;
669 default:
Florian Westphal4ea26072017-10-11 10:28:00 +0200670 dev = dev_get_by_index(net, ifindex);
Ian Morris63159f22015-03-29 14:00:04 +0100671 if (!dev)
Florian Westphal4ea26072017-10-11 10:28:00 +0200672 return -EINVAL;
673 in6_dev = in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +0100674 if (!in6_dev)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000675 goto errout;
676 devconf = &in6_dev->cnf;
677 break;
678 }
679
680 err = -ENOBUFS;
Florian Westphal4ea26072017-10-11 10:28:00 +0200681 skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100682 if (!skb)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000683 goto errout;
684
685 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
686 NETLINK_CB(in_skb).portid,
687 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
Zhang Shengju136ba622016-03-10 08:55:50 +0000688 NETCONFA_ALL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000689 if (err < 0) {
690 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
691 WARN_ON(err == -EMSGSIZE);
692 kfree_skb(skb);
693 goto errout;
694 }
695 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
696errout:
Florian Westphal4ea26072017-10-11 10:28:00 +0200697 if (in6_dev)
698 in6_dev_put(in6_dev);
699 if (dev)
700 dev_put(dev);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000701 return err;
702}
703
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000704static int inet6_netconf_dump_devconf(struct sk_buff *skb,
705 struct netlink_callback *cb)
706{
David Ahernaddd3832018-10-07 20:16:41 -0700707 const struct nlmsghdr *nlh = cb->nlh;
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000708 struct net *net = sock_net(skb->sk);
709 int h, s_h;
710 int idx, s_idx;
711 struct net_device *dev;
712 struct inet6_dev *idev;
713 struct hlist_head *head;
714
David Ahernaddd3832018-10-07 20:16:41 -0700715 if (cb->strict_check) {
716 struct netlink_ext_ack *extack = cb->extack;
717 struct netconfmsg *ncm;
718
719 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ncm))) {
720 NL_SET_ERR_MSG_MOD(extack, "Invalid header for netconf dump request");
721 return -EINVAL;
722 }
723
724 if (nlmsg_attrlen(nlh, sizeof(*ncm))) {
725 NL_SET_ERR_MSG_MOD(extack, "Invalid data after header in netconf dump request");
726 return -EINVAL;
727 }
728 }
729
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000730 s_h = cb->args[0];
731 s_idx = idx = cb->args[1];
732
733 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
734 idx = 0;
735 head = &net->dev_index_head[h];
736 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000737 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
738 net->dev_base_seq;
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000739 hlist_for_each_entry_rcu(dev, head, index_hlist) {
740 if (idx < s_idx)
741 goto cont;
742 idev = __in6_dev_get(dev);
743 if (!idev)
744 goto cont;
745
746 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
747 &idev->cnf,
748 NETLINK_CB(cb->skb).portid,
David Ahernaddd3832018-10-07 20:16:41 -0700749 nlh->nlmsg_seq,
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000750 RTM_NEWNETCONF,
751 NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000752 NETCONFA_ALL) < 0) {
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000753 rcu_read_unlock();
754 goto done;
755 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000756 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000757cont:
758 idx++;
759 }
760 rcu_read_unlock();
761 }
762 if (h == NETDEV_HASHENTRIES) {
763 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
764 net->ipv6.devconf_all,
765 NETLINK_CB(cb->skb).portid,
David Ahernaddd3832018-10-07 20:16:41 -0700766 nlh->nlmsg_seq,
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000767 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000768 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000769 goto done;
770 else
771 h++;
772 }
773 if (h == NETDEV_HASHENTRIES + 1) {
774 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
775 net->ipv6.devconf_dflt,
776 NETLINK_CB(cb->skb).portid,
David Ahernaddd3832018-10-07 20:16:41 -0700777 nlh->nlmsg_seq,
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000778 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000779 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000780 goto done;
781 else
782 h++;
783 }
784done:
785 cb->args[0] = h;
786 cb->args[1] = idx;
787
788 return skb->len;
789}
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791#ifdef CONFIG_SYSCTL
792static void dev_forward_change(struct inet6_dev *idev)
793{
794 struct net_device *dev;
795 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 if (!idev)
798 return;
799 dev = idev->dev;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700800 if (idev->cnf.forwarding)
801 dev_disable_lro(dev);
Amerigo Wang1a940832012-10-28 17:43:53 +0000802 if (dev->flags & IFF_MULTICAST) {
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000803 if (idev->cnf.forwarding) {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900804 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000805 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
806 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
807 } else {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900808 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000809 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
810 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
stephen hemminger502a2ff2010-03-17 20:31:13 +0000813
814 list_for_each_entry(ifa, &idev->addr_list, if_list) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800815 if (ifa->flags&IFA_F_TENTATIVE)
816 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (idev->cnf.forwarding)
818 addrconf_join_anycast(ifa);
819 else
820 addrconf_leave_anycast(ifa);
821 }
David Ahern85b3daa2017-03-28 14:28:04 -0700822 inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
823 NETCONFA_FORWARDING,
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000824 dev->ifindex, &idev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
827
Pavel Emelyanove1869322008-01-10 17:43:50 -0800828static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 struct net_device *dev;
831 struct inet6_dev *idev;
832
Ben Hutchings4acd4942012-08-14 08:54:51 +0000833 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 idev = __in6_dev_get(dev);
835 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800836 int changed = (!idev->cnf.forwarding) ^ (!newf);
837 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (changed)
839 dev_forward_change(idev);
840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800843
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000844static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800845{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800846 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000847 int old;
848
849 if (!rtnl_trylock())
850 return restart_syscall();
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800851
852 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000853 old = *p;
854 *p = newf;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800855
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000856 if (p == &net->ipv6.devconf_dflt->forwarding) {
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000857 if ((!newf) ^ (!old))
David Ahern85b3daa2017-03-28 14:28:04 -0700858 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
859 NETCONFA_FORWARDING,
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000860 NETCONFA_IFINDEX_DEFAULT,
861 net->ipv6.devconf_dflt);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000862 rtnl_unlock();
863 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +0000864 }
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000865
Pavel Emelyanove1869322008-01-10 17:43:50 -0800866 if (p == &net->ipv6.devconf_all->forwarding) {
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200867 int old_dflt = net->ipv6.devconf_dflt->forwarding;
868
Pavel Emelyanove1869322008-01-10 17:43:50 -0800869 net->ipv6.devconf_dflt->forwarding = newf;
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200870 if ((!newf) ^ (!old_dflt))
David Ahern85b3daa2017-03-28 14:28:04 -0700871 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
872 NETCONFA_FORWARDING,
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200873 NETCONFA_IFINDEX_DEFAULT,
874 net->ipv6.devconf_dflt);
875
Pavel Emelyanove1869322008-01-10 17:43:50 -0800876 addrconf_forward_change(net, newf);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000877 if ((!newf) ^ (!old))
David Ahern85b3daa2017-03-28 14:28:04 -0700878 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
879 NETCONFA_FORWARDING,
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000880 NETCONFA_IFINDEX_ALL,
881 net->ipv6.devconf_all);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000882 } else if ((!newf) ^ (!old))
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800883 dev_forward_change((struct inet6_dev *)table->extra1);
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700884 rtnl_unlock();
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800885
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000886 if (newf)
Daniel Lezcano7b4da532008-03-04 13:47:14 -0800887 rt6_purge_dflt_routers(net);
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000888 return 1;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800889}
Andy Gospodarek35103d12015-08-13 10:39:01 -0400890
891static void addrconf_linkdown_change(struct net *net, __s32 newf)
892{
893 struct net_device *dev;
894 struct inet6_dev *idev;
895
896 for_each_netdev(net, dev) {
897 idev = __in6_dev_get(dev);
898 if (idev) {
899 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
900
901 idev->cnf.ignore_routes_with_linkdown = newf;
902 if (changed)
903 inet6_netconf_notify_devconf(dev_net(dev),
David Ahern85b3daa2017-03-28 14:28:04 -0700904 RTM_NEWNETCONF,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400905 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
906 dev->ifindex,
907 &idev->cnf);
908 }
909 }
910}
911
912static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
913{
914 struct net *net;
915 int old;
916
917 if (!rtnl_trylock())
918 return restart_syscall();
919
920 net = (struct net *)table->extra2;
921 old = *p;
922 *p = newf;
923
924 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
925 if ((!newf) ^ (!old))
926 inet6_netconf_notify_devconf(net,
David Ahern85b3daa2017-03-28 14:28:04 -0700927 RTM_NEWNETCONF,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400928 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
929 NETCONFA_IFINDEX_DEFAULT,
930 net->ipv6.devconf_dflt);
931 rtnl_unlock();
932 return 0;
933 }
934
935 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
936 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
937 addrconf_linkdown_change(net, newf);
938 if ((!newf) ^ (!old))
939 inet6_netconf_notify_devconf(net,
David Ahern85b3daa2017-03-28 14:28:04 -0700940 RTM_NEWNETCONF,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400941 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
942 NETCONFA_IFINDEX_ALL,
943 net->ipv6.devconf_all);
944 }
945 rtnl_unlock();
946
947 return 1;
948}
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950#endif
951
stephen hemminger5c578aed2010-03-17 20:31:11 +0000952/* Nobody refers to this ifaddr, destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
954{
stephen hemmingerc2e21292010-03-17 20:31:10 +0000955 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957#ifdef NET_REFCNT_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +0000958 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959#endif
960
961 in6_dev_put(ifp->idev);
962
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100963 if (cancel_delayed_work(&ifp->dad_work))
964 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
965 ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Herbert Xue9d3e082010-05-18 15:36:06 -0700967 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
Joe Perchesf3213832012-05-15 14:11:53 +0000968 pr_warn("Freeing alive inet6 address %p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return;
970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Lai Jiangshane5785982011-03-15 18:00:14 +0800972 kfree_rcu(ifp, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Brian Haleye55ffac2006-07-10 15:25:51 -0700975static void
976ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
977{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000978 struct list_head *p;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700979 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700980
981 /*
982 * Each device address list is sorted in order of scope -
983 * global before linklocal.
984 */
stephen hemminger502a2ff2010-03-17 20:31:13 +0000985 list_for_each(p, &idev->addr_list) {
986 struct inet6_ifaddr *ifa
987 = list_entry(p, struct inet6_ifaddr, if_list);
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700988 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700989 break;
990 }
991
Eric Dumazet8ef802a2017-10-07 19:30:23 -0700992 list_add_tail_rcu(&ifp->if_list, p);
Brian Haleye55ffac2006-07-10 15:25:51 -0700993}
994
Eric Dumazet3f27fb22017-10-23 16:17:47 -0700995static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr)
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900996{
Eric Dumazet3f27fb22017-10-23 16:17:47 -0700997 u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net);
998
999 return hash_32(val, IN6_ADDR_HSIZE_SHIFT);
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +09001000}
1001
Eric Dumazet56fc7092017-10-23 16:17:45 -07001002static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
Eric Dumazet752a9292017-10-23 16:17:46 -07001003 struct net_device *dev, unsigned int hash)
Eric Dumazet56fc7092017-10-23 16:17:45 -07001004{
Eric Dumazet56fc7092017-10-23 16:17:45 -07001005 struct inet6_ifaddr *ifp;
1006
1007 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
1008 if (!net_eq(dev_net(ifp->idev->dev), net))
1009 continue;
1010 if (ipv6_addr_equal(&ifp->addr, addr)) {
1011 if (!dev || ifp->idev->dev == dev)
1012 return true;
1013 }
1014 }
1015 return false;
1016}
1017
David Ahernf3d98322017-10-18 09:56:52 -07001018static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa)
1019{
Eric Dumazet3f27fb22017-10-23 16:17:47 -07001020 unsigned int hash = inet6_addr_hash(dev_net(dev), &ifa->addr);
David Ahernf3d98322017-10-18 09:56:52 -07001021 int err = 0;
1022
1023 spin_lock(&addrconf_hash_lock);
1024
1025 /* Ignore adding duplicate addresses on an interface */
Eric Dumazet752a9292017-10-23 16:17:46 -07001026 if (ipv6_chk_same_addr(dev_net(dev), &ifa->addr, dev, hash)) {
Joe Perchese32ac252018-03-26 08:35:01 -07001027 netdev_dbg(dev, "ipv6_add_addr: already assigned\n");
David Ahernf3d98322017-10-18 09:56:52 -07001028 err = -EEXIST;
Eric Dumazet752a9292017-10-23 16:17:46 -07001029 } else {
1030 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
David Ahernf3d98322017-10-18 09:56:52 -07001031 }
1032
David Ahernf3d98322017-10-18 09:56:52 -07001033 spin_unlock(&addrconf_hash_lock);
1034
1035 return err;
1036}
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038/* On success it returns ifp with increased reference count */
1039
1040static struct inet6_ifaddr *
David Aherne6464b82018-05-27 08:09:53 -07001041ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg,
David Ahernde95e042017-10-18 09:56:54 -07001042 bool can_block, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
David Ahernf3d98322017-10-18 09:56:52 -07001044 gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC;
David Aherne6464b82018-05-27 08:09:53 -07001045 int addr_type = ipv6_addr_type(cfg->pfx);
David Forsterdf789fe2017-02-23 16:27:18 +00001046 struct net *net = dev_net(idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 struct inet6_ifaddr *ifa = NULL;
David Ahern360a9882018-04-18 15:39:00 -07001048 struct fib6_info *f6i = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 int err = 0;
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +09001050
1051 if (addr_type == IPV6_ADDR_ANY ||
1052 addr_type & IPV6_ADDR_MULTICAST ||
1053 (!(idev->dev->flags & IFF_LOOPBACK) &&
Robert Shearman3ede0bb2018-09-19 13:56:53 +01001054 !netif_is_l3_master(idev->dev) &&
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +09001055 addr_type & IPV6_ADDR_LOOPBACK))
1056 return ERR_PTR(-EADDRNOTAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (idev->dead) {
1059 err = -ENODEV; /*XXX*/
David Ahernf3d98322017-10-18 09:56:52 -07001060 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062
Brian Haley56d417b2009-06-01 03:07:33 -07001063 if (idev->cnf.disable_ipv6) {
Brian Haley9bdd8d42009-03-18 18:22:48 -07001064 err = -EACCES;
David Ahernf3d98322017-10-18 09:56:52 -07001065 goto out;
Brian Haley9bdd8d42009-03-18 18:22:48 -07001066 }
1067
David Ahernff7883e2017-10-18 09:56:53 -07001068 /* validator notifier needs to be blocking;
1069 * do not call in atomic context
1070 */
1071 if (can_block) {
1072 struct in6_validator_info i6vi = {
David Aherne6464b82018-05-27 08:09:53 -07001073 .i6vi_addr = *cfg->pfx,
David Ahernff7883e2017-10-18 09:56:53 -07001074 .i6vi_dev = idev,
David Ahernde95e042017-10-18 09:56:54 -07001075 .extack = extack,
David Ahernff7883e2017-10-18 09:56:53 -07001076 };
1077
1078 err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi);
1079 err = notifier_to_errno(err);
1080 if (err < 0)
1081 goto out;
1082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
David Ahernf3d98322017-10-18 09:56:52 -07001084 ifa = kzalloc(sizeof(*ifa), gfp_flags);
Ian Morris63159f22015-03-29 14:00:04 +01001085 if (!ifa) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 err = -ENOBUFS;
1087 goto out;
1088 }
1089
David Aherne6464b82018-05-27 08:09:53 -07001090 f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags);
David Ahern360a9882018-04-18 15:39:00 -07001091 if (IS_ERR(f6i)) {
1092 err = PTR_ERR(f6i);
1093 f6i = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 goto out;
1095 }
1096
David Forsterdf789fe2017-02-23 16:27:18 +00001097 if (net->ipv6.devconf_all->disable_policy ||
1098 idev->cnf.disable_policy)
David Ahern360a9882018-04-18 15:39:00 -07001099 f6i->dst_nopolicy = true;
David Forsterdf789fe2017-02-23 16:27:18 +00001100
Jiri Pirkobba24892013-12-07 19:26:57 +01001101 neigh_parms_data_state_setall(idev->nd_parms);
1102
David Aherne6464b82018-05-27 08:09:53 -07001103 ifa->addr = *cfg->pfx;
1104 if (cfg->peer_pfx)
1105 ifa->peer_addr = *cfg->peer_pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 spin_lock_init(&ifa->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001108 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001109 INIT_HLIST_NODE(&ifa->addr_lst);
David Aherne6464b82018-05-27 08:09:53 -07001110 ifa->scope = cfg->scope;
1111 ifa->prefix_len = cfg->plen;
David Ahern8308f3f2018-05-27 08:09:58 -07001112 ifa->rt_priority = cfg->rt_priority;
David Aherne6464b82018-05-27 08:09:53 -07001113 ifa->flags = cfg->ifa_flags;
Mahesh Bandewar66eb9f82017-05-12 17:03:39 -07001114 /* No need to add the TENTATIVE flag for addresses with NODAD */
David Aherne6464b82018-05-27 08:09:53 -07001115 if (!(cfg->ifa_flags & IFA_F_NODAD))
Mahesh Bandewar66eb9f82017-05-12 17:03:39 -07001116 ifa->flags |= IFA_F_TENTATIVE;
David Aherne6464b82018-05-27 08:09:53 -07001117 ifa->valid_lft = cfg->valid_lft;
1118 ifa->prefered_lft = cfg->preferred_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 ifa->cstamp = ifa->tstamp = jiffies;
Daniel Borkmann617fe292013-04-09 03:47:16 +00001120 ifa->tokenized = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
David Ahern360a9882018-04-18 15:39:00 -07001122 ifa->rt = f6i;
Keir Fraser57f5f542006-08-29 02:43:49 -07001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 ifa->idev = idev;
David Ahernf3d98322017-10-18 09:56:52 -07001125 in6_dev_hold(idev);
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 /* For caller */
Reshetova, Elena271201c2017-07-04 09:34:56 +03001128 refcount_set(&ifa->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
David Ahernf3d98322017-10-18 09:56:52 -07001130 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
David Ahernf3d98322017-10-18 09:56:52 -07001132 err = ipv6_add_addr_hash(idev->dev, ifa);
1133 if (err < 0) {
1134 rcu_read_unlock_bh();
1135 goto out;
1136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 write_lock(&idev->lock);
David Ahernf3d98322017-10-18 09:56:52 -07001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -07001141 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 if (ifa->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001144 list_add(&ifa->tmp_list, &idev->tempaddr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 in6_ifa_hold(ifa);
1146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 in6_ifa_hold(ifa);
1149 write_unlock(&idev->lock);
David Ahernf3d98322017-10-18 09:56:52 -07001150
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001151 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
David Ahernf3d98322017-10-18 09:56:52 -07001153 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
1154out:
1155 if (unlikely(err < 0)) {
David Ahern360a9882018-04-18 15:39:00 -07001156 fib6_info_release(f6i);
David Ahern93531c62018-04-17 17:33:25 -07001157
David Ahernf3d98322017-10-18 09:56:52 -07001158 if (ifa) {
1159 if (ifa->idev)
1160 in6_dev_put(ifa->idev);
1161 kfree(ifa);
1162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 ifa = ERR_PTR(err);
1164 }
1165
1166 return ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
1168
Thomas Haller5b84efe2014-01-15 15:36:59 +01001169enum cleanup_prefix_rt_t {
1170 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1171 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1172 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1173};
1174
1175/*
1176 * Check, whether the prefix for ifp would still need a prefix route
1177 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1178 * constants.
1179 *
1180 * 1) we don't purge prefix if address was not permanent.
1181 * prefix is managed by its own lifetime.
1182 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1183 * 3) if there are no addresses, delete prefix.
1184 * 4) if there are still other permanent address(es),
1185 * corresponding prefix is still permanent.
1186 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1187 * don't purge the prefix, assume user space is managing it.
1188 * 6) otherwise, update prefix lifetime to the
1189 * longest valid lifetime among the corresponding
1190 * addresses on the device.
1191 * Note: subsequent RA will update lifetime.
1192 **/
1193static enum cleanup_prefix_rt_t
1194check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1195{
1196 struct inet6_ifaddr *ifa;
1197 struct inet6_dev *idev = ifp->idev;
1198 unsigned long lifetime;
1199 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1200
1201 *expires = jiffies;
1202
1203 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1204 if (ifa == ifp)
1205 continue;
Zhiqiang Liue75913c2019-02-11 10:57:46 +08001206 if (ifa->prefix_len != ifp->prefix_len ||
1207 !ipv6_prefix_equal(&ifa->addr, &ifp->addr,
Thomas Haller5b84efe2014-01-15 15:36:59 +01001208 ifp->prefix_len))
1209 continue;
1210 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1211 return CLEANUP_PREFIX_RT_NOP;
1212
1213 action = CLEANUP_PREFIX_RT_EXPIRE;
1214
1215 spin_lock(&ifa->lock);
1216
1217 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1218 /*
1219 * Note: Because this address is
1220 * not permanent, lifetime <
1221 * LONG_MAX / HZ here.
1222 */
1223 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1224 *expires = ifa->tstamp + lifetime * HZ;
1225 spin_unlock(&ifa->lock);
1226 }
1227
1228 return action;
1229}
1230
1231static void
1232cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
1233{
David Ahern93c2fb22018-04-18 15:38:59 -07001234 struct fib6_info *f6i;
Thomas Haller5b84efe2014-01-15 15:36:59 +01001235
David Ahern2b2450c2019-03-27 20:53:52 -07001236 f6i = addrconf_get_prefix_route(&ifp->addr, ifp->prefix_len,
1237 ifp->idev->dev, 0, RTF_DEFAULT, true);
David Ahern93c2fb22018-04-18 15:38:59 -07001238 if (f6i) {
Thomas Haller5b84efe2014-01-15 15:36:59 +01001239 if (del_rt)
David Ahern93c2fb22018-04-18 15:38:59 -07001240 ip6_del_rt(dev_net(ifp->idev->dev), f6i);
Thomas Haller5b84efe2014-01-15 15:36:59 +01001241 else {
David Ahern93c2fb22018-04-18 15:38:59 -07001242 if (!(f6i->fib6_flags & RTF_EXPIRES))
1243 fib6_set_expires(f6i, expires);
1244 fib6_info_release(f6i);
Thomas Haller5b84efe2014-01-15 15:36:59 +01001245 }
1246 }
1247}
1248
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250/* This function wants to get referenced ifp and releases it before return */
1251
1252static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1253{
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001254 int state;
Thomas Haller5b84efe2014-01-15 15:36:59 +01001255 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1256 unsigned long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001258 ASSERT_RTNL();
1259
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001260 spin_lock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001261 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -07001262 ifp->state = INET6_IFADDR_STATE_DEAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001263 spin_unlock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001264
1265 if (state == INET6_IFADDR_STATE_DEAD)
1266 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
stephen hemminger5c578aed2010-03-17 20:31:11 +00001268 spin_lock_bh(&addrconf_hash_lock);
1269 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578aed2010-03-17 20:31:11 +00001270 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Thomas Haller5b84efe2014-01-15 15:36:59 +01001272 write_lock_bh(&ifp->idev->lock);
David S. Miller5d9efa72013-10-28 20:07:50 -04001273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001275 list_del(&ifp->tmp_list);
1276 if (ifp->ifpub) {
1277 in6_ifa_put(ifp->ifpub);
1278 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 }
stephen hemminger372e6c82010-03-17 20:31:09 +00001280 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Thomas Haller5b84efe2014-01-15 15:36:59 +01001283 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1284 action = check_cleanup_prefix_route(ifp, &expires);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001285
Eric Dumazet8ef802a2017-10-07 19:30:23 -07001286 list_del_rcu(&ifp->if_list);
Thomas Haller5b84efe2014-01-15 15:36:59 +01001287 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Thomas Haller5b84efe2014-01-15 15:36:59 +01001289 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001291 addrconf_del_dad_work(ifp);
Andrey Vaginb2238562008-07-08 15:13:31 -07001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 ipv6_ifa_notify(RTM_DELADDR, ifp);
1294
Cong Wangf88c91d2013-04-14 23:18:43 +08001295 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Thomas Haller5b84efe2014-01-15 15:36:59 +01001297 if (action != CLEANUP_PREFIX_RT_NOP) {
1298 cleanup_prefix_route(ifp, expires,
1299 action == CLEANUP_PREFIX_RT_DEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301
Daniel Walterc3968a82011-04-13 21:10:57 +00001302 /* clean up prefsrc entries */
1303 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001304out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 in6_ifa_put(ifp);
1306}
1307
Eric Dumazetfffcefe2017-11-06 14:13:29 -08001308static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp,
1309 struct inet6_ifaddr *ift,
1310 bool block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 struct inet6_dev *idev = ifp->idev;
1313 struct in6_addr addr, *tmpaddr;
David Aherne6464b82018-05-27 08:09:53 -07001314 unsigned long tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001315 unsigned long regen_advance;
David Aherne6464b82018-05-27 08:09:53 -07001316 struct ifa6_config cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 int ret = 0;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001318 unsigned long now = jiffies;
Jiri Bohac76506a92016-10-13 18:52:15 +02001319 long max_desync_factor;
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001320 s32 cnf_temp_preferred_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Jiri Pirko53bd6742013-12-06 09:45:22 +01001322 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (ift) {
1324 spin_lock_bh(&ift->lock);
1325 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1326 spin_unlock_bh(&ift->lock);
1327 tmpaddr = &addr;
1328 } else {
1329 tmpaddr = NULL;
1330 }
1331retry:
1332 in6_dev_hold(idev);
1333 if (idev->cnf.use_tempaddr <= 0) {
Jiri Pirko53bd6742013-12-06 09:45:22 +01001334 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001335 pr_info("%s: use_tempaddr is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 in6_dev_put(idev);
1337 ret = -1;
1338 goto out;
1339 }
1340 spin_lock_bh(&ifp->lock);
1341 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1342 idev->cnf.use_tempaddr = -1; /*XXX*/
1343 spin_unlock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01001344 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001345 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1346 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 in6_dev_put(idev);
1348 ret = -1;
1349 goto out;
1350 }
1351 in6_ifa_hold(ifp);
1352 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
Jiri Bohac9d6280d2016-10-13 18:50:02 +02001353 ipv6_try_regen_rndid(idev, tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001355 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001357 regen_advance = idev->cnf.regen_max_retry *
Ian Morris67ba4152014-08-24 21:53:10 +01001358 idev->cnf.dad_transmits *
1359 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
Jiri Bohac76506a92016-10-13 18:52:15 +02001360
1361 /* recalculate max_desync_factor each time and update
1362 * idev->desync_factor if it's larger
1363 */
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001364 cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
Jiri Bohac76506a92016-10-13 18:52:15 +02001365 max_desync_factor = min_t(__u32,
1366 idev->cnf.max_desync_factor,
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001367 cnf_temp_preferred_lft - regen_advance);
Jiri Bohac76506a92016-10-13 18:52:15 +02001368
1369 if (unlikely(idev->desync_factor > max_desync_factor)) {
1370 if (max_desync_factor > 0) {
1371 get_random_bytes(&idev->desync_factor,
1372 sizeof(idev->desync_factor));
1373 idev->desync_factor %= max_desync_factor;
1374 } else {
1375 idev->desync_factor = 0;
1376 }
1377 }
1378
David Ahern3f2d67b2018-06-11 07:12:12 -07001379 memset(&cfg, 0, sizeof(cfg));
David Aherne6464b82018-05-27 08:09:53 -07001380 cfg.valid_lft = min_t(__u32, ifp->valid_lft,
Jiri Bohac76506a92016-10-13 18:52:15 +02001381 idev->cnf.temp_valid_lft + age);
David Aherne6464b82018-05-27 08:09:53 -07001382 cfg.preferred_lft = cnf_temp_preferred_lft + age - idev->desync_factor;
1383 cfg.preferred_lft = min_t(__u32, ifp->prefered_lft, cfg.preferred_lft);
1384
1385 cfg.plen = ifp->prefix_len;
Jiri Bohac76506a92016-10-13 18:52:15 +02001386 tmp_tstamp = ifp->tstamp;
1387 spin_unlock_bh(&ifp->lock);
1388
Jiri Pirko53bd6742013-12-06 09:45:22 +01001389 write_unlock_bh(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -07001390
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001391 /* A temporary address is created only if this calculated Preferred
1392 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1393 * an implementation must not create a temporary address with a zero
1394 * Preferred Lifetime.
Heiner Kallweitecab6702014-03-12 22:13:19 +01001395 * Use age calculation as in addrconf_verify to avoid unnecessary
1396 * temporary addresses being generated.
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001397 */
Heiner Kallweitecab6702014-03-12 22:13:19 +01001398 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
David Aherne6464b82018-05-27 08:09:53 -07001399 if (cfg.preferred_lft <= regen_advance + age) {
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001400 in6_ifa_put(ifp);
1401 in6_dev_put(idev);
1402 ret = -1;
1403 goto out;
1404 }
1405
David Aherne6464b82018-05-27 08:09:53 -07001406 cfg.ifa_flags = IFA_F_TEMPORARY;
Neil Horman95c385b2007-04-25 17:08:10 -07001407 /* set in addrconf_prefix_rcv() */
1408 if (ifp->flags & IFA_F_OPTIMISTIC)
David Aherne6464b82018-05-27 08:09:53 -07001409 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
Neil Horman95c385b2007-04-25 17:08:10 -07001410
David Aherne6464b82018-05-27 08:09:53 -07001411 cfg.pfx = &addr;
1412 cfg.scope = ipv6_addr_scope(cfg.pfx);
1413
1414 ift = ipv6_add_addr(idev, &cfg, block, NULL);
Hannes Frederic Sowa4b08a8f2013-08-16 13:02:27 +02001415 if (IS_ERR(ift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 in6_ifa_put(ifp);
1417 in6_dev_put(idev);
Joe Perchesf3213832012-05-15 14:11:53 +00001418 pr_info("%s: retry temporary address regeneration\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 tmpaddr = &addr;
Jiri Pirko53bd6742013-12-06 09:45:22 +01001420 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 goto retry;
1422 }
1423
1424 spin_lock_bh(&ift->lock);
1425 ift->ifpub = ifp;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001426 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 ift->tstamp = tmp_tstamp;
1428 spin_unlock_bh(&ift->lock);
1429
David S. Millercf22f9a2012-04-14 21:37:40 -04001430 addrconf_dad_start(ift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 in6_ifa_put(ift);
1432 in6_dev_put(idev);
1433out:
1434 return ret;
1435}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437/*
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001438 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001440enum {
1441 IPV6_SADDR_RULE_INIT = 0,
1442 IPV6_SADDR_RULE_LOCAL,
1443 IPV6_SADDR_RULE_SCOPE,
1444 IPV6_SADDR_RULE_PREFERRED,
1445#ifdef CONFIG_IPV6_MIP6
1446 IPV6_SADDR_RULE_HOA,
1447#endif
1448 IPV6_SADDR_RULE_OIF,
1449 IPV6_SADDR_RULE_LABEL,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001450 IPV6_SADDR_RULE_PRIVACY,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001451 IPV6_SADDR_RULE_ORCHID,
1452 IPV6_SADDR_RULE_PREFIX,
Erik Kline7fd25612014-10-28 18:11:14 +09001453#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1454 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1455#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001456 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001457};
1458
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001459struct ipv6_saddr_score {
1460 int rule;
1461 int addr_type;
1462 struct inet6_ifaddr *ifa;
1463 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1464 int scopedist;
1465 int matchlen;
1466};
1467
1468struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001469 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001470 int ifindex;
1471 int scope;
1472 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001473 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001474};
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001475
Dave Jonesb6f99a22007-03-22 12:27:49 -07001476static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
Ulrich Weber45bb0062010-02-25 23:28:58 +00001478 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001479 return 1;
1480 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482
Matteo Croce35e015e2017-09-12 17:46:37 +02001483static bool ipv6_use_optimistic_addr(struct net *net,
1484 struct inet6_dev *idev)
Erik Kline7fd25612014-10-28 18:11:14 +09001485{
1486#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Matteo Croce35e015e2017-09-12 17:46:37 +02001487 if (!idev)
1488 return false;
1489 if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
1490 return false;
1491 if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic)
1492 return false;
1493
1494 return true;
Erik Kline7fd25612014-10-28 18:11:14 +09001495#else
1496 return false;
1497#endif
1498}
1499
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01001500static bool ipv6_allow_optimistic_dad(struct net *net,
1501 struct inet6_dev *idev)
1502{
1503#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1504 if (!idev)
1505 return false;
1506 if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
1507 return false;
1508
1509 return true;
1510#else
1511 return false;
1512#endif
1513}
1514
Benjamin Thery3de23252008-05-28 14:51:24 +02001515static int ipv6_get_saddr_eval(struct net *net,
1516 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001517 struct ipv6_saddr_dst *dst,
1518 int i)
1519{
1520 int ret;
1521
1522 if (i <= score->rule) {
1523 switch (i) {
1524 case IPV6_SADDR_RULE_SCOPE:
1525 ret = score->scopedist;
1526 break;
1527 case IPV6_SADDR_RULE_PREFIX:
1528 ret = score->matchlen;
1529 break;
1530 default:
1531 ret = !!test_bit(i, score->scorebits);
1532 }
1533 goto out;
1534 }
1535
1536 switch (i) {
1537 case IPV6_SADDR_RULE_INIT:
1538 /* Rule 0: remember if hiscore is not ready yet */
1539 ret = !!score->ifa;
1540 break;
1541 case IPV6_SADDR_RULE_LOCAL:
1542 /* Rule 1: Prefer same address */
1543 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1544 break;
1545 case IPV6_SADDR_RULE_SCOPE:
1546 /* Rule 2: Prefer appropriate scope
1547 *
1548 * ret
1549 * ^
1550 * -1 | d 15
1551 * ---+--+-+---> scope
1552 * |
1553 * | d is scope of the destination.
1554 * B-d | \
1555 * | \ <- smaller scope is better if
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08001556 * B-15 | \ if scope is enough for destination.
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001557 * | ret = B - scope (-1 <= scope >= d <= 15).
1558 * d-C-1 | /
1559 * |/ <- greater is better
1560 * -C / if scope is not enough for destination.
1561 * /| ret = scope - C (-1 <= d < scope <= 15).
1562 *
1563 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1564 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1565 * Assume B = 0 and we get C > 29.
1566 */
1567 ret = __ipv6_addr_src_scope(score->addr_type);
1568 if (ret >= dst->scope)
1569 ret = -ret;
1570 else
1571 ret -= 128; /* 30 is enough */
1572 score->scopedist = ret;
1573 break;
1574 case IPV6_SADDR_RULE_PREFERRED:
Erik Kline7fd25612014-10-28 18:11:14 +09001575 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001576 /* Rule 3: Avoid deprecated and optimistic addresses */
Erik Kline7fd25612014-10-28 18:11:14 +09001577 u8 avoid = IFA_F_DEPRECATED;
1578
Matteo Croce35e015e2017-09-12 17:46:37 +02001579 if (!ipv6_use_optimistic_addr(net, score->ifa->idev))
Erik Kline7fd25612014-10-28 18:11:14 +09001580 avoid |= IFA_F_OPTIMISTIC;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001581 ret = ipv6_saddr_preferred(score->addr_type) ||
Erik Kline7fd25612014-10-28 18:11:14 +09001582 !(score->ifa->flags & avoid);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001583 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001584 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001585#ifdef CONFIG_IPV6_MIP6
1586 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001587 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001588 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001589 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1590 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001591 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001592 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001593#endif
1594 case IPV6_SADDR_RULE_OIF:
1595 /* Rule 5: Prefer outgoing interface */
1596 ret = (!dst->ifindex ||
1597 dst->ifindex == score->ifa->idev->dev->ifindex);
1598 break;
1599 case IPV6_SADDR_RULE_LABEL:
1600 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001601 ret = ipv6_addr_label(net,
1602 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001603 score->ifa->idev->dev->ifindex) == dst->label;
1604 break;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001605 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001606 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001607 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001608 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001609 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001610 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1611 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1612 score->ifa->idev->cnf.use_tempaddr >= 2;
1613 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001614 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001615 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001616 case IPV6_SADDR_RULE_ORCHID:
1617 /* Rule 8-: Prefer ORCHID vs ORCHID or
1618 * non-ORCHID vs non-ORCHID
1619 */
1620 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1621 ipv6_addr_orchid(dst->addr));
1622 break;
1623 case IPV6_SADDR_RULE_PREFIX:
1624 /* Rule 8: Use longest matching prefix */
YOSHIFUJI Hideaki / 吉藤英明91b4b042012-09-10 18:41:07 +00001625 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1626 if (ret > score->ifa->prefix_len)
1627 ret = score->ifa->prefix_len;
1628 score->matchlen = ret;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001629 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001630#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1631 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1632 /* Optimistic addresses still have lower precedence than other
1633 * preferred addresses.
1634 */
1635 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1636 break;
1637#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001638 default:
1639 ret = 0;
1640 }
1641
1642 if (ret)
1643 __set_bit(i, score->scorebits);
1644 score->rule = i;
1645out:
1646 return ret;
1647}
1648
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001649static int __ipv6_dev_get_saddr(struct net *net,
1650 struct ipv6_saddr_dst *dst,
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001651 struct inet6_dev *idev,
1652 struct ipv6_saddr_score *scores,
1653 int hiscore_idx)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001654{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001655 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001656
Eric Dumazetf59c0312017-10-07 19:30:27 -07001657 list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001658 int i;
1659
1660 /*
1661 * - Tentative Address (RFC2462 section 5.4)
1662 * - A tentative address is not considered
1663 * "assigned to an interface" in the traditional
1664 * sense, unless it is also flagged as optimistic.
1665 * - Candidate Source Address (section 4)
1666 * - In any case, anycast addresses, multicast
1667 * addresses, and the unspecified address MUST
1668 * NOT be included in a candidate set.
1669 */
1670 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1671 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1672 continue;
1673
1674 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1675
1676 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1677 score->addr_type & IPV6_ADDR_MULTICAST)) {
1678 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1679 idev->dev->name);
1680 continue;
1681 }
1682
1683 score->rule = -1;
1684 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1685
1686 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1687 int minihiscore, miniscore;
1688
1689 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1690 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1691
1692 if (minihiscore > miniscore) {
1693 if (i == IPV6_SADDR_RULE_SCOPE &&
1694 score->scopedist > 0) {
1695 /*
1696 * special case:
1697 * each remaining entry
1698 * has too small (not enough)
1699 * scope, because ifa entries
1700 * are sorted by their scope
1701 * values.
1702 */
1703 goto out;
1704 }
1705 break;
1706 } else if (minihiscore < miniscore) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001707 swap(hiscore, score);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001708 hiscore_idx = 1 - hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001709
1710 /* restore our iterator */
1711 score->ifa = hiscore->ifa;
1712
1713 break;
1714 }
1715 }
1716 }
1717out:
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001718 return hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001719}
1720
David Ahernafbac6012016-06-16 16:24:26 -07001721static int ipv6_get_saddr_master(struct net *net,
1722 const struct net_device *dst_dev,
1723 const struct net_device *master,
1724 struct ipv6_saddr_dst *dst,
1725 struct ipv6_saddr_score *scores,
1726 int hiscore_idx)
1727{
1728 struct inet6_dev *idev;
1729
1730 idev = __in6_dev_get(dst_dev);
1731 if (idev)
1732 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1733 scores, hiscore_idx);
1734
1735 idev = __in6_dev_get(master);
1736 if (idev)
1737 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1738 scores, hiscore_idx);
1739
1740 return hiscore_idx;
1741}
1742
Patrick McHardyb3f644f2012-08-26 19:14:14 +02001743int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001744 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001745 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001747 struct ipv6_saddr_score scores[2], *hiscore;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001748 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001749 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001750 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001751 int dst_type;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001752 bool use_oif_addr = false;
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001753 int hiscore_idx = 0;
Eric Dumazetcc429c82017-10-07 19:30:28 -07001754 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001756 dst_type = __ipv6_addr_type(daddr);
1757 dst.addr = daddr;
1758 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1759 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001760 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001761 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001762
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001763 scores[hiscore_idx].rule = -1;
1764 scores[hiscore_idx].ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001766 rcu_read_lock();
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001767
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001768 /* Candidate Source Address (section 4)
1769 * - multicast and link-local destination address,
1770 * the set of candidate source address MUST only
1771 * include addresses assigned to interfaces
1772 * belonging to the same link as the outgoing
1773 * interface.
1774 * (- For site-local destination addresses, the
1775 * set of candidate source addresses MUST only
1776 * include addresses assigned to interfaces
1777 * belonging to the same site as the outgoing
1778 * interface.)
Erik Kline3985e8a2015-07-22 16:38:25 +09001779 * - "It is RECOMMENDED that the candidate source addresses
1780 * be the set of unicast addresses assigned to the
1781 * interface that will be used to send to the destination
1782 * (the 'outgoing' interface)." (RFC 6724)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001783 */
1784 if (dst_dev) {
Erik Kline3985e8a2015-07-22 16:38:25 +09001785 idev = __in6_dev_get(dst_dev);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001786 if ((dst_type & IPV6_ADDR_MULTICAST) ||
Erik Kline3985e8a2015-07-22 16:38:25 +09001787 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1788 (idev && idev->cnf.use_oif_addrs_only)) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001789 use_oif_addr = true;
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001790 }
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001791 }
1792
1793 if (use_oif_addr) {
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001794 if (idev)
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001795 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001796 } else {
David Ahernafbac6012016-06-16 16:24:26 -07001797 const struct net_device *master;
1798 int master_idx = 0;
1799
1800 /* if dst_dev exists and is enslaved to an L3 device, then
1801 * prefer addresses from dst_dev and then the master over
1802 * any other enslaved devices in the L3 domain.
1803 */
1804 master = l3mdev_master_dev_rcu(dst_dev);
1805 if (master) {
1806 master_idx = master->ifindex;
1807
1808 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1809 master, &dst,
1810 scores, hiscore_idx);
1811
1812 if (scores[hiscore_idx].ifa)
1813 goto out;
1814 }
1815
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001816 for_each_netdev_rcu(net, dev) {
David Ahernafbac6012016-06-16 16:24:26 -07001817 /* only consider addresses on devices in the
1818 * same L3 domain
1819 */
1820 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1821 continue;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001822 idev = __in6_dev_get(dev);
1823 if (!idev)
1824 continue;
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001825 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001826 }
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001827 }
David Ahernafbac6012016-06-16 16:24:26 -07001828
1829out:
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001830 hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001831 if (!hiscore->ifa)
Eric Dumazetcc429c82017-10-07 19:30:28 -07001832 ret = -EADDRNOTAVAIL;
1833 else
1834 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001835
Eric Dumazetcc429c82017-10-07 19:30:28 -07001836 rcu_read_unlock();
1837 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001839EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Amerigo Wang89657792013-06-29 21:30:49 +08001841int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001842 u32 banned_flags)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001843{
1844 struct inet6_ifaddr *ifp;
1845 int err = -EADDRNOTAVAIL;
1846
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01001847 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1848 if (ifp->scope > IFA_LINK)
1849 break;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001850 if (ifp->scope == IFA_LINK &&
1851 !(ifp->flags & banned_flags)) {
1852 *addr = ifp->addr;
1853 err = 0;
1854 break;
1855 }
1856 }
1857 return err;
1858}
1859
Neil Horman95c385b2007-04-25 17:08:10 -07001860int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001861 u32 banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 struct inet6_dev *idev;
1864 int err = -EADDRNOTAVAIL;
1865
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001866 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001867 idev = __in6_dev_get(dev);
1868 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 read_lock_bh(&idev->lock);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001870 err = __ipv6_get_lladdr(idev, addr, banned_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 read_unlock_bh(&idev->lock);
1872 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001873 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return err;
1875}
1876
Eric Dumazetd9bf82c2017-10-07 19:30:24 -07001877static int ipv6_count_addresses(const struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Eric Dumazetd9bf82c2017-10-07 19:30:24 -07001879 const struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 int cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Eric Dumazetd9bf82c2017-10-07 19:30:24 -07001882 rcu_read_lock();
1883 list_for_each_entry_rcu(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 cnt++;
Eric Dumazetd9bf82c2017-10-07 19:30:24 -07001885 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return cnt;
1887}
1888
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001889int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Florian Westphal2a7851b2013-05-17 03:56:10 +00001890 const struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
David Ahern232378e2018-03-13 08:29:37 -07001892 return ipv6_chk_addr_and_flags(net, addr, dev, !dev,
1893 strict, IFA_F_TENTATIVE);
Erik Klinec58da4c2015-02-04 20:01:23 +09001894}
1895EXPORT_SYMBOL(ipv6_chk_addr);
1896
David Ahern1893ff22018-03-13 08:29:38 -07001897/* device argument is used to find the L3 domain of interest. If
1898 * skip_dev_check is set, then the ifp device is not checked against
1899 * the passed in dev argument. So the 2 cases for addresses checks are:
1900 * 1. does the address exist in the L3 domain that dev is part of
1901 * (skip_dev_check = true), or
1902 *
1903 * 2. does the address exist on the specific device
1904 * (skip_dev_check = false)
1905 */
Erik Klinec58da4c2015-02-04 20:01:23 +09001906int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
David Ahern232378e2018-03-13 08:29:37 -07001907 const struct net_device *dev, bool skip_dev_check,
1908 int strict, u32 banned_flags)
Erik Klinec58da4c2015-02-04 20:01:23 +09001909{
Eric Dumazet3f27fb22017-10-23 16:17:47 -07001910 unsigned int hash = inet6_addr_hash(net, addr);
David Ahern1893ff22018-03-13 08:29:38 -07001911 const struct net_device *l3mdev;
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001912 struct inet6_ifaddr *ifp;
Erik Klinec58da4c2015-02-04 20:01:23 +09001913 u32 ifp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Eric Dumazet480318a2017-10-23 16:17:48 -07001915 rcu_read_lock();
David Ahern232378e2018-03-13 08:29:37 -07001916
David Ahern1893ff22018-03-13 08:29:38 -07001917 l3mdev = l3mdev_master_dev_rcu(dev);
David Ahern232378e2018-03-13 08:29:37 -07001918 if (skip_dev_check)
1919 dev = NULL;
1920
Sasha Levinb67bfe02013-02-27 17:06:00 -08001921 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001922 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001923 continue;
David Ahern1893ff22018-03-13 08:29:38 -07001924
1925 if (l3mdev_master_dev_rcu(ifp->idev->dev) != l3mdev)
1926 continue;
1927
Erik Klinec58da4c2015-02-04 20:01:23 +09001928 /* Decouple optimistic from tentative for evaluation here.
1929 * Ban optimistic addresses explicitly, when required.
1930 */
1931 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1932 ? (ifp->flags&~IFA_F_TENTATIVE)
1933 : ifp->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (ipv6_addr_equal(&ifp->addr, addr) &&
Erik Klinec58da4c2015-02-04 20:01:23 +09001935 !(ifp_flags&banned_flags) &&
Ian Morris63159f22015-03-29 14:00:04 +01001936 (!dev || ifp->idev->dev == dev ||
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001937 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
Eric Dumazet480318a2017-10-23 16:17:48 -07001938 rcu_read_unlock();
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001939 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
1941 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00001942
Eric Dumazet480318a2017-10-23 16:17:48 -07001943 rcu_read_unlock();
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001944 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
Erik Klinec58da4c2015-02-04 20:01:23 +09001946EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001949/* Compares an address/prefix_len with addresses on device @dev.
1950 * If one is found it returns true.
1951 */
1952bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1953 const unsigned int prefix_len, struct net_device *dev)
1954{
Eric Dumazet47e26942017-10-07 19:30:25 -07001955 const struct inet6_ifaddr *ifa;
1956 const struct inet6_dev *idev;
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001957 bool ret = false;
1958
1959 rcu_read_lock();
1960 idev = __in6_dev_get(dev);
1961 if (idev) {
Eric Dumazet47e26942017-10-07 19:30:25 -07001962 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001963 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1964 if (ret)
1965 break;
1966 }
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001967 }
1968 rcu_read_unlock();
1969
1970 return ret;
1971}
1972EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1973
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001974int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001975{
Eric Dumazet24ba3332017-10-07 19:30:26 -07001976 const struct inet6_ifaddr *ifa;
1977 const struct inet6_dev *idev;
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001978 int onlink;
1979
1980 onlink = 0;
1981 rcu_read_lock();
1982 idev = __in6_dev_get(dev);
1983 if (idev) {
Eric Dumazet24ba3332017-10-07 19:30:26 -07001984 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001985 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1986 ifa->prefix_len);
1987 if (onlink)
1988 break;
1989 }
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001990 }
1991 rcu_read_unlock();
1992 return onlink;
1993}
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001994EXPORT_SYMBOL(ipv6_chk_prefix);
1995
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001996struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001997 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
Eric Dumazet3f27fb22017-10-23 16:17:47 -07001999 unsigned int hash = inet6_addr_hash(net, addr);
David S. Millerb79d1d52010-03-25 21:39:21 -07002000 struct inet6_ifaddr *ifp, *result = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Eric Dumazet24f226d2017-10-23 16:17:49 -07002002 rcu_read_lock();
2003 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09002004 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08002005 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01002007 if (!dev || ifp->idev->dev == dev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07002009 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 in6_ifa_hold(ifp);
2011 break;
2012 }
2013 }
2014 }
Eric Dumazet24f226d2017-10-23 16:17:49 -07002015 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
David S. Millerb79d1d52010-03-25 21:39:21 -07002017 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018}
2019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020/* Gets referenced address, destroys ifaddr */
2021
Brian Haleycc411d02009-09-09 14:41:32 +00002022static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
Lubomir Rintel3d171f32016-01-08 13:47:23 +01002024 if (dad_failed)
2025 ifp->flags |= IFA_F_DADFAILED;
2026
Sabrina Dubrocaec8add22017-06-29 16:56:54 +02002027 if (ifp->flags&IFA_F_TEMPORARY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 struct inet6_ifaddr *ifpub;
2029 spin_lock_bh(&ifp->lock);
2030 ifpub = ifp->ifpub;
2031 if (ifpub) {
2032 in6_ifa_hold(ifpub);
2033 spin_unlock_bh(&ifp->lock);
Eric Dumazetfffcefe2017-11-06 14:13:29 -08002034 ipv6_create_tempaddr(ifpub, ifp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 in6_ifa_put(ifpub);
2036 } else {
2037 spin_unlock_bh(&ifp->lock);
2038 }
2039 ipv6_del_addr(ifp);
Sabrina Dubrocaec8add22017-06-29 16:56:54 +02002040 } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
2041 spin_lock_bh(&ifp->lock);
2042 addrconf_del_dad_work(ifp);
2043 ifp->flags |= IFA_F_TENTATIVE;
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01002044 if (dad_failed)
2045 ifp->flags &= ~IFA_F_OPTIMISTIC;
Sabrina Dubrocaec8add22017-06-29 16:56:54 +02002046 spin_unlock_bh(&ifp->lock);
2047 if (dad_failed)
2048 ipv6_ifa_notify(0, ifp);
2049 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002050 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053}
2054
Herbert Xuf2344a12010-05-18 15:55:27 -07002055static int addrconf_dad_end(struct inet6_ifaddr *ifp)
2056{
2057 int err = -ENOENT;
2058
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01002059 spin_lock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07002060 if (ifp->state == INET6_IFADDR_STATE_DAD) {
2061 ifp->state = INET6_IFADDR_STATE_POSTDAD;
2062 err = 0;
2063 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01002064 spin_unlock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07002065
2066 return err;
2067}
2068
Vishwanath Paida13c592017-10-30 19:38:52 -04002069void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002070{
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09002071 struct inet6_dev *idev = ifp->idev;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01002072 struct net *net = dev_net(ifp->idev->dev);
Brian Haley9bdd8d42009-03-18 18:22:48 -07002073
Ursula Braun853dc2e2010-10-24 23:06:43 +00002074 if (addrconf_dad_end(ifp)) {
2075 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07002076 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00002077 }
Herbert Xuf2344a12010-05-18 15:55:27 -07002078
Vishwanath Paida13c592017-10-30 19:38:52 -04002079 net_info_ratelimited("%s: IPv6 duplicate address %pI6c used by %pM detected!\n",
2080 ifp->idev->dev->name, &ifp->addr, eth_hdr(skb)->h_source);
Brian Haley9bdd8d42009-03-18 18:22:48 -07002081
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002082 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09002083
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002084 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002085 struct in6_addr new_addr;
2086 struct inet6_ifaddr *ifp2;
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002087 int retries = ifp->stable_privacy_retry + 1;
David Aherne6464b82018-05-27 08:09:53 -07002088 struct ifa6_config cfg = {
2089 .pfx = &new_addr,
2090 .plen = ifp->prefix_len,
2091 .ifa_flags = ifp->flags,
2092 .valid_lft = ifp->valid_lft,
2093 .preferred_lft = ifp->prefered_lft,
2094 .scope = ifp->scope,
2095 };
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002096
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01002097 if (retries > net->ipv6.sysctl.idgen_retries) {
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002098 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
2099 ifp->idev->dev->name);
2100 goto errdad;
2101 }
2102
2103 new_addr = ifp->addr;
2104 if (ipv6_generate_stable_address(&new_addr, retries,
2105 idev))
2106 goto errdad;
2107
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002108 spin_unlock_bh(&ifp->lock);
2109
2110 if (idev->cnf.max_addresses &&
2111 ipv6_count_addresses(idev) >=
2112 idev->cnf.max_addresses)
2113 goto lock_errdad;
2114
2115 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
2116 ifp->idev->dev->name);
2117
David Aherne6464b82018-05-27 08:09:53 -07002118 ifp2 = ipv6_add_addr(idev, &cfg, false, NULL);
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002119 if (IS_ERR(ifp2))
2120 goto lock_errdad;
2121
2122 spin_lock_bh(&ifp2->lock);
2123 ifp2->stable_privacy_retry = retries;
2124 ifp2->state = INET6_IFADDR_STATE_PREDAD;
2125 spin_unlock_bh(&ifp2->lock);
2126
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01002127 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002128 in6_ifa_put(ifp2);
2129lock_errdad:
2130 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09002131 }
2132
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002133errdad:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002134 /* transition from _POSTDAD to _ERRDAD */
2135 ifp->state = INET6_IFADDR_STATE_ERRDAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01002136 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002137
2138 addrconf_mod_dad_work(ifp, 0);
Wei Yongjun751eb6b2016-09-05 16:06:31 +08002139 in6_ifa_put(ifp);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002140}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002142/* Join to solicited addr multicast group.
2143 * caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002144void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
2146 struct in6_addr maddr;
2147
2148 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2149 return;
2150
2151 addrconf_addr_solict_mult(addr, &maddr);
2152 ipv6_dev_mc_inc(dev, &maddr);
2153}
2154
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002155/* caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002156void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
2158 struct in6_addr maddr;
2159
2160 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2161 return;
2162
2163 addrconf_addr_solict_mult(addr, &maddr);
2164 __ipv6_dev_mc_dec(idev, &maddr);
2165}
2166
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002167/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002168static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
2170 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002171
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002172 if (ifp->prefix_len >= 127) /* RFC 6164 */
Bjørn Mork2bda8a02011-07-05 23:04:13 +00002173 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2175 if (ipv6_addr_any(&addr))
2176 return;
WANG Cong013b4d92014-09-11 15:35:11 -07002177 __ipv6_dev_ac_inc(ifp->idev, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178}
2179
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002180/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002181static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
2183 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002184
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002185 if (ifp->prefix_len >= 127) /* RFC 6164 */
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00002186 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2188 if (ipv6_addr_any(&addr))
2189 return;
2190 __ipv6_dev_ac_dec(ifp->idev, &addr);
2191}
2192
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002193static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002194{
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002195 switch (dev->addr_len) {
2196 case ETH_ALEN:
Luiz Augusto von Dentz9dae2e02017-03-12 10:19:38 +02002197 memcpy(eui, dev->dev_addr, 3);
2198 eui[3] = 0xFF;
2199 eui[4] = 0xFE;
2200 memcpy(eui + 5, dev->dev_addr + 3, 3);
2201 break;
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002202 case EUI64_ADDR_LEN:
2203 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
2204 eui[0] ^= 2;
2205 break;
2206 default:
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002207 return -1;
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002208 }
2209
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002210 return 0;
2211}
2212
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002213static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2214{
2215 union fwnet_hwaddr *ha;
2216
2217 if (dev->addr_len != FWNET_ALEN)
2218 return -1;
2219
2220 ha = (union fwnet_hwaddr *)dev->dev_addr;
2221
2222 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2223 eui[0] ^= 2;
2224 return 0;
2225}
2226
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002227static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2228{
2229 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2230 if (dev->addr_len != ARCNET_ALEN)
2231 return -1;
2232 memset(eui, 0, 7);
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002233 eui[7] = *(u8 *)dev->dev_addr;
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002234 return 0;
2235}
2236
2237static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2238{
2239 if (dev->addr_len != INFINIBAND_ALEN)
2240 return -1;
2241 memcpy(eui, dev->dev_addr + 12, 8);
2242 eui[0] |= 2;
2243 return 0;
2244}
2245
stephen hemmingerc61393e2010-10-04 20:17:53 +00002246static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002247{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00002248 if (addr == 0)
2249 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002250 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2251 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2252 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2253 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2254 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2255 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2256 eui[1] = 0;
2257 eui[2] = 0x5E;
2258 eui[3] = 0xFE;
2259 memcpy(eui + 4, &addr, 4);
2260 return 0;
2261}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002262
2263static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2264{
2265 if (dev->priv_flags & IFF_ISATAP)
2266 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2267 return -1;
2268}
2269
stephen hemmingeraee80b52011-06-08 10:44:30 +00002270static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2271{
2272 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2273}
2274
Nicolas Dichtele8377352013-08-20 12:16:06 +02002275static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2276{
2277 memcpy(eui, dev->perm_addr, 3);
2278 memcpy(eui + 5, dev->perm_addr + 3, 3);
2279 eui[3] = 0xFF;
2280 eui[4] = 0xFE;
2281 eui[0] ^= 2;
2282 return 0;
2283}
2284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2286{
2287 switch (dev->type) {
2288 case ARPHRD_ETHER:
2289 case ARPHRD_FDDI:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002290 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002292 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002294 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002295 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002296 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002297 case ARPHRD_IPGRE:
Felix Jia45ce0fd2017-01-26 16:59:18 +13002298 case ARPHRD_TUNNEL:
stephen hemmingeraee80b52011-06-08 10:44:30 +00002299 return addrconf_ifid_gre(eui, dev);
Jukka Rissanene74bccb82013-12-11 17:05:36 +02002300 case ARPHRD_6LOWPAN:
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002301 return addrconf_ifid_6lowpan(eui, dev);
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002302 case ARPHRD_IEEE1394:
2303 return addrconf_ifid_ieee1394(eui, dev);
Nicolas Dichtele8377352013-08-20 12:16:06 +02002304 case ARPHRD_TUNNEL6:
Felix Jia45ce0fd2017-01-26 16:59:18 +13002305 case ARPHRD_IP6GRE:
Subash Abhinov Kasiviswanathan9deb4412018-06-04 19:26:07 -06002306 case ARPHRD_RAWIP:
Nicolas Dichtele8377352013-08-20 12:16:06 +02002307 return addrconf_ifid_ip6tnl(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
2309 return -1;
2310}
2311
2312static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2313{
2314 int err = -1;
2315 struct inet6_ifaddr *ifp;
2316
2317 read_lock_bh(&idev->lock);
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01002318 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2319 if (ifp->scope > IFA_LINK)
2320 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2322 memcpy(eui, ifp->addr.s6_addr+8, 8);
2323 err = 0;
2324 break;
2325 }
2326 }
2327 read_unlock_bh(&idev->lock);
2328 return err;
2329}
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002332static void ipv6_regen_rndid(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08002335 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
2338 /*
2339 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2340 * check if generated address is not inappropriate
2341 *
2342 * - Reserved subnet anycast (RFC 2526)
2343 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002344 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 * 00-00-5E-FE-xx-xx-xx-xx
2346 * - value 0
2347 * - XXX: already assigned to an address on the device
2348 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002349 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2351 (idev->rndid[7]&0x80))
2352 goto regen;
2353 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2354 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2355 goto regen;
2356 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2357 goto regen;
2358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359}
2360
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002361static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002362{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002364 ipv6_regen_rndid(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367/*
2368 * Add prefix route.
2369 */
2370
2371static void
David Ahern8308f3f2018-05-27 08:09:58 -07002372addrconf_prefix_route(struct in6_addr *pfx, int plen, u32 metric,
2373 struct net_device *dev, unsigned long expires,
2374 u32 flags, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375{
Thomas Graf86872cb2006-08-22 00:01:08 -07002376 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002377 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
David Ahern8308f3f2018-05-27 08:09:58 -07002378 .fc_metric = metric ? : IP6_RT_PRIO_ADDRCONF,
Thomas Graf86872cb2006-08-22 00:01:08 -07002379 .fc_ifindex = dev->ifindex,
2380 .fc_expires = expires,
2381 .fc_dst_len = plen,
2382 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002383 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07002384 .fc_protocol = RTPROT_KERNEL,
David Aherne8478e82018-04-17 17:33:13 -07002385 .fc_type = RTN_UNICAST,
Thomas Graf86872cb2006-08-22 00:01:08 -07002386 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002388 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
2390 /* Prevent useless cloning on PtP SIT.
2391 This thing is done here expecting that the whole
2392 class of non-broadcast devices need not cloning.
2393 */
Amerigo Wang07a93622012-10-29 16:23:10 +00002394#if IS_ENABLED(CONFIG_IPV6_SIT)
Thomas Graf86872cb2006-08-22 00:01:08 -07002395 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2396 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002397#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
David Ahernacb54e32018-04-17 17:33:22 -07002399 ip6_route_add(&cfg, gfp_flags, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400}
2401
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002402
David Ahern8d1c8022018-04-17 17:33:26 -07002403static struct fib6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002404 int plen,
2405 const struct net_device *dev,
David Ahern2b2450c2019-03-27 20:53:52 -07002406 u32 flags, u32 noflags,
2407 bool no_gw)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002408{
2409 struct fib6_node *fn;
David Ahern8d1c8022018-04-17 17:33:26 -07002410 struct fib6_info *rt = NULL;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002411 struct fib6_table *table;
David Ahernca254492015-10-12 11:47:10 -07002412 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002413
David Ahernca254492015-10-12 11:47:10 -07002414 table = fib6_get_table(dev_net(dev), tb_id);
Ian Morris63159f22015-03-29 14:00:04 +01002415 if (!table)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002416 return NULL;
2417
Wei Wang66f5d6c2017-10-06 12:06:10 -07002418 rcu_read_lock();
Wei Wang38fbeee2017-10-06 12:06:02 -07002419 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002420 if (!fn)
2421 goto out;
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07002422
Wei Wang66f5d6c2017-10-06 12:06:10 -07002423 for_each_fib6_node_rt_rcu(fn) {
David Ahern1cf844c2019-05-22 20:27:59 -07002424 if (rt->fib6_nh->fib_nh_dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002425 continue;
David Ahern1cf844c2019-05-22 20:27:59 -07002426 if (no_gw && rt->fib6_nh->fib_nh_gw_family)
David Ahern2b2450c2019-03-27 20:53:52 -07002427 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002428 if ((rt->fib6_flags & flags) != flags)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002429 continue;
David Ahern93c2fb22018-04-18 15:38:59 -07002430 if ((rt->fib6_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002431 continue;
Wei Wange873e4b2018-07-21 20:56:32 -07002432 if (!fib6_info_hold_safe(rt))
2433 continue;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002434 break;
2435 }
2436out:
Wei Wang66f5d6c2017-10-06 12:06:10 -07002437 rcu_read_unlock();
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002438 return rt;
2439}
2440
2441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442/* Create "default" multicast route to the interface */
2443
2444static void addrconf_add_mroute(struct net_device *dev)
2445{
Thomas Graf86872cb2006-08-22 00:01:08 -07002446 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002447 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002448 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2449 .fc_ifindex = dev->ifindex,
2450 .fc_dst_len = 8,
2451 .fc_flags = RTF_UP,
David Aherne8478e82018-04-17 17:33:13 -07002452 .fc_type = RTN_UNICAST,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002453 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002454 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Thomas Graf86872cb2006-08-22 00:01:08 -07002456 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2457
Cong Wange500c6d2018-08-22 12:58:34 -07002458 ip6_route_add(&cfg, GFP_KERNEL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459}
2460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2462{
2463 struct inet6_dev *idev;
2464
2465 ASSERT_RTNL();
2466
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002467 idev = ipv6_find_idev(dev);
2468 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00002469 return ERR_PTR(-ENOBUFS);
2470
2471 if (idev->cnf.disable_ipv6)
2472 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
2474 /* Add default multicast route */
David Ahernba46ee42016-06-13 13:44:18 -07002475 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
Li Wei4af04ab2011-12-06 21:23:45 +00002476 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 return idev;
2479}
2480
Jiri Pirko53bd6742013-12-06 09:45:22 +01002481static void manage_tempaddrs(struct inet6_dev *idev,
2482 struct inet6_ifaddr *ifp,
2483 __u32 valid_lft, __u32 prefered_lft,
2484 bool create, unsigned long now)
2485{
2486 u32 flags;
2487 struct inet6_ifaddr *ift;
2488
2489 read_lock_bh(&idev->lock);
2490 /* update all temporary addresses in the list */
2491 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2492 int age, max_valid, max_prefered;
2493
2494 if (ifp != ift->ifpub)
2495 continue;
2496
2497 /* RFC 4941 section 3.3:
2498 * If a received option will extend the lifetime of a public
2499 * address, the lifetimes of temporary addresses should
2500 * be extended, subject to the overall constraint that no
2501 * temporary addresses should ever remain "valid" or "preferred"
2502 * for a time longer than (TEMP_VALID_LIFETIME) or
2503 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2504 */
2505 age = (now - ift->cstamp) / HZ;
2506 max_valid = idev->cnf.temp_valid_lft - age;
2507 if (max_valid < 0)
2508 max_valid = 0;
2509
2510 max_prefered = idev->cnf.temp_prefered_lft -
Jiri Bohac76506a92016-10-13 18:52:15 +02002511 idev->desync_factor - age;
Jiri Pirko53bd6742013-12-06 09:45:22 +01002512 if (max_prefered < 0)
2513 max_prefered = 0;
2514
2515 if (valid_lft > max_valid)
2516 valid_lft = max_valid;
2517
2518 if (prefered_lft > max_prefered)
2519 prefered_lft = max_prefered;
2520
2521 spin_lock(&ift->lock);
2522 flags = ift->flags;
2523 ift->valid_lft = valid_lft;
2524 ift->prefered_lft = prefered_lft;
2525 ift->tstamp = now;
2526 if (prefered_lft > 0)
2527 ift->flags &= ~IFA_F_DEPRECATED;
2528
2529 spin_unlock(&ift->lock);
2530 if (!(flags&IFA_F_TENTATIVE))
2531 ipv6_ifa_notify(0, ift);
2532 }
2533
2534 if ((create || list_empty(&idev->tempaddr_list)) &&
2535 idev->cnf.use_tempaddr > 0) {
2536 /* When a new public address is created as described
2537 * in [ADDRCONF], also create a new temporary address.
2538 * Also create a temporary address if it's enabled but
2539 * no temporary address currently exists.
2540 */
2541 read_unlock_bh(&idev->lock);
Eric Dumazetfffcefe2017-11-06 14:13:29 -08002542 ipv6_create_tempaddr(ifp, NULL, false);
Jiri Pirko53bd6742013-12-06 09:45:22 +01002543 } else {
2544 read_unlock_bh(&idev->lock);
2545 }
2546}
2547
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002548static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2549{
Felix Jiad35a00b2017-01-26 16:59:17 +13002550 return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2551 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002552}
2553
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002554int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2555 const struct prefix_info *pinfo,
2556 struct inet6_dev *in6_dev,
2557 const struct in6_addr *addr, int addr_type,
2558 u32 addr_flags, bool sllao, bool tokenized,
2559 __u32 valid_lft, u32 prefered_lft)
Alexander Aring4f672232016-06-15 21:20:22 +02002560{
2561 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2562 int create = 0, update_lft = 0;
2563
2564 if (!ifp && valid_lft) {
2565 int max_addresses = in6_dev->cnf.max_addresses;
David Aherne6464b82018-05-27 08:09:53 -07002566 struct ifa6_config cfg = {
2567 .pfx = addr,
2568 .plen = pinfo->prefix_len,
2569 .ifa_flags = addr_flags,
2570 .valid_lft = valid_lft,
2571 .preferred_lft = prefered_lft,
2572 .scope = addr_type & IPV6_ADDR_SCOPE_MASK,
2573 };
Alexander Aring4f672232016-06-15 21:20:22 +02002574
2575#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Matteo Croce35e015e2017-09-12 17:46:37 +02002576 if ((net->ipv6.devconf_all->optimistic_dad ||
2577 in6_dev->cnf.optimistic_dad) &&
Alexander Aring4f672232016-06-15 21:20:22 +02002578 !net->ipv6.devconf_all->forwarding && sllao)
David Aherne6464b82018-05-27 08:09:53 -07002579 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
Alexander Aring4f672232016-06-15 21:20:22 +02002580#endif
2581
2582 /* Do not allow to create too much of autoconfigured
2583 * addresses; this would be too easy way to crash kernel.
2584 */
2585 if (!max_addresses ||
2586 ipv6_count_addresses(in6_dev) < max_addresses)
David Aherne6464b82018-05-27 08:09:53 -07002587 ifp = ipv6_add_addr(in6_dev, &cfg, false, NULL);
Alexander Aring4f672232016-06-15 21:20:22 +02002588
2589 if (IS_ERR_OR_NULL(ifp))
2590 return -1;
2591
Alexander Aring4f672232016-06-15 21:20:22 +02002592 create = 1;
2593 spin_lock_bh(&ifp->lock);
2594 ifp->flags |= IFA_F_MANAGETEMPADDR;
2595 ifp->cstamp = jiffies;
2596 ifp->tokenized = tokenized;
2597 spin_unlock_bh(&ifp->lock);
2598 addrconf_dad_start(ifp);
2599 }
2600
2601 if (ifp) {
2602 u32 flags;
2603 unsigned long now;
2604 u32 stored_lft;
2605
2606 /* update lifetime (RFC2462 5.5.3 e) */
2607 spin_lock_bh(&ifp->lock);
2608 now = jiffies;
2609 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2610 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2611 else
2612 stored_lft = 0;
Lorenzo Bianconif85f94b2018-04-16 17:52:59 +02002613 if (!create && stored_lft) {
Alexander Aring4f672232016-06-15 21:20:22 +02002614 const u32 minimum_lft = min_t(u32,
2615 stored_lft, MIN_VALID_LIFETIME);
2616 valid_lft = max(valid_lft, minimum_lft);
2617
2618 /* RFC4862 Section 5.5.3e:
2619 * "Note that the preferred lifetime of the
2620 * corresponding address is always reset to
2621 * the Preferred Lifetime in the received
2622 * Prefix Information option, regardless of
2623 * whether the valid lifetime is also reset or
2624 * ignored."
2625 *
2626 * So we should always update prefered_lft here.
2627 */
2628 update_lft = 1;
2629 }
2630
2631 if (update_lft) {
2632 ifp->valid_lft = valid_lft;
2633 ifp->prefered_lft = prefered_lft;
2634 ifp->tstamp = now;
2635 flags = ifp->flags;
2636 ifp->flags &= ~IFA_F_DEPRECATED;
2637 spin_unlock_bh(&ifp->lock);
2638
2639 if (!(flags&IFA_F_TENTATIVE))
2640 ipv6_ifa_notify(0, ifp);
2641 } else
2642 spin_unlock_bh(&ifp->lock);
2643
2644 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2645 create, now);
2646
2647 in6_ifa_put(ifp);
2648 addrconf_verify();
2649 }
2650
2651 return 0;
2652}
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002653EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
Alexander Aring4f672232016-06-15 21:20:22 +02002654
Neil Hormane6bff992012-01-04 10:49:15 +00002655void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
2657 struct prefix_info *pinfo;
2658 __u32 valid_lft;
2659 __u32 prefered_lft;
Alexander Aring4f672232016-06-15 21:20:22 +02002660 int addr_type, err;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002661 u32 addr_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07002663 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
2665 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002666
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 if (len < sizeof(struct prefix_info)) {
Joe Perchese32ac252018-03-26 08:35:01 -07002668 netdev_dbg(dev, "addrconf: prefix option too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 return;
2670 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 /*
2673 * Validation checks ([ADDRCONF], page 19)
2674 */
2675
2676 addr_type = ipv6_addr_type(&pinfo->prefix);
2677
2678 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2679 return;
2680
2681 valid_lft = ntohl(pinfo->valid);
2682 prefered_lft = ntohl(pinfo->prefered);
2683
2684 if (prefered_lft > valid_lft) {
Joe Perchese87cc472012-05-13 21:56:26 +00002685 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 return;
2687 }
2688
2689 in6_dev = in6_dev_get(dev);
2690
Ian Morris63159f22015-03-29 14:00:04 +01002691 if (!in6_dev) {
Joe Perchese87cc472012-05-13 21:56:26 +00002692 net_dbg_ratelimited("addrconf: device %s not configured\n",
2693 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 return;
2695 }
2696
2697 /*
2698 * Two things going on here:
2699 * 1) Add routes for on-link prefixes
2700 * 2) Configure prefixes with the auto flag set
2701 */
2702
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002703 if (pinfo->onlink) {
David Ahern8d1c8022018-04-17 17:33:26 -07002704 struct fib6_info *rt;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002705 unsigned long rt_expires;
2706
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002707 /* Avoid arithmetic overflow. Really, we could
2708 * save rt_expires in seconds, likely valid_lft,
2709 * but it would require division in fib gc, that it
2710 * not good.
2711 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002712 if (HZ > USER_HZ)
2713 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2714 else
2715 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08002716
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002717 if (addrconf_finite_timeout(rt_expires))
2718 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002720 rt = addrconf_get_prefix_route(&pinfo->prefix,
2721 pinfo->prefix_len,
2722 dev,
2723 RTF_ADDRCONF | RTF_PREFIX_RT,
David Ahern2b2450c2019-03-27 20:53:52 -07002724 RTF_DEFAULT, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002726 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002727 /* Autoconf prefix route */
2728 if (valid_lft == 0) {
David Ahernafb1d4b52018-04-17 17:33:11 -07002729 ip6_del_rt(net, rt);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002730 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002731 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002732 /* not infinity */
David Ahern14895682018-04-17 17:33:17 -07002733 fib6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002734 } else {
David Ahern14895682018-04-17 17:33:17 -07002735 fib6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 }
2737 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002738 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002739 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2740 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002741 /* not infinity */
2742 flags |= RTF_EXPIRES;
2743 expires = jiffies_to_clock_t(rt_expires);
2744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
David Ahern8308f3f2018-05-27 08:09:58 -07002746 0, dev, expires, flags,
2747 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 }
David Ahern93531c62018-04-17 17:33:25 -07002749 fib6_info_release(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 }
2751
2752 /* Try to figure out our local address for this prefix */
2753
2754 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 struct in6_addr addr;
Alexander Aringf997c552016-06-15 21:20:23 +02002756 bool tokenized = false, dev_addr_generated = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
2758 if (pinfo->prefix_len == 64) {
2759 memcpy(&addr, &pinfo->prefix, 8);
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002760
2761 if (!ipv6_addr_any(&in6_dev->token)) {
2762 read_lock_bh(&in6_dev->lock);
2763 memcpy(addr.s6_addr + 8,
2764 in6_dev->token.s6_addr + 8, 8);
2765 read_unlock_bh(&in6_dev->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00002766 tokenized = true;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002767 } else if (is_addr_mode_generate_stable(in6_dev) &&
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01002768 !ipv6_generate_stable_address(&addr, 0,
2769 in6_dev)) {
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002770 addr_flags |= IFA_F_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01002771 goto ok;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002772 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2773 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
Alexander Aring4f672232016-06-15 21:20:22 +02002774 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002775 } else {
2776 dev_addr_generated = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 }
2778 goto ok;
2779 }
Joe Perchese87cc472012-05-13 21:56:26 +00002780 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2781 pinfo->prefix_len);
Alexander Aring4f672232016-06-15 21:20:22 +02002782 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
2784ok:
Alexander Aring4f672232016-06-15 21:20:22 +02002785 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2786 &addr, addr_type,
2787 addr_flags, sllao,
2788 tokenized, valid_lft,
2789 prefered_lft);
2790 if (err)
2791 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002792
2793 /* Ignore error case here because previous prefix add addr was
2794 * successful which will be notified.
2795 */
2796 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2797 addr_type, addr_flags, sllao,
2798 tokenized, valid_lft,
2799 prefered_lft,
2800 dev_addr_generated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 }
2802 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
Alexander Aring4f672232016-06-15 21:20:22 +02002803put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 in6_dev_put(in6_dev);
2805}
2806
2807/*
2808 * Set destination address.
2809 * Special case for SIT interfaces where we create a new "virtual"
2810 * device.
2811 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002812int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813{
2814 struct in6_ifreq ireq;
2815 struct net_device *dev;
2816 int err = -EINVAL;
2817
2818 rtnl_lock();
2819
2820 err = -EFAULT;
2821 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2822 goto err_exit;
2823
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002824 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
2826 err = -ENODEV;
Ian Morris63159f22015-03-29 14:00:04 +01002827 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 goto err_exit;
2829
Amerigo Wang07a93622012-10-29 16:23:10 +00002830#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002832 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 struct ip_tunnel_parm p;
2835
2836 err = -EADDRNOTAVAIL;
2837 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2838 goto err_exit;
2839
2840 memset(&p, 0, sizeof(p));
2841 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2842 p.iph.saddr = 0;
2843 p.iph.version = 4;
2844 p.iph.ihl = 5;
2845 p.iph.protocol = IPPROTO_IPV6;
2846 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002847 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002849 if (ops->ndo_do_ioctl) {
2850 mm_segment_t oldfs = get_fs();
2851
2852 set_fs(KERNEL_DS);
2853 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2854 set_fs(oldfs);
2855 } else
2856 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
2858 if (err == 0) {
2859 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002860 dev = __dev_get_by_name(net, p.name);
2861 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 goto err_exit;
Petr Machata00f54e62018-12-06 17:05:36 +00002863 err = dev_open(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 }
2865 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002866#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
2868err_exit:
2869 rtnl_unlock();
2870 return err;
2871}
2872
Madhu Challa93a714d2015-02-25 09:58:35 -08002873static int ipv6_mc_config(struct sock *sk, bool join,
2874 const struct in6_addr *addr, int ifindex)
2875{
2876 int ret;
2877
2878 ASSERT_RTNL();
2879
2880 lock_sock(sk);
2881 if (join)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002882 ret = ipv6_sock_mc_join(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002883 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002884 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002885 release_sock(sk);
2886
2887 return ret;
2888}
2889
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890/*
2891 * Manual configuration of address on an interface
2892 */
Jiri Pirko479840f2013-12-06 09:45:21 +01002893static int inet6_addr_add(struct net *net, int ifindex,
David Ahern19b15182018-05-27 08:09:54 -07002894 struct ifa6_config *cfg,
David Ahernde95e042017-10-18 09:56:54 -07002895 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896{
2897 struct inet6_ifaddr *ifp;
2898 struct inet6_dev *idev;
2899 struct net_device *dev;
Madhu Challa93a714d2015-02-25 09:58:35 -08002900 unsigned long timeout;
2901 clock_t expires;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002902 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
2904 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002905
David Ahern19b15182018-05-27 08:09:54 -07002906 if (cfg->plen > 128)
Thomas Graf24ef0da2008-05-28 16:54:22 +02002907 return -EINVAL;
2908
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002909 /* check the lifetime */
David Ahern19b15182018-05-27 08:09:54 -07002910 if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002911 return -EINVAL;
2912
David Ahern19b15182018-05-27 08:09:54 -07002913 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64)
Jiri Pirko53bd6742013-12-06 09:45:22 +01002914 return -EINVAL;
2915
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002916 dev = __dev_get_by_index(net, ifindex);
2917 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002919
Brian Haley64e724f2010-07-20 10:34:30 +00002920 idev = addrconf_add_dev(dev);
2921 if (IS_ERR(idev))
2922 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
David Ahern19b15182018-05-27 08:09:54 -07002924 if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
Madhu Challa93a714d2015-02-25 09:58:35 -08002925 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
David Ahern19b15182018-05-27 08:09:54 -07002926 true, cfg->pfx, ifindex);
Madhu Challa93a714d2015-02-25 09:58:35 -08002927
2928 if (ret < 0)
2929 return ret;
2930 }
2931
David Ahern19b15182018-05-27 08:09:54 -07002932 cfg->scope = ipv6_addr_scope(cfg->pfx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
David Ahern19b15182018-05-27 08:09:54 -07002934 timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002935 if (addrconf_finite_timeout(timeout)) {
2936 expires = jiffies_to_clock_t(timeout * HZ);
David Ahern19b15182018-05-27 08:09:54 -07002937 cfg->valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002938 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002939 } else {
2940 expires = 0;
2941 flags = 0;
David Ahern19b15182018-05-27 08:09:54 -07002942 cfg->ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002943 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002944
David Ahern19b15182018-05-27 08:09:54 -07002945 timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002946 if (addrconf_finite_timeout(timeout)) {
2947 if (timeout == 0)
David Ahern19b15182018-05-27 08:09:54 -07002948 cfg->ifa_flags |= IFA_F_DEPRECATED;
2949 cfg->preferred_lft = timeout;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002950 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002951
David Ahern19b15182018-05-27 08:09:54 -07002952 ifp = ipv6_add_addr(idev, cfg, true, extack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 if (!IS_ERR(ifp)) {
David Ahern19b15182018-05-27 08:09:54 -07002954 if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
David Ahern8308f3f2018-05-27 08:09:58 -07002955 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
2956 ifp->rt_priority, dev, expires,
2957 flags, GFP_KERNEL);
Thomas Haller761aac72014-01-15 15:36:58 +01002958 }
2959
Lorenzo Bianconia2d481b2018-04-17 11:54:39 +02002960 /* Send a netlink notification if DAD is enabled and
2961 * optimistic flag is not set
2962 */
2963 if (!(ifp->flags & (IFA_F_OPTIMISTIC | IFA_F_NODAD)))
2964 ipv6_ifa_notify(0, ifp);
Neil Horman95c385b2007-04-25 17:08:10 -07002965 /*
2966 * Note that section 3.1 of RFC 4429 indicates
2967 * that the Optimistic flag should not be set for
2968 * manually configured addresses
2969 */
David S. Millercf22f9a2012-04-14 21:37:40 -04002970 addrconf_dad_start(ifp);
David Ahern19b15182018-05-27 08:09:54 -07002971 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR)
2972 manage_tempaddrs(idev, ifp, cfg->valid_lft,
2973 cfg->preferred_lft, true, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002975 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 return 0;
David Ahern19b15182018-05-27 08:09:54 -07002977 } else if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
2978 ipv6_mc_config(net->ipv6.mc_autojoin_sk, false,
2979 cfg->pfx, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 }
2981
2982 return PTR_ERR(ifp);
2983}
2984
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002985static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2986 const struct in6_addr *pfx, unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987{
2988 struct inet6_ifaddr *ifp;
2989 struct inet6_dev *idev;
2990 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002991
Thomas Graf24ef0da2008-05-28 16:54:22 +02002992 if (plen > 128)
2993 return -EINVAL;
2994
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002995 dev = __dev_get_by_index(net, ifindex);
2996 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 return -ENODEV;
2998
Ian Morrise5d08d72014-11-23 21:28:43 +00002999 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003000 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 return -ENXIO;
3002
3003 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003004 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 if (ifp->prefix_len == plen &&
3006 ipv6_addr_equal(pfx, &ifp->addr)) {
3007 in6_ifa_hold(ifp);
3008 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003009
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02003010 if (!(ifp->flags & IFA_F_TEMPORARY) &&
3011 (ifa_flags & IFA_F_MANAGETEMPADDR))
3012 manage_tempaddrs(idev, ifp, 0, 0, false,
3013 jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 ipv6_del_addr(ifp);
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02003015 addrconf_verify_rtnl();
Madhu Challa93a714d2015-02-25 09:58:35 -08003016 if (ipv6_addr_is_multicast(pfx)) {
3017 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
3018 false, pfx, dev->ifindex);
3019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 return 0;
3021 }
3022 }
3023 read_unlock_bh(&idev->lock);
3024 return -EADDRNOTAVAIL;
3025}
3026
3027
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003028int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
David Ahern19b15182018-05-27 08:09:54 -07003030 struct ifa6_config cfg = {
3031 .ifa_flags = IFA_F_PERMANENT,
3032 .preferred_lft = INFINITY_LIFE_TIME,
3033 .valid_lft = INFINITY_LIFE_TIME,
3034 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 struct in6_ifreq ireq;
3036 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003037
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00003038 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003040
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3042 return -EFAULT;
3043
David Ahern19b15182018-05-27 08:09:54 -07003044 cfg.pfx = &ireq.ifr6_addr;
3045 cfg.plen = ireq.ifr6_prefixlen;
3046
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 rtnl_lock();
David Ahern19b15182018-05-27 08:09:54 -07003048 err = inet6_addr_add(net, ireq.ifr6_ifindex, &cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 rtnl_unlock();
3050 return err;
3051}
3052
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003053int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
3055 struct in6_ifreq ireq;
3056 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003057
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00003058 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 return -EPERM;
3060
3061 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
3062 return -EFAULT;
3063
3064 rtnl_lock();
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02003065 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003066 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 rtnl_unlock();
3068 return err;
3069}
3070
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003071static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
3072 int plen, int scope)
3073{
3074 struct inet6_ifaddr *ifp;
David Aherne6464b82018-05-27 08:09:53 -07003075 struct ifa6_config cfg = {
3076 .pfx = addr,
3077 .plen = plen,
3078 .ifa_flags = IFA_F_PERMANENT,
3079 .valid_lft = INFINITY_LIFE_TIME,
3080 .preferred_lft = INFINITY_LIFE_TIME,
3081 .scope = scope
3082 };
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003083
David Aherne6464b82018-05-27 08:09:53 -07003084 ifp = ipv6_add_addr(idev, &cfg, true, NULL);
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003085 if (!IS_ERR(ifp)) {
3086 spin_lock_bh(&ifp->lock);
3087 ifp->flags &= ~IFA_F_TENTATIVE;
3088 spin_unlock_bh(&ifp->lock);
Paolo Abeni764d3be2016-11-22 16:57:40 +01003089 rt_genid_bump_ipv6(dev_net(idev->dev));
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003090 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3091 in6_ifa_put(ifp);
3092 }
3093}
3094
Amerigo Wang07a93622012-10-29 16:23:10 +00003095#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096static void sit_add_v4_addrs(struct inet6_dev *idev)
3097{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 struct in6_addr addr;
3099 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003100 struct net *net = dev_net(idev->dev);
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01003101 int scope, plen;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01003102 u32 pflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
3104 ASSERT_RTNL();
3105
3106 memset(&addr, 0, sizeof(struct in6_addr));
3107 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
3108
3109 if (idev->dev->flags&IFF_POINTOPOINT) {
3110 addr.s6_addr32[0] = htonl(0xfe800000);
3111 scope = IFA_LINK;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01003112 plen = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 } else {
3114 scope = IPV6_ADDR_COMPATv4;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01003115 plen = 96;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01003116 pflags |= RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 }
3118
3119 if (addr.s6_addr32[3]) {
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01003120 add_addr(idev, &addr, plen, scope);
David Ahern8308f3f2018-05-27 08:09:58 -07003121 addrconf_prefix_route(&addr, plen, 0, idev->dev, 0, pflags,
Cong Wange500c6d2018-08-22 12:58:34 -07003122 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 return;
3124 }
3125
Benjamin Thery6fda7352008-03-05 10:47:47 -08003126 for_each_netdev(net, dev) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003127 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 if (in_dev && (dev->flags & IFF_UP)) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003129 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
3131 int flag = scope;
3132
3133 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
3135 addr.s6_addr32[3] = ifa->ifa_local;
3136
3137 if (ifa->ifa_scope == RT_SCOPE_LINK)
3138 continue;
3139 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
3140 if (idev->dev->flags&IFF_POINTOPOINT)
3141 continue;
3142 flag |= IFA_HOST;
3143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003145 add_addr(idev, &addr, plen, flag);
David Ahern8308f3f2018-05-27 08:09:58 -07003146 addrconf_prefix_route(&addr, plen, 0, idev->dev,
Cong Wange500c6d2018-08-22 12:58:34 -07003147 0, pflags, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 }
3149 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003152#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
3154static void init_loopback(struct net_device *dev)
3155{
3156 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
3158 /* ::1 */
3159
3160 ASSERT_RTNL();
3161
Ian Morrise5d08d72014-11-23 21:28:43 +00003162 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003163 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003164 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 return;
3166 }
3167
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003168 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169}
3170
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003171void addrconf_add_linklocal(struct inet6_dev *idev,
3172 const struct in6_addr *addr, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
David Aherne6464b82018-05-27 08:09:53 -07003174 struct ifa6_config cfg = {
3175 .pfx = addr,
3176 .plen = 64,
3177 .ifa_flags = flags | IFA_F_PERMANENT,
3178 .valid_lft = INFINITY_LIFE_TIME,
3179 .preferred_lft = INFINITY_LIFE_TIME,
3180 .scope = IFA_LINK
3181 };
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003182 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Neil Horman95c385b2007-04-25 17:08:10 -07003184#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Matteo Croce35e015e2017-09-12 17:46:37 +02003185 if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad ||
3186 idev->cnf.optimistic_dad) &&
David Miller702beb82008-07-20 18:17:02 -07003187 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
David Aherne6464b82018-05-27 08:09:53 -07003188 cfg.ifa_flags |= IFA_F_OPTIMISTIC;
Neil Horman95c385b2007-04-25 17:08:10 -07003189#endif
3190
David Aherne6464b82018-05-27 08:09:53 -07003191 ifp = ipv6_add_addr(idev, &cfg, true, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 if (!IS_ERR(ifp)) {
David Ahern8308f3f2018-05-27 08:09:58 -07003193 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, 0, idev->dev,
David Ahernacb54e32018-04-17 17:33:22 -07003194 0, 0, GFP_ATOMIC);
David S. Millercf22f9a2012-04-14 21:37:40 -04003195 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 in6_ifa_put(ifp);
3197 }
3198}
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003199EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003201static bool ipv6_reserved_interfaceid(struct in6_addr address)
3202{
3203 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3204 return true;
3205
3206 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3207 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3208 return true;
3209
3210 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3211 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3212 return true;
3213
3214 return false;
3215}
3216
3217static int ipv6_generate_stable_address(struct in6_addr *address,
3218 u8 dad_count,
3219 const struct inet6_dev *idev)
3220{
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003221 static DEFINE_SPINLOCK(lock);
3222 static __u32 digest[SHA_DIGEST_WORDS];
3223 static __u32 workspace[SHA_WORKSPACE_WORDS];
3224
3225 static union {
3226 char __data[SHA_MESSAGE_BYTES];
3227 struct {
3228 struct in6_addr secret;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003229 __be32 prefix[2];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003230 unsigned char hwaddr[MAX_ADDR_LEN];
3231 u8 dad_count;
3232 } __packed;
3233 } data;
3234
3235 struct in6_addr secret;
3236 struct in6_addr temp;
3237 struct net *net = dev_net(idev->dev);
3238
3239 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3240
3241 if (idev->cnf.stable_secret.initialized)
3242 secret = idev->cnf.stable_secret.secret;
3243 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3244 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3245 else
3246 return -1;
3247
3248retry:
3249 spin_lock_bh(&lock);
3250
3251 sha_init(digest);
3252 memset(&data, 0, sizeof(data));
3253 memset(workspace, 0, sizeof(workspace));
3254 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003255 data.prefix[0] = address->s6_addr32[0];
3256 data.prefix[1] = address->s6_addr32[1];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003257 data.secret = secret;
3258 data.dad_count = dad_count;
3259
3260 sha_transform(digest, data.__data, workspace);
3261
3262 temp = *address;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003263 temp.s6_addr32[2] = (__force __be32)digest[0];
3264 temp.s6_addr32[3] = (__force __be32)digest[1];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003265
3266 spin_unlock_bh(&lock);
3267
3268 if (ipv6_reserved_interfaceid(temp)) {
3269 dad_count++;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01003270 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003271 return -1;
3272 goto retry;
3273 }
3274
3275 *address = temp;
3276 return 0;
3277}
3278
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003279static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3280{
3281 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3282
3283 if (s->initialized)
3284 return;
3285 s = &idev->cnf.stable_secret;
3286 get_random_bytes(&s->secret, sizeof(s->secret));
3287 s->initialized = true;
3288}
3289
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003290static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3291{
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003292 struct in6_addr addr;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003293
David Ahernca254492015-10-12 11:47:10 -07003294 /* no link local addresses on L3 master devices */
3295 if (netif_is_l3_master(idev->dev))
3296 return;
3297
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003298 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3299
Felix Jiad35a00b2017-01-26 16:59:17 +13003300 switch (idev->cnf.addr_gen_mode) {
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003301 case IN6_ADDR_GEN_MODE_RANDOM:
3302 ipv6_gen_mode_random_init(idev);
3303 /* fallthrough */
3304 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003305 if (!ipv6_generate_stable_address(&addr, 0, idev))
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003306 addrconf_add_linklocal(idev, &addr,
3307 IFA_F_STABLE_PRIVACY);
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003308 else if (prefix_route)
David Ahern8308f3f2018-05-27 08:09:58 -07003309 addrconf_prefix_route(&addr, 64, 0, idev->dev,
David Ahernacb54e32018-04-17 17:33:22 -07003310 0, 0, GFP_KERNEL);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003311 break;
3312 case IN6_ADDR_GEN_MODE_EUI64:
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003313 /* addrconf_add_linklocal also adds a prefix_route and we
3314 * only need to care about prefix routes if ipv6_generate_eui64
3315 * couldn't generate one.
3316 */
3317 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003318 addrconf_add_linklocal(idev, &addr, 0);
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003319 else if (prefix_route)
David Ahern8308f3f2018-05-27 08:09:58 -07003320 addrconf_prefix_route(&addr, 64, 0, idev->dev,
David Ahern27b10602018-04-18 15:39:06 -07003321 0, 0, GFP_KERNEL);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003322 break;
3323 case IN6_ADDR_GEN_MODE_NONE:
3324 default:
3325 /* will not add any link local address */
3326 break;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003327 }
3328}
3329
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330static void addrconf_dev_config(struct net_device *dev)
3331{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003332 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333
3334 ASSERT_RTNL();
3335
Herbert Xu74235a22007-06-14 13:02:55 -07003336 if ((dev->type != ARPHRD_ETHER) &&
3337 (dev->type != ARPHRD_FDDI) &&
Herbert Xu74235a22007-06-14 13:02:55 -07003338 (dev->type != ARPHRD_ARCNET) &&
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00003339 (dev->type != ARPHRD_INFINIBAND) &&
Nicolas Dichtele8377352013-08-20 12:16:06 +02003340 (dev->type != ARPHRD_IEEE1394) &&
Jukka Rissanene74bccb82013-12-11 17:05:36 +02003341 (dev->type != ARPHRD_TUNNEL6) &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003342 (dev->type != ARPHRD_6LOWPAN) &&
Felix Jia45ce0fd2017-01-26 16:59:18 +13003343 (dev->type != ARPHRD_IP6GRE) &&
3344 (dev->type != ARPHRD_IPGRE) &&
3345 (dev->type != ARPHRD_TUNNEL) &&
Subash Abhinov Kasiviswanathan9deb4412018-06-04 19:26:07 -06003346 (dev->type != ARPHRD_NONE) &&
3347 (dev->type != ARPHRD_RAWIP)) {
Herbert Xu74235a22007-06-14 13:02:55 -07003348 /* Alas, we support only Ethernet autoconfiguration. */
3349 return;
3350 }
3351
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00003353 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 return;
3355
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003356 /* this device type has no EUI support */
3357 if (dev->type == ARPHRD_NONE &&
Felix Jiad35a00b2017-01-26 16:59:17 +13003358 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3359 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003360
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003361 addrconf_addr_gen(idev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362}
3363
Amerigo Wang07a93622012-10-29 16:23:10 +00003364#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365static void addrconf_sit_config(struct net_device *dev)
3366{
3367 struct inet6_dev *idev;
3368
3369 ASSERT_RTNL();
3370
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003371 /*
3372 * Configure the tunnel with one of our IPv4
3373 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 * our v4 addrs in the tunnel
3375 */
3376
Ian Morrise5d08d72014-11-23 21:28:43 +00003377 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003378 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003379 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 return;
3381 }
3382
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003383 if (dev->priv_flags & IFF_ISATAP) {
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003384 addrconf_addr_gen(idev, false);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003385 return;
3386 }
3387
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 sit_add_v4_addrs(idev);
3389
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00003390 if (dev->flags&IFF_POINTOPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003393#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394
Amerigo Wang07a93622012-10-29 16:23:10 +00003395#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003396static void addrconf_gre_config(struct net_device *dev)
3397{
3398 struct inet6_dev *idev;
stephen hemmingeraee80b52011-06-08 10:44:30 +00003399
stephen hemmingeraee80b52011-06-08 10:44:30 +00003400 ASSERT_RTNL();
3401
Ian Morrise5d08d72014-11-23 21:28:43 +00003402 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003403 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003404 pr_debug("%s: add_dev failed\n", __func__);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003405 return;
3406 }
3407
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003408 addrconf_addr_gen(idev, true);
Hannes Frederic Sowad9e4ce62015-10-08 18:19:39 +02003409 if (dev->flags & IFF_POINTOPOINT)
3410 addrconf_add_mroute(dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003411}
3412#endif
3413
David Ahernafb1d4b52018-04-17 17:33:11 -07003414static int fixup_permanent_addr(struct net *net,
3415 struct inet6_dev *idev,
David Ahernf1705ec2016-02-24 09:25:37 -08003416 struct inet6_ifaddr *ifp)
3417{
David Ahern93c2fb22018-04-18 15:38:59 -07003418 /* !fib6_node means the host route was removed from the
David Ahern8048ced2017-04-25 09:17:29 -07003419 * FIB, for example, if 'lo' device is taken down. In that
3420 * case regenerate the host route.
3421 */
David Ahern93c2fb22018-04-18 15:38:59 -07003422 if (!ifp->rt || !ifp->rt->fib6_node) {
David Ahern360a9882018-04-18 15:39:00 -07003423 struct fib6_info *f6i, *prev;
David Ahernf1705ec2016-02-24 09:25:37 -08003424
David Ahern360a9882018-04-18 15:39:00 -07003425 f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false,
3426 GFP_ATOMIC);
3427 if (IS_ERR(f6i))
3428 return PTR_ERR(f6i);
David Ahernf1705ec2016-02-24 09:25:37 -08003429
David Ahern8048ced2017-04-25 09:17:29 -07003430 /* ifp->rt can be accessed outside of rtnl */
3431 spin_lock(&ifp->lock);
3432 prev = ifp->rt;
David Ahern360a9882018-04-18 15:39:00 -07003433 ifp->rt = f6i;
David Ahern8048ced2017-04-25 09:17:29 -07003434 spin_unlock(&ifp->lock);
3435
David Ahern93531c62018-04-17 17:33:25 -07003436 fib6_info_release(prev);
David Ahernf1705ec2016-02-24 09:25:37 -08003437 }
3438
3439 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3440 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
David Ahern8308f3f2018-05-27 08:09:58 -07003441 ifp->rt_priority, idev->dev, 0, 0,
3442 GFP_ATOMIC);
David Ahernf1705ec2016-02-24 09:25:37 -08003443 }
3444
David Ahern6d717132017-05-02 14:43:44 -07003445 if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3446 addrconf_dad_start(ifp);
David Ahernf1705ec2016-02-24 09:25:37 -08003447
3448 return 0;
3449}
3450
David Ahernafb1d4b52018-04-17 17:33:11 -07003451static void addrconf_permanent_addr(struct net *net, struct net_device *dev)
David Ahernf1705ec2016-02-24 09:25:37 -08003452{
3453 struct inet6_ifaddr *ifp, *tmp;
3454 struct inet6_dev *idev;
3455
3456 idev = __in6_dev_get(dev);
3457 if (!idev)
3458 return;
3459
3460 write_lock_bh(&idev->lock);
3461
3462 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3463 if ((ifp->flags & IFA_F_PERMANENT) &&
David Ahernafb1d4b52018-04-17 17:33:11 -07003464 fixup_permanent_addr(net, idev, ifp) < 0) {
David Ahernf1705ec2016-02-24 09:25:37 -08003465 write_unlock_bh(&idev->lock);
Eric Dumazete669b862017-10-30 22:47:09 -07003466 in6_ifa_hold(ifp);
David Ahernf1705ec2016-02-24 09:25:37 -08003467 ipv6_del_addr(ifp);
3468 write_lock_bh(&idev->lock);
3469
3470 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3471 idev->dev->name, &ifp->addr);
3472 }
3473 }
3474
3475 write_unlock_bh(&idev->lock);
3476}
3477
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003478static int addrconf_notify(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00003479 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480{
Jiri Pirko351638e2013-05-28 01:30:21 +00003481 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Hangbin Liu896585d2018-11-21 21:52:33 +08003482 struct netdev_notifier_change_info *change_info;
David Ahern4f7f34e2016-04-07 11:10:41 -07003483 struct netdev_notifier_changeupper_info *info;
Eric Dumazet748e2d92012-08-22 21:50:59 +00003484 struct inet6_dev *idev = __in6_dev_get(dev);
WANG Cong60abc0b2017-06-21 14:34:58 -07003485 struct net *net = dev_net(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003486 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07003487 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003489 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003490 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07003491 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003492 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -07003493 if (IS_ERR(idev))
3494 return notifier_from_errno(PTR_ERR(idev));
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003495 }
3496 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003497
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003498 case NETDEV_CHANGEMTU:
3499 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3500 if (dev->mtu < IPV6_MIN_MTU) {
WANG Cong60abc0b2017-06-21 14:34:58 -07003501 addrconf_ifdown(dev, dev != net->loopback_dev);
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003502 break;
3503 }
3504
3505 if (idev) {
3506 rt6_mtu_change(dev, dev->mtu);
3507 idev->cnf.mtu6 = dev->mtu;
3508 break;
3509 }
3510
3511 /* allocate new idev */
3512 idev = ipv6_add_dev(dev);
3513 if (IS_ERR(idev))
3514 break;
3515
3516 /* device is still not ready */
3517 if (!(idev->if_flags & IF_READY))
3518 break;
3519
3520 run_pending = 1;
3521
3522 /* fall through */
3523
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003525 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07003526 if (dev->flags & IFF_SLAVE)
3527 break;
3528
WANG Cong3ce62a82014-09-11 15:07:16 -07003529 if (idev && idev->cnf.disable_ipv6)
3530 break;
3531
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003532 if (event == NETDEV_UP) {
David Ahern38bd10c2016-04-21 20:56:12 -07003533 /* restore routes for permanent addresses */
David Ahernafb1d4b52018-04-17 17:33:11 -07003534 addrconf_permanent_addr(net, dev);
David Ahern38bd10c2016-04-21 20:56:12 -07003535
Mike Manning1f372c72017-09-25 22:01:36 +01003536 if (!addrconf_link_ready(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003537 /* device is not ready yet. */
Lubomir Rintel7c62b8d2019-01-21 14:54:20 +01003538 pr_debug("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
3539 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003540 break;
3541 }
Kristian Slavov99081042006-02-08 16:10:53 -08003542
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11003543 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3544 idev = ipv6_add_dev(dev);
3545
WANG Conga317a2f2014-07-25 15:25:09 -07003546 if (!IS_ERR_OR_NULL(idev)) {
Kristian Slavov99081042006-02-08 16:10:53 -08003547 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08003548 run_pending = 1;
3549 }
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003550 } else if (event == NETDEV_CHANGE) {
Mike Manning1f372c72017-09-25 22:01:36 +01003551 if (!addrconf_link_ready(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003552 /* device is still not ready. */
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003553 rt6_sync_down_dev(dev, event);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003554 break;
3555 }
3556
Hangbin Liu896585d2018-11-21 21:52:33 +08003557 if (!IS_ERR_OR_NULL(idev)) {
Linus Lüssinga088d1d2017-02-03 08:11:03 +01003558 if (idev->if_flags & IF_READY) {
3559 /* device is already configured -
3560 * but resend MLD reports, we might
3561 * have roamed and need to update
3562 * multicast snooping switches
3563 */
3564 ipv6_mc_up(idev);
Hangbin Liu896585d2018-11-21 21:52:33 +08003565 change_info = ptr;
3566 if (change_info->flags_changed & IFF_NOARP)
3567 addrconf_dad_run(idev, true);
Ido Schimmel27c6fa72018-01-07 12:45:05 +02003568 rt6_sync_up(dev, RTNH_F_LINKDOWN);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003569 break;
Linus Lüssinga088d1d2017-02-03 08:11:03 +01003570 }
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003571 idev->if_flags |= IF_READY;
3572 }
3573
Joe Perchesf3213832012-05-15 14:11:53 +00003574 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3575 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003576
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003577 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003578 }
3579
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003580 switch (dev->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +00003581#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 case ARPHRD_SIT:
3583 addrconf_sit_config(dev);
3584 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07003585#endif
Amerigo Wang07a93622012-10-29 16:23:10 +00003586#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003587 case ARPHRD_IPGRE:
3588 addrconf_gre_config(dev);
3589 break;
3590#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 case ARPHRD_LOOPBACK:
3592 init_loopback(dev);
3593 break;
3594
3595 default:
3596 addrconf_dev_config(dev);
3597 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003598 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003599
WANG Conga317a2f2014-07-25 15:25:09 -07003600 if (!IS_ERR_OR_NULL(idev)) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003601 if (run_pending)
Hangbin Liu896585d2018-11-21 21:52:33 +08003602 addrconf_dad_run(idev, false);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003603
Ido Schimmel2127d952018-01-07 12:45:03 +02003604 /* Device has an address by now */
3605 rt6_sync_up(dev, RTNH_F_DEAD);
3606
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003607 /*
3608 * If the MTU changed during the interface down,
3609 * when the interface up, the changed MTU must be
3610 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003612 if (idev->cnf.mtu6 != dev->mtu &&
3613 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 rt6_mtu_change(dev, dev->mtu);
3615 idev->cnf.mtu6 = dev->mtu;
3616 }
3617 idev->tstamp = jiffies;
3618 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003619
3620 /*
3621 * If the changed mtu during down is lower than
3622 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 */
3624 if (dev->mtu < IPV6_MIN_MTU)
WANG Cong60abc0b2017-06-21 14:34:58 -07003625 addrconf_ifdown(dev, dev != net->loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 }
3627 break;
3628
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 case NETDEV_DOWN:
3630 case NETDEV_UNREGISTER:
3631 /*
3632 * Remove all addresses from this interface.
3633 */
3634 addrconf_ifdown(dev, event != NETDEV_DOWN);
3635 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003636
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07003639 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003640 addrconf_sysctl_unregister(idev);
WANG Conga317a2f2014-07-25 15:25:09 -07003641 err = addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07003642 if (err)
3643 return notifier_from_errno(err);
WANG Conga317a2f2014-07-25 15:25:09 -07003644 err = snmp6_register_dev(idev);
3645 if (err) {
3646 addrconf_sysctl_unregister(idev);
3647 return notifier_from_errno(err);
3648 }
Stephen Hemminger5632c512007-04-28 21:16:39 -07003649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003651
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003652 case NETDEV_PRE_TYPE_CHANGE:
3653 case NETDEV_POST_TYPE_CHANGE:
Andrew Lunn3ef09522015-12-03 21:12:32 +01003654 if (idev)
3655 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07003656 break;
David Ahern4f7f34e2016-04-07 11:10:41 -07003657
3658 case NETDEV_CHANGEUPPER:
3659 info = ptr;
3660
3661 /* flush all routes if dev is linked to or unlinked from
3662 * an L3 master device (e.g., VRF)
3663 */
3664 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3665 addrconf_ifdown(dev, 0);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
3668 return NOTIFY_OK;
3669}
3670
3671/*
3672 * addrconf module should be notified of a device going up
3673 */
3674static struct notifier_block ipv6_dev_notf = {
3675 .notifier_call = addrconf_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07003676 .priority = ADDRCONF_NOTIFY_PRIORITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677};
3678
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003679static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07003680{
3681 struct inet6_dev *idev;
3682 ASSERT_RTNL();
3683
3684 idev = __in6_dev_get(dev);
3685
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003686 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003687 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003688 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003689 ipv6_mc_unmap(idev);
3690}
3691
David Ahern70af9212016-04-08 12:01:21 -07003692static bool addr_is_local(const struct in6_addr *addr)
3693{
3694 return ipv6_addr_type(addr) &
3695 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3696}
3697
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698static int addrconf_ifdown(struct net_device *dev, int how)
3699{
Ido Schimmel4c981e22018-01-07 12:45:04 +02003700 unsigned long event = how ? NETDEV_UNREGISTER : NETDEV_DOWN;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003701 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003702 struct inet6_dev *idev;
David Ahernf1705ec2016-02-24 09:25:37 -08003703 struct inet6_ifaddr *ifa, *tmp;
Ivan Vecera0aef78a2018-04-24 19:51:29 +02003704 bool keep_addr = false;
David S. Miller73a8bd72011-01-23 23:27:15 -08003705 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706
3707 ASSERT_RTNL();
3708
Ido Schimmel4c981e22018-01-07 12:45:04 +02003709 rt6_disable_ip(dev, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
3711 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003712 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 return -ENODEV;
3714
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003715 /*
3716 * Step 1: remove reference to ipv6 device from parent device.
3717 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003719 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003721
3722 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003723 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
3725 /* Step 1.5: remove snmp6 entry */
3726 snmp6_unregister_dev(idev);
3727
3728 }
3729
David Ahernf1705ec2016-02-24 09:25:37 -08003730 /* combine the user config with event to determine if permanent
3731 * addresses are to be removed from address hash table
3732 */
Ivan Vecera0aef78a2018-04-24 19:51:29 +02003733 if (!how && !idev->cnf.disable_ipv6) {
3734 /* aggregate the system setting and interface setting */
3735 int _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3736
3737 if (!_keep_addr)
3738 _keep_addr = idev->cnf.keep_addr_on_down;
3739
3740 keep_addr = (_keep_addr > 0);
3741 }
David Ahernf1705ec2016-02-24 09:25:37 -08003742
David S. Miller73a8bd72011-01-23 23:27:15 -08003743 /* Step 2: clear hash table */
3744 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3745 struct hlist_head *h = &inet6_addr_lst[i];
David S. Miller73a8bd72011-01-23 23:27:15 -08003746
3747 spin_lock_bh(&addrconf_hash_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01003748restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08003749 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
David S. Miller73a8bd72011-01-23 23:27:15 -08003750 if (ifa->idev == idev) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003751 addrconf_del_dad_work(ifa);
David Ahernf1705ec2016-02-24 09:25:37 -08003752 /* combined flag + permanent flag decide if
3753 * address is retained on a down event
3754 */
3755 if (!keep_addr ||
David Ahern70af9212016-04-08 12:01:21 -07003756 !(ifa->flags & IFA_F_PERMANENT) ||
3757 addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003758 hlist_del_init_rcu(&ifa->addr_lst);
3759 goto restart;
3760 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003761 }
3762 }
3763 spin_unlock_bh(&addrconf_hash_lock);
3764 }
3765
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 write_lock_bh(&idev->lock);
3767
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003768 addrconf_del_rs_timer(idev);
3769
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003770 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003771 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003772 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003774 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00003775 while (!list_empty(&idev->tempaddr_list)) {
3776 ifa = list_first_entry(&idev->tempaddr_list,
3777 struct inet6_ifaddr, tmp_list);
3778 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 write_unlock_bh(&idev->lock);
3780 spin_lock_bh(&ifa->lock);
3781
3782 if (ifa->ifpub) {
3783 in6_ifa_put(ifa->ifpub);
3784 ifa->ifpub = NULL;
3785 }
3786 spin_unlock_bh(&ifa->lock);
3787 in6_ifa_put(ifa);
3788 write_lock_bh(&idev->lock);
3789 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003790
David Ahernf1705ec2016-02-24 09:25:37 -08003791 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
David Ahern8d1c8022018-04-17 17:33:26 -07003792 struct fib6_info *rt = NULL;
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003793 bool keep;
David Ahern38bd10c2016-04-21 20:56:12 -07003794
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003795 addrconf_del_dad_work(ifa);
stephen hemminger84e8b802010-03-02 13:32:46 +00003796
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003797 keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3798 !addr_is_local(&ifa->addr);
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003799
David S. Miller73a8bd72011-01-23 23:27:15 -08003800 write_unlock_bh(&idev->lock);
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003801 spin_lock_bh(&ifa->lock);
David Ahernf1705ec2016-02-24 09:25:37 -08003802
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003803 if (keep) {
David Ahernf1705ec2016-02-24 09:25:37 -08003804 /* set state to skip the notifier below */
3805 state = INET6_IFADDR_STATE_DEAD;
David Ahern6d717132017-05-02 14:43:44 -07003806 ifa->state = INET6_IFADDR_STATE_PREDAD;
David Ahernf1705ec2016-02-24 09:25:37 -08003807 if (!(ifa->flags & IFA_F_NODAD))
3808 ifa->flags |= IFA_F_TENTATIVE;
David Ahern38bd10c2016-04-21 20:56:12 -07003809
3810 rt = ifa->rt;
3811 ifa->rt = NULL;
David Ahernf1705ec2016-02-24 09:25:37 -08003812 } else {
3813 state = ifa->state;
3814 ifa->state = INET6_IFADDR_STATE_DEAD;
David Ahernf1705ec2016-02-24 09:25:37 -08003815 }
3816
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003817 spin_unlock_bh(&ifa->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003818
David Ahern38bd10c2016-04-21 20:56:12 -07003819 if (rt)
David Ahernafb1d4b52018-04-17 17:33:11 -07003820 ip6_del_rt(net, rt);
David Ahern38bd10c2016-04-21 20:56:12 -07003821
David S. Miller73a8bd72011-01-23 23:27:15 -08003822 if (state != INET6_IFADDR_STATE_DEAD) {
3823 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Cong Wangf88c91d2013-04-14 23:18:43 +08003824 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
Mike Manningea06f712016-07-22 18:32:11 +01003825 } else {
3826 if (idev->cnf.forwarding)
3827 addrconf_leave_anycast(ifa);
3828 addrconf_leave_solict(ifa->idev, &ifa->addr);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00003829 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003830
David S. Miller73a8bd72011-01-23 23:27:15 -08003831 write_lock_bh(&idev->lock);
Eric Dumazet8ef802a2017-10-07 19:30:23 -07003832 if (!keep) {
3833 list_del_rcu(&ifa->if_list);
3834 in6_ifa_put(ifa);
3835 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003836 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003837
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 write_unlock_bh(&idev->lock);
3839
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003840 /* Step 5: Discard anycast and multicast list */
3841 if (how) {
3842 ipv6_ac_destroy_dev(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 ipv6_mc_destroy_dev(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003844 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 ipv6_mc_down(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003849
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003850 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003851 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003852 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 neigh_parms_release(&nd_tbl, idev->nd_parms);
3854 neigh_ifdown(&nd_tbl, dev);
3855 in6_dev_put(idev);
3856 }
3857 return 0;
3858}
3859
Kees Cooke99e88a2017-10-16 14:43:17 -07003860static void addrconf_rs_timer(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861{
Kees Cooke99e88a2017-10-16 14:43:17 -07003862 struct inet6_dev *idev = from_timer(idev, t, rs_timer);
Cong Wangcaf92bc2013-08-31 13:44:32 +08003863 struct net_device *dev = idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003864 struct in6_addr lladdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003866 write_lock(&idev->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00003867 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 goto out;
3869
Shmulik Ladkani9ba2add2012-12-02 01:44:53 +00003870 if (!ipv6_accept_ra(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003872
3873 /* Announcement received after solicitation was sent */
3874 if (idev->if_flags & IF_RA_RCVD)
3875 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003877 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
Cong Wangcaf92bc2013-08-31 13:44:32 +08003878 write_unlock(&idev->lock);
3879 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3880 ndisc_send_rs(dev, &lladdr,
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003881 &in6addr_linklocal_allrouters);
3882 else
Cong Wangcaf92bc2013-08-31 13:44:32 +08003883 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
Cong Wangcaf92bc2013-08-31 13:44:32 +08003885 write_lock(&idev->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003886 idev->rs_interval = rfc3315_s14_backoff_update(
3887 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003888 /* The wait after the last probe can be shorter */
3889 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3890 idev->cnf.rtr_solicits) ?
3891 idev->cnf.rtr_solicit_delay :
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003892 idev->rs_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 /*
3895 * Note: we do not support deprecated "all on-link"
3896 * assumption any longer.
3897 */
Joe Perches91df42b2012-05-15 14:11:54 +00003898 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 }
3900
3901out:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003902 write_unlock(&idev->lock);
Cong Wangcaf92bc2013-08-31 13:44:32 +08003903put:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003904 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905}
3906
3907/*
3908 * Duplicate Address Detection
3909 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003910static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3911{
3912 unsigned long rand_num;
3913 struct inet6_dev *idev = ifp->idev;
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003914 u64 nonce;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003915
Neil Horman95c385b2007-04-25 17:08:10 -07003916 if (ifp->flags & IFA_F_OPTIMISTIC)
3917 rand_num = 0;
3918 else
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003919 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
Neil Horman95c385b2007-04-25 17:08:10 -07003920
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003921 nonce = 0;
3922 if (idev->cnf.enhanced_dad ||
3923 dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
3924 do
3925 get_random_bytes(&nonce, 6);
3926 while (nonce == 0);
3927 }
3928 ifp->dad_nonce = nonce;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003929 ifp->dad_probes = idev->cnf.dad_transmits;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003930 addrconf_mod_dad_work(ifp, rand_num);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003931}
3932
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003933static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934{
3935 struct inet6_dev *idev = ifp->idev;
3936 struct net_device *dev = idev->dev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003937 bool bump_id, notify = false;
David Ahernafb1d4b52018-04-17 17:33:11 -07003938 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
3940 addrconf_join_solict(dev, &ifp->addr);
3941
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003942 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943
3944 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003945 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003946 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948
David Ahernafb1d4b52018-04-17 17:33:11 -07003949 net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
David Ahernafb1d4b52018-04-17 17:33:11 -07003951 (net->ipv6.devconf_all->accept_dad < 1 &&
Matteo Crocea2d3f3e2017-10-05 19:03:05 +02003952 idev->cnf.accept_dad < 1) ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003953 !(ifp->flags&IFA_F_TENTATIVE) ||
3954 ifp->flags & IFA_F_NODAD) {
David Ahernc76fe2d2018-01-25 20:16:29 -08003955 bool send_na = false;
3956
3957 if (ifp->flags & IFA_F_TENTATIVE &&
3958 !(ifp->flags & IFA_F_OPTIMISTIC))
3959 send_na = true;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003960 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003961 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003962 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 read_unlock_bh(&idev->lock);
3964
David Ahernc76fe2d2018-01-25 20:16:29 -08003965 addrconf_dad_completed(ifp, bump_id, send_na);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 return;
3967 }
3968
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003969 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003970 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003971 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003972 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003973 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003974 * - keep it tentative if it is a permanent address.
3975 * - otherwise, kill it.
3976 */
3977 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003978 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003979 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003980 }
Neil Horman95c385b2007-04-25 17:08:10 -07003981
3982 /*
3983 * Optimistic nodes can start receiving
3984 * Frames right away
3985 */
Erik Kline7fd25612014-10-28 18:11:14 +09003986 if (ifp->flags & IFA_F_OPTIMISTIC) {
David Ahernafb1d4b52018-04-17 17:33:11 -07003987 ip6_ins_rt(net, ifp->rt);
3988 if (ipv6_use_optimistic_addr(net, idev)) {
Erik Kline7fd25612014-10-28 18:11:14 +09003989 /* Because optimistic nodes can use this address,
3990 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3991 */
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003992 notify = true;
Erik Kline7fd25612014-10-28 18:11:14 +09003993 }
3994 }
Neil Horman95c385b2007-04-25 17:08:10 -07003995
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003996 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003998 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 read_unlock_bh(&idev->lock);
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00004000 if (notify)
4001 ipv6_ifa_notify(RTM_NEWADDR, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002}
4003
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004004static void addrconf_dad_start(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004006 bool begin_dad = false;
4007
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01004008 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004009 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
4010 ifp->state = INET6_IFADDR_STATE_PREDAD;
4011 begin_dad = true;
4012 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01004013 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004014
4015 if (begin_dad)
4016 addrconf_mod_dad_work(ifp, 0);
4017}
4018
4019static void addrconf_dad_work(struct work_struct *w)
4020{
4021 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
4022 struct inet6_ifaddr,
4023 dad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 struct inet6_dev *idev = ifp->idev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01004025 bool bump_id, disable_ipv6 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 struct in6_addr mcaddr;
4027
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004028 enum {
4029 DAD_PROCESS,
4030 DAD_BEGIN,
4031 DAD_ABORT,
4032 } action = DAD_PROCESS;
4033
4034 rtnl_lock();
4035
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01004036 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004037 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
4038 action = DAD_BEGIN;
4039 ifp->state = INET6_IFADDR_STATE_DAD;
4040 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
4041 action = DAD_ABORT;
4042 ifp->state = INET6_IFADDR_STATE_POSTDAD;
Mike Manning85b51b12016-08-18 14:39:40 +01004043
Matteo Croce35e015e2017-09-12 17:46:37 +02004044 if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 ||
4045 idev->cnf.accept_dad > 1) &&
4046 !idev->cnf.disable_ipv6 &&
Mike Manning85b51b12016-08-18 14:39:40 +01004047 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
4048 struct in6_addr addr;
4049
4050 addr.s6_addr32[0] = htonl(0xfe800000);
4051 addr.s6_addr32[1] = 0;
4052
4053 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
4054 ipv6_addr_equal(&ifp->addr, &addr)) {
4055 /* DAD failed for link-local based on MAC */
4056 idev->cnf.disable_ipv6 = 1;
4057
4058 pr_info("%s: IPv6 being disabled!\n",
4059 ifp->idev->dev->name);
4060 disable_ipv6 = true;
4061 }
4062 }
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004063 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01004064 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004065
4066 if (action == DAD_BEGIN) {
4067 addrconf_dad_begin(ifp);
4068 goto out;
4069 } else if (action == DAD_ABORT) {
Wei Yongjun751eb6b2016-09-05 16:06:31 +08004070 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004071 addrconf_dad_stop(ifp, 1);
Mike Manning85b51b12016-08-18 14:39:40 +01004072 if (disable_ipv6)
4073 addrconf_ifdown(idev->dev, 0);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004074 goto out;
4075 }
4076
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004077 if (!ifp->dad_probes && addrconf_dad_end(ifp))
Herbert Xuf2344a12010-05-18 15:55:27 -07004078 goto out;
4079
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004080 write_lock_bh(&idev->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00004081 if (idev->dead || !(idev->if_flags & IF_READY)) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004082 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 goto out;
4084 }
stephen hemminger21809fa2010-02-08 19:48:52 +00004085
4086 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07004087 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
4088 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004089 write_unlock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07004090 goto out;
4091 }
4092
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004093 if (ifp->dad_probes == 0) {
David Ahernc76fe2d2018-01-25 20:16:29 -08004094 bool send_na = false;
4095
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 /*
4097 * DAD was successful
4098 */
4099
David Ahernc76fe2d2018-01-25 20:16:29 -08004100 if (ifp->flags & IFA_F_TENTATIVE &&
4101 !(ifp->flags & IFA_F_OPTIMISTIC))
4102 send_na = true;
Paolo Abeni764d3be2016-11-22 16:57:40 +01004103 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00004104 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00004105 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004106 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
David Ahernc76fe2d2018-01-25 20:16:29 -08004108 addrconf_dad_completed(ifp, bump_id, send_na);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
4110 goto out;
4111 }
4112
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004113 ifp->dad_probes--;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004114 addrconf_mod_dad_work(ifp,
4115 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
stephen hemminger21809fa2010-02-08 19:48:52 +00004116 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004117 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118
4119 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -08004121 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
4122 ifp->dad_nonce);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123out:
4124 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004125 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126}
4127
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004128/* ifp->idev must be at least read locked */
4129static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
4130{
4131 struct inet6_ifaddr *ifpiter;
4132 struct inet6_dev *idev = ifp->idev;
4133
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01004134 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
4135 if (ifpiter->scope > IFA_LINK)
4136 break;
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004137 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
4138 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
4139 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
4140 IFA_F_PERMANENT)
4141 return false;
4142 }
4143 return true;
4144}
4145
David Ahernc76fe2d2018-01-25 20:16:29 -08004146static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id,
4147 bool send_na)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004149 struct net_device *dev = ifp->idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004150 struct in6_addr lladdr;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004151 bool send_rs, send_mld;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004152
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004153 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
4155 /*
4156 * Configure the address for reception. Now it is valid.
4157 */
4158
4159 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4160
Tore Anderson026359b2011-08-28 23:47:33 +00004161 /* If added prefix is link local and we are prepared to process
4162 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 */
4164
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004165 read_lock_bh(&ifp->idev->lock);
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004166 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004167 send_rs = send_mld &&
4168 ipv6_accept_ra(ifp->idev) &&
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004169 ifp->idev->cnf.rtr_solicits != 0 &&
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004170 (dev->flags&IFF_LOOPBACK) == 0;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004171 read_unlock_bh(&ifp->idev->lock);
4172
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004173 /* While dad is in progress mld report's source address is in6_addrany.
4174 * Resend with proper ll now.
4175 */
4176 if (send_mld)
4177 ipv6_mc_dad_complete(ifp->idev);
4178
David Ahernc76fe2d2018-01-25 20:16:29 -08004179 /* send unsolicited NA if enabled */
4180 if (send_na &&
4181 (ifp->idev->cnf.ndisc_notify ||
4182 dev_net(dev)->ipv6.devconf_all->ndisc_notify)) {
4183 ndisc_send_na(dev, &in6addr_linklocal_allnodes, &ifp->addr,
4184 /*router=*/ !!ifp->idev->cnf.forwarding,
4185 /*solicited=*/ false, /*override=*/ true,
4186 /*inc_opt=*/ true);
4187 }
4188
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004189 if (send_rs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 /*
4191 * If a host as already performed a random delay
4192 * [...] as part of DAD [...] there is no need
4193 * to delay again before sending the first RS
4194 */
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004195 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004196 return;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004197 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004199 write_lock_bh(&ifp->idev->lock);
4200 spin_lock(&ifp->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004201 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4202 ifp->idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004203 ifp->idev->rs_probes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 ifp->idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004205 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004206 spin_unlock(&ifp->lock);
4207 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 }
Paolo Abeni764d3be2016-11-22 16:57:40 +01004209
4210 if (bump_id)
4211 rt_genid_bump_ipv6(dev_net(dev));
Marcus Huewea11a7f72017-02-06 18:34:56 +01004212
4213 /* Make sure that a new temporary address will be created
4214 * before this temporary address becomes deprecated.
4215 */
4216 if (ifp->flags & IFA_F_TEMPORARY)
4217 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218}
4219
Hangbin Liu896585d2018-11-21 21:52:33 +08004220static void addrconf_dad_run(struct inet6_dev *idev, bool restart)
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004221{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004222 struct inet6_ifaddr *ifp;
4223
4224 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00004225 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00004226 spin_lock(&ifp->lock);
Hangbin Liu896585d2018-11-21 21:52:33 +08004227 if ((ifp->flags & IFA_F_TENTATIVE &&
4228 ifp->state == INET6_IFADDR_STATE_DAD) || restart) {
4229 if (restart)
4230 ifp->state = INET6_IFADDR_STATE_PREDAD;
Herbert Xuf2344a12010-05-18 15:55:27 -07004231 addrconf_dad_kick(ifp);
Hangbin Liu896585d2018-11-21 21:52:33 +08004232 }
stephen hemminger21809fa2010-02-08 19:48:52 +00004233 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004234 }
4235 read_unlock_bh(&idev->lock);
4236}
4237
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238#ifdef CONFIG_PROC_FS
4239struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08004240 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004242 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243};
4244
Mihai Maruseac1d578302012-01-03 23:31:35 +00004245static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004248 struct net *net = seq_file_net(seq);
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004249 struct inet6_ifaddr *ifa = NULL;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004250 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
Mihai Maruseac1d578302012-01-03 23:31:35 +00004252 /* initial bucket if pos is 0 */
4253 if (pos == 0) {
4254 state->bucket = 0;
4255 state->offset = 0;
4256 }
4257
4258 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004259 hlist_for_each_entry_rcu(ifa, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00004260 addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004261 if (!net_eq(dev_net(ifa->idev->dev), net))
4262 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004263 /* sync with offset */
4264 if (p < state->offset) {
4265 p++;
4266 continue;
4267 }
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004268 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004269 }
4270
4271 /* prepare for next bucket */
4272 state->offset = 0;
4273 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004275 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276}
4277
stephen hemmingerc2e21292010-03-17 20:31:10 +00004278static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4279 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280{
4281 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004282 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004284 hlist_for_each_entry_continue_rcu(ifa, addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004285 if (!net_eq(dev_net(ifa->idev->dev), net))
4286 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004287 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004288 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004289 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004290
Jeff Barnhill86f9bd12018-09-21 00:45:27 +00004291 state->offset = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004292 while (++state->bucket < IN6_ADDR_HSIZE) {
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004293 hlist_for_each_entry_rcu(ifa,
stephen hemmingerc2e21292010-03-17 20:31:10 +00004294 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004295 if (!net_eq(dev_net(ifa->idev->dev), net))
4296 continue;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004297 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08004298 }
4299 }
4300
stephen hemmingerc2e21292010-03-17 20:31:10 +00004301 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302}
4303
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004305 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306{
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004307 rcu_read_lock();
Mihai Maruseac1d578302012-01-03 23:31:35 +00004308 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309}
4310
4311static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4312{
4313 struct inet6_ifaddr *ifa;
4314
4315 ifa = if6_get_next(seq, v);
4316 ++*pos;
4317 return ifa;
4318}
4319
4320static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004321 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322{
Eric Dumazeta5c1d982017-10-23 16:17:50 -07004323 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324}
4325
4326static int if6_seq_show(struct seq_file *seq, void *v)
4327{
4328 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Jiri Pirko971a3512013-12-10 13:56:29 +01004329 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07004330 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 ifp->idev->dev->ifindex,
4332 ifp->prefix_len,
4333 ifp->scope,
Jiri Pirko971a3512013-12-10 13:56:29 +01004334 (u8) ifp->flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 ifp->idev->dev->name);
4336 return 0;
4337}
4338
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004339static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 .start = if6_seq_start,
4341 .next = if6_seq_next,
4342 .show = if6_seq_show,
4343 .stop = if6_seq_stop,
4344};
4345
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004346static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347{
Christoph Hellwigc3506372018-04-10 19:42:55 +02004348 if (!proc_create_net("if_inet6", 0444, net->proc_net, &if6_seq_ops,
4349 sizeof(struct if6_iter_state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 return -ENOMEM;
4351 return 0;
4352}
4353
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004354static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08004355{
Gao fengece31ff2013-02-18 01:34:56 +00004356 remove_proc_entry("if_inet6", net->proc_net);
Daniel Lezcano3c400902008-01-10 22:42:49 -08004357}
4358
4359static struct pernet_operations if6_proc_net_ops = {
Ian Morris67ba4152014-08-24 21:53:10 +01004360 .init = if6_proc_net_init,
4361 .exit = if6_proc_net_exit,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004362};
4363
4364int __init if6_proc_init(void)
4365{
4366 return register_pernet_subsys(&if6_proc_net_ops);
4367}
4368
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369void if6_proc_exit(void)
4370{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004371 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372}
4373#endif /* CONFIG_PROC_FS */
4374
Amerigo Wang07a93622012-10-29 16:23:10 +00004375#if IS_ENABLED(CONFIG_IPV6_MIP6)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004376/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004377int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004378{
Eric Dumazet3f27fb22017-10-23 16:17:47 -07004379 unsigned int hash = inet6_addr_hash(net, addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004380 struct inet6_ifaddr *ifp = NULL;
Eric Dumazet3f27fb22017-10-23 16:17:47 -07004381 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004382
Eric Dumazet4e5f47a2017-10-23 16:17:51 -07004383 rcu_read_lock();
4384 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09004385 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08004386 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09004387 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004388 (ifp->flags & IFA_F_HOMEADDRESS)) {
4389 ret = 1;
4390 break;
4391 }
4392 }
Eric Dumazet4e5f47a2017-10-23 16:17:51 -07004393 rcu_read_unlock();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004394 return ret;
4395}
4396#endif
4397
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398/*
4399 * Periodic address status verification
4400 */
4401
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004402static void addrconf_verify_rtnl(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004404 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 int i;
4407
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004408 ASSERT_RTNL();
4409
stephen hemminger5c578aed2010-03-17 20:31:11 +00004410 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004412 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004414 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004416 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417restart:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004418 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
Yasushi Asanofad8da32013-12-31 12:04:19 +09004421 /* When setting preferred_lft to a value not zero or
4422 * infinity, while valid_lft is infinity
4423 * IFA_F_PERMANENT has a non-infinity life time.
4424 */
4425 if ((ifp->flags & IFA_F_PERMANENT) &&
4426 (ifp->prefered_lft == INFINITY_LIFE_TIME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 continue;
4428
4429 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004430 /* We try to batch several events at once. */
4431 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004433 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4434 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 spin_unlock(&ifp->lock);
4436 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 ipv6_del_addr(ifp);
4438 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004439 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4440 spin_unlock(&ifp->lock);
4441 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00004443 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 int deprecate = 0;
4445
4446 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4447 deprecate = 1;
4448 ifp->flags |= IFA_F_DEPRECATED;
4449 }
4450
Yasushi Asanofad8da32013-12-31 12:04:19 +09004451 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4452 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 next = ifp->tstamp + ifp->valid_lft * HZ;
4454
4455 spin_unlock(&ifp->lock);
4456
4457 if (deprecate) {
4458 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
4460 ipv6_ifa_notify(0, ifp);
4461 in6_ifa_put(ifp);
4462 goto restart;
4463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4465 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00004466 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4467 ifp->idev->cnf.dad_transmits *
Jiri Pirko1f9248e2013-12-07 19:26:53 +01004468 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
stephen hemminger88949cf2010-03-17 20:31:17 +00004469
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 if (age >= ifp->prefered_lft - regen_advance) {
4471 struct inet6_ifaddr *ifpub = ifp->ifpub;
4472 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4473 next = ifp->tstamp + ifp->prefered_lft * HZ;
4474 if (!ifp->regen_count && ifpub) {
4475 ifp->regen_count++;
4476 in6_ifa_hold(ifp);
4477 in6_ifa_hold(ifpub);
4478 spin_unlock(&ifp->lock);
stephen hemminger5c578aed2010-03-17 20:31:11 +00004479
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08004480 spin_lock(&ifpub->lock);
4481 ifpub->regen_count = 0;
4482 spin_unlock(&ifpub->lock);
Eric Dumazete64e4692018-01-26 16:10:43 -08004483 rcu_read_unlock_bh();
Eric Dumazetfffcefe2017-11-06 14:13:29 -08004484 ipv6_create_tempaddr(ifpub, ifp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 in6_ifa_put(ifpub);
4486 in6_ifa_put(ifp);
Eric Dumazete64e4692018-01-26 16:10:43 -08004487 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 goto restart;
4489 }
4490 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4491 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4492 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 } else {
4494 /* ifp->prefered_lft <= ifp->valid_lft */
4495 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4496 next = ifp->tstamp + ifp->prefered_lft * HZ;
4497 spin_unlock(&ifp->lock);
4498 }
4499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 }
4501
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004502 next_sec = round_jiffies_up(next);
4503 next_sched = next;
4504
4505 /* If rounded timeout is accurate enough, accept it. */
4506 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4507 next_sched = next_sec;
4508
4509 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4510 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4511 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4512
Joe Perchese32ac252018-03-26 08:35:01 -07004513 pr_debug("now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4514 now, next, next_sec, next_sched);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004515 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
stephen hemminger5c578aed2010-03-17 20:31:11 +00004516 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517}
4518
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004519static void addrconf_verify_work(struct work_struct *w)
4520{
4521 rtnl_lock();
4522 addrconf_verify_rtnl();
4523 rtnl_unlock();
4524}
4525
4526static void addrconf_verify(void)
4527{
4528 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4529}
4530
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004531static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4532 struct in6_addr **peer_pfx)
Thomas Graf461d8832006-09-18 00:09:49 -07004533{
4534 struct in6_addr *pfx = NULL;
4535
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004536 *peer_pfx = NULL;
4537
Thomas Graf461d8832006-09-18 00:09:49 -07004538 if (addr)
4539 pfx = nla_data(addr);
4540
4541 if (local) {
4542 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004543 *peer_pfx = pfx;
4544 pfx = nla_data(local);
Thomas Graf461d8832006-09-18 00:09:49 -07004545 }
4546
4547 return pfx;
4548}
4549
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004550static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07004551 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4552 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4553 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
Jiri Pirko479840f2013-12-06 09:45:21 +01004554 [IFA_FLAGS] = { .len = sizeof(u32) },
David Ahern8308f3f2018-05-27 08:09:58 -07004555 [IFA_RT_PRIORITY] = { .len = sizeof(u32) },
Christian Brauner6ecf4c32018-09-04 21:53:50 +02004556 [IFA_TARGET_NETNSID] = { .type = NLA_S32 },
Thomas Graf461d8832006-09-18 00:09:49 -07004557};
4558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559static int
David Ahernc21ef3e2017-04-16 09:48:24 -07004560inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
4561 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004563 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07004564 struct ifaddrmsg *ifm;
4565 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004566 struct in6_addr *pfx, *peer_pfx;
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004567 u32 ifa_flags;
Thomas Grafb933f712006-09-18 00:10:19 -07004568 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
Johannes Berg8cb08172019-04-26 14:07:28 +02004570 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
4571 ifa_ipv6_policy, extack);
Thomas Grafb933f712006-09-18 00:10:19 -07004572 if (err < 0)
4573 return err;
4574
4575 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004576 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004577 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 return -EINVAL;
4579
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004580 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4581
4582 /* We ignore other flags so far. */
4583 ifa_flags &= IFA_F_MANAGETEMPADDR;
4584
4585 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4586 ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587}
4588
David Ahern8308f3f2018-05-27 08:09:58 -07004589static int modify_prefix_route(struct inet6_ifaddr *ifp,
4590 unsigned long expires, u32 flags)
4591{
4592 struct fib6_info *f6i;
David Aherne7c7faa2018-06-28 13:36:55 -07004593 u32 prio;
David Ahern8308f3f2018-05-27 08:09:58 -07004594
David Ahern2b2450c2019-03-27 20:53:52 -07004595 f6i = addrconf_get_prefix_route(&ifp->addr, ifp->prefix_len,
4596 ifp->idev->dev, 0, RTF_DEFAULT, true);
David Ahern8308f3f2018-05-27 08:09:58 -07004597 if (!f6i)
4598 return -ENOENT;
4599
David Aherne7c7faa2018-06-28 13:36:55 -07004600 prio = ifp->rt_priority ? : IP6_RT_PRIO_ADDRCONF;
4601 if (f6i->fib6_metric != prio) {
4602 /* delete old one */
4603 ip6_del_rt(dev_net(ifp->idev->dev), f6i);
4604
David Ahern8308f3f2018-05-27 08:09:58 -07004605 /* add new one */
4606 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
4607 ifp->rt_priority, ifp->idev->dev,
4608 expires, flags, GFP_KERNEL);
David Ahern8308f3f2018-05-27 08:09:58 -07004609 } else {
4610 if (!expires)
4611 fib6_clean_expires(f6i);
4612 else
4613 fib6_set_expires(f6i, expires);
4614
4615 fib6_info_release(f6i);
4616 }
4617
4618 return 0;
4619}
4620
David Ahernd169a1f2018-05-27 08:09:55 -07004621static int inet6_addr_modify(struct inet6_ifaddr *ifp, struct ifa6_config *cfg)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004622{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004623 u32 flags;
4624 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004625 unsigned long timeout;
Jiri Pirko53bd6742013-12-06 09:45:22 +01004626 bool was_managetempaddr;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004627 bool had_prefixroute;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09004628
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004629 ASSERT_RTNL();
4630
David Ahernd169a1f2018-05-27 08:09:55 -07004631 if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004632 return -EINVAL;
4633
David Ahernd169a1f2018-05-27 08:09:55 -07004634 if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR &&
Jiri Pirko53bd6742013-12-06 09:45:22 +01004635 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4636 return -EINVAL;
4637
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01004638 if (!(ifp->flags & IFA_F_TENTATIVE) || ifp->flags & IFA_F_DADFAILED)
David Ahernd169a1f2018-05-27 08:09:55 -07004639 cfg->ifa_flags &= ~IFA_F_OPTIMISTIC;
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01004640
David Ahernd169a1f2018-05-27 08:09:55 -07004641 timeout = addrconf_timeout_fixup(cfg->valid_lft, HZ);
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004642 if (addrconf_finite_timeout(timeout)) {
4643 expires = jiffies_to_clock_t(timeout * HZ);
David Ahernd169a1f2018-05-27 08:09:55 -07004644 cfg->valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004645 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004646 } else {
4647 expires = 0;
4648 flags = 0;
David Ahernd169a1f2018-05-27 08:09:55 -07004649 cfg->ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004650 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004651
David Ahernd169a1f2018-05-27 08:09:55 -07004652 timeout = addrconf_timeout_fixup(cfg->preferred_lft, HZ);
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004653 if (addrconf_finite_timeout(timeout)) {
4654 if (timeout == 0)
David Ahernd169a1f2018-05-27 08:09:55 -07004655 cfg->ifa_flags |= IFA_F_DEPRECATED;
4656 cfg->preferred_lft = timeout;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004657 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004658
4659 spin_lock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004660 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004661 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4662 !(ifp->flags & IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004663 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
Thomas Haller761aac72014-01-15 15:36:58 +01004664 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4665 IFA_F_NOPREFIXROUTE);
David Ahernd169a1f2018-05-27 08:09:55 -07004666 ifp->flags |= cfg->ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004667 ifp->tstamp = jiffies;
David Ahernd169a1f2018-05-27 08:09:55 -07004668 ifp->valid_lft = cfg->valid_lft;
4669 ifp->prefered_lft = cfg->preferred_lft;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004670
David Ahern8308f3f2018-05-27 08:09:58 -07004671 if (cfg->rt_priority && cfg->rt_priority != ifp->rt_priority)
4672 ifp->rt_priority = cfg->rt_priority;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004673
4674 spin_unlock_bh(&ifp->lock);
4675 if (!(ifp->flags&IFA_F_TENTATIVE))
4676 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004677
David Ahernd169a1f2018-05-27 08:09:55 -07004678 if (!(cfg->ifa_flags & IFA_F_NOPREFIXROUTE)) {
David Ahern8308f3f2018-05-27 08:09:58 -07004679 int rc = -ENOENT;
4680
4681 if (had_prefixroute)
4682 rc = modify_prefix_route(ifp, expires, flags);
4683
4684 /* prefix route could have been deleted; if so restore it */
4685 if (rc == -ENOENT) {
4686 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
4687 ifp->rt_priority, ifp->idev->dev,
4688 expires, flags, GFP_KERNEL);
4689 }
Thomas Haller5b84efe2014-01-15 15:36:59 +01004690 } else if (had_prefixroute) {
4691 enum cleanup_prefix_rt_t action;
4692 unsigned long rt_expires;
4693
4694 write_lock_bh(&ifp->idev->lock);
4695 action = check_cleanup_prefix_route(ifp, &rt_expires);
4696 write_unlock_bh(&ifp->idev->lock);
4697
4698 if (action != CLEANUP_PREFIX_RT_NOP) {
4699 cleanup_prefix_route(ifp, rt_expires,
4700 action == CLEANUP_PREFIX_RT_DEL);
4701 }
Thomas Haller761aac72014-01-15 15:36:58 +01004702 }
Jiri Pirko53bd6742013-12-06 09:45:22 +01004703
4704 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
David Ahernd169a1f2018-05-27 08:09:55 -07004705 if (was_managetempaddr &&
4706 !(ifp->flags & IFA_F_MANAGETEMPADDR)) {
4707 cfg->valid_lft = 0;
4708 cfg->preferred_lft = 0;
4709 }
4710 manage_tempaddrs(ifp->idev, ifp, cfg->valid_lft,
4711 cfg->preferred_lft, !was_managetempaddr,
4712 jiffies);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004713 }
4714
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004715 addrconf_verify_rtnl();
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004716
4717 return 0;
4718}
4719
4720static int
David Ahernc21ef3e2017-04-16 09:48:24 -07004721inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
4722 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004724 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07004725 struct ifaddrmsg *ifm;
4726 struct nlattr *tb[IFA_MAX+1];
David Ahern19b15182018-05-27 08:09:54 -07004727 struct in6_addr *peer_pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07004728 struct inet6_ifaddr *ifa;
4729 struct net_device *dev;
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01004730 struct inet6_dev *idev;
David Ahern19b15182018-05-27 08:09:54 -07004731 struct ifa6_config cfg;
Thomas Graf461d8832006-09-18 00:09:49 -07004732 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
Johannes Berg8cb08172019-04-26 14:07:28 +02004734 err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
4735 ifa_ipv6_policy, extack);
Thomas Graf461d8832006-09-18 00:09:49 -07004736 if (err < 0)
4737 return err;
4738
David Ahern19b15182018-05-27 08:09:54 -07004739 memset(&cfg, 0, sizeof(cfg));
4740
Thomas Graf461d8832006-09-18 00:09:49 -07004741 ifm = nlmsg_data(nlh);
David Ahern19b15182018-05-27 08:09:54 -07004742 cfg.pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
4743 if (!cfg.pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 return -EINVAL;
4745
David Ahern19b15182018-05-27 08:09:54 -07004746 cfg.peer_pfx = peer_pfx;
4747 cfg.plen = ifm->ifa_prefixlen;
David Ahern8308f3f2018-05-27 08:09:58 -07004748 if (tb[IFA_RT_PRIORITY])
4749 cfg.rt_priority = nla_get_u32(tb[IFA_RT_PRIORITY]);
4750
David Ahern19b15182018-05-27 08:09:54 -07004751 cfg.valid_lft = INFINITY_LIFE_TIME;
4752 cfg.preferred_lft = INFINITY_LIFE_TIME;
4753
Thomas Graf461d8832006-09-18 00:09:49 -07004754 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004755 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07004756
4757 ci = nla_data(tb[IFA_CACHEINFO]);
David Ahern19b15182018-05-27 08:09:54 -07004758 cfg.valid_lft = ci->ifa_valid;
4759 cfg.preferred_lft = ci->ifa_prefered;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004760 }
4761
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004762 dev = __dev_get_by_index(net, ifm->ifa_index);
Ian Morris63159f22015-03-29 14:00:04 +01004763 if (!dev)
Thomas Graf7198f8c2006-09-18 00:13:46 -07004764 return -ENODEV;
4765
David Ahern19b15182018-05-27 08:09:54 -07004766 if (tb[IFA_FLAGS])
4767 cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]);
4768 else
4769 cfg.ifa_flags = ifm->ifa_flags;
Jiri Pirko479840f2013-12-06 09:45:21 +01004770
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004771 /* We ignore other flags so far. */
David Ahern19b15182018-05-27 08:09:54 -07004772 cfg.ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS |
4773 IFA_F_MANAGETEMPADDR | IFA_F_NOPREFIXROUTE |
4774 IFA_F_MCAUTOJOIN | IFA_F_OPTIMISTIC;
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01004775
4776 idev = ipv6_find_idev(dev);
Christophe JAILLET178fe942018-12-28 23:28:21 +01004777 if (!idev)
4778 return -ENOBUFS;
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01004779
4780 if (!ipv6_allow_optimistic_dad(net, idev))
David Ahern19b15182018-05-27 08:09:54 -07004781 cfg.ifa_flags &= ~IFA_F_OPTIMISTIC;
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01004782
David Ahern19b15182018-05-27 08:09:54 -07004783 if (cfg.ifa_flags & IFA_F_NODAD &&
4784 cfg.ifa_flags & IFA_F_OPTIMISTIC) {
Sabrina Dubrocaf1c02cf2018-02-28 16:40:08 +01004785 NL_SET_ERR_MSG(extack, "IFA_F_NODAD and IFA_F_OPTIMISTIC are mutually exclusive");
4786 return -EINVAL;
4787 }
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004788
David Ahern19b15182018-05-27 08:09:54 -07004789 ifa = ipv6_get_ifaddr(net, cfg.pfx, dev, 1);
Ian Morris63159f22015-03-29 14:00:04 +01004790 if (!ifa) {
Thomas Graf7198f8c2006-09-18 00:13:46 -07004791 /*
4792 * It would be best to check for !NLM_F_CREATE here but
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08004793 * userspace already relies on not having to provide this.
Thomas Graf7198f8c2006-09-18 00:13:46 -07004794 */
David Ahern19b15182018-05-27 08:09:54 -07004795 return inet6_addr_add(net, ifm->ifa_index, &cfg, extack);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004796 }
4797
Thomas Graf7198f8c2006-09-18 00:13:46 -07004798 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4799 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4800 err = -EEXIST;
4801 else
David Ahernd169a1f2018-05-27 08:09:55 -07004802 err = inet6_addr_modify(ifa, &cfg);
Thomas Graf7198f8c2006-09-18 00:13:46 -07004803
4804 in6_ifa_put(ifa);
4805
4806 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807}
4808
Jiri Pirko479840f2013-12-06 09:45:21 +01004809static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
Thomas Graf101bb222006-09-18 00:11:52 -07004810 u8 scope, int ifindex)
4811{
4812 struct ifaddrmsg *ifm;
4813
4814 ifm = nlmsg_data(nlh);
4815 ifm->ifa_family = AF_INET6;
4816 ifm->ifa_prefixlen = prefixlen;
4817 ifm->ifa_flags = flags;
4818 ifm->ifa_scope = scope;
4819 ifm->ifa_index = ifindex;
4820}
4821
Thomas Graf85486af2006-09-18 00:11:24 -07004822static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4823 unsigned long tstamp, u32 preferred, u32 valid)
4824{
4825 struct ifa_cacheinfo ci;
4826
Thomas Graf18a31e12010-11-17 04:12:02 +00004827 ci.cstamp = cstamp_delta(cstamp);
4828 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07004829 ci.ifa_prefered = preferred;
4830 ci.ifa_valid = valid;
4831
4832 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4833}
4834
Thomas Graf101bb222006-09-18 00:11:52 -07004835static inline int rt_scope(int ifa_scope)
4836{
4837 if (ifa_scope & IFA_HOST)
4838 return RT_SCOPE_HOST;
4839 else if (ifa_scope & IFA_LINK)
4840 return RT_SCOPE_LINK;
4841 else if (ifa_scope & IFA_SITE)
4842 return RT_SCOPE_SITE;
4843 else
4844 return RT_SCOPE_UNIVERSE;
4845}
4846
Thomas Graf0ab68032006-09-18 00:12:35 -07004847static inline int inet6_ifaddr_msgsize(void)
4848{
Thomas Graf339bf982006-11-10 14:10:15 -08004849 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004850 + nla_total_size(16) /* IFA_LOCAL */
Thomas Graf339bf982006-11-10 14:10:15 -08004851 + nla_total_size(16) /* IFA_ADDRESS */
Jiri Pirko479840f2013-12-06 09:45:21 +01004852 + nla_total_size(sizeof(struct ifa_cacheinfo))
David Ahern8308f3f2018-05-27 08:09:58 -07004853 + nla_total_size(4) /* IFA_FLAGS */
4854 + nla_total_size(4) /* IFA_RT_PRIORITY */;
Thomas Graf0ab68032006-09-18 00:12:35 -07004855}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004856
David Ahern6ba1e6e2018-10-07 20:16:26 -07004857enum addr_type_t {
4858 UNICAST_ADDR,
4859 MULTICAST_ADDR,
4860 ANYCAST_ADDR,
4861};
4862
Christian Brauner203651b2018-09-04 21:53:55 +02004863struct inet6_fill_args {
4864 u32 portid;
4865 u32 seq;
4866 int event;
4867 unsigned int flags;
4868 int netnsid;
David Ahern6371a712018-10-19 12:45:30 -07004869 int ifindex;
David Ahern6ba1e6e2018-10-07 20:16:26 -07004870 enum addr_type_t type;
Christian Brauner203651b2018-09-04 21:53:55 +02004871};
4872
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Christian Brauner203651b2018-09-04 21:53:55 +02004874 struct inet6_fill_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07004877 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878
Christian Brauner203651b2018-09-04 21:53:55 +02004879 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
4880 sizeof(struct ifaddrmsg), args->flags);
Ian Morris63159f22015-03-29 14:00:04 +01004881 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004882 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004883
Thomas Graf101bb222006-09-18 00:11:52 -07004884 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4885 ifa->idev->dev->ifindex);
4886
Christian Brauner203651b2018-09-04 21:53:55 +02004887 if (args->netnsid >= 0 &&
4888 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
Christian Brauner6ecf4c32018-09-04 21:53:50 +02004889 goto error;
4890
Yasushi Asanofad8da32013-12-31 12:04:19 +09004891 if (!((ifa->flags&IFA_F_PERMANENT) &&
4892 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
Thomas Graf85486af2006-09-18 00:11:24 -07004893 preferred = ifa->prefered_lft;
4894 valid = ifa->valid_lft;
4895 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00004897 if (preferred > tval)
4898 preferred -= tval;
4899 else
4900 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00004901 if (valid != INFINITY_LIFE_TIME) {
4902 if (valid > tval)
4903 valid -= tval;
4904 else
4905 valid = 0;
4906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 }
4908 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07004909 preferred = INFINITY_LIFE_TIME;
4910 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 }
Thomas Graf85486af2006-09-18 00:11:24 -07004912
Cong Wang7996c792013-05-22 05:41:06 +00004913 if (!ipv6_addr_any(&ifa->peer_addr)) {
Jiri Benc930345e2015-03-29 16:59:25 +02004914 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4915 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004916 goto error;
4917 } else
Jiri Benc930345e2015-03-29 16:59:25 +02004918 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004919 goto error;
4920
David Ahern8308f3f2018-05-27 08:09:58 -07004921 if (ifa->rt_priority &&
4922 nla_put_u32(skb, IFA_RT_PRIORITY, ifa->rt_priority))
4923 goto error;
4924
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004925 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4926 goto error;
Thomas Graf85486af2006-09-18 00:11:24 -07004927
Jiri Pirko479840f2013-12-06 09:45:21 +01004928 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4929 goto error;
4930
Johannes Berg053c0952015-01-16 22:09:00 +01004931 nlmsg_end(skb, nlh);
4932 return 0;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004933
4934error:
4935 nlmsg_cancel(skb, nlh);
4936 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937}
4938
4939static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Christian Brauner203651b2018-09-04 21:53:55 +02004940 struct inet6_fill_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004943 u8 scope = RT_SCOPE_UNIVERSE;
4944 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945
Thomas Graf101bb222006-09-18 00:11:52 -07004946 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4947 scope = RT_SCOPE_SITE;
4948
Christian Brauner203651b2018-09-04 21:53:55 +02004949 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
4950 sizeof(struct ifaddrmsg), args->flags);
Ian Morris63159f22015-03-29 14:00:04 +01004951 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004952 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004953
Christian Brauner203651b2018-09-04 21:53:55 +02004954 if (args->netnsid >= 0 &&
4955 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
Christian Brauner6ecf4c32018-09-04 21:53:50 +02004956 return -EMSGSIZE;
4957
Thomas Graf101bb222006-09-18 00:11:52 -07004958 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004959 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004960 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004961 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4962 nlmsg_cancel(skb, nlh);
4963 return -EMSGSIZE;
4964 }
Thomas Graf85486af2006-09-18 00:11:24 -07004965
Johannes Berg053c0952015-01-16 22:09:00 +01004966 nlmsg_end(skb, nlh);
4967 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968}
4969
4970static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Christian Brauner203651b2018-09-04 21:53:55 +02004971 struct inet6_fill_args *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972{
David Ahern9ee8cbb2018-04-18 15:39:01 -07004973 struct net_device *dev = fib6_info_nh_dev(ifaca->aca_rt);
4974 int ifindex = dev ? dev->ifindex : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004976 u8 scope = RT_SCOPE_UNIVERSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
Thomas Graf101bb222006-09-18 00:11:52 -07004978 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4979 scope = RT_SCOPE_SITE;
4980
Christian Brauner203651b2018-09-04 21:53:55 +02004981 nlh = nlmsg_put(skb, args->portid, args->seq, args->event,
4982 sizeof(struct ifaddrmsg), args->flags);
Ian Morris63159f22015-03-29 14:00:04 +01004983 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004984 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004985
Christian Brauner203651b2018-09-04 21:53:55 +02004986 if (args->netnsid >= 0 &&
4987 nla_put_s32(skb, IFA_TARGET_NETNSID, args->netnsid))
Christian Brauner6ecf4c32018-09-04 21:53:50 +02004988 return -EMSGSIZE;
4989
Thomas Graf101bb222006-09-18 00:11:52 -07004990 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004991 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004992 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004993 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4994 nlmsg_cancel(skb, nlh);
4995 return -EMSGSIZE;
4996 }
Thomas Graf85486af2006-09-18 00:11:24 -07004997
Johannes Berg053c0952015-01-16 22:09:00 +01004998 nlmsg_end(skb, nlh);
4999 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000}
5001
Eric Dumazet234b27c2009-11-12 04:11:50 +00005002/* called with rcu_read_lock() */
5003static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
David Ahernfe884c22018-10-19 12:45:28 -07005004 struct netlink_callback *cb, int s_ip_idx,
David Ahern6ba1e6e2018-10-07 20:16:26 -07005005 struct inet6_fill_args *fillargs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 struct ifmcaddr6 *ifmca;
5008 struct ifacaddr6 *ifaca;
David Ahernfe884c22018-10-19 12:45:28 -07005009 int ip_idx = 0;
Eric Dumazet234b27c2009-11-12 04:11:50 +00005010 int err = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011
Eric Dumazet234b27c2009-11-12 04:11:50 +00005012 read_lock_bh(&idev->lock);
David Ahern6ba1e6e2018-10-07 20:16:26 -07005013 switch (fillargs->type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00005014 case UNICAST_ADDR: {
5015 struct inet6_ifaddr *ifa;
David Ahern6ba1e6e2018-10-07 20:16:26 -07005016 fillargs->event = RTM_NEWADDR;
stephen hemminger502a2ff2010-03-17 20:31:13 +00005017
Eric Dumazet234b27c2009-11-12 04:11:50 +00005018 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00005019 list_for_each_entry(ifa, &idev->addr_list, if_list) {
David Ahern4ba4c562018-10-19 10:00:19 -07005020 if (ip_idx < s_ip_idx)
5021 goto next;
David Ahern6ba1e6e2018-10-07 20:16:26 -07005022 err = inet6_fill_ifaddr(skb, ifa, fillargs);
Johannes Berg053c0952015-01-16 22:09:00 +01005023 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00005024 break;
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005025 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
David Ahern4ba4c562018-10-19 10:00:19 -07005026next:
5027 ip_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00005029 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00005030 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00005031 case MULTICAST_ADDR:
David Ahern6ba1e6e2018-10-07 20:16:26 -07005032 fillargs->event = RTM_GETMULTICAST;
Christian Brauner203651b2018-09-04 21:53:55 +02005033
Eric Dumazet234b27c2009-11-12 04:11:50 +00005034 /* multicast address */
5035 for (ifmca = idev->mc_list; ifmca;
5036 ifmca = ifmca->next, ip_idx++) {
5037 if (ip_idx < s_ip_idx)
5038 continue;
David Ahern6ba1e6e2018-10-07 20:16:26 -07005039 err = inet6_fill_ifmcaddr(skb, ifmca, fillargs);
Johannes Berg053c0952015-01-16 22:09:00 +01005040 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00005041 break;
5042 }
5043 break;
5044 case ANYCAST_ADDR:
David Ahern6ba1e6e2018-10-07 20:16:26 -07005045 fillargs->event = RTM_GETANYCAST;
Eric Dumazet234b27c2009-11-12 04:11:50 +00005046 /* anycast address */
5047 for (ifaca = idev->ac_list; ifaca;
5048 ifaca = ifaca->aca_next, ip_idx++) {
5049 if (ip_idx < s_ip_idx)
5050 continue;
David Ahern6ba1e6e2018-10-07 20:16:26 -07005051 err = inet6_fill_ifacaddr(skb, ifaca, fillargs);
Johannes Berg053c0952015-01-16 22:09:00 +01005052 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00005053 break;
5054 }
5055 break;
5056 default:
5057 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00005059 read_unlock_bh(&idev->lock);
David Ahernfe884c22018-10-19 12:45:28 -07005060 cb->args[2] = ip_idx;
Eric Dumazet234b27c2009-11-12 04:11:50 +00005061 return err;
5062}
5063
David Aherned6eff12018-10-07 20:16:29 -07005064static int inet6_valid_dump_ifaddr_req(const struct nlmsghdr *nlh,
5065 struct inet6_fill_args *fillargs,
5066 struct net **tgt_net, struct sock *sk,
David Ahern6371a712018-10-19 12:45:30 -07005067 struct netlink_callback *cb)
David Aherned6eff12018-10-07 20:16:29 -07005068{
David Ahern6371a712018-10-19 12:45:30 -07005069 struct netlink_ext_ack *extack = cb->extack;
David Aherned6eff12018-10-07 20:16:29 -07005070 struct nlattr *tb[IFA_MAX+1];
5071 struct ifaddrmsg *ifm;
5072 int err, i;
5073
5074 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5075 NL_SET_ERR_MSG_MOD(extack, "Invalid header for address dump request");
5076 return -EINVAL;
5077 }
5078
5079 ifm = nlmsg_data(nlh);
5080 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5081 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for address dump request");
5082 return -EINVAL;
5083 }
David Ahern6371a712018-10-19 12:45:30 -07005084
5085 fillargs->ifindex = ifm->ifa_index;
5086 if (fillargs->ifindex) {
5087 cb->answer_flags |= NLM_F_DUMP_FILTERED;
5088 fillargs->flags |= NLM_F_DUMP_FILTERED;
David Aherned6eff12018-10-07 20:16:29 -07005089 }
5090
Johannes Berg8cb08172019-04-26 14:07:28 +02005091 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5092 ifa_ipv6_policy, extack);
David Aherned6eff12018-10-07 20:16:29 -07005093 if (err < 0)
5094 return err;
5095
5096 for (i = 0; i <= IFA_MAX; ++i) {
5097 if (!tb[i])
5098 continue;
5099
5100 if (i == IFA_TARGET_NETNSID) {
5101 struct net *net;
5102
5103 fillargs->netnsid = nla_get_s32(tb[i]);
5104 net = rtnl_get_net_ns_capable(sk, fillargs->netnsid);
5105 if (IS_ERR(net)) {
Bjørn Morkbf4cc402018-10-25 21:18:25 +02005106 fillargs->netnsid = -1;
David Aherned6eff12018-10-07 20:16:29 -07005107 NL_SET_ERR_MSG_MOD(extack, "Invalid target network namespace id");
5108 return PTR_ERR(net);
5109 }
5110 *tgt_net = net;
5111 } else {
5112 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in dump request");
5113 return -EINVAL;
5114 }
5115 }
5116
5117 return 0;
5118}
5119
Eric Dumazet234b27c2009-11-12 04:11:50 +00005120static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
5121 enum addr_type_t type)
5122{
David Aherned6eff12018-10-07 20:16:29 -07005123 const struct nlmsghdr *nlh = cb->nlh;
David Ahern6ba1e6e2018-10-07 20:16:26 -07005124 struct inet6_fill_args fillargs = {
5125 .portid = NETLINK_CB(cb->skb).portid,
5126 .seq = cb->nlh->nlmsg_seq,
5127 .flags = NLM_F_MULTI,
5128 .netnsid = -1,
5129 .type = type,
5130 };
Eric Dumazet234b27c2009-11-12 04:11:50 +00005131 struct net *net = sock_net(skb->sk);
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005132 struct net *tgt_net = net;
David Ahernfe884c22018-10-19 12:45:28 -07005133 int idx, s_idx, s_ip_idx;
Eric Dumazet234b27c2009-11-12 04:11:50 +00005134 int h, s_h;
Eric Dumazet234b27c2009-11-12 04:11:50 +00005135 struct net_device *dev;
5136 struct inet6_dev *idev;
5137 struct hlist_head *head;
David Ahern242afaa2018-10-24 12:59:00 -07005138 int err = 0;
Eric Dumazet234b27c2009-11-12 04:11:50 +00005139
5140 s_h = cb->args[0];
5141 s_idx = idx = cb->args[1];
David Ahernfe884c22018-10-19 12:45:28 -07005142 s_ip_idx = cb->args[2];
Eric Dumazet234b27c2009-11-12 04:11:50 +00005143
David Aherned6eff12018-10-07 20:16:29 -07005144 if (cb->strict_check) {
David Aherned6eff12018-10-07 20:16:29 -07005145 err = inet6_valid_dump_ifaddr_req(nlh, &fillargs, &tgt_net,
David Ahern6371a712018-10-19 12:45:30 -07005146 skb->sk, cb);
David Aherned6eff12018-10-07 20:16:29 -07005147 if (err < 0)
David Ahern242afaa2018-10-24 12:59:00 -07005148 goto put_tgt_net;
David Ahern6371a712018-10-19 12:45:30 -07005149
David Ahern242afaa2018-10-24 12:59:00 -07005150 err = 0;
David Ahern6371a712018-10-19 12:45:30 -07005151 if (fillargs.ifindex) {
5152 dev = __dev_get_by_index(tgt_net, fillargs.ifindex);
David Ahern242afaa2018-10-24 12:59:00 -07005153 if (!dev) {
5154 err = -ENODEV;
5155 goto put_tgt_net;
5156 }
David Ahern6371a712018-10-19 12:45:30 -07005157 idev = __in6_dev_get(dev);
5158 if (idev) {
5159 err = in6_dump_addrs(idev, skb, cb, s_ip_idx,
5160 &fillargs);
Jakub Kicinski15180392019-01-22 14:47:19 -08005161 if (err > 0)
5162 err = 0;
David Ahern6371a712018-10-19 12:45:30 -07005163 }
5164 goto put_tgt_net;
5165 }
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005166 }
5167
Eric Dumazet234b27c2009-11-12 04:11:50 +00005168 rcu_read_lock();
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005169 cb->seq = atomic_read(&tgt_net->ipv6.dev_addr_genid) ^ tgt_net->dev_base_seq;
Eric Dumazet234b27c2009-11-12 04:11:50 +00005170 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5171 idx = 0;
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005172 head = &tgt_net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08005173 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet234b27c2009-11-12 04:11:50 +00005174 if (idx < s_idx)
5175 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07005176 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00005177 s_ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005178 idev = __in6_dev_get(dev);
5179 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00005180 goto cont;
5181
David Ahernfe884c22018-10-19 12:45:28 -07005182 if (in6_dump_addrs(idev, skb, cb, s_ip_idx,
David Ahern6ba1e6e2018-10-07 20:16:26 -07005183 &fillargs) < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00005184 goto done;
5185cont:
5186 idx++;
5187 }
5188 }
5189done:
5190 rcu_read_unlock();
5191 cb->args[0] = h;
5192 cb->args[1] = idx;
David Ahern6371a712018-10-19 12:45:30 -07005193put_tgt_net:
David Ahern6ba1e6e2018-10-07 20:16:26 -07005194 if (fillargs.netnsid >= 0)
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005195 put_net(tgt_net);
Eric Dumazet234b27c2009-11-12 04:11:50 +00005196
Arthur Gautier7c1e8a32018-12-31 02:10:58 +00005197 return skb->len ? : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198}
5199
5200static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
5201{
5202 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11005203
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 return inet6_dump_addr(skb, cb, type);
5205}
5206
5207static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
5208{
5209 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11005210
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 return inet6_dump_addr(skb, cb, type);
5212}
5213
5214
5215static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
5216{
5217 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11005218
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 return inet6_dump_addr(skb, cb, type);
5220}
5221
Jakub Kicinski4b1373d2019-01-18 10:46:21 -08005222static int inet6_rtm_valid_getaddr_req(struct sk_buff *skb,
5223 const struct nlmsghdr *nlh,
5224 struct nlattr **tb,
5225 struct netlink_ext_ack *extack)
5226{
5227 struct ifaddrmsg *ifm;
5228 int i, err;
5229
5230 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5231 NL_SET_ERR_MSG_MOD(extack, "Invalid header for get address request");
5232 return -EINVAL;
5233 }
5234
5235 ifm = nlmsg_data(nlh);
5236 if (ifm->ifa_prefixlen || ifm->ifa_flags || ifm->ifa_scope) {
5237 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for get address request");
5238 return -EINVAL;
5239 }
5240
5241 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02005242 return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFA_MAX,
5243 ifa_ipv6_policy, extack);
Jakub Kicinski4b1373d2019-01-18 10:46:21 -08005244
Johannes Berg8cb08172019-04-26 14:07:28 +02005245 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFA_MAX,
5246 ifa_ipv6_policy, extack);
Jakub Kicinski4b1373d2019-01-18 10:46:21 -08005247 if (err)
5248 return err;
5249
5250 for (i = 0; i <= IFA_MAX; i++) {
5251 if (!tb[i])
5252 continue;
5253
5254 switch (i) {
5255 case IFA_TARGET_NETNSID:
5256 case IFA_ADDRESS:
5257 case IFA_LOCAL:
5258 break;
5259 default:
5260 NL_SET_ERR_MSG_MOD(extack, "Unsupported attribute in get address request");
5261 return -EINVAL;
5262 }
5263 }
5264
5265 return 0;
5266}
5267
David Ahernc21ef3e2017-04-16 09:48:24 -07005268static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
5269 struct netlink_ext_ack *extack)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005270{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005271 struct net *net = sock_net(in_skb->sk);
Christian Brauner203651b2018-09-04 21:53:55 +02005272 struct inet6_fill_args fillargs = {
5273 .portid = NETLINK_CB(in_skb).portid,
5274 .seq = nlh->nlmsg_seq,
5275 .event = RTM_NEWADDR,
5276 .flags = 0,
5277 .netnsid = -1,
5278 };
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005279 struct net *tgt_net = net;
Thomas Graf1b29fc22006-09-18 00:10:50 -07005280 struct ifaddrmsg *ifm;
5281 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005282 struct in6_addr *addr = NULL, *peer;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005283 struct net_device *dev = NULL;
5284 struct inet6_ifaddr *ifa;
5285 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005286 int err;
5287
Jakub Kicinski4b1373d2019-01-18 10:46:21 -08005288 err = inet6_rtm_valid_getaddr_req(in_skb, nlh, tb, extack);
Thomas Graf1b29fc22006-09-18 00:10:50 -07005289 if (err < 0)
Florian Westphalc24675f2017-10-11 10:28:01 +02005290 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005291
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005292 if (tb[IFA_TARGET_NETNSID]) {
Christian Brauner203651b2018-09-04 21:53:55 +02005293 fillargs.netnsid = nla_get_s32(tb[IFA_TARGET_NETNSID]);
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005294
5295 tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(in_skb).sk,
Christian Brauner203651b2018-09-04 21:53:55 +02005296 fillargs.netnsid);
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005297 if (IS_ERR(tgt_net))
5298 return PTR_ERR(tgt_net);
5299 }
5300
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005301 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
Florian Westphalc24675f2017-10-11 10:28:01 +02005302 if (!addr)
5303 return -EINVAL;
Thomas Graf1b29fc22006-09-18 00:10:50 -07005304
5305 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005306 if (ifm->ifa_index)
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005307 dev = dev_get_by_index(tgt_net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005308
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005309 ifa = ipv6_get_ifaddr(tgt_net, addr, dev, 1);
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005310 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07005311 err = -EADDRNOTAVAIL;
5312 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005313 }
5314
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005315 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
5316 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07005317 err = -ENOBUFS;
5318 goto errout_ifa;
5319 }
5320
Christian Brauner203651b2018-09-04 21:53:55 +02005321 err = inet6_fill_ifaddr(skb, ifa, &fillargs);
Patrick McHardy26932562007-01-31 23:16:40 -08005322 if (err < 0) {
5323 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5324 WARN_ON(err == -EMSGSIZE);
5325 kfree_skb(skb);
5326 goto errout_ifa;
5327 }
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005328 err = rtnl_unicast(skb, tgt_net, NETLINK_CB(in_skb).portid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07005329errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005330 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07005331errout:
Florian Westphalc24675f2017-10-11 10:28:01 +02005332 if (dev)
5333 dev_put(dev);
Christian Brauner203651b2018-09-04 21:53:55 +02005334 if (fillargs.netnsid >= 0)
Christian Brauner6ecf4c32018-09-04 21:53:50 +02005335 put_net(tgt_net);
5336
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005337 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09005338}
5339
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
5341{
5342 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005343 struct net *net = dev_net(ifa->idev->dev);
Christian Brauner203651b2018-09-04 21:53:55 +02005344 struct inet6_fill_args fillargs = {
5345 .portid = 0,
5346 .seq = 0,
5347 .event = event,
5348 .flags = 0,
5349 .netnsid = -1,
5350 };
Thomas Graf5d620262006-08-15 00:35:02 -07005351 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352
Thomas Graf0ab68032006-09-18 00:12:35 -07005353 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005354 if (!skb)
Thomas Graf5d620262006-08-15 00:35:02 -07005355 goto errout;
5356
Christian Brauner203651b2018-09-04 21:53:55 +02005357 err = inet6_fill_ifaddr(skb, ifa, &fillargs);
Patrick McHardy26932562007-01-31 23:16:40 -08005358 if (err < 0) {
5359 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
5360 WARN_ON(err == -EMSGSIZE);
5361 kfree_skb(skb);
5362 goto errout;
5363 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005364 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
5365 return;
Thomas Graf5d620262006-08-15 00:35:02 -07005366errout:
5367 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08005368 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369}
5370
Dave Jonesb6f99a22007-03-22 12:27:49 -07005371static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 __s32 *array, int bytes)
5373{
Thomas Graf04561c12006-11-14 19:53:58 -08005374 BUG_ON(bytes < (DEVCONF_MAX * 4));
5375
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 memset(array, 0, bytes);
5377 array[DEVCONF_FORWARDING] = cnf->forwarding;
5378 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
5379 array[DEVCONF_MTU6] = cnf->mtu6;
5380 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
5381 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
5382 array[DEVCONF_AUTOCONF] = cnf->autoconf;
5383 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
5384 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00005385 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
5386 jiffies_to_msecs(cnf->rtr_solicit_interval);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005387 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
5388 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
Thomas Graf93908d12010-11-17 01:44:24 +00005389 array[DEVCONF_RTR_SOLICIT_DELAY] =
5390 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02005392 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
5393 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
5394 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
5395 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
5397 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
5398 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
5399 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
5400 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08005402 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
Hangbin Liu8013d1d2015-07-30 14:28:42 +08005403 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08005404 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005405#ifdef CONFIG_IPV6_ROUTER_PREF
5406 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00005407 array[DEVCONF_RTR_PROBE_INTERVAL] =
5408 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08005409#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzbbea1242017-03-22 18:19:04 +09005410 array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08005411 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
5412#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005413#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07005414 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07005415 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07005416#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5417 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
Erik Kline7fd25612014-10-28 18:11:14 +09005418 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
Neil Horman95c385b2007-04-25 17:08:10 -07005419#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005420#ifdef CONFIG_IPV6_MROUTE
5421 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
5422#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09005423 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09005424 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00005425 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00005426 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02005427 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
Ben Greeard9333192014-06-25 14:44:53 -07005428 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
Harout Hedeshianc2943f12015-01-20 10:06:05 -07005429 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
Andy Gospodarek35103d12015-08-13 10:39:01 -04005430 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005431 /* we omit DEVCONF_STABLE_SECRET for now */
Erik Kline3985e8a2015-07-22 16:38:25 +09005432 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
Johannes Bergabbc3042016-02-04 13:31:19 +01005433 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
Johannes Berg7a02bf82016-02-04 13:31:20 +01005434 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
David Ahernf1705ec2016-02-24 09:25:37 -08005435 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
David Lebrun1ababeb2016-11-08 14:57:39 +01005436 array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
David Lebrunbf355b82016-11-08 14:57:42 +01005437#ifdef CONFIG_IPV6_SEG6_HMAC
5438 array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5439#endif
Erik Nordmarkadc176c2016-12-02 14:00:08 -08005440 array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
Felix Jiad35a00b2017-01-26 16:59:17 +13005441 array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
David Forsterdf789fe2017-02-23 16:27:18 +00005442 array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
Maciej Żenczykowski2210d6b2017-11-07 21:52:09 -08005443 array[DEVCONF_NDISC_TCLASS] = cnf->ndisc_tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444}
5445
Thomas Grafb382b192010-11-16 04:33:57 +00005446static inline size_t inet6_ifla6_size(void)
5447{
5448 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5449 + nla_total_size(sizeof(struct ifla_cacheinfo))
5450 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5451 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005452 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
Sabrina Dubrocabdd72f42018-07-09 12:25:16 +02005453 + nla_total_size(sizeof(struct in6_addr)) /* IFLA_INET6_TOKEN */
5454 + nla_total_size(1) /* IFLA_INET6_ADDR_GEN_MODE */
5455 + 0;
Thomas Grafb382b192010-11-16 04:33:57 +00005456}
5457
Thomas Graf339bf982006-11-10 14:10:15 -08005458static inline size_t inet6_if_nlmsg_size(void)
5459{
5460 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5461 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5462 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5463 + nla_total_size(4) /* IFLA_MTU */
5464 + nla_total_size(4) /* IFLA_LINK */
Andy Gospodarek03443382015-08-13 15:26:35 -04005465 + nla_total_size(1) /* IFLA_OPERSTATE */
Thomas Grafb382b192010-11-16 04:33:57 +00005466 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08005467}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005468
Eric Dumazetbe281e52011-05-19 01:14:23 +00005469static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Jia Heaca05672016-09-30 11:29:03 +08005470 int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005471{
5472 int i;
Jia Heaca05672016-09-30 11:29:03 +08005473 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005474 BUG_ON(pad < 0);
5475
5476 /* Use put_unaligned() because stats may not be aligned for u64. */
Jia Heaca05672016-09-30 11:29:03 +08005477 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5478 for (i = 1; i < ICMP6_MIB_MAX; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00005479 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005480
Jia Heaca05672016-09-30 11:29:03 +08005481 memset(&stats[ICMP6_MIB_MAX], 0, pad);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005482}
5483
WANG Cong698365f2014-05-05 15:55:55 -07005484static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305485 int bytes, size_t syncpoff)
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005486{
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305487 int i, c;
5488 u64 buff[IPSTATS_MIB_MAX];
5489 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5490
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005491 BUG_ON(pad < 0);
5492
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305493 memset(buff, 0, sizeof(buff));
5494 buff[0] = IPSTATS_MIB_MAX;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005495
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305496 for_each_possible_cpu(c) {
5497 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5498 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5499 }
5500
5501 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5502 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005503}
5504
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005505static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5506 int bytes)
5507{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005508 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005509 case IFLA_INET6_STATS:
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305510 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5511 offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005512 break;
5513 case IFLA_INET6_ICMP6STATS:
Jia Heaca05672016-09-30 11:29:03 +08005514 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005515 break;
5516 }
5517}
5518
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005519static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5520 u32 ext_filter_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005522 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08005523 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524
David S. Millerc78679e2012-04-01 20:27:33 -04005525 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5526 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08005528 ci.tstamp = cstamp_delta(idev->tstamp);
5529 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
Jiri Pirko1f9248e2013-12-07 19:26:53 +01005530 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
David S. Millerc78679e2012-04-01 20:27:33 -04005531 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5532 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005533 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
Ian Morris63159f22015-03-29 14:00:04 +01005534 if (!nla)
Thomas Graf04561c12006-11-14 19:53:58 -08005535 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005536 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005538 /* XXX - MC not implemented */
5539
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005540 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5541 return 0;
5542
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005543 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005544 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005545 goto nla_put_failure;
5546 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5547
5548 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005549 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005550 goto nla_put_failure;
5551 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005553 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
Ian Morris63159f22015-03-29 14:00:04 +01005554 if (!nla)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005555 goto nla_put_failure;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005556
Felix Jiad35a00b2017-01-26 16:59:17 +13005557 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005558 goto nla_put_failure;
5559
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005560 read_lock_bh(&idev->lock);
5561 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5562 read_unlock_bh(&idev->lock);
5563
Thomas Grafb382b192010-11-16 04:33:57 +00005564 return 0;
5565
5566nla_put_failure:
5567 return -EMSGSIZE;
5568}
5569
Arad, Ronenb1974ed2015-10-19 09:23:28 -07005570static size_t inet6_get_link_af_size(const struct net_device *dev,
5571 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005572{
5573 if (!__in6_dev_get(dev))
5574 return 0;
5575
5576 return inet6_ifla6_size();
5577}
5578
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005579static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5580 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005581{
5582 struct inet6_dev *idev = __in6_dev_get(dev);
5583
5584 if (!idev)
5585 return -ENODATA;
5586
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005587 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005588 return -EMSGSIZE;
5589
5590 return 0;
5591}
5592
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005593static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5594{
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005595 struct inet6_ifaddr *ifp;
5596 struct net_device *dev = idev->dev;
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005597 bool clear_token, update_rs = false;
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005598 struct in6_addr ll_addr;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005599
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005600 ASSERT_RTNL();
5601
Ian Morris63159f22015-03-29 14:00:04 +01005602 if (!token)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005603 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005604 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5605 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005606 if (!ipv6_accept_ra(idev))
5607 return -EINVAL;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005608 if (idev->cnf.rtr_solicits == 0)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005609 return -EINVAL;
5610
5611 write_lock_bh(&idev->lock);
5612
5613 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5614 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5615
5616 write_unlock_bh(&idev->lock);
5617
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005618 clear_token = ipv6_addr_any(token);
5619 if (clear_token)
5620 goto update_lft;
5621
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005622 if (!idev->dead && (idev->if_flags & IF_READY) &&
5623 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5624 IFA_F_OPTIMISTIC)) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005625 /* If we're not ready, then normal ifup will take care
5626 * of this. Otherwise, we need to request our rs here.
5627 */
5628 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5629 update_rs = true;
5630 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005631
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005632update_lft:
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005633 write_lock_bh(&idev->lock);
Daniel Borkmannfc403832013-04-09 03:47:15 +00005634
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005635 if (update_rs) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005636 idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005637 idev->rs_interval = rfc3315_s14_backoff_init(
5638 idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005639 idev->rs_probes = 1;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005640 addrconf_mod_rs_timer(idev, idev->rs_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005641 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005642
5643 /* Well, that's kinda nasty ... */
5644 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5645 spin_lock(&ifp->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00005646 if (ifp->tokenized) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005647 ifp->valid_lft = 0;
5648 ifp->prefered_lft = 0;
5649 }
5650 spin_unlock(&ifp->lock);
5651 }
5652
5653 write_unlock_bh(&idev->lock);
Lubomir Rintelb2ed64a2014-10-27 17:39:16 +01005654 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005655 addrconf_verify_rtnl();
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005656 return 0;
5657}
5658
Daniel Borkmann11b1f822015-02-05 14:39:11 +01005659static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5660 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5661 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5662};
5663
5664static int inet6_validate_link_af(const struct net_device *dev,
5665 const struct nlattr *nla)
5666{
5667 struct nlattr *tb[IFLA_INET6_MAX + 1];
5668
5669 if (dev && !__in6_dev_get(dev))
5670 return -EAFNOSUPPORT;
5671
Johannes Berg8cb08172019-04-26 14:07:28 +02005672 return nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla,
5673 inet6_af_policy, NULL);
Daniel Borkmann11b1f822015-02-05 14:39:11 +01005674}
5675
Felix Jiad35a00b2017-01-26 16:59:17 +13005676static int check_addr_gen_mode(int mode)
5677{
5678 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5679 mode != IN6_ADDR_GEN_MODE_NONE &&
5680 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5681 mode != IN6_ADDR_GEN_MODE_RANDOM)
5682 return -EINVAL;
5683 return 1;
5684}
5685
5686static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5687 int mode)
5688{
5689 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5690 !idev->cnf.stable_secret.initialized &&
5691 !net->ipv6.devconf_dflt->stable_secret.initialized)
5692 return -EINVAL;
5693 return 1;
5694}
5695
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005696static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5697{
5698 int err = -EINVAL;
5699 struct inet6_dev *idev = __in6_dev_get(dev);
5700 struct nlattr *tb[IFLA_INET6_MAX + 1];
5701
5702 if (!idev)
5703 return -EAFNOSUPPORT;
5704
Johannes Berg8cb08172019-04-26 14:07:28 +02005705 if (nla_parse_nested_deprecated(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005706 BUG();
5707
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005708 if (tb[IFLA_INET6_TOKEN]) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005709 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005710 if (err)
5711 return err;
5712 }
5713
5714 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5715 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5716
Felix Jiad35a00b2017-01-26 16:59:17 +13005717 if (check_addr_gen_mode(mode) < 0 ||
5718 check_stable_privacy(idev, dev_net(dev), mode) < 0)
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005719 return -EINVAL;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005720
Felix Jiad35a00b2017-01-26 16:59:17 +13005721 idev->cnf.addr_gen_mode = mode;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005722 err = 0;
5723 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005724
5725 return err;
5726}
5727
Thomas Grafb382b192010-11-16 04:33:57 +00005728static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005729 u32 portid, u32 seq, int event, unsigned int flags)
Thomas Grafb382b192010-11-16 04:33:57 +00005730{
5731 struct net_device *dev = idev->dev;
5732 struct ifinfomsg *hdr;
5733 struct nlmsghdr *nlh;
5734 void *protoinfo;
5735
Eric W. Biederman15e47302012-09-07 20:12:54 +00005736 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005737 if (!nlh)
Thomas Grafb382b192010-11-16 04:33:57 +00005738 return -EMSGSIZE;
5739
5740 hdr = nlmsg_data(nlh);
5741 hdr->ifi_family = AF_INET6;
5742 hdr->__ifi_pad = 0;
5743 hdr->ifi_type = dev->type;
5744 hdr->ifi_index = dev->ifindex;
5745 hdr->ifi_flags = dev_get_flags(dev);
5746 hdr->ifi_change = 0;
5747
David S. Millerc78679e2012-04-01 20:27:33 -04005748 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5749 (dev->addr_len &&
5750 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5751 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02005752 (dev->ifindex != dev_get_iflink(dev) &&
Andy Gospodarek03443382015-08-13 15:26:35 -04005753 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5754 nla_put_u8(skb, IFLA_OPERSTATE,
5755 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
David S. Millerc78679e2012-04-01 20:27:33 -04005756 goto nla_put_failure;
Michal Kubecekae0be8d2019-04-26 11:13:06 +02005757 protoinfo = nla_nest_start_noflag(skb, IFLA_PROTINFO);
Ian Morris63159f22015-03-29 14:00:04 +01005758 if (!protoinfo)
Thomas Grafb382b192010-11-16 04:33:57 +00005759 goto nla_put_failure;
5760
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005761 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005762 goto nla_put_failure;
5763
Thomas Graf04561c12006-11-14 19:53:58 -08005764 nla_nest_end(skb, protoinfo);
Johannes Berg053c0952015-01-16 22:09:00 +01005765 nlmsg_end(skb, nlh);
5766 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767
Thomas Graf04561c12006-11-14 19:53:58 -08005768nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005769 nlmsg_cancel(skb, nlh);
5770 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771}
5772
David Ahern786e0002018-10-07 20:16:33 -07005773static int inet6_valid_dump_ifinfo(const struct nlmsghdr *nlh,
5774 struct netlink_ext_ack *extack)
5775{
5776 struct ifinfomsg *ifm;
5777
5778 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
5779 NL_SET_ERR_MSG_MOD(extack, "Invalid header for link dump request");
5780 return -EINVAL;
5781 }
5782
5783 if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
5784 NL_SET_ERR_MSG_MOD(extack, "Invalid data after header");
5785 return -EINVAL;
5786 }
5787
5788 ifm = nlmsg_data(nlh);
5789 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
5790 ifm->ifi_change || ifm->ifi_index) {
5791 NL_SET_ERR_MSG_MOD(extack, "Invalid values in header for dump request");
5792 return -EINVAL;
5793 }
5794
5795 return 0;
5796}
5797
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5799{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005800 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00005801 int h, s_h;
David S. Miller434a8a52009-11-11 18:53:00 -08005802 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 struct net_device *dev;
5804 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00005805 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806
David Ahern786e0002018-10-07 20:16:33 -07005807 /* only requests using strict checking can pass data to
5808 * influence the dump
5809 */
5810 if (cb->strict_check) {
5811 int err = inet6_valid_dump_ifinfo(cb->nlh, cb->extack);
5812
5813 if (err < 0)
5814 return err;
5815 }
5816
Eric Dumazet84d26972009-11-09 12:11:28 +00005817 s_h = cb->args[0];
5818 s_idx = cb->args[1];
5819
5820 rcu_read_lock();
5821 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5822 idx = 0;
5823 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08005824 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet84d26972009-11-09 12:11:28 +00005825 if (idx < s_idx)
5826 goto cont;
5827 idev = __in6_dev_get(dev);
5828 if (!idev)
5829 goto cont;
5830 if (inet6_fill_ifinfo(skb, idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005831 NETLINK_CB(cb->skb).portid,
Eric Dumazet84d26972009-11-09 12:11:28 +00005832 cb->nlh->nlmsg_seq,
Johannes Berg053c0952015-01-16 22:09:00 +01005833 RTM_NEWLINK, NLM_F_MULTI) < 0)
Eric Dumazet84d26972009-11-09 12:11:28 +00005834 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07005835cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00005836 idx++;
5837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 }
Eric Dumazet84d26972009-11-09 12:11:28 +00005839out:
5840 rcu_read_unlock();
5841 cb->args[1] = idx;
5842 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843
5844 return skb->len;
5845}
5846
5847void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5848{
5849 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005850 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005851 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005852
Thomas Graf339bf982006-11-10 14:10:15 -08005853 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005854 if (!skb)
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005855 goto errout;
5856
5857 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005858 if (err < 0) {
5859 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5860 WARN_ON(err == -EMSGSIZE);
5861 kfree_skb(skb);
5862 goto errout;
5863 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005864 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005865 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005866errout:
5867 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005868 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869}
5870
Thomas Graf339bf982006-11-10 14:10:15 -08005871static inline size_t inet6_prefix_nlmsg_size(void)
5872{
5873 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5874 + nla_total_size(sizeof(struct in6_addr))
5875 + nla_total_size(sizeof(struct prefix_cacheinfo));
5876}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005877
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005879 struct prefix_info *pinfo, u32 portid, u32 seq,
Thomas Graf6051e2f2006-11-14 19:54:19 -08005880 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881{
Thomas Graf6051e2f2006-11-14 19:54:19 -08005882 struct prefixmsg *pmsg;
5883 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 struct prefix_cacheinfo ci;
5885
Eric W. Biederman15e47302012-09-07 20:12:54 +00005886 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005887 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005888 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08005889
5890 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07005892 pmsg->prefix_pad1 = 0;
5893 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 pmsg->prefix_ifindex = idev->dev->ifindex;
5895 pmsg->prefix_len = pinfo->prefix_len;
5896 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07005897 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 pmsg->prefix_flags = 0;
5899 if (pinfo->onlink)
5900 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5901 if (pinfo->autoconf)
5902 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5903
David S. Millerc78679e2012-04-01 20:27:33 -04005904 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5905 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 ci.preferred_time = ntohl(pinfo->prefered);
5907 ci.valid_time = ntohl(pinfo->valid);
David S. Millerc78679e2012-04-01 20:27:33 -04005908 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5909 goto nla_put_failure;
Johannes Berg053c0952015-01-16 22:09:00 +01005910 nlmsg_end(skb, nlh);
5911 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912
Thomas Graf6051e2f2006-11-14 19:54:19 -08005913nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005914 nlmsg_cancel(skb, nlh);
5915 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916}
5917
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005918static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 struct prefix_info *pinfo)
5920{
5921 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005922 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005923 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924
Thomas Graf339bf982006-11-10 14:10:15 -08005925 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005926 if (!skb)
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005927 goto errout;
5928
5929 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005930 if (err < 0) {
5931 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5932 WARN_ON(err == -EMSGSIZE);
5933 kfree_skb(skb);
5934 goto errout;
5935 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005936 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5937 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005938errout:
5939 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08005940 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941}
5942
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5944{
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005945 struct net *net = dev_net(ifp->idev->dev);
5946
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005947 if (event)
5948 ASSERT_RTNL();
5949
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5951
5952 switch (event) {
5953 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07005954 /*
5955 * If the address was optimistic
5956 * we inserted the route at the start of
5957 * our DAD process, so we don't need
5958 * to do it again
5959 */
David Ahern93c2fb22018-04-18 15:38:59 -07005960 if (!rcu_access_pointer(ifp->rt->fib6_node))
David Ahernafb1d4b52018-04-17 17:33:11 -07005961 ip6_ins_rt(net, ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 if (ifp->idev->cnf.forwarding)
5963 addrconf_join_anycast(ifp);
Cong Wang7996c792013-05-22 05:41:06 +00005964 if (!ipv6_addr_any(&ifp->peer_addr))
David Ahern8308f3f2018-05-27 08:09:58 -07005965 addrconf_prefix_route(&ifp->peer_addr, 128, 0,
David Ahernacb54e32018-04-17 17:33:22 -07005966 ifp->idev->dev, 0, 0,
David Ahern27b10602018-04-18 15:39:06 -07005967 GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 break;
5969 case RTM_DELADDR:
5970 if (ifp->idev->cnf.forwarding)
5971 addrconf_leave_anycast(ifp);
5972 addrconf_leave_solict(ifp->idev, &ifp->addr);
Cong Wang7996c792013-05-22 05:41:06 +00005973 if (!ipv6_addr_any(&ifp->peer_addr)) {
David Ahern8d1c8022018-04-17 17:33:26 -07005974 struct fib6_info *rt;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005975
Nicolas Dichtele7478df2014-09-03 23:59:22 +02005976 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
David Ahern2b2450c2019-03-27 20:53:52 -07005977 ifp->idev->dev, 0, 0,
5978 false);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07005979 if (rt)
David Ahernafb1d4b52018-04-17 17:33:11 -07005980 ip6_del_rt(net, rt);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005981 }
David Ahern38bd10c2016-04-21 20:56:12 -07005982 if (ifp->rt) {
David Ahern93531c62018-04-17 17:33:25 -07005983 ip6_del_rt(net, ifp->rt);
5984 ifp->rt = NULL;
David Ahern38bd10c2016-04-21 20:56:12 -07005985 }
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02005986 rt_genid_bump_ipv6(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 break;
5988 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005989 atomic_inc(&net->ipv6.dev_addr_genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990}
5991
5992static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5993{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005994 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 if (likely(ifp->idev->dead == 0))
5996 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005997 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998}
5999
6000#ifdef CONFIG_SYSCTL
6001
6002static
Joe Perchesfe2c6332013-06-11 23:04:25 -07006003int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 void __user *buffer, size_t *lenp, loff_t *ppos)
6005{
6006 int *valp = ctl->data;
6007 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00006008 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07006009 struct ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 int ret;
6011
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00006012 /*
6013 * ctl->data points to idev->cnf.forwarding, we should
6014 * not modify it until we get the rtnl lock.
6015 */
6016 lctl = *ctl;
6017 lctl.data = &val;
6018
6019 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08006021 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00006022 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00006023 if (ret)
6024 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006025 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026}
6027
Marcelo Leitner77751422015-02-23 11:17:13 -03006028static
6029int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
6030 void __user *buffer, size_t *lenp, loff_t *ppos)
6031{
6032 struct inet6_dev *idev = ctl->extra1;
6033 int min_mtu = IPV6_MIN_MTU;
6034 struct ctl_table lctl;
6035
6036 lctl = *ctl;
6037 lctl.extra1 = &min_mtu;
6038 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
6039
6040 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
6041}
6042
Brian Haley56d417b2009-06-01 03:07:33 -07006043static void dev_disable_change(struct inet6_dev *idev)
6044{
Cong Wang75538c22013-05-29 11:30:50 +08006045 struct netdev_notifier_info info;
6046
Brian Haley56d417b2009-06-01 03:07:33 -07006047 if (!idev || !idev->dev)
6048 return;
6049
Cong Wang75538c22013-05-29 11:30:50 +08006050 netdev_notifier_info_init(&info, idev->dev);
Brian Haley56d417b2009-06-01 03:07:33 -07006051 if (idev->cnf.disable_ipv6)
Cong Wang75538c22013-05-29 11:30:50 +08006052 addrconf_notify(NULL, NETDEV_DOWN, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07006053 else
Cong Wang75538c22013-05-29 11:30:50 +08006054 addrconf_notify(NULL, NETDEV_UP, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07006055}
6056
6057static void addrconf_disable_change(struct net *net, __s32 newf)
6058{
6059 struct net_device *dev;
6060 struct inet6_dev *idev;
6061
Kefeng Wang03e4def2017-01-19 16:26:21 +08006062 for_each_netdev(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07006063 idev = __in6_dev_get(dev);
6064 if (idev) {
6065 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
6066 idev->cnf.disable_ipv6 = newf;
6067 if (changed)
6068 dev_disable_change(idev);
6069 }
Brian Haley56d417b2009-06-01 03:07:33 -07006070 }
Brian Haley56d417b2009-06-01 03:07:33 -07006071}
6072
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00006073static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07006074{
6075 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00006076 int old;
6077
6078 if (!rtnl_trylock())
6079 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07006080
6081 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00006082 old = *p;
6083 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07006084
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00006085 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
6086 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07006087 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00006088 }
Brian Haley56d417b2009-06-01 03:07:33 -07006089
6090 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07006091 net->ipv6.devconf_dflt->disable_ipv6 = newf;
6092 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00006093 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07006094 dev_disable_change((struct inet6_dev *)table->extra1);
6095
6096 rtnl_unlock();
6097 return 0;
6098}
6099
6100static
Joe Perchesfe2c6332013-06-11 23:04:25 -07006101int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07006102 void __user *buffer, size_t *lenp, loff_t *ppos)
6103{
6104 int *valp = ctl->data;
6105 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00006106 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07006107 struct ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07006108 int ret;
6109
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00006110 /*
6111 * ctl->data points to idev->cnf.disable_ipv6, we should
6112 * not modify it until we get the rtnl lock.
6113 */
6114 lctl = *ctl;
6115 lctl.data = &val;
6116
6117 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07006118
6119 if (write)
6120 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00006121 if (ret)
6122 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07006123 return ret;
6124}
6125
stephen hemmingerc92d5492013-12-17 22:37:14 -08006126static
6127int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
6128 void __user *buffer, size_t *lenp, loff_t *ppos)
6129{
6130 int *valp = ctl->data;
6131 int ret;
6132 int old, new;
6133
6134 old = *valp;
6135 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
6136 new = *valp;
6137
6138 if (write && old != new) {
6139 struct net *net = ctl->extra2;
6140
6141 if (!rtnl_trylock())
6142 return restart_syscall();
6143
6144 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
David Ahern85b3daa2017-03-28 14:28:04 -07006145 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6146 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08006147 NETCONFA_IFINDEX_DEFAULT,
6148 net->ipv6.devconf_dflt);
6149 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
David Ahern85b3daa2017-03-28 14:28:04 -07006150 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6151 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08006152 NETCONFA_IFINDEX_ALL,
6153 net->ipv6.devconf_all);
6154 else {
6155 struct inet6_dev *idev = ctl->extra1;
6156
David Ahern85b3daa2017-03-28 14:28:04 -07006157 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
6158 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08006159 idev->dev->ifindex,
6160 &idev->cnf);
6161 }
6162 rtnl_unlock();
6163 }
6164
6165 return ret;
6166}
6167
Felix Jiad35a00b2017-01-26 16:59:17 +13006168static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
6169 void __user *buffer, size_t *lenp,
6170 loff_t *ppos)
6171{
6172 int ret = 0;
Sabrina Dubrocac6dbf7a2018-07-09 12:25:14 +02006173 u32 new_val;
Felix Jiad35a00b2017-01-26 16:59:17 +13006174 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
6175 struct net *net = (struct net *)ctl->extra2;
Sabrina Dubrocac6dbf7a2018-07-09 12:25:14 +02006176 struct ctl_table tmp = {
6177 .data = &new_val,
6178 .maxlen = sizeof(new_val),
6179 .mode = ctl->mode,
6180 };
Felix Jiad35a00b2017-01-26 16:59:17 +13006181
Felix Jia8c171d62017-02-27 12:41:23 +13006182 if (!rtnl_trylock())
6183 return restart_syscall();
6184
Sabrina Dubrocac6dbf7a2018-07-09 12:25:14 +02006185 new_val = *((u32 *)ctl->data);
6186
6187 ret = proc_douintvec(&tmp, write, buffer, lenp, ppos);
6188 if (ret != 0)
6189 goto out;
Felix Jiad35a00b2017-01-26 16:59:17 +13006190
6191 if (write) {
Felix Jia8c171d62017-02-27 12:41:23 +13006192 if (check_addr_gen_mode(new_val) < 0) {
6193 ret = -EINVAL;
6194 goto out;
6195 }
Felix Jiad35a00b2017-01-26 16:59:17 +13006196
Sabrina Dubrocac6dbf7a2018-07-09 12:25:14 +02006197 if (idev) {
Felix Jia8c171d62017-02-27 12:41:23 +13006198 if (check_stable_privacy(idev, net, new_val) < 0) {
6199 ret = -EINVAL;
6200 goto out;
6201 }
Felix Jiad35a00b2017-01-26 16:59:17 +13006202
6203 if (idev->cnf.addr_gen_mode != new_val) {
6204 idev->cnf.addr_gen_mode = new_val;
Felix Jiad35a00b2017-01-26 16:59:17 +13006205 addrconf_dev_config(idev->dev);
Felix Jiad35a00b2017-01-26 16:59:17 +13006206 }
Sabrina Dubrocaf24c5982018-07-09 12:25:17 +02006207 } else if (&net->ipv6.devconf_all->addr_gen_mode == ctl->data) {
6208 struct net_device *dev;
6209
6210 net->ipv6.devconf_dflt->addr_gen_mode = new_val;
6211 for_each_netdev(net, dev) {
6212 idev = __in6_dev_get(dev);
6213 if (idev &&
6214 idev->cnf.addr_gen_mode != new_val) {
6215 idev->cnf.addr_gen_mode = new_val;
6216 addrconf_dev_config(idev->dev);
6217 }
6218 }
Felix Jiad35a00b2017-01-26 16:59:17 +13006219 }
Sabrina Dubrocac6dbf7a2018-07-09 12:25:14 +02006220
6221 *((u32 *)ctl->data) = new_val;
Felix Jiad35a00b2017-01-26 16:59:17 +13006222 }
6223
Felix Jia8c171d62017-02-27 12:41:23 +13006224out:
6225 rtnl_unlock();
6226
Felix Jiad35a00b2017-01-26 16:59:17 +13006227 return ret;
6228}
6229
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006230static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
6231 void __user *buffer, size_t *lenp,
6232 loff_t *ppos)
6233{
6234 int err;
6235 struct in6_addr addr;
6236 char str[IPV6_MAX_STRLEN];
6237 struct ctl_table lctl = *ctl;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01006238 struct net *net = ctl->extra2;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006239 struct ipv6_stable_secret *secret = ctl->data;
6240
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01006241 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
6242 return -EIO;
6243
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006244 lctl.maxlen = IPV6_MAX_STRLEN;
6245 lctl.data = str;
6246
6247 if (!rtnl_trylock())
6248 return restart_syscall();
6249
6250 if (!write && !secret->initialized) {
6251 err = -EIO;
6252 goto out;
6253 }
6254
WANG Cong5449a5c2015-12-21 10:55:45 -08006255 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
6256 if (err >= sizeof(str)) {
6257 err = -EIO;
6258 goto out;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006259 }
6260
6261 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
6262 if (err || !write)
6263 goto out;
6264
6265 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
6266 err = -EIO;
6267 goto out;
6268 }
6269
6270 secret->initialized = true;
6271 secret->secret = addr;
6272
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01006273 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
6274 struct net_device *dev;
6275
6276 for_each_netdev(net, dev) {
6277 struct inet6_dev *idev = __in6_dev_get(dev);
6278
6279 if (idev) {
Felix Jiad35a00b2017-01-26 16:59:17 +13006280 idev->cnf.addr_gen_mode =
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01006281 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
6282 }
6283 }
6284 } else {
6285 struct inet6_dev *idev = ctl->extra1;
6286
Felix Jiad35a00b2017-01-26 16:59:17 +13006287 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01006288 }
6289
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006290out:
6291 rtnl_unlock();
6292
6293 return err;
6294}
stephen hemmingerc92d5492013-12-17 22:37:14 -08006295
Andy Gospodarek35103d12015-08-13 10:39:01 -04006296static
6297int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
6298 int write,
6299 void __user *buffer,
6300 size_t *lenp,
6301 loff_t *ppos)
6302{
6303 int *valp = ctl->data;
6304 int val = *valp;
6305 loff_t pos = *ppos;
6306 struct ctl_table lctl;
6307 int ret;
6308
6309 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
6310 * we should not modify it until we get the rtnl lock.
6311 */
6312 lctl = *ctl;
6313 lctl.data = &val;
6314
6315 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6316
6317 if (write)
6318 ret = addrconf_fixup_linkdown(ctl, valp, val);
6319 if (ret)
6320 *ppos = pos;
6321 return ret;
6322}
6323
David Forsterdf789fe2017-02-23 16:27:18 +00006324static
6325void addrconf_set_nopolicy(struct rt6_info *rt, int action)
6326{
6327 if (rt) {
6328 if (action)
6329 rt->dst.flags |= DST_NOPOLICY;
6330 else
6331 rt->dst.flags &= ~DST_NOPOLICY;
6332 }
6333}
6334
6335static
6336void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
6337{
6338 struct inet6_ifaddr *ifa;
6339
6340 read_lock_bh(&idev->lock);
6341 list_for_each_entry(ifa, &idev->addr_list, if_list) {
6342 spin_lock(&ifa->lock);
6343 if (ifa->rt) {
David Ahern1cf844c2019-05-22 20:27:59 -07006344 struct fib6_nh *nh = ifa->rt->fib6_nh;
David Forsterdf789fe2017-02-23 16:27:18 +00006345 int cpu;
6346
Wei Wang66f5d6c2017-10-06 12:06:10 -07006347 rcu_read_lock();
David Ahern3b6761d2018-04-17 17:33:20 -07006348 ifa->rt->dst_nopolicy = val ? true : false;
David Ahernf40b6ae2019-05-22 20:27:55 -07006349 if (nh->rt6i_pcpu) {
David Forsterdf789fe2017-02-23 16:27:18 +00006350 for_each_possible_cpu(cpu) {
6351 struct rt6_info **rtp;
6352
David Ahernf40b6ae2019-05-22 20:27:55 -07006353 rtp = per_cpu_ptr(nh->rt6i_pcpu, cpu);
David Forsterdf789fe2017-02-23 16:27:18 +00006354 addrconf_set_nopolicy(*rtp, val);
6355 }
6356 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07006357 rcu_read_unlock();
David Forsterdf789fe2017-02-23 16:27:18 +00006358 }
6359 spin_unlock(&ifa->lock);
6360 }
6361 read_unlock_bh(&idev->lock);
6362}
6363
6364static
6365int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
6366{
6367 struct inet6_dev *idev;
6368 struct net *net;
6369
6370 if (!rtnl_trylock())
6371 return restart_syscall();
6372
6373 *valp = val;
6374
6375 net = (struct net *)ctl->extra2;
6376 if (valp == &net->ipv6.devconf_dflt->disable_policy) {
6377 rtnl_unlock();
6378 return 0;
6379 }
6380
6381 if (valp == &net->ipv6.devconf_all->disable_policy) {
6382 struct net_device *dev;
6383
6384 for_each_netdev(net, dev) {
6385 idev = __in6_dev_get(dev);
6386 if (idev)
6387 addrconf_disable_policy_idev(idev, val);
6388 }
6389 } else {
6390 idev = (struct inet6_dev *)ctl->extra1;
6391 addrconf_disable_policy_idev(idev, val);
6392 }
6393
6394 rtnl_unlock();
6395 return 0;
6396}
6397
6398static
6399int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
6400 void __user *buffer, size_t *lenp,
6401 loff_t *ppos)
6402{
6403 int *valp = ctl->data;
6404 int val = *valp;
6405 loff_t pos = *ppos;
6406 struct ctl_table lctl;
6407 int ret;
6408
6409 lctl = *ctl;
6410 lctl.data = &val;
6411 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
6412
6413 if (write && (*valp != val))
6414 ret = addrconf_disable_policy(ctl, valp, val);
6415
6416 if (ret)
6417 *ppos = pos;
6418
6419 return ret;
6420}
6421
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07006422static int minus_one = -1;
Maciej Żenczykowski2210d6b2017-11-07 21:52:09 -08006423static const int zero = 0;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006424static const int one = 1;
6425static const int two_five_five = 255;
6426
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006427static const struct ctl_table addrconf_sysctl[] = {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006428 {
6429 .procname = "forwarding",
6430 .data = &ipv6_devconf.forwarding,
6431 .maxlen = sizeof(int),
6432 .mode = 0644,
6433 .proc_handler = addrconf_sysctl_forward,
6434 },
6435 {
6436 .procname = "hop_limit",
6437 .data = &ipv6_devconf.hop_limit,
6438 .maxlen = sizeof(int),
6439 .mode = 0644,
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006440 .proc_handler = proc_dointvec_minmax,
6441 .extra1 = (void *)&one,
6442 .extra2 = (void *)&two_five_five,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006443 },
6444 {
6445 .procname = "mtu",
6446 .data = &ipv6_devconf.mtu6,
6447 .maxlen = sizeof(int),
6448 .mode = 0644,
6449 .proc_handler = addrconf_sysctl_mtu,
6450 },
6451 {
6452 .procname = "accept_ra",
6453 .data = &ipv6_devconf.accept_ra,
6454 .maxlen = sizeof(int),
6455 .mode = 0644,
6456 .proc_handler = proc_dointvec,
6457 },
6458 {
6459 .procname = "accept_redirects",
6460 .data = &ipv6_devconf.accept_redirects,
6461 .maxlen = sizeof(int),
6462 .mode = 0644,
6463 .proc_handler = proc_dointvec,
6464 },
6465 {
6466 .procname = "autoconf",
6467 .data = &ipv6_devconf.autoconf,
6468 .maxlen = sizeof(int),
6469 .mode = 0644,
6470 .proc_handler = proc_dointvec,
6471 },
6472 {
6473 .procname = "dad_transmits",
6474 .data = &ipv6_devconf.dad_transmits,
6475 .maxlen = sizeof(int),
6476 .mode = 0644,
6477 .proc_handler = proc_dointvec,
6478 },
6479 {
6480 .procname = "router_solicitations",
6481 .data = &ipv6_devconf.rtr_solicits,
6482 .maxlen = sizeof(int),
6483 .mode = 0644,
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07006484 .proc_handler = proc_dointvec_minmax,
6485 .extra1 = &minus_one,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006486 },
6487 {
6488 .procname = "router_solicitation_interval",
6489 .data = &ipv6_devconf.rtr_solicit_interval,
6490 .maxlen = sizeof(int),
6491 .mode = 0644,
6492 .proc_handler = proc_dointvec_jiffies,
6493 },
6494 {
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07006495 .procname = "router_solicitation_max_interval",
6496 .data = &ipv6_devconf.rtr_solicit_max_interval,
6497 .maxlen = sizeof(int),
6498 .mode = 0644,
6499 .proc_handler = proc_dointvec_jiffies,
6500 },
6501 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006502 .procname = "router_solicitation_delay",
6503 .data = &ipv6_devconf.rtr_solicit_delay,
6504 .maxlen = sizeof(int),
6505 .mode = 0644,
6506 .proc_handler = proc_dointvec_jiffies,
6507 },
6508 {
6509 .procname = "force_mld_version",
6510 .data = &ipv6_devconf.force_mld_version,
6511 .maxlen = sizeof(int),
6512 .mode = 0644,
6513 .proc_handler = proc_dointvec,
6514 },
6515 {
6516 .procname = "mldv1_unsolicited_report_interval",
6517 .data =
6518 &ipv6_devconf.mldv1_unsolicited_report_interval,
6519 .maxlen = sizeof(int),
6520 .mode = 0644,
6521 .proc_handler = proc_dointvec_ms_jiffies,
6522 },
6523 {
6524 .procname = "mldv2_unsolicited_report_interval",
6525 .data =
6526 &ipv6_devconf.mldv2_unsolicited_report_interval,
6527 .maxlen = sizeof(int),
6528 .mode = 0644,
6529 .proc_handler = proc_dointvec_ms_jiffies,
6530 },
6531 {
6532 .procname = "use_tempaddr",
6533 .data = &ipv6_devconf.use_tempaddr,
6534 .maxlen = sizeof(int),
6535 .mode = 0644,
6536 .proc_handler = proc_dointvec,
6537 },
6538 {
6539 .procname = "temp_valid_lft",
6540 .data = &ipv6_devconf.temp_valid_lft,
6541 .maxlen = sizeof(int),
6542 .mode = 0644,
6543 .proc_handler = proc_dointvec,
6544 },
6545 {
6546 .procname = "temp_prefered_lft",
6547 .data = &ipv6_devconf.temp_prefered_lft,
6548 .maxlen = sizeof(int),
6549 .mode = 0644,
6550 .proc_handler = proc_dointvec,
6551 },
6552 {
6553 .procname = "regen_max_retry",
6554 .data = &ipv6_devconf.regen_max_retry,
6555 .maxlen = sizeof(int),
6556 .mode = 0644,
6557 .proc_handler = proc_dointvec,
6558 },
6559 {
6560 .procname = "max_desync_factor",
6561 .data = &ipv6_devconf.max_desync_factor,
6562 .maxlen = sizeof(int),
6563 .mode = 0644,
6564 .proc_handler = proc_dointvec,
6565 },
6566 {
6567 .procname = "max_addresses",
6568 .data = &ipv6_devconf.max_addresses,
6569 .maxlen = sizeof(int),
6570 .mode = 0644,
6571 .proc_handler = proc_dointvec,
6572 },
6573 {
6574 .procname = "accept_ra_defrtr",
6575 .data = &ipv6_devconf.accept_ra_defrtr,
6576 .maxlen = sizeof(int),
6577 .mode = 0644,
6578 .proc_handler = proc_dointvec,
6579 },
6580 {
6581 .procname = "accept_ra_min_hop_limit",
6582 .data = &ipv6_devconf.accept_ra_min_hop_limit,
6583 .maxlen = sizeof(int),
6584 .mode = 0644,
6585 .proc_handler = proc_dointvec,
6586 },
6587 {
6588 .procname = "accept_ra_pinfo",
6589 .data = &ipv6_devconf.accept_ra_pinfo,
6590 .maxlen = sizeof(int),
6591 .mode = 0644,
6592 .proc_handler = proc_dointvec,
6593 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08006594#ifdef CONFIG_IPV6_ROUTER_PREF
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006595 {
6596 .procname = "accept_ra_rtr_pref",
6597 .data = &ipv6_devconf.accept_ra_rtr_pref,
6598 .maxlen = sizeof(int),
6599 .mode = 0644,
6600 .proc_handler = proc_dointvec,
6601 },
6602 {
6603 .procname = "router_probe_interval",
6604 .data = &ipv6_devconf.rtr_probe_interval,
6605 .maxlen = sizeof(int),
6606 .mode = 0644,
6607 .proc_handler = proc_dointvec_jiffies,
6608 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08006609#ifdef CONFIG_IPV6_ROUTE_INFO
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006610 {
Joel Scherpelzbbea1242017-03-22 18:19:04 +09006611 .procname = "accept_ra_rt_info_min_plen",
6612 .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
6613 .maxlen = sizeof(int),
6614 .mode = 0644,
6615 .proc_handler = proc_dointvec,
6616 },
6617 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006618 .procname = "accept_ra_rt_info_max_plen",
6619 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
6620 .maxlen = sizeof(int),
6621 .mode = 0644,
6622 .proc_handler = proc_dointvec,
6623 },
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08006624#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08006625#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006626 {
6627 .procname = "proxy_ndp",
6628 .data = &ipv6_devconf.proxy_ndp,
6629 .maxlen = sizeof(int),
6630 .mode = 0644,
6631 .proc_handler = addrconf_sysctl_proxy_ndp,
6632 },
6633 {
6634 .procname = "accept_source_route",
6635 .data = &ipv6_devconf.accept_source_route,
6636 .maxlen = sizeof(int),
6637 .mode = 0644,
6638 .proc_handler = proc_dointvec,
6639 },
Neil Horman95c385b2007-04-25 17:08:10 -07006640#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006641 {
6642 .procname = "optimistic_dad",
6643 .data = &ipv6_devconf.optimistic_dad,
6644 .maxlen = sizeof(int),
6645 .mode = 0644,
6646 .proc_handler = proc_dointvec,
6647 },
6648 {
6649 .procname = "use_optimistic",
6650 .data = &ipv6_devconf.use_optimistic,
6651 .maxlen = sizeof(int),
6652 .mode = 0644,
6653 .proc_handler = proc_dointvec,
6654 },
Neil Horman95c385b2007-04-25 17:08:10 -07006655#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006656#ifdef CONFIG_IPV6_MROUTE
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006657 {
6658 .procname = "mc_forwarding",
6659 .data = &ipv6_devconf.mc_forwarding,
6660 .maxlen = sizeof(int),
6661 .mode = 0444,
6662 .proc_handler = proc_dointvec,
6663 },
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006664#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006665 {
6666 .procname = "disable_ipv6",
6667 .data = &ipv6_devconf.disable_ipv6,
6668 .maxlen = sizeof(int),
6669 .mode = 0644,
6670 .proc_handler = addrconf_sysctl_disable,
6671 },
6672 {
6673 .procname = "accept_dad",
6674 .data = &ipv6_devconf.accept_dad,
6675 .maxlen = sizeof(int),
6676 .mode = 0644,
6677 .proc_handler = proc_dointvec,
6678 },
6679 {
6680 .procname = "force_tllao",
6681 .data = &ipv6_devconf.force_tllao,
6682 .maxlen = sizeof(int),
6683 .mode = 0644,
6684 .proc_handler = proc_dointvec
6685 },
6686 {
6687 .procname = "ndisc_notify",
6688 .data = &ipv6_devconf.ndisc_notify,
6689 .maxlen = sizeof(int),
6690 .mode = 0644,
6691 .proc_handler = proc_dointvec
6692 },
6693 {
6694 .procname = "suppress_frag_ndisc",
6695 .data = &ipv6_devconf.suppress_frag_ndisc,
6696 .maxlen = sizeof(int),
6697 .mode = 0644,
6698 .proc_handler = proc_dointvec
6699 },
6700 {
6701 .procname = "accept_ra_from_local",
6702 .data = &ipv6_devconf.accept_ra_from_local,
6703 .maxlen = sizeof(int),
6704 .mode = 0644,
6705 .proc_handler = proc_dointvec,
6706 },
6707 {
6708 .procname = "accept_ra_mtu",
6709 .data = &ipv6_devconf.accept_ra_mtu,
6710 .maxlen = sizeof(int),
6711 .mode = 0644,
6712 .proc_handler = proc_dointvec,
6713 },
6714 {
6715 .procname = "stable_secret",
6716 .data = &ipv6_devconf.stable_secret,
6717 .maxlen = IPV6_MAX_STRLEN,
6718 .mode = 0600,
6719 .proc_handler = addrconf_sysctl_stable_secret,
6720 },
6721 {
6722 .procname = "use_oif_addrs_only",
6723 .data = &ipv6_devconf.use_oif_addrs_only,
6724 .maxlen = sizeof(int),
6725 .mode = 0644,
6726 .proc_handler = proc_dointvec,
6727 },
6728 {
6729 .procname = "ignore_routes_with_linkdown",
6730 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6731 .maxlen = sizeof(int),
6732 .mode = 0644,
6733 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6734 },
6735 {
6736 .procname = "drop_unicast_in_l2_multicast",
6737 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6738 .maxlen = sizeof(int),
6739 .mode = 0644,
6740 .proc_handler = proc_dointvec,
6741 },
6742 {
6743 .procname = "drop_unsolicited_na",
6744 .data = &ipv6_devconf.drop_unsolicited_na,
6745 .maxlen = sizeof(int),
6746 .mode = 0644,
6747 .proc_handler = proc_dointvec,
6748 },
6749 {
6750 .procname = "keep_addr_on_down",
6751 .data = &ipv6_devconf.keep_addr_on_down,
6752 .maxlen = sizeof(int),
6753 .mode = 0644,
6754 .proc_handler = proc_dointvec,
David Ahernf1705ec2016-02-24 09:25:37 -08006755
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006756 },
6757 {
David Lebrun1ababeb2016-11-08 14:57:39 +01006758 .procname = "seg6_enabled",
6759 .data = &ipv6_devconf.seg6_enabled,
6760 .maxlen = sizeof(int),
6761 .mode = 0644,
6762 .proc_handler = proc_dointvec,
6763 },
David Lebrunbf355b82016-11-08 14:57:42 +01006764#ifdef CONFIG_IPV6_SEG6_HMAC
6765 {
6766 .procname = "seg6_require_hmac",
6767 .data = &ipv6_devconf.seg6_require_hmac,
6768 .maxlen = sizeof(int),
6769 .mode = 0644,
6770 .proc_handler = proc_dointvec,
6771 },
6772#endif
David Lebrun1ababeb2016-11-08 14:57:39 +01006773 {
Erik Nordmarkadc176c2016-12-02 14:00:08 -08006774 .procname = "enhanced_dad",
6775 .data = &ipv6_devconf.enhanced_dad,
6776 .maxlen = sizeof(int),
6777 .mode = 0644,
6778 .proc_handler = proc_dointvec,
6779 },
6780 {
Felix Jiad35a00b2017-01-26 16:59:17 +13006781 .procname = "addr_gen_mode",
6782 .data = &ipv6_devconf.addr_gen_mode,
6783 .maxlen = sizeof(int),
6784 .mode = 0644,
6785 .proc_handler = addrconf_sysctl_addr_gen_mode,
6786 },
6787 {
David Forsterdf789fe2017-02-23 16:27:18 +00006788 .procname = "disable_policy",
6789 .data = &ipv6_devconf.disable_policy,
6790 .maxlen = sizeof(int),
6791 .mode = 0644,
6792 .proc_handler = addrconf_sysctl_disable_policy,
6793 },
6794 {
Maciej Żenczykowski2210d6b2017-11-07 21:52:09 -08006795 .procname = "ndisc_tclass",
6796 .data = &ipv6_devconf.ndisc_tclass,
6797 .maxlen = sizeof(int),
6798 .mode = 0644,
6799 .proc_handler = proc_dointvec_minmax,
6800 .extra1 = (void *)&zero,
6801 .extra2 = (void *)&two_five_five,
6802 },
6803 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006804 /* sentinel */
6805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806};
6807
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08006808static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006809 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810{
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006811 int i, ifindex;
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006812 struct ctl_table *table;
Eric W. Biederman6105e292012-04-19 13:41:24 +00006813 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11006814
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006815 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6816 if (!table)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006817 goto out;
6818
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006819 for (i = 0; table[i].data; i++) {
6820 table[i].data += (char *)p - (char *)&ipv6_devconf;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006821 /* If one of these is already set, then it is not safe to
6822 * overwrite either of them: this makes proc_dointvec_minmax
6823 * usable.
6824 */
6825 if (!table[i].extra1 && !table[i].extra2) {
6826 table[i].extra1 = idev; /* embedded; no ref */
6827 table[i].extra2 = net;
6828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830
Eric W. Biederman6105e292012-04-19 13:41:24 +00006831 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006833 p->sysctl_header = register_net_sysctl(net, path, table);
6834 if (!p->sysctl_header)
Eric W. Biederman6105e292012-04-19 13:41:24 +00006835 goto free;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006836
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006837 if (!strcmp(dev_name, "all"))
6838 ifindex = NETCONFA_IFINDEX_ALL;
6839 else if (!strcmp(dev_name, "default"))
6840 ifindex = NETCONFA_IFINDEX_DEFAULT;
6841 else
6842 ifindex = idev->dev->ifindex;
David Ahern85b3daa2017-03-28 14:28:04 -07006843 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
6844 ifindex, p);
Pavel Emelyanov95897312008-01-10 17:41:45 -08006845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006847free:
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006848 kfree(table);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006849out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08006850 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006851}
6852
David Ahern23452172017-03-28 14:28:05 -07006853static void __addrconf_sysctl_unregister(struct net *net,
6854 struct ipv6_devconf *p, int ifindex)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006855{
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006856 struct ctl_table *table;
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006857
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006858 if (!p->sysctl_header)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006859 return;
6860
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006861 table = p->sysctl_header->ctl_table_arg;
6862 unregister_net_sysctl_table(p->sysctl_header);
6863 p->sysctl_header = NULL;
6864 kfree(table);
David Ahern23452172017-03-28 14:28:05 -07006865
6866 inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006867}
6868
WANG Conga317a2f2014-07-25 15:25:09 -07006869static int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006870{
WANG Conga317a2f2014-07-25 15:25:09 -07006871 int err;
6872
6873 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6874 return -EINVAL;
6875
6876 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6877 &ndisc_ifinfo_sysctl_change);
6878 if (err)
6879 return err;
6880 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6881 idev, &idev->cnf);
6882 if (err)
6883 neigh_sysctl_unregister(idev->nd_parms);
6884
6885 return err;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006886}
6887
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006888static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889{
David Ahern23452172017-03-28 14:28:05 -07006890 __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
6891 idev->dev->ifindex);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006892 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893}
6894
6895
6896#endif
6897
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006898static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006899{
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006900 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006901 struct ipv6_devconf *all, *dflt;
6902
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006903 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006904 if (!all)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006905 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006906
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006907 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006908 if (!dflt)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006909 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006910
Arnd Bergmanna154d5d2019-03-04 21:38:03 +01006911 if (IS_ENABLED(CONFIG_SYSCTL) &&
6912 sysctl_devconf_inherit_init_net == 1 && !net_eq(net, &init_net)) {
Cong Wang856c3952019-01-17 23:27:11 -08006913 memcpy(all, init_net.ipv6.devconf_all, sizeof(ipv6_devconf));
6914 memcpy(dflt, init_net.ipv6.devconf_dflt, sizeof(ipv6_devconf_dflt));
6915 }
6916
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006917 /* these will be inherited by all namespaces */
6918 dflt->autoconf = ipv6_defaults.autoconf;
6919 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006920
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006921 dflt->stable_secret.initialized = false;
6922 all->stable_secret.initialized = false;
6923
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006924 net->ipv6.devconf_all = all;
6925 net->ipv6.devconf_dflt = dflt;
6926
6927#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006928 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006929 if (err < 0)
6930 goto err_reg_all;
6931
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006932 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006933 if (err < 0)
6934 goto err_reg_dflt;
6935#endif
6936 return 0;
6937
6938#ifdef CONFIG_SYSCTL
6939err_reg_dflt:
David Ahern23452172017-03-28 14:28:05 -07006940 __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006941err_reg_all:
6942 kfree(dflt);
6943#endif
6944err_alloc_dflt:
6945 kfree(all);
6946err_alloc_all:
6947 return err;
6948}
6949
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006950static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006951{
6952#ifdef CONFIG_SYSCTL
David Ahern23452172017-03-28 14:28:05 -07006953 __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
6954 NETCONFA_IFINDEX_DEFAULT);
6955 __addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
6956 NETCONFA_IFINDEX_ALL);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006957#endif
zhuyj73cf0e922014-11-26 10:25:58 +08006958 kfree(net->ipv6.devconf_dflt);
6959 kfree(net->ipv6.devconf_all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006960}
6961
6962static struct pernet_operations addrconf_ops = {
6963 .init = addrconf_init_net,
6964 .exit = addrconf_exit_net,
6965};
6966
Daniel Borkmann207895fd32015-01-29 12:15:03 +01006967static struct rtnl_af_ops inet6_ops __read_mostly = {
Thomas Grafb382b192010-11-16 04:33:57 +00006968 .family = AF_INET6,
6969 .fill_link_af = inet6_fill_link_af,
6970 .get_link_af_size = inet6_get_link_af_size,
Daniel Borkmann11b1f822015-02-05 14:39:11 +01006971 .validate_link_af = inet6_validate_link_af,
Daniel Borkmannf53adae2013-04-08 04:01:30 +00006972 .set_link_af = inet6_set_link_af,
Thomas Grafb382b192010-11-16 04:33:57 +00006973};
6974
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975/*
6976 * Init / cleanup code
6977 */
6978
6979int __init addrconf_init(void)
6980{
WANG Conga317a2f2014-07-25 15:25:09 -07006981 struct inet6_dev *idev;
stephen hemmingerc2e21292010-03-17 20:31:10 +00006982 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006983
Stephen Hemmingere21e8462010-03-20 16:09:01 -07006984 err = ipv6_addr_label_init();
6985 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00006986 pr_crit("%s: cannot initialize default policy table: %d\n",
6987 __func__, err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006988 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006991 err = register_pernet_subsys(&addrconf_ops);
6992 if (err < 0)
6993 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006994
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006995 addrconf_wq = create_workqueue("ipv6_addrconf");
6996 if (!addrconf_wq) {
6997 err = -ENOMEM;
6998 goto out_nowq;
6999 }
7000
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 /* The addrconf netdev notifier requires that loopback_dev
7002 * has it's ipv6 private information allocated and setup
7003 * before it can bring up and give link-local addresses
7004 * to other devices which are up.
7005 *
7006 * Unfortunately, loopback_dev is not necessarily the first
7007 * entry in the global dev_base list of net devices. In fact,
7008 * it is likely to be the very last entry on that list.
7009 * So this causes the notifier registry below to try and
7010 * give link-local addresses to all devices besides loopback_dev
7011 * first, then loopback_dev, which cases all the non-loopback_dev
7012 * devices to fail to get a link-local address.
7013 *
7014 * So, as a temporary fix, allocate the ipv6 structure for
7015 * loopback_dev first by hand.
7016 * Longer term, all of the dependencies ipv6 has upon the loopback
7017 * device and it being up should be removed.
7018 */
7019 rtnl_lock();
WANG Conga317a2f2014-07-25 15:25:09 -07007020 idev = ipv6_add_dev(init_net.loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021 rtnl_unlock();
WANG Conga317a2f2014-07-25 15:25:09 -07007022 if (IS_ERR(idev)) {
7023 err = PTR_ERR(idev);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08007024 goto errlo;
WANG Conga317a2f2014-07-25 15:25:09 -07007025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026
WANG Cong2f460932017-05-03 22:07:31 -07007027 ip6_route_init_special_entries();
7028
stephen hemmingerc2e21292010-03-17 20:31:10 +00007029 for (i = 0; i < IN6_ADDR_HSIZE; i++)
7030 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
7031
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 register_netdevice_notifier(&ipv6_dev_notf);
7033
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01007034 addrconf_verify();
Thomas Grafc127ea22007-03-22 11:58:32 -07007035
stephen hemminger3678a9d2013-12-30 10:41:32 -08007036 rtnl_af_register(&inet6_ops);
Thomas Grafb382b192010-11-16 04:33:57 +00007037
Florian Westphal16feebc2017-12-02 21:44:08 +01007038 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETLINK,
7039 NULL, inet6_dump_ifinfo, 0);
Thomas Grafc127ea22007-03-22 11:58:32 -07007040 if (err < 0)
7041 goto errout;
7042
Florian Westphal16feebc2017-12-02 21:44:08 +01007043 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_NEWADDR,
7044 inet6_rtm_newaddr, NULL, 0);
7045 if (err < 0)
7046 goto errout;
7047 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_DELADDR,
7048 inet6_rtm_deladdr, NULL, 0);
7049 if (err < 0)
7050 goto errout;
7051 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETADDR,
7052 inet6_rtm_getaddr, inet6_dump_ifaddr,
7053 RTNL_FLAG_DOIT_UNLOCKED);
7054 if (err < 0)
7055 goto errout;
7056 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETMULTICAST,
7057 NULL, inet6_dump_ifmcaddr, 0);
7058 if (err < 0)
7059 goto errout;
7060 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETANYCAST,
7061 NULL, inet6_dump_ifacaddr, 0);
7062 if (err < 0)
7063 goto errout;
7064 err = rtnl_register_module(THIS_MODULE, PF_INET6, RTM_GETNETCONF,
7065 inet6_netconf_get_devconf,
7066 inet6_netconf_dump_devconf,
7067 RTNL_FLAG_DOIT_UNLOCKED);
7068 if (err < 0)
7069 goto errout;
Florian Westphala3fde2a2017-12-04 19:19:18 +01007070 err = ipv6_addr_label_rtnl_register();
7071 if (err < 0)
7072 goto errout;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09007073
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07007075errout:
Florian Westphal16feebc2017-12-02 21:44:08 +01007076 rtnl_unregister_all(PF_INET6);
Thomas Grafb382b192010-11-16 04:33:57 +00007077 rtnl_af_unregister(&inet6_ops);
Thomas Grafc127ea22007-03-22 11:58:32 -07007078 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08007079errlo:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01007080 destroy_workqueue(addrconf_wq);
7081out_nowq:
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08007082 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00007083out_addrlabel:
7084 ipv6_addr_label_cleanup();
7085out:
Thomas Grafc127ea22007-03-22 11:58:32 -07007086 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087}
7088
Daniel Lezcano09f77092007-12-13 05:34:58 -08007089void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08007091 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092 int i;
7093
7094 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08007095 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00007096 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097
Florian Westphal5c451212017-10-04 15:58:49 +02007098 rtnl_af_unregister(&inet6_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099
Florian Westphal5c451212017-10-04 15:58:49 +02007100 rtnl_lock();
Thomas Grafb382b192010-11-16 04:33:57 +00007101
Daniel Lezcano176c39a2009-03-03 01:06:45 -08007102 /* clean dev list */
7103 for_each_netdev(&init_net, dev) {
7104 if (__in6_dev_get(dev) == NULL)
7105 continue;
7106 addrconf_ifdown(dev, 1);
7107 }
7108 addrconf_ifdown(init_net.loopback_dev, 2);
7109
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111 * Check hash table.
7112 */
stephen hemminger5c578aed2010-03-17 20:31:11 +00007113 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00007114 for (i = 0; i < IN6_ADDR_HSIZE; i++)
7115 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578aed2010-03-17 20:31:11 +00007116 spin_unlock_bh(&addrconf_hash_lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01007117 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118 rtnl_unlock();
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01007119
7120 destroy_workqueue(addrconf_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121}