blob: 6c1e7ffb62ff2333d63b3c7639d99649d43b32fc [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
97/* Set to 3 to get tracing... */
98#define ACONF_DEBUG 2
99
100#if ACONF_DEBUG >= 3
dingtianhongba3542e2013-08-17 10:27:04 +0800101#define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#else
dingtianhongba3542e2013-08-17 10:27:04 +0800103#define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#endif
105
106#define INFINITY_LIFE_TIME 0xFFFFFFFF
Thomas Graf18a31e12010-11-17 04:12:02 +0000107
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100108#define IPV6_MAX_STRLEN \
109 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
110
Thomas Graf18a31e12010-11-17 04:12:02 +0000111static inline u32 cstamp_delta(unsigned long cstamp)
112{
113 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
114}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700116static inline s32 rfc3315_s14_backoff_init(s32 irt)
117{
118 /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
119 u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
120 do_div(tmp, 1000000);
121 return (s32)tmp;
122}
123
124static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
125{
126 /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
127 u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
128 do_div(tmp, 1000000);
129 if ((s32)tmp > mrt) {
130 /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
131 tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
132 do_div(tmp, 1000000);
133 }
134 return (s32)tmp;
135}
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#ifdef CONFIG_SYSCTL
WANG Conga317a2f2014-07-25 15:25:09 -0700138static int addrconf_sysctl_register(struct inet6_dev *idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800139static void addrconf_sysctl_unregister(struct inet6_dev *idev);
140#else
WANG Conga317a2f2014-07-25 15:25:09 -0700141static inline int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800142{
WANG Conga317a2f2014-07-25 15:25:09 -0700143 return 0;
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800144}
145
146static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
147{
148}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#endif
150
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200151static void ipv6_regen_rndid(struct inet6_dev *idev);
152static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900154static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
Eric Dumazetd9bf82c2017-10-07 19:30:24 -0700155static int ipv6_count_addresses(const struct inet6_dev *idev);
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +0100156static int ipv6_generate_stable_address(struct in6_addr *addr,
157 u8 dad_count,
158 const struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160/*
161 * Configured unicast address hash table
162 */
stephen hemmingerc2e21292010-03-17 20:31:10 +0000163static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
stephen hemminger5c578aed2010-03-17 20:31:11 +0000164static DEFINE_SPINLOCK(addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100166static void addrconf_verify(void);
167static void addrconf_verify_rtnl(void);
168static void addrconf_verify_work(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100170static struct workqueue_struct *addrconf_wq;
171static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
174static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
175
Jiri Pirko93d9b7d2010-03-10 10:28:56 +0000176static void addrconf_type_change(struct net_device *dev,
177 unsigned long event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178static int addrconf_ifdown(struct net_device *dev, int how);
179
Romain Kuntz21caa662013-01-09 21:06:03 +0000180static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
181 int plen,
182 const struct net_device *dev,
183 u32 flags, u32 noflags);
184
David S. Millercf22f9a2012-04-14 21:37:40 -0400185static void addrconf_dad_start(struct inet6_ifaddr *ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100186static void addrconf_dad_work(struct work_struct *w);
Paolo Abeni764d3be2016-11-22 16:57:40 +0100187static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900188static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static void addrconf_rs_timer(unsigned long data);
190static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
191static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
192
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900193static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct prefix_info *pinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Adrian Bunk888c8482008-07-22 14:21:58 -0700196static struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 .forwarding = 0,
198 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
199 .mtu6 = IPV6_MIN_MTU,
200 .accept_ra = 1,
201 .accept_redirects = 1,
202 .autoconf = 1,
203 .force_mld_version = 0,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200204 .mldv1_unsolicited_report_interval = 10 * HZ,
205 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 .dad_transmits = 1,
207 .rtr_solicits = MAX_RTR_SOLICITATIONS,
208 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700209 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Ian Morris67ba4152014-08-24 21:53:10 +0100211 .use_tempaddr = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 .temp_valid_lft = TEMP_VALID_LIFETIME,
213 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
214 .regen_max_retry = REGEN_MAX_RETRY,
215 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800217 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700218 .accept_ra_from_local = 0,
Hangbin Liu8013d1d72015-07-30 14:28:42 +0800219 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800220 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800221#ifdef CONFIG_IPV6_ROUTER_PREF
222 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800223 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800224#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzbbea1242017-03-22 18:19:04 +0900225 .accept_ra_rt_info_min_plen = 0,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800226 .accept_ra_rt_info_max_plen = 0,
227#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800228#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700229 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700230 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900231 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900232 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200233 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700234 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100235 .stable_secret = {
236 .initialized = false,
Erik Kline3985e8a2015-07-22 16:38:25 +0900237 },
238 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400239 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800240 .keep_addr_on_down = 0,
David Lebrun1ababeb2016-11-08 14:57:39 +0100241 .seg6_enabled = 0,
David Lebrunbf355b82016-11-08 14:57:42 +0100242#ifdef CONFIG_IPV6_SEG6_HMAC
243 .seg6_require_hmac = 0,
244#endif
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800245 .enhanced_dad = 1,
Felix Jiad35a00b2017-01-26 16:59:17 +1300246 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
David Forsterdf789fe2017-02-23 16:27:18 +0000247 .disable_policy = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248};
249
Brian Haleyab32ea52006-09-22 14:15:41 -0700250static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 .forwarding = 0,
252 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
253 .mtu6 = IPV6_MIN_MTU,
254 .accept_ra = 1,
255 .accept_redirects = 1,
256 .autoconf = 1,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200257 .force_mld_version = 0,
258 .mldv1_unsolicited_report_interval = 10 * HZ,
259 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 .dad_transmits = 1,
261 .rtr_solicits = MAX_RTR_SOLICITATIONS,
262 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700263 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 .use_tempaddr = 0,
266 .temp_valid_lft = TEMP_VALID_LIFETIME,
267 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
268 .regen_max_retry = REGEN_MAX_RETRY,
269 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800271 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700272 .accept_ra_from_local = 0,
Hangbin Liu8013d1d72015-07-30 14:28:42 +0800273 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800274 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800275#ifdef CONFIG_IPV6_ROUTER_PREF
276 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800277 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800278#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzbbea1242017-03-22 18:19:04 +0900279 .accept_ra_rt_info_min_plen = 0,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800280 .accept_ra_rt_info_max_plen = 0,
281#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800282#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700283 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700284 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900285 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900286 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200287 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700288 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100289 .stable_secret = {
290 .initialized = false,
291 },
Erik Kline3985e8a2015-07-22 16:38:25 +0900292 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400293 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800294 .keep_addr_on_down = 0,
David Lebrun1ababeb2016-11-08 14:57:39 +0100295 .seg6_enabled = 0,
David Lebrunbf355b82016-11-08 14:57:42 +0100296#ifdef CONFIG_IPV6_SEG6_HMAC
297 .seg6_require_hmac = 0,
298#endif
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800299 .enhanced_dad = 1,
Felix Jiad35a00b2017-01-26 16:59:17 +1300300 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
David Forsterdf789fe2017-02-23 16:27:18 +0000301 .disable_policy = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302};
303
Mike Manning1f372c72017-09-25 22:01:36 +0100304/* Check if link is ready: is it up and is a valid qdisc available */
305static inline bool addrconf_link_ready(const struct net_device *dev)
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700306{
Mike Manning1f372c72017-09-25 22:01:36 +0100307 return netif_oper_up(dev) && !qdisc_tx_is_noop(dev);
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700308}
309
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200310static void addrconf_del_rs_timer(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200312 if (del_timer(&idev->rs_timer))
313 __in6_dev_put(idev);
314}
315
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100316static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200317{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100318 if (cancel_delayed_work(&ifp->dad_work))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 __in6_ifa_put(ifp);
320}
321
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200322static void addrconf_mod_rs_timer(struct inet6_dev *idev,
323 unsigned long when)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200325 if (!timer_pending(&idev->rs_timer))
326 in6_dev_hold(idev);
327 mod_timer(&idev->rs_timer, jiffies + when);
328}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100330static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
331 unsigned long delay)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200332{
Xin Longf8a894b2017-06-15 16:33:58 +0800333 in6_ifa_hold(ifp);
334 if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay))
335 in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700338static int snmp6_alloc_dev(struct inet6_dev *idev)
339{
John Stultz827da442013-10-07 15:51:58 -0700340 int i;
341
WANG Cong698365f2014-05-05 15:55:55 -0700342 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
343 if (!idev->stats.ipv6)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700344 goto err_ip;
John Stultz827da442013-10-07 15:51:58 -0700345
346 for_each_possible_cpu(i) {
347 struct ipstats_mib *addrconf_stats;
WANG Cong698365f2014-05-05 15:55:55 -0700348 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
John Stultz827da442013-10-07 15:51:58 -0700349 u64_stats_init(&addrconf_stats->syncp);
John Stultz827da442013-10-07 15:51:58 -0700350 }
351
352
Eric Dumazetbe281e52011-05-19 01:14:23 +0000353 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
354 GFP_KERNEL);
355 if (!idev->stats.icmpv6dev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700356 goto err_icmp;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000357 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
358 GFP_KERNEL);
359 if (!idev->stats.icmpv6msgdev)
David L Stevens14878f72007-09-16 16:52:35 -0700360 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700361
362 return 0;
363
David L Stevens14878f72007-09-16 16:52:35 -0700364err_icmpmsg:
Eric Dumazetbe281e52011-05-19 01:14:23 +0000365 kfree(idev->stats.icmpv6dev);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700366err_icmp:
WANG Cong698365f2014-05-05 15:55:55 -0700367 free_percpu(idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700368err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700369 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700370}
371
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000372static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 struct inet6_dev *ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700375 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 ASSERT_RTNL();
378
379 if (dev->mtu < IPV6_MIN_MTU)
WANG Conga317a2f2014-07-25 15:25:09 -0700380 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900382 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100383 if (!ndev)
WANG Conga317a2f2014-07-25 15:25:09 -0700384 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Ingo Oeser322f74a2006-03-20 23:01:47 -0800386 rwlock_init(&ndev->lock);
387 ndev->dev = dev;
stephen hemminger502a2ff2010-03-17 20:31:13 +0000388 INIT_LIST_HEAD(&ndev->addr_list);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200389 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
390 (unsigned long)ndev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900391 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100392
393 if (ndev->cnf.stable_secret.initialized)
Felix Jiad35a00b2017-01-26 16:59:17 +1300394 ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100395 else
Felix Jiad35a00b2017-01-26 16:59:17 +1300396 ndev->cnf.addr_gen_mode = ipv6_devconf_dflt.addr_gen_mode;
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100397
Ingo Oeser322f74a2006-03-20 23:01:47 -0800398 ndev->cnf.mtu6 = dev->mtu;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800399 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
Ian Morris63159f22015-03-29 14:00:04 +0100400 if (!ndev->nd_parms) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800401 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700402 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800403 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700404 if (ndev->cnf.forwarding)
405 dev_disable_lro(dev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800406 /* We refer to the device */
407 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Ingo Oeser322f74a2006-03-20 23:01:47 -0800409 if (snmp6_alloc_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800410 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000411 "%s: cannot allocate memory for statistics; dev=%s.\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800412 __func__, dev->name);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800413 neigh_parms_release(&nd_tbl, ndev->nd_parms);
Roy Li8603e332011-09-20 15:10:16 -0400414 dev_put(dev);
415 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700416 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Ingo Oeser322f74a2006-03-20 23:01:47 -0800419 if (snmp6_register_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800420 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000421 "%s: cannot create /proc/net/dev_snmp6/%s\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800422 __func__, dev->name);
WANG Conga317a2f2014-07-25 15:25:09 -0700423 goto err_release;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200426 /* One reference from device. */
Reshetova, Elena1be92462017-07-04 09:34:55 +0300427 refcount_set(&ndev->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900429 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
430 ndev->cnf.accept_dad = -1;
431
Amerigo Wang07a93622012-10-29 16:23:10 +0000432#if IS_ENABLED(CONFIG_IPV6_SIT)
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700433 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
Joe Perchesf3213832012-05-15 14:11:53 +0000434 pr_info("%s: Disabled Multicast RS\n", dev->name);
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700435 ndev->cnf.rtr_solicits = 0;
436 }
437#endif
438
stephen hemminger372e6c82010-03-17 20:31:09 +0000439 INIT_LIST_HEAD(&ndev->tempaddr_list);
Jiri Bohac76506a92016-10-13 18:52:15 +0200440 ndev->desync_factor = U32_MAX;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800441 if ((dev->flags&IFF_LOOPBACK) ||
442 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700443 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700444 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700445 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800446 ndev->cnf.use_tempaddr = -1;
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200447 } else
448 ipv6_regen_rndid(ndev);
David S. Miller5d9efa72013-10-28 20:07:50 -0400449
Daniel Borkmann914faa12013-04-09 03:47:14 +0000450 ndev->token = in6addr_any;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800451
Mike Manning1f372c72017-09-25 22:01:36 +0100452 if (netif_running(dev) && addrconf_link_ready(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700453 ndev->if_flags |= IF_READY;
454
Ingo Oeser322f74a2006-03-20 23:01:47 -0800455 ipv6_mc_init_dev(ndev);
456 ndev->tstamp = jiffies;
WANG Conga317a2f2014-07-25 15:25:09 -0700457 err = addrconf_sysctl_register(ndev);
458 if (err) {
459 ipv6_mc_destroy_dev(ndev);
Sabrina Dubroca2a189f9e2015-11-04 14:47:53 +0100460 snmp6_unregister_dev(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700461 goto err_release;
462 }
David L Stevens30c4cf52007-01-04 12:31:14 -0800463 /* protected by rtnl_lock */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000464 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800465
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000466 /* Join interface-local all-node multicast group */
467 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
468
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800469 /* Join all-node multicast group */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900470 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800471
Li Weid6ddef92012-03-05 14:45:17 +0000472 /* Join all-router multicast group if forwarding is set */
Li Wei8b2aaed2012-03-07 14:58:07 +0000473 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
Li Weid6ddef92012-03-05 14:45:17 +0000474 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700477
478err_release:
479 neigh_parms_release(&nd_tbl, ndev->nd_parms);
480 ndev->dead = 1;
481 in6_dev_finish_destroy(ndev);
482 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000485static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct inet6_dev *idev;
488
489 ASSERT_RTNL();
490
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700491 idev = __in6_dev_get(dev);
492 if (!idev) {
493 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -0700494 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return NULL;
496 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (dev->flags&IFF_UP)
499 ipv6_mc_up(idev);
500 return idev;
501}
502
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000503static int inet6_netconf_msgsize_devconf(int type)
504{
505 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
506 + nla_total_size(4); /* NETCONFA_IFINDEX */
Zhang Shengju136ba622016-03-10 08:55:50 +0000507 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000508
Zhang Shengju136ba622016-03-10 08:55:50 +0000509 if (type == NETCONFA_ALL)
510 all = true;
511
512 if (all || type == NETCONFA_FORWARDING)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000513 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500514#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000515 if (all || type == NETCONFA_MC_FORWARDING)
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000516 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500517#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000518 if (all || type == NETCONFA_PROXY_NEIGH)
stephen hemmingerc92d5492013-12-17 22:37:14 -0800519 size += nla_total_size(4);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000520
Zhang Shengju136ba622016-03-10 08:55:50 +0000521 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
Andy Gospodarek35103d12015-08-13 10:39:01 -0400522 size += nla_total_size(4);
523
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000524 return size;
525}
526
527static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
528 struct ipv6_devconf *devconf, u32 portid,
529 u32 seq, int event, unsigned int flags,
530 int type)
531{
532 struct nlmsghdr *nlh;
533 struct netconfmsg *ncm;
Zhang Shengju136ba622016-03-10 08:55:50 +0000534 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000535
536 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
537 flags);
Ian Morris63159f22015-03-29 14:00:04 +0100538 if (!nlh)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000539 return -EMSGSIZE;
540
Zhang Shengju136ba622016-03-10 08:55:50 +0000541 if (type == NETCONFA_ALL)
542 all = true;
543
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000544 ncm = nlmsg_data(nlh);
545 ncm->ncm_family = AF_INET6;
546
547 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
548 goto nla_put_failure;
549
David Ahern23452172017-03-28 14:28:05 -0700550 if (!devconf)
551 goto out;
552
Zhang Shengju136ba622016-03-10 08:55:50 +0000553 if ((all || type == NETCONFA_FORWARDING) &&
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000554 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
555 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500556#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000557 if ((all || type == NETCONFA_MC_FORWARDING) &&
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000558 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
559 devconf->mc_forwarding) < 0)
560 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500561#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000562 if ((all || type == NETCONFA_PROXY_NEIGH) &&
stephen hemmingerc92d5492013-12-17 22:37:14 -0800563 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
564 goto nla_put_failure;
565
Zhang Shengju136ba622016-03-10 08:55:50 +0000566 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
Andy Gospodarek35103d12015-08-13 10:39:01 -0400567 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
568 devconf->ignore_routes_with_linkdown) < 0)
569 goto nla_put_failure;
570
David Ahern23452172017-03-28 14:28:05 -0700571out:
Johannes Berg053c0952015-01-16 22:09:00 +0100572 nlmsg_end(skb, nlh);
573 return 0;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000574
575nla_put_failure:
576 nlmsg_cancel(skb, nlh);
577 return -EMSGSIZE;
578}
579
David Ahern85b3daa2017-03-28 14:28:04 -0700580void inet6_netconf_notify_devconf(struct net *net, int event, int type,
581 int ifindex, struct ipv6_devconf *devconf)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000582{
583 struct sk_buff *skb;
584 int err = -ENOBUFS;
585
Eric Dumazet927265b2016-07-08 05:46:04 +0200586 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100587 if (!skb)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000588 goto errout;
589
590 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
David Ahern85b3daa2017-03-28 14:28:04 -0700591 event, 0, type);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000592 if (err < 0) {
593 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
594 WARN_ON(err == -EMSGSIZE);
595 kfree_skb(skb);
596 goto errout;
597 }
Eric Dumazet927265b2016-07-08 05:46:04 +0200598 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000599 return;
600errout:
Cong Dingbd779022012-12-18 12:08:56 +0000601 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000602}
603
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000604static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
605 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
606 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
stephen hemmingerc92d5492013-12-17 22:37:14 -0800607 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
Andy Gospodarek35103d12015-08-13 10:39:01 -0400608 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000609};
610
611static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
David Ahernc21ef3e2017-04-16 09:48:24 -0700612 struct nlmsghdr *nlh,
613 struct netlink_ext_ack *extack)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000614{
615 struct net *net = sock_net(in_skb->sk);
616 struct nlattr *tb[NETCONFA_MAX+1];
Florian Westphal4ea26072017-10-11 10:28:00 +0200617 struct inet6_dev *in6_dev = NULL;
618 struct net_device *dev = NULL;
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000619 struct netconfmsg *ncm;
620 struct sk_buff *skb;
621 struct ipv6_devconf *devconf;
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000622 int ifindex;
623 int err;
624
625 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
David Ahernc21ef3e2017-04-16 09:48:24 -0700626 devconf_ipv6_policy, extack);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000627 if (err < 0)
Florian Westphal4ea26072017-10-11 10:28:00 +0200628 return err;
629
630 if (!tb[NETCONFA_IFINDEX])
631 return -EINVAL;
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000632
Anton Protopopova97eb332016-02-16 21:43:16 -0500633 err = -EINVAL;
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000634 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
635 switch (ifindex) {
636 case NETCONFA_IFINDEX_ALL:
637 devconf = net->ipv6.devconf_all;
638 break;
639 case NETCONFA_IFINDEX_DEFAULT:
640 devconf = net->ipv6.devconf_dflt;
641 break;
642 default:
Florian Westphal4ea26072017-10-11 10:28:00 +0200643 dev = dev_get_by_index(net, ifindex);
Ian Morris63159f22015-03-29 14:00:04 +0100644 if (!dev)
Florian Westphal4ea26072017-10-11 10:28:00 +0200645 return -EINVAL;
646 in6_dev = in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +0100647 if (!in6_dev)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000648 goto errout;
649 devconf = &in6_dev->cnf;
650 break;
651 }
652
653 err = -ENOBUFS;
Florian Westphal4ea26072017-10-11 10:28:00 +0200654 skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100655 if (!skb)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000656 goto errout;
657
658 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
659 NETLINK_CB(in_skb).portid,
660 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
Zhang Shengju136ba622016-03-10 08:55:50 +0000661 NETCONFA_ALL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000662 if (err < 0) {
663 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
664 WARN_ON(err == -EMSGSIZE);
665 kfree_skb(skb);
666 goto errout;
667 }
668 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
669errout:
Florian Westphal4ea26072017-10-11 10:28:00 +0200670 if (in6_dev)
671 in6_dev_put(in6_dev);
672 if (dev)
673 dev_put(dev);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000674 return err;
675}
676
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000677static int inet6_netconf_dump_devconf(struct sk_buff *skb,
678 struct netlink_callback *cb)
679{
680 struct net *net = sock_net(skb->sk);
681 int h, s_h;
682 int idx, s_idx;
683 struct net_device *dev;
684 struct inet6_dev *idev;
685 struct hlist_head *head;
686
687 s_h = cb->args[0];
688 s_idx = idx = cb->args[1];
689
690 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
691 idx = 0;
692 head = &net->dev_index_head[h];
693 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000694 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
695 net->dev_base_seq;
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000696 hlist_for_each_entry_rcu(dev, head, index_hlist) {
697 if (idx < s_idx)
698 goto cont;
699 idev = __in6_dev_get(dev);
700 if (!idev)
701 goto cont;
702
703 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
704 &idev->cnf,
705 NETLINK_CB(cb->skb).portid,
706 cb->nlh->nlmsg_seq,
707 RTM_NEWNETCONF,
708 NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000709 NETCONFA_ALL) < 0) {
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000710 rcu_read_unlock();
711 goto done;
712 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000713 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000714cont:
715 idx++;
716 }
717 rcu_read_unlock();
718 }
719 if (h == NETDEV_HASHENTRIES) {
720 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
721 net->ipv6.devconf_all,
722 NETLINK_CB(cb->skb).portid,
723 cb->nlh->nlmsg_seq,
724 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000725 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000726 goto done;
727 else
728 h++;
729 }
730 if (h == NETDEV_HASHENTRIES + 1) {
731 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
732 net->ipv6.devconf_dflt,
733 NETLINK_CB(cb->skb).portid,
734 cb->nlh->nlmsg_seq,
735 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000736 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000737 goto done;
738 else
739 h++;
740 }
741done:
742 cb->args[0] = h;
743 cb->args[1] = idx;
744
745 return skb->len;
746}
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748#ifdef CONFIG_SYSCTL
749static void dev_forward_change(struct inet6_dev *idev)
750{
751 struct net_device *dev;
752 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 if (!idev)
755 return;
756 dev = idev->dev;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700757 if (idev->cnf.forwarding)
758 dev_disable_lro(dev);
Amerigo Wang1a940832012-10-28 17:43:53 +0000759 if (dev->flags & IFF_MULTICAST) {
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000760 if (idev->cnf.forwarding) {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900761 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000762 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
763 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
764 } else {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900765 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000766 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
767 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
stephen hemminger502a2ff2010-03-17 20:31:13 +0000770
771 list_for_each_entry(ifa, &idev->addr_list, if_list) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800772 if (ifa->flags&IFA_F_TENTATIVE)
773 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (idev->cnf.forwarding)
775 addrconf_join_anycast(ifa);
776 else
777 addrconf_leave_anycast(ifa);
778 }
David Ahern85b3daa2017-03-28 14:28:04 -0700779 inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
780 NETCONFA_FORWARDING,
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000781 dev->ifindex, &idev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
784
Pavel Emelyanove1869322008-01-10 17:43:50 -0800785static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 struct net_device *dev;
788 struct inet6_dev *idev;
789
Ben Hutchings4acd4942012-08-14 08:54:51 +0000790 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 idev = __in6_dev_get(dev);
792 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800793 int changed = (!idev->cnf.forwarding) ^ (!newf);
794 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (changed)
796 dev_forward_change(idev);
797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800800
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000801static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800802{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800803 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000804 int old;
805
806 if (!rtnl_trylock())
807 return restart_syscall();
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800808
809 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000810 old = *p;
811 *p = newf;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800812
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000813 if (p == &net->ipv6.devconf_dflt->forwarding) {
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000814 if ((!newf) ^ (!old))
David Ahern85b3daa2017-03-28 14:28:04 -0700815 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
816 NETCONFA_FORWARDING,
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000817 NETCONFA_IFINDEX_DEFAULT,
818 net->ipv6.devconf_dflt);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000819 rtnl_unlock();
820 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +0000821 }
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000822
Pavel Emelyanove1869322008-01-10 17:43:50 -0800823 if (p == &net->ipv6.devconf_all->forwarding) {
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200824 int old_dflt = net->ipv6.devconf_dflt->forwarding;
825
Pavel Emelyanove1869322008-01-10 17:43:50 -0800826 net->ipv6.devconf_dflt->forwarding = newf;
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200827 if ((!newf) ^ (!old_dflt))
David Ahern85b3daa2017-03-28 14:28:04 -0700828 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
829 NETCONFA_FORWARDING,
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200830 NETCONFA_IFINDEX_DEFAULT,
831 net->ipv6.devconf_dflt);
832
Pavel Emelyanove1869322008-01-10 17:43:50 -0800833 addrconf_forward_change(net, newf);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000834 if ((!newf) ^ (!old))
David Ahern85b3daa2017-03-28 14:28:04 -0700835 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
836 NETCONFA_FORWARDING,
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000837 NETCONFA_IFINDEX_ALL,
838 net->ipv6.devconf_all);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000839 } else if ((!newf) ^ (!old))
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800840 dev_forward_change((struct inet6_dev *)table->extra1);
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700841 rtnl_unlock();
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800842
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000843 if (newf)
Daniel Lezcano7b4da532008-03-04 13:47:14 -0800844 rt6_purge_dflt_routers(net);
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000845 return 1;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800846}
Andy Gospodarek35103d12015-08-13 10:39:01 -0400847
848static void addrconf_linkdown_change(struct net *net, __s32 newf)
849{
850 struct net_device *dev;
851 struct inet6_dev *idev;
852
853 for_each_netdev(net, dev) {
854 idev = __in6_dev_get(dev);
855 if (idev) {
856 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
857
858 idev->cnf.ignore_routes_with_linkdown = newf;
859 if (changed)
860 inet6_netconf_notify_devconf(dev_net(dev),
David Ahern85b3daa2017-03-28 14:28:04 -0700861 RTM_NEWNETCONF,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400862 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
863 dev->ifindex,
864 &idev->cnf);
865 }
866 }
867}
868
869static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
870{
871 struct net *net;
872 int old;
873
874 if (!rtnl_trylock())
875 return restart_syscall();
876
877 net = (struct net *)table->extra2;
878 old = *p;
879 *p = newf;
880
881 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
882 if ((!newf) ^ (!old))
883 inet6_netconf_notify_devconf(net,
David Ahern85b3daa2017-03-28 14:28:04 -0700884 RTM_NEWNETCONF,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400885 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
886 NETCONFA_IFINDEX_DEFAULT,
887 net->ipv6.devconf_dflt);
888 rtnl_unlock();
889 return 0;
890 }
891
892 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
893 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
894 addrconf_linkdown_change(net, newf);
895 if ((!newf) ^ (!old))
896 inet6_netconf_notify_devconf(net,
David Ahern85b3daa2017-03-28 14:28:04 -0700897 RTM_NEWNETCONF,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400898 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
899 NETCONFA_IFINDEX_ALL,
900 net->ipv6.devconf_all);
901 }
902 rtnl_unlock();
903
904 return 1;
905}
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907#endif
908
stephen hemminger5c578aed2010-03-17 20:31:11 +0000909/* Nobody refers to this ifaddr, destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
911{
stephen hemmingerc2e21292010-03-17 20:31:10 +0000912 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914#ifdef NET_REFCNT_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +0000915 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916#endif
917
918 in6_dev_put(ifp->idev);
919
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100920 if (cancel_delayed_work(&ifp->dad_work))
921 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
922 ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Herbert Xue9d3e082010-05-18 15:36:06 -0700924 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
Joe Perchesf3213832012-05-15 14:11:53 +0000925 pr_warn("Freeing alive inet6 address %p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return;
927 }
Amerigo Wang94e187c2012-10-29 00:13:19 +0000928 ip6_rt_put(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Lai Jiangshane5785982011-03-15 18:00:14 +0800930 kfree_rcu(ifp, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
Brian Haleye55ffac2006-07-10 15:25:51 -0700933static void
934ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
935{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000936 struct list_head *p;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700937 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700938
939 /*
940 * Each device address list is sorted in order of scope -
941 * global before linklocal.
942 */
stephen hemminger502a2ff2010-03-17 20:31:13 +0000943 list_for_each(p, &idev->addr_list) {
944 struct inet6_ifaddr *ifa
945 = list_entry(p, struct inet6_ifaddr, if_list);
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700946 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700947 break;
948 }
949
Eric Dumazet8ef802a2017-10-07 19:30:23 -0700950 list_add_tail_rcu(&ifp->if_list, p);
Brian Haleye55ffac2006-07-10 15:25:51 -0700951}
952
Eric Dumazet3f27fb22017-10-23 16:17:47 -0700953static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr)
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900954{
Eric Dumazet3f27fb22017-10-23 16:17:47 -0700955 u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net);
956
957 return hash_32(val, IN6_ADDR_HSIZE_SHIFT);
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900958}
959
Eric Dumazet56fc7092017-10-23 16:17:45 -0700960static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
Eric Dumazet752a9292017-10-23 16:17:46 -0700961 struct net_device *dev, unsigned int hash)
Eric Dumazet56fc7092017-10-23 16:17:45 -0700962{
Eric Dumazet56fc7092017-10-23 16:17:45 -0700963 struct inet6_ifaddr *ifp;
964
965 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
966 if (!net_eq(dev_net(ifp->idev->dev), net))
967 continue;
968 if (ipv6_addr_equal(&ifp->addr, addr)) {
969 if (!dev || ifp->idev->dev == dev)
970 return true;
971 }
972 }
973 return false;
974}
975
David Ahernf3d98322017-10-18 09:56:52 -0700976static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa)
977{
Eric Dumazet3f27fb22017-10-23 16:17:47 -0700978 unsigned int hash = inet6_addr_hash(dev_net(dev), &ifa->addr);
David Ahernf3d98322017-10-18 09:56:52 -0700979 int err = 0;
980
981 spin_lock(&addrconf_hash_lock);
982
983 /* Ignore adding duplicate addresses on an interface */
Eric Dumazet752a9292017-10-23 16:17:46 -0700984 if (ipv6_chk_same_addr(dev_net(dev), &ifa->addr, dev, hash)) {
David Ahernf3d98322017-10-18 09:56:52 -0700985 ADBG("ipv6_add_addr: already assigned\n");
986 err = -EEXIST;
Eric Dumazet752a9292017-10-23 16:17:46 -0700987 } else {
988 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
David Ahernf3d98322017-10-18 09:56:52 -0700989 }
990
David Ahernf3d98322017-10-18 09:56:52 -0700991 spin_unlock(&addrconf_hash_lock);
992
993 return err;
994}
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996/* On success it returns ifp with increased reference count */
997
998static struct inet6_ifaddr *
Jiri Pirko3f8f5292013-08-01 10:41:27 +0200999ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
1000 const struct in6_addr *peer_addr, int pfxlen,
David Ahernf3d98322017-10-18 09:56:52 -07001001 int scope, u32 flags, u32 valid_lft, u32 prefered_lft,
David Ahernde95e042017-10-18 09:56:54 -07001002 bool can_block, struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
David Ahernf3d98322017-10-18 09:56:52 -07001004 gfp_t gfp_flags = can_block ? GFP_KERNEL : GFP_ATOMIC;
David Forsterdf789fe2017-02-23 16:27:18 +00001005 struct net *net = dev_net(idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 struct inet6_ifaddr *ifa = NULL;
David Ahernf3d98322017-10-18 09:56:52 -07001007 struct rt6_info *rt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 int err = 0;
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +09001009 int addr_type = ipv6_addr_type(addr);
1010
1011 if (addr_type == IPV6_ADDR_ANY ||
1012 addr_type & IPV6_ADDR_MULTICAST ||
1013 (!(idev->dev->flags & IFF_LOOPBACK) &&
1014 addr_type & IPV6_ADDR_LOOPBACK))
1015 return ERR_PTR(-EADDRNOTAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (idev->dead) {
1018 err = -ENODEV; /*XXX*/
David Ahernf3d98322017-10-18 09:56:52 -07001019 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021
Brian Haley56d417b2009-06-01 03:07:33 -07001022 if (idev->cnf.disable_ipv6) {
Brian Haley9bdd8d42009-03-18 18:22:48 -07001023 err = -EACCES;
David Ahernf3d98322017-10-18 09:56:52 -07001024 goto out;
Brian Haley9bdd8d42009-03-18 18:22:48 -07001025 }
1026
David Ahernff7883e2017-10-18 09:56:53 -07001027 /* validator notifier needs to be blocking;
1028 * do not call in atomic context
1029 */
1030 if (can_block) {
1031 struct in6_validator_info i6vi = {
1032 .i6vi_addr = *addr,
1033 .i6vi_dev = idev,
David Ahernde95e042017-10-18 09:56:54 -07001034 .extack = extack,
David Ahernff7883e2017-10-18 09:56:53 -07001035 };
1036
1037 err = inet6addr_validator_notifier_call_chain(NETDEV_UP, &i6vi);
1038 err = notifier_to_errno(err);
1039 if (err < 0)
1040 goto out;
1041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
David Ahernf3d98322017-10-18 09:56:52 -07001043 ifa = kzalloc(sizeof(*ifa), gfp_flags);
Ian Morris63159f22015-03-29 14:00:04 +01001044 if (!ifa) {
dingtianhongba3542e2013-08-17 10:27:04 +08001045 ADBG("ipv6_add_addr: malloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 err = -ENOBUFS;
1047 goto out;
1048 }
1049
David S. Miller8f031512011-12-06 16:48:14 -05001050 rt = addrconf_dst_alloc(idev, addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (IS_ERR(rt)) {
1052 err = PTR_ERR(rt);
David Ahernf3d98322017-10-18 09:56:52 -07001053 rt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 goto out;
1055 }
1056
David Forsterdf789fe2017-02-23 16:27:18 +00001057 if (net->ipv6.devconf_all->disable_policy ||
1058 idev->cnf.disable_policy)
1059 rt->dst.flags |= DST_NOPOLICY;
1060
Jiri Pirkobba24892013-12-07 19:26:57 +01001061 neigh_parms_data_state_setall(idev->nd_parms);
1062
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001063 ifa->addr = *addr;
Jiri Pirko3f8f5292013-08-01 10:41:27 +02001064 if (peer_addr)
1065 ifa->peer_addr = *peer_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 spin_lock_init(&ifa->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001068 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001069 INIT_HLIST_NODE(&ifa->addr_lst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 ifa->scope = scope;
1071 ifa->prefix_len = pfxlen;
Mahesh Bandewar66eb9f82017-05-12 17:03:39 -07001072 ifa->flags = flags;
1073 /* No need to add the TENTATIVE flag for addresses with NODAD */
1074 if (!(flags & IFA_F_NODAD))
1075 ifa->flags |= IFA_F_TENTATIVE;
Jiri Benc8a226b22013-08-01 10:41:28 +02001076 ifa->valid_lft = valid_lft;
1077 ifa->prefered_lft = prefered_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 ifa->cstamp = ifa->tstamp = jiffies;
Daniel Borkmann617fe292013-04-09 03:47:16 +00001079 ifa->tokenized = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Keir Fraser57f5f542006-08-29 02:43:49 -07001081 ifa->rt = rt;
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 ifa->idev = idev;
David Ahernf3d98322017-10-18 09:56:52 -07001084 in6_dev_hold(idev);
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 /* For caller */
Reshetova, Elena271201c2017-07-04 09:34:56 +03001087 refcount_set(&ifa->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
David Ahernf3d98322017-10-18 09:56:52 -07001089 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
David Ahernf3d98322017-10-18 09:56:52 -07001091 err = ipv6_add_addr_hash(idev->dev, ifa);
1092 if (err < 0) {
1093 rcu_read_unlock_bh();
1094 goto out;
1095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 write_lock(&idev->lock);
David Ahernf3d98322017-10-18 09:56:52 -07001098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -07001100 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (ifa->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001103 list_add(&ifa->tmp_list, &idev->tempaddr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 in6_ifa_hold(ifa);
1105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 in6_ifa_hold(ifa);
1108 write_unlock(&idev->lock);
David Ahernf3d98322017-10-18 09:56:52 -07001109
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001110 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
David Ahernf3d98322017-10-18 09:56:52 -07001112 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
1113out:
1114 if (unlikely(err < 0)) {
1115 if (rt)
1116 ip6_rt_put(rt);
1117 if (ifa) {
1118 if (ifa->idev)
1119 in6_dev_put(ifa->idev);
1120 kfree(ifa);
1121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 ifa = ERR_PTR(err);
1123 }
1124
1125 return ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
Thomas Haller5b84efe2014-01-15 15:36:59 +01001128enum cleanup_prefix_rt_t {
1129 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1130 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1131 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1132};
1133
1134/*
1135 * Check, whether the prefix for ifp would still need a prefix route
1136 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1137 * constants.
1138 *
1139 * 1) we don't purge prefix if address was not permanent.
1140 * prefix is managed by its own lifetime.
1141 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1142 * 3) if there are no addresses, delete prefix.
1143 * 4) if there are still other permanent address(es),
1144 * corresponding prefix is still permanent.
1145 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1146 * don't purge the prefix, assume user space is managing it.
1147 * 6) otherwise, update prefix lifetime to the
1148 * longest valid lifetime among the corresponding
1149 * addresses on the device.
1150 * Note: subsequent RA will update lifetime.
1151 **/
1152static enum cleanup_prefix_rt_t
1153check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1154{
1155 struct inet6_ifaddr *ifa;
1156 struct inet6_dev *idev = ifp->idev;
1157 unsigned long lifetime;
1158 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1159
1160 *expires = jiffies;
1161
1162 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1163 if (ifa == ifp)
1164 continue;
1165 if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
1166 ifp->prefix_len))
1167 continue;
1168 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1169 return CLEANUP_PREFIX_RT_NOP;
1170
1171 action = CLEANUP_PREFIX_RT_EXPIRE;
1172
1173 spin_lock(&ifa->lock);
1174
1175 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1176 /*
1177 * Note: Because this address is
1178 * not permanent, lifetime <
1179 * LONG_MAX / HZ here.
1180 */
1181 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1182 *expires = ifa->tstamp + lifetime * HZ;
1183 spin_unlock(&ifa->lock);
1184 }
1185
1186 return action;
1187}
1188
1189static void
1190cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
1191{
1192 struct rt6_info *rt;
1193
1194 rt = addrconf_get_prefix_route(&ifp->addr,
1195 ifp->prefix_len,
1196 ifp->idev->dev,
1197 0, RTF_GATEWAY | RTF_DEFAULT);
1198 if (rt) {
1199 if (del_rt)
1200 ip6_del_rt(rt);
1201 else {
1202 if (!(rt->rt6i_flags & RTF_EXPIRES))
1203 rt6_set_expires(rt, expires);
1204 ip6_rt_put(rt);
1205 }
1206 }
1207}
1208
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210/* This function wants to get referenced ifp and releases it before return */
1211
1212static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1213{
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001214 int state;
Thomas Haller5b84efe2014-01-15 15:36:59 +01001215 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1216 unsigned long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001218 ASSERT_RTNL();
1219
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001220 spin_lock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001221 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -07001222 ifp->state = INET6_IFADDR_STATE_DEAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001223 spin_unlock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001224
1225 if (state == INET6_IFADDR_STATE_DEAD)
1226 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
stephen hemminger5c578aed2010-03-17 20:31:11 +00001228 spin_lock_bh(&addrconf_hash_lock);
1229 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578aed2010-03-17 20:31:11 +00001230 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Thomas Haller5b84efe2014-01-15 15:36:59 +01001232 write_lock_bh(&ifp->idev->lock);
David S. Miller5d9efa72013-10-28 20:07:50 -04001233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001235 list_del(&ifp->tmp_list);
1236 if (ifp->ifpub) {
1237 in6_ifa_put(ifp->ifpub);
1238 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
stephen hemminger372e6c82010-03-17 20:31:09 +00001240 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Thomas Haller5b84efe2014-01-15 15:36:59 +01001243 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1244 action = check_cleanup_prefix_route(ifp, &expires);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001245
Eric Dumazet8ef802a2017-10-07 19:30:23 -07001246 list_del_rcu(&ifp->if_list);
Thomas Haller5b84efe2014-01-15 15:36:59 +01001247 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Thomas Haller5b84efe2014-01-15 15:36:59 +01001249 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001251 addrconf_del_dad_work(ifp);
Andrey Vaginb2238562008-07-08 15:13:31 -07001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 ipv6_ifa_notify(RTM_DELADDR, ifp);
1254
Cong Wangf88c91d2013-04-14 23:18:43 +08001255 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Thomas Haller5b84efe2014-01-15 15:36:59 +01001257 if (action != CLEANUP_PREFIX_RT_NOP) {
1258 cleanup_prefix_route(ifp, expires,
1259 action == CLEANUP_PREFIX_RT_DEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261
Daniel Walterc3968a82011-04-13 21:10:57 +00001262 /* clean up prefsrc entries */
1263 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001264out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 in6_ifa_put(ifp);
1266}
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1269{
1270 struct inet6_dev *idev = ifp->idev;
1271 struct in6_addr addr, *tmpaddr;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001272 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001273 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 int tmp_plen;
1275 int ret = 0;
Neil Horman95c385b2007-04-25 17:08:10 -07001276 u32 addr_flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001277 unsigned long now = jiffies;
Jiri Bohac76506a92016-10-13 18:52:15 +02001278 long max_desync_factor;
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001279 s32 cnf_temp_preferred_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Jiri Pirko53bd6742013-12-06 09:45:22 +01001281 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (ift) {
1283 spin_lock_bh(&ift->lock);
1284 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1285 spin_unlock_bh(&ift->lock);
1286 tmpaddr = &addr;
1287 } else {
1288 tmpaddr = NULL;
1289 }
1290retry:
1291 in6_dev_hold(idev);
1292 if (idev->cnf.use_tempaddr <= 0) {
Jiri Pirko53bd6742013-12-06 09:45:22 +01001293 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001294 pr_info("%s: use_tempaddr is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 in6_dev_put(idev);
1296 ret = -1;
1297 goto out;
1298 }
1299 spin_lock_bh(&ifp->lock);
1300 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1301 idev->cnf.use_tempaddr = -1; /*XXX*/
1302 spin_unlock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01001303 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001304 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1305 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 in6_dev_put(idev);
1307 ret = -1;
1308 goto out;
1309 }
1310 in6_ifa_hold(ifp);
1311 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
Jiri Bohac9d6280d2016-10-13 18:50:02 +02001312 ipv6_try_regen_rndid(idev, tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001314 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001316 regen_advance = idev->cnf.regen_max_retry *
Ian Morris67ba4152014-08-24 21:53:10 +01001317 idev->cnf.dad_transmits *
1318 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
Jiri Bohac76506a92016-10-13 18:52:15 +02001319
1320 /* recalculate max_desync_factor each time and update
1321 * idev->desync_factor if it's larger
1322 */
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001323 cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
Jiri Bohac76506a92016-10-13 18:52:15 +02001324 max_desync_factor = min_t(__u32,
1325 idev->cnf.max_desync_factor,
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001326 cnf_temp_preferred_lft - regen_advance);
Jiri Bohac76506a92016-10-13 18:52:15 +02001327
1328 if (unlikely(idev->desync_factor > max_desync_factor)) {
1329 if (max_desync_factor > 0) {
1330 get_random_bytes(&idev->desync_factor,
1331 sizeof(idev->desync_factor));
1332 idev->desync_factor %= max_desync_factor;
1333 } else {
1334 idev->desync_factor = 0;
1335 }
1336 }
1337
1338 tmp_valid_lft = min_t(__u32,
1339 ifp->valid_lft,
1340 idev->cnf.temp_valid_lft + age);
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001341 tmp_prefered_lft = cnf_temp_preferred_lft + age -
Jiri Bohac76506a92016-10-13 18:52:15 +02001342 idev->desync_factor;
Jiri Bohac76506a92016-10-13 18:52:15 +02001343 tmp_prefered_lft = min_t(__u32, ifp->prefered_lft, tmp_prefered_lft);
1344 tmp_plen = ifp->prefix_len;
1345 tmp_tstamp = ifp->tstamp;
1346 spin_unlock_bh(&ifp->lock);
1347
Jiri Pirko53bd6742013-12-06 09:45:22 +01001348 write_unlock_bh(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -07001349
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001350 /* A temporary address is created only if this calculated Preferred
1351 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1352 * an implementation must not create a temporary address with a zero
1353 * Preferred Lifetime.
Heiner Kallweitecab6702014-03-12 22:13:19 +01001354 * Use age calculation as in addrconf_verify to avoid unnecessary
1355 * temporary addresses being generated.
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001356 */
Heiner Kallweitecab6702014-03-12 22:13:19 +01001357 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1358 if (tmp_prefered_lft <= regen_advance + age) {
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001359 in6_ifa_put(ifp);
1360 in6_dev_put(idev);
1361 ret = -1;
1362 goto out;
1363 }
1364
Neil Horman95c385b2007-04-25 17:08:10 -07001365 addr_flags = IFA_F_TEMPORARY;
1366 /* set in addrconf_prefix_rcv() */
1367 if (ifp->flags & IFA_F_OPTIMISTIC)
1368 addr_flags |= IFA_F_OPTIMISTIC;
1369
Hannes Frederic Sowa4b08a8f2013-08-16 13:02:27 +02001370 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1371 ipv6_addr_scope(&addr), addr_flags,
David Ahernde95e042017-10-18 09:56:54 -07001372 tmp_valid_lft, tmp_prefered_lft, true, NULL);
Hannes Frederic Sowa4b08a8f2013-08-16 13:02:27 +02001373 if (IS_ERR(ift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 in6_ifa_put(ifp);
1375 in6_dev_put(idev);
Joe Perchesf3213832012-05-15 14:11:53 +00001376 pr_info("%s: retry temporary address regeneration\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 tmpaddr = &addr;
Jiri Pirko53bd6742013-12-06 09:45:22 +01001378 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 goto retry;
1380 }
1381
1382 spin_lock_bh(&ift->lock);
1383 ift->ifpub = ifp;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001384 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 ift->tstamp = tmp_tstamp;
1386 spin_unlock_bh(&ift->lock);
1387
David S. Millercf22f9a2012-04-14 21:37:40 -04001388 addrconf_dad_start(ift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 in6_ifa_put(ift);
1390 in6_dev_put(idev);
1391out:
1392 return ret;
1393}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395/*
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001396 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001398enum {
1399 IPV6_SADDR_RULE_INIT = 0,
1400 IPV6_SADDR_RULE_LOCAL,
1401 IPV6_SADDR_RULE_SCOPE,
1402 IPV6_SADDR_RULE_PREFERRED,
1403#ifdef CONFIG_IPV6_MIP6
1404 IPV6_SADDR_RULE_HOA,
1405#endif
1406 IPV6_SADDR_RULE_OIF,
1407 IPV6_SADDR_RULE_LABEL,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001408 IPV6_SADDR_RULE_PRIVACY,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001409 IPV6_SADDR_RULE_ORCHID,
1410 IPV6_SADDR_RULE_PREFIX,
Erik Kline7fd25612014-10-28 18:11:14 +09001411#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1412 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1413#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001414 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001415};
1416
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001417struct ipv6_saddr_score {
1418 int rule;
1419 int addr_type;
1420 struct inet6_ifaddr *ifa;
1421 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1422 int scopedist;
1423 int matchlen;
1424};
1425
1426struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001427 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001428 int ifindex;
1429 int scope;
1430 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001431 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001432};
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001433
Dave Jonesb6f99a22007-03-22 12:27:49 -07001434static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Ulrich Weber45bb0062010-02-25 23:28:58 +00001436 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001437 return 1;
1438 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
Matteo Croce35e015e2017-09-12 17:46:37 +02001441static bool ipv6_use_optimistic_addr(struct net *net,
1442 struct inet6_dev *idev)
Erik Kline7fd25612014-10-28 18:11:14 +09001443{
1444#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Matteo Croce35e015e2017-09-12 17:46:37 +02001445 if (!idev)
1446 return false;
1447 if (!net->ipv6.devconf_all->optimistic_dad && !idev->cnf.optimistic_dad)
1448 return false;
1449 if (!net->ipv6.devconf_all->use_optimistic && !idev->cnf.use_optimistic)
1450 return false;
1451
1452 return true;
Erik Kline7fd25612014-10-28 18:11:14 +09001453#else
1454 return false;
1455#endif
1456}
1457
Benjamin Thery3de23252008-05-28 14:51:24 +02001458static int ipv6_get_saddr_eval(struct net *net,
1459 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001460 struct ipv6_saddr_dst *dst,
1461 int i)
1462{
1463 int ret;
1464
1465 if (i <= score->rule) {
1466 switch (i) {
1467 case IPV6_SADDR_RULE_SCOPE:
1468 ret = score->scopedist;
1469 break;
1470 case IPV6_SADDR_RULE_PREFIX:
1471 ret = score->matchlen;
1472 break;
1473 default:
1474 ret = !!test_bit(i, score->scorebits);
1475 }
1476 goto out;
1477 }
1478
1479 switch (i) {
1480 case IPV6_SADDR_RULE_INIT:
1481 /* Rule 0: remember if hiscore is not ready yet */
1482 ret = !!score->ifa;
1483 break;
1484 case IPV6_SADDR_RULE_LOCAL:
1485 /* Rule 1: Prefer same address */
1486 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1487 break;
1488 case IPV6_SADDR_RULE_SCOPE:
1489 /* Rule 2: Prefer appropriate scope
1490 *
1491 * ret
1492 * ^
1493 * -1 | d 15
1494 * ---+--+-+---> scope
1495 * |
1496 * | d is scope of the destination.
1497 * B-d | \
1498 * | \ <- smaller scope is better if
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08001499 * B-15 | \ if scope is enough for destination.
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001500 * | ret = B - scope (-1 <= scope >= d <= 15).
1501 * d-C-1 | /
1502 * |/ <- greater is better
1503 * -C / if scope is not enough for destination.
1504 * /| ret = scope - C (-1 <= d < scope <= 15).
1505 *
1506 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1507 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1508 * Assume B = 0 and we get C > 29.
1509 */
1510 ret = __ipv6_addr_src_scope(score->addr_type);
1511 if (ret >= dst->scope)
1512 ret = -ret;
1513 else
1514 ret -= 128; /* 30 is enough */
1515 score->scopedist = ret;
1516 break;
1517 case IPV6_SADDR_RULE_PREFERRED:
Erik Kline7fd25612014-10-28 18:11:14 +09001518 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001519 /* Rule 3: Avoid deprecated and optimistic addresses */
Erik Kline7fd25612014-10-28 18:11:14 +09001520 u8 avoid = IFA_F_DEPRECATED;
1521
Matteo Croce35e015e2017-09-12 17:46:37 +02001522 if (!ipv6_use_optimistic_addr(net, score->ifa->idev))
Erik Kline7fd25612014-10-28 18:11:14 +09001523 avoid |= IFA_F_OPTIMISTIC;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001524 ret = ipv6_saddr_preferred(score->addr_type) ||
Erik Kline7fd25612014-10-28 18:11:14 +09001525 !(score->ifa->flags & avoid);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001526 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001527 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001528#ifdef CONFIG_IPV6_MIP6
1529 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001530 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001531 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001532 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1533 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001534 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001535 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001536#endif
1537 case IPV6_SADDR_RULE_OIF:
1538 /* Rule 5: Prefer outgoing interface */
1539 ret = (!dst->ifindex ||
1540 dst->ifindex == score->ifa->idev->dev->ifindex);
1541 break;
1542 case IPV6_SADDR_RULE_LABEL:
1543 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001544 ret = ipv6_addr_label(net,
1545 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001546 score->ifa->idev->dev->ifindex) == dst->label;
1547 break;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001548 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001549 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001550 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001551 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001552 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001553 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1554 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1555 score->ifa->idev->cnf.use_tempaddr >= 2;
1556 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001557 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001558 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001559 case IPV6_SADDR_RULE_ORCHID:
1560 /* Rule 8-: Prefer ORCHID vs ORCHID or
1561 * non-ORCHID vs non-ORCHID
1562 */
1563 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1564 ipv6_addr_orchid(dst->addr));
1565 break;
1566 case IPV6_SADDR_RULE_PREFIX:
1567 /* Rule 8: Use longest matching prefix */
YOSHIFUJI Hideaki / 吉藤英明91b4b042012-09-10 18:41:07 +00001568 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1569 if (ret > score->ifa->prefix_len)
1570 ret = score->ifa->prefix_len;
1571 score->matchlen = ret;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001572 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001573#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1574 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1575 /* Optimistic addresses still have lower precedence than other
1576 * preferred addresses.
1577 */
1578 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1579 break;
1580#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001581 default:
1582 ret = 0;
1583 }
1584
1585 if (ret)
1586 __set_bit(i, score->scorebits);
1587 score->rule = i;
1588out:
1589 return ret;
1590}
1591
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001592static int __ipv6_dev_get_saddr(struct net *net,
1593 struct ipv6_saddr_dst *dst,
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001594 struct inet6_dev *idev,
1595 struct ipv6_saddr_score *scores,
1596 int hiscore_idx)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001597{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001598 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001599
Eric Dumazetf59c0312017-10-07 19:30:27 -07001600 list_for_each_entry_rcu(score->ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001601 int i;
1602
1603 /*
1604 * - Tentative Address (RFC2462 section 5.4)
1605 * - A tentative address is not considered
1606 * "assigned to an interface" in the traditional
1607 * sense, unless it is also flagged as optimistic.
1608 * - Candidate Source Address (section 4)
1609 * - In any case, anycast addresses, multicast
1610 * addresses, and the unspecified address MUST
1611 * NOT be included in a candidate set.
1612 */
1613 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1614 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1615 continue;
1616
1617 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1618
1619 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1620 score->addr_type & IPV6_ADDR_MULTICAST)) {
1621 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1622 idev->dev->name);
1623 continue;
1624 }
1625
1626 score->rule = -1;
1627 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1628
1629 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1630 int minihiscore, miniscore;
1631
1632 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1633 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1634
1635 if (minihiscore > miniscore) {
1636 if (i == IPV6_SADDR_RULE_SCOPE &&
1637 score->scopedist > 0) {
1638 /*
1639 * special case:
1640 * each remaining entry
1641 * has too small (not enough)
1642 * scope, because ifa entries
1643 * are sorted by their scope
1644 * values.
1645 */
1646 goto out;
1647 }
1648 break;
1649 } else if (minihiscore < miniscore) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001650 swap(hiscore, score);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001651 hiscore_idx = 1 - hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001652
1653 /* restore our iterator */
1654 score->ifa = hiscore->ifa;
1655
1656 break;
1657 }
1658 }
1659 }
1660out:
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001661 return hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001662}
1663
David Ahernafbac6012016-06-16 16:24:26 -07001664static int ipv6_get_saddr_master(struct net *net,
1665 const struct net_device *dst_dev,
1666 const struct net_device *master,
1667 struct ipv6_saddr_dst *dst,
1668 struct ipv6_saddr_score *scores,
1669 int hiscore_idx)
1670{
1671 struct inet6_dev *idev;
1672
1673 idev = __in6_dev_get(dst_dev);
1674 if (idev)
1675 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1676 scores, hiscore_idx);
1677
1678 idev = __in6_dev_get(master);
1679 if (idev)
1680 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1681 scores, hiscore_idx);
1682
1683 return hiscore_idx;
1684}
1685
Patrick McHardyb3f644f2012-08-26 19:14:14 +02001686int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001687 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001688 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001690 struct ipv6_saddr_score scores[2], *hiscore;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001691 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001692 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001693 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001694 int dst_type;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001695 bool use_oif_addr = false;
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001696 int hiscore_idx = 0;
Eric Dumazetcc429c82017-10-07 19:30:28 -07001697 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001699 dst_type = __ipv6_addr_type(daddr);
1700 dst.addr = daddr;
1701 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1702 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001703 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001704 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001705
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001706 scores[hiscore_idx].rule = -1;
1707 scores[hiscore_idx].ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001709 rcu_read_lock();
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001710
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001711 /* Candidate Source Address (section 4)
1712 * - multicast and link-local destination address,
1713 * the set of candidate source address MUST only
1714 * include addresses assigned to interfaces
1715 * belonging to the same link as the outgoing
1716 * interface.
1717 * (- For site-local destination addresses, the
1718 * set of candidate source addresses MUST only
1719 * include addresses assigned to interfaces
1720 * belonging to the same site as the outgoing
1721 * interface.)
Erik Kline3985e8a2015-07-22 16:38:25 +09001722 * - "It is RECOMMENDED that the candidate source addresses
1723 * be the set of unicast addresses assigned to the
1724 * interface that will be used to send to the destination
1725 * (the 'outgoing' interface)." (RFC 6724)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001726 */
1727 if (dst_dev) {
Erik Kline3985e8a2015-07-22 16:38:25 +09001728 idev = __in6_dev_get(dst_dev);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001729 if ((dst_type & IPV6_ADDR_MULTICAST) ||
Erik Kline3985e8a2015-07-22 16:38:25 +09001730 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1731 (idev && idev->cnf.use_oif_addrs_only)) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001732 use_oif_addr = true;
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001733 }
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001734 }
1735
1736 if (use_oif_addr) {
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001737 if (idev)
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001738 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001739 } else {
David Ahernafbac6012016-06-16 16:24:26 -07001740 const struct net_device *master;
1741 int master_idx = 0;
1742
1743 /* if dst_dev exists and is enslaved to an L3 device, then
1744 * prefer addresses from dst_dev and then the master over
1745 * any other enslaved devices in the L3 domain.
1746 */
1747 master = l3mdev_master_dev_rcu(dst_dev);
1748 if (master) {
1749 master_idx = master->ifindex;
1750
1751 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1752 master, &dst,
1753 scores, hiscore_idx);
1754
1755 if (scores[hiscore_idx].ifa)
1756 goto out;
1757 }
1758
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001759 for_each_netdev_rcu(net, dev) {
David Ahernafbac6012016-06-16 16:24:26 -07001760 /* only consider addresses on devices in the
1761 * same L3 domain
1762 */
1763 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1764 continue;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001765 idev = __in6_dev_get(dev);
1766 if (!idev)
1767 continue;
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001768 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001769 }
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001770 }
David Ahernafbac6012016-06-16 16:24:26 -07001771
1772out:
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001773 hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001774 if (!hiscore->ifa)
Eric Dumazetcc429c82017-10-07 19:30:28 -07001775 ret = -EADDRNOTAVAIL;
1776 else
1777 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001778
Eric Dumazetcc429c82017-10-07 19:30:28 -07001779 rcu_read_unlock();
1780 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001782EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Amerigo Wang89657792013-06-29 21:30:49 +08001784int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001785 u32 banned_flags)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001786{
1787 struct inet6_ifaddr *ifp;
1788 int err = -EADDRNOTAVAIL;
1789
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01001790 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1791 if (ifp->scope > IFA_LINK)
1792 break;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001793 if (ifp->scope == IFA_LINK &&
1794 !(ifp->flags & banned_flags)) {
1795 *addr = ifp->addr;
1796 err = 0;
1797 break;
1798 }
1799 }
1800 return err;
1801}
1802
Neil Horman95c385b2007-04-25 17:08:10 -07001803int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001804 u32 banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 struct inet6_dev *idev;
1807 int err = -EADDRNOTAVAIL;
1808
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001809 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001810 idev = __in6_dev_get(dev);
1811 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 read_lock_bh(&idev->lock);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001813 err = __ipv6_get_lladdr(idev, addr, banned_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 read_unlock_bh(&idev->lock);
1815 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001816 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 return err;
1818}
1819
Eric Dumazetd9bf82c2017-10-07 19:30:24 -07001820static int ipv6_count_addresses(const struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Eric Dumazetd9bf82c2017-10-07 19:30:24 -07001822 const struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 int cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Eric Dumazetd9bf82c2017-10-07 19:30:24 -07001825 rcu_read_lock();
1826 list_for_each_entry_rcu(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 cnt++;
Eric Dumazetd9bf82c2017-10-07 19:30:24 -07001828 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return cnt;
1830}
1831
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001832int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Florian Westphal2a7851b2013-05-17 03:56:10 +00001833 const struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
Erik Klinec58da4c2015-02-04 20:01:23 +09001835 return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1836}
1837EXPORT_SYMBOL(ipv6_chk_addr);
1838
1839int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1840 const struct net_device *dev, int strict,
1841 u32 banned_flags)
1842{
Eric Dumazet3f27fb22017-10-23 16:17:47 -07001843 unsigned int hash = inet6_addr_hash(net, addr);
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001844 struct inet6_ifaddr *ifp;
Erik Klinec58da4c2015-02-04 20:01:23 +09001845 u32 ifp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Eric Dumazet480318a2017-10-23 16:17:48 -07001847 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001848 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001849 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001850 continue;
Erik Klinec58da4c2015-02-04 20:01:23 +09001851 /* Decouple optimistic from tentative for evaluation here.
1852 * Ban optimistic addresses explicitly, when required.
1853 */
1854 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1855 ? (ifp->flags&~IFA_F_TENTATIVE)
1856 : ifp->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 if (ipv6_addr_equal(&ifp->addr, addr) &&
Erik Klinec58da4c2015-02-04 20:01:23 +09001858 !(ifp_flags&banned_flags) &&
Ian Morris63159f22015-03-29 14:00:04 +01001859 (!dev || ifp->idev->dev == dev ||
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001860 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
Eric Dumazet480318a2017-10-23 16:17:48 -07001861 rcu_read_unlock();
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001862 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
1864 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00001865
Eric Dumazet480318a2017-10-23 16:17:48 -07001866 rcu_read_unlock();
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001867 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
Erik Klinec58da4c2015-02-04 20:01:23 +09001869EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001872/* Compares an address/prefix_len with addresses on device @dev.
1873 * If one is found it returns true.
1874 */
1875bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1876 const unsigned int prefix_len, struct net_device *dev)
1877{
Eric Dumazet47e26942017-10-07 19:30:25 -07001878 const struct inet6_ifaddr *ifa;
1879 const struct inet6_dev *idev;
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001880 bool ret = false;
1881
1882 rcu_read_lock();
1883 idev = __in6_dev_get(dev);
1884 if (idev) {
Eric Dumazet47e26942017-10-07 19:30:25 -07001885 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001886 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1887 if (ret)
1888 break;
1889 }
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001890 }
1891 rcu_read_unlock();
1892
1893 return ret;
1894}
1895EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1896
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001897int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001898{
Eric Dumazet24ba3332017-10-07 19:30:26 -07001899 const struct inet6_ifaddr *ifa;
1900 const struct inet6_dev *idev;
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001901 int onlink;
1902
1903 onlink = 0;
1904 rcu_read_lock();
1905 idev = __in6_dev_get(dev);
1906 if (idev) {
Eric Dumazet24ba3332017-10-07 19:30:26 -07001907 list_for_each_entry_rcu(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001908 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1909 ifa->prefix_len);
1910 if (onlink)
1911 break;
1912 }
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001913 }
1914 rcu_read_unlock();
1915 return onlink;
1916}
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001917EXPORT_SYMBOL(ipv6_chk_prefix);
1918
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001919struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001920 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
Eric Dumazet3f27fb22017-10-23 16:17:47 -07001922 unsigned int hash = inet6_addr_hash(net, addr);
David S. Millerb79d1d52010-03-25 21:39:21 -07001923 struct inet6_ifaddr *ifp, *result = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Eric Dumazet24f226d2017-10-23 16:17:49 -07001925 rcu_read_lock();
1926 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001927 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001928 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001930 if (!dev || ifp->idev->dev == dev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07001932 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 in6_ifa_hold(ifp);
1934 break;
1935 }
1936 }
1937 }
Eric Dumazet24f226d2017-10-23 16:17:49 -07001938 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
David S. Millerb79d1d52010-03-25 21:39:21 -07001940 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943/* Gets referenced address, destroys ifaddr */
1944
Brian Haleycc411d02009-09-09 14:41:32 +00001945static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Lubomir Rintel3d171f32016-01-08 13:47:23 +01001947 if (dad_failed)
1948 ifp->flags |= IFA_F_DADFAILED;
1949
Sabrina Dubrocaec8add22017-06-29 16:56:54 +02001950 if (ifp->flags&IFA_F_TEMPORARY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 struct inet6_ifaddr *ifpub;
1952 spin_lock_bh(&ifp->lock);
1953 ifpub = ifp->ifpub;
1954 if (ifpub) {
1955 in6_ifa_hold(ifpub);
1956 spin_unlock_bh(&ifp->lock);
1957 ipv6_create_tempaddr(ifpub, ifp);
1958 in6_ifa_put(ifpub);
1959 } else {
1960 spin_unlock_bh(&ifp->lock);
1961 }
1962 ipv6_del_addr(ifp);
Sabrina Dubrocaec8add22017-06-29 16:56:54 +02001963 } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
1964 spin_lock_bh(&ifp->lock);
1965 addrconf_del_dad_work(ifp);
1966 ifp->flags |= IFA_F_TENTATIVE;
1967 spin_unlock_bh(&ifp->lock);
1968 if (dad_failed)
1969 ipv6_ifa_notify(0, ifp);
1970 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001971 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
1975
Herbert Xuf2344a12010-05-18 15:55:27 -07001976static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1977{
1978 int err = -ENOENT;
1979
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001980 spin_lock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001981 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1982 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1983 err = 0;
1984 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001985 spin_unlock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001986
1987 return err;
1988}
1989
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001990void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1991{
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001992 struct inet6_dev *idev = ifp->idev;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001993 struct net *net = dev_net(ifp->idev->dev);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001994
Ursula Braun853dc2e2010-10-24 23:06:43 +00001995 if (addrconf_dad_end(ifp)) {
1996 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07001997 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00001998 }
Herbert Xuf2344a12010-05-18 15:55:27 -07001999
Joe Perchese87cc472012-05-13 21:56:26 +00002000 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
2001 ifp->idev->dev->name, &ifp->addr);
Brian Haley9bdd8d42009-03-18 18:22:48 -07002002
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002003 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09002004
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002005 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
2006 int scope = ifp->scope;
2007 u32 flags = ifp->flags;
2008 struct in6_addr new_addr;
2009 struct inet6_ifaddr *ifp2;
2010 u32 valid_lft, preferred_lft;
2011 int pfxlen = ifp->prefix_len;
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002012 int retries = ifp->stable_privacy_retry + 1;
2013
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01002014 if (retries > net->ipv6.sysctl.idgen_retries) {
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002015 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
2016 ifp->idev->dev->name);
2017 goto errdad;
2018 }
2019
2020 new_addr = ifp->addr;
2021 if (ipv6_generate_stable_address(&new_addr, retries,
2022 idev))
2023 goto errdad;
2024
2025 valid_lft = ifp->valid_lft;
2026 preferred_lft = ifp->prefered_lft;
2027
2028 spin_unlock_bh(&ifp->lock);
2029
2030 if (idev->cnf.max_addresses &&
2031 ipv6_count_addresses(idev) >=
2032 idev->cnf.max_addresses)
2033 goto lock_errdad;
2034
2035 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
2036 ifp->idev->dev->name);
2037
2038 ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
2039 scope, flags, valid_lft,
David Ahernde95e042017-10-18 09:56:54 -07002040 preferred_lft, false, NULL);
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002041 if (IS_ERR(ifp2))
2042 goto lock_errdad;
2043
2044 spin_lock_bh(&ifp2->lock);
2045 ifp2->stable_privacy_retry = retries;
2046 ifp2->state = INET6_IFADDR_STATE_PREDAD;
2047 spin_unlock_bh(&ifp2->lock);
2048
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01002049 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002050 in6_ifa_put(ifp2);
2051lock_errdad:
2052 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09002053 }
2054
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002055errdad:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002056 /* transition from _POSTDAD to _ERRDAD */
2057 ifp->state = INET6_IFADDR_STATE_ERRDAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01002058 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002059
2060 addrconf_mod_dad_work(ifp, 0);
Wei Yongjun751eb6b2016-09-05 16:06:31 +08002061 in6_ifa_put(ifp);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002062}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002064/* Join to solicited addr multicast group.
2065 * caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002066void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
2068 struct in6_addr maddr;
2069
2070 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2071 return;
2072
2073 addrconf_addr_solict_mult(addr, &maddr);
2074 ipv6_dev_mc_inc(dev, &maddr);
2075}
2076
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002077/* caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002078void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079{
2080 struct in6_addr maddr;
2081
2082 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2083 return;
2084
2085 addrconf_addr_solict_mult(addr, &maddr);
2086 __ipv6_dev_mc_dec(idev, &maddr);
2087}
2088
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002089/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002090static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
2092 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002093
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002094 if (ifp->prefix_len >= 127) /* RFC 6164 */
Bjørn Mork2bda8a02011-07-05 23:04:13 +00002095 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2097 if (ipv6_addr_any(&addr))
2098 return;
WANG Cong013b4d92014-09-11 15:35:11 -07002099 __ipv6_dev_ac_inc(ifp->idev, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002102/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002103static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
2105 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002106
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002107 if (ifp->prefix_len >= 127) /* RFC 6164 */
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00002108 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2110 if (ipv6_addr_any(&addr))
2111 return;
2112 __ipv6_dev_ac_dec(ifp->idev, &addr);
2113}
2114
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002115static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002116{
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002117 switch (dev->addr_len) {
2118 case ETH_ALEN:
Luiz Augusto von Dentz9dae2e02017-03-12 10:19:38 +02002119 memcpy(eui, dev->dev_addr, 3);
2120 eui[3] = 0xFF;
2121 eui[4] = 0xFE;
2122 memcpy(eui + 5, dev->dev_addr + 3, 3);
2123 break;
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002124 case EUI64_ADDR_LEN:
2125 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
2126 eui[0] ^= 2;
2127 break;
2128 default:
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002129 return -1;
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002130 }
2131
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002132 return 0;
2133}
2134
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002135static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2136{
2137 union fwnet_hwaddr *ha;
2138
2139 if (dev->addr_len != FWNET_ALEN)
2140 return -1;
2141
2142 ha = (union fwnet_hwaddr *)dev->dev_addr;
2143
2144 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2145 eui[0] ^= 2;
2146 return 0;
2147}
2148
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002149static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2150{
2151 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2152 if (dev->addr_len != ARCNET_ALEN)
2153 return -1;
2154 memset(eui, 0, 7);
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002155 eui[7] = *(u8 *)dev->dev_addr;
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002156 return 0;
2157}
2158
2159static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2160{
2161 if (dev->addr_len != INFINIBAND_ALEN)
2162 return -1;
2163 memcpy(eui, dev->dev_addr + 12, 8);
2164 eui[0] |= 2;
2165 return 0;
2166}
2167
stephen hemmingerc61393e2010-10-04 20:17:53 +00002168static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002169{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00002170 if (addr == 0)
2171 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002172 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2173 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2174 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2175 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2176 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2177 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2178 eui[1] = 0;
2179 eui[2] = 0x5E;
2180 eui[3] = 0xFE;
2181 memcpy(eui + 4, &addr, 4);
2182 return 0;
2183}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002184
2185static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2186{
2187 if (dev->priv_flags & IFF_ISATAP)
2188 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2189 return -1;
2190}
2191
stephen hemmingeraee80b52011-06-08 10:44:30 +00002192static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2193{
2194 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2195}
2196
Nicolas Dichtele8377352013-08-20 12:16:06 +02002197static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2198{
2199 memcpy(eui, dev->perm_addr, 3);
2200 memcpy(eui + 5, dev->perm_addr + 3, 3);
2201 eui[3] = 0xFF;
2202 eui[4] = 0xFE;
2203 eui[0] ^= 2;
2204 return 0;
2205}
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2208{
2209 switch (dev->type) {
2210 case ARPHRD_ETHER:
2211 case ARPHRD_FDDI:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002212 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002214 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002216 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002217 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002218 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002219 case ARPHRD_IPGRE:
Felix Jia45ce0fd2017-01-26 16:59:18 +13002220 case ARPHRD_TUNNEL:
stephen hemmingeraee80b52011-06-08 10:44:30 +00002221 return addrconf_ifid_gre(eui, dev);
Jukka Rissanene74bccb82013-12-11 17:05:36 +02002222 case ARPHRD_6LOWPAN:
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002223 return addrconf_ifid_6lowpan(eui, dev);
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002224 case ARPHRD_IEEE1394:
2225 return addrconf_ifid_ieee1394(eui, dev);
Nicolas Dichtele8377352013-08-20 12:16:06 +02002226 case ARPHRD_TUNNEL6:
Felix Jia45ce0fd2017-01-26 16:59:18 +13002227 case ARPHRD_IP6GRE:
Nicolas Dichtele8377352013-08-20 12:16:06 +02002228 return addrconf_ifid_ip6tnl(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 }
2230 return -1;
2231}
2232
2233static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2234{
2235 int err = -1;
2236 struct inet6_ifaddr *ifp;
2237
2238 read_lock_bh(&idev->lock);
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01002239 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2240 if (ifp->scope > IFA_LINK)
2241 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2243 memcpy(eui, ifp->addr.s6_addr+8, 8);
2244 err = 0;
2245 break;
2246 }
2247 }
2248 read_unlock_bh(&idev->lock);
2249 return err;
2250}
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002253static void ipv6_regen_rndid(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08002256 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
2259 /*
2260 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2261 * check if generated address is not inappropriate
2262 *
2263 * - Reserved subnet anycast (RFC 2526)
2264 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002265 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 * 00-00-5E-FE-xx-xx-xx-xx
2267 * - value 0
2268 * - XXX: already assigned to an address on the device
2269 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002270 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2272 (idev->rndid[7]&0x80))
2273 goto regen;
2274 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2275 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2276 goto regen;
2277 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2278 goto regen;
2279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
2281
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002282static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002283{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002285 ipv6_regen_rndid(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288/*
2289 * Add prefix route.
2290 */
2291
2292static void
2293addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002294 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
Thomas Graf86872cb2006-08-22 00:01:08 -07002296 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002297 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
Thomas Graf86872cb2006-08-22 00:01:08 -07002298 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2299 .fc_ifindex = dev->ifindex,
2300 .fc_expires = expires,
2301 .fc_dst_len = plen,
2302 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002303 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07002304 .fc_protocol = RTPROT_KERNEL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002305 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002307 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 /* Prevent useless cloning on PtP SIT.
2310 This thing is done here expecting that the whole
2311 class of non-broadcast devices need not cloning.
2312 */
Amerigo Wang07a93622012-10-29 16:23:10 +00002313#if IS_ENABLED(CONFIG_IPV6_SIT)
Thomas Graf86872cb2006-08-22 00:01:08 -07002314 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2315 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002316#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
David Ahern333c4302017-05-21 10:12:04 -06002318 ip6_route_add(&cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319}
2320
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002321
2322static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2323 int plen,
2324 const struct net_device *dev,
2325 u32 flags, u32 noflags)
2326{
2327 struct fib6_node *fn;
2328 struct rt6_info *rt = NULL;
2329 struct fib6_table *table;
David Ahernca254492015-10-12 11:47:10 -07002330 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002331
David Ahernca254492015-10-12 11:47:10 -07002332 table = fib6_get_table(dev_net(dev), tb_id);
Ian Morris63159f22015-03-29 14:00:04 +01002333 if (!table)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002334 return NULL;
2335
Wei Wang66f5d6c2017-10-06 12:06:10 -07002336 rcu_read_lock();
Wei Wang38fbeee2017-10-06 12:06:02 -07002337 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0, true);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002338 if (!fn)
2339 goto out;
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07002340
Wei Wang66f5d6c2017-10-06 12:06:10 -07002341 for_each_fib6_node_rt_rcu(fn) {
David S. Millerd1918542011-12-28 20:19:20 -05002342 if (rt->dst.dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002343 continue;
2344 if ((rt->rt6i_flags & flags) != flags)
2345 continue;
Romain Kuntz85da53b2013-01-09 15:02:26 +01002346 if ((rt->rt6i_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002347 continue;
Wei Wangd3843fe2017-10-06 12:06:06 -07002348 if (!dst_hold_safe(&rt->dst))
2349 rt = NULL;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002350 break;
2351 }
2352out:
Wei Wang66f5d6c2017-10-06 12:06:10 -07002353 rcu_read_unlock();
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002354 return rt;
2355}
2356
2357
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358/* Create "default" multicast route to the interface */
2359
2360static void addrconf_add_mroute(struct net_device *dev)
2361{
Thomas Graf86872cb2006-08-22 00:01:08 -07002362 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002363 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002364 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2365 .fc_ifindex = dev->ifindex,
2366 .fc_dst_len = 8,
2367 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002368 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002369 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Thomas Graf86872cb2006-08-22 00:01:08 -07002371 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2372
David Ahern333c4302017-05-21 10:12:04 -06002373 ip6_route_add(&cfg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374}
2375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2377{
2378 struct inet6_dev *idev;
2379
2380 ASSERT_RTNL();
2381
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002382 idev = ipv6_find_idev(dev);
2383 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00002384 return ERR_PTR(-ENOBUFS);
2385
2386 if (idev->cnf.disable_ipv6)
2387 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389 /* Add default multicast route */
David Ahernba46ee42016-06-13 13:44:18 -07002390 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
Li Wei4af04ab2011-12-06 21:23:45 +00002391 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 return idev;
2394}
2395
Jiri Pirko53bd6742013-12-06 09:45:22 +01002396static void manage_tempaddrs(struct inet6_dev *idev,
2397 struct inet6_ifaddr *ifp,
2398 __u32 valid_lft, __u32 prefered_lft,
2399 bool create, unsigned long now)
2400{
2401 u32 flags;
2402 struct inet6_ifaddr *ift;
2403
2404 read_lock_bh(&idev->lock);
2405 /* update all temporary addresses in the list */
2406 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2407 int age, max_valid, max_prefered;
2408
2409 if (ifp != ift->ifpub)
2410 continue;
2411
2412 /* RFC 4941 section 3.3:
2413 * If a received option will extend the lifetime of a public
2414 * address, the lifetimes of temporary addresses should
2415 * be extended, subject to the overall constraint that no
2416 * temporary addresses should ever remain "valid" or "preferred"
2417 * for a time longer than (TEMP_VALID_LIFETIME) or
2418 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2419 */
2420 age = (now - ift->cstamp) / HZ;
2421 max_valid = idev->cnf.temp_valid_lft - age;
2422 if (max_valid < 0)
2423 max_valid = 0;
2424
2425 max_prefered = idev->cnf.temp_prefered_lft -
Jiri Bohac76506a92016-10-13 18:52:15 +02002426 idev->desync_factor - age;
Jiri Pirko53bd6742013-12-06 09:45:22 +01002427 if (max_prefered < 0)
2428 max_prefered = 0;
2429
2430 if (valid_lft > max_valid)
2431 valid_lft = max_valid;
2432
2433 if (prefered_lft > max_prefered)
2434 prefered_lft = max_prefered;
2435
2436 spin_lock(&ift->lock);
2437 flags = ift->flags;
2438 ift->valid_lft = valid_lft;
2439 ift->prefered_lft = prefered_lft;
2440 ift->tstamp = now;
2441 if (prefered_lft > 0)
2442 ift->flags &= ~IFA_F_DEPRECATED;
2443
2444 spin_unlock(&ift->lock);
2445 if (!(flags&IFA_F_TENTATIVE))
2446 ipv6_ifa_notify(0, ift);
2447 }
2448
2449 if ((create || list_empty(&idev->tempaddr_list)) &&
2450 idev->cnf.use_tempaddr > 0) {
2451 /* When a new public address is created as described
2452 * in [ADDRCONF], also create a new temporary address.
2453 * Also create a temporary address if it's enabled but
2454 * no temporary address currently exists.
2455 */
2456 read_unlock_bh(&idev->lock);
2457 ipv6_create_tempaddr(ifp, NULL);
2458 } else {
2459 read_unlock_bh(&idev->lock);
2460 }
2461}
2462
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002463static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2464{
Felix Jiad35a00b2017-01-26 16:59:17 +13002465 return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2466 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002467}
2468
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002469int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2470 const struct prefix_info *pinfo,
2471 struct inet6_dev *in6_dev,
2472 const struct in6_addr *addr, int addr_type,
2473 u32 addr_flags, bool sllao, bool tokenized,
2474 __u32 valid_lft, u32 prefered_lft)
Alexander Aring4f672232016-06-15 21:20:22 +02002475{
2476 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2477 int create = 0, update_lft = 0;
2478
2479 if (!ifp && valid_lft) {
2480 int max_addresses = in6_dev->cnf.max_addresses;
2481
2482#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Matteo Croce35e015e2017-09-12 17:46:37 +02002483 if ((net->ipv6.devconf_all->optimistic_dad ||
2484 in6_dev->cnf.optimistic_dad) &&
Alexander Aring4f672232016-06-15 21:20:22 +02002485 !net->ipv6.devconf_all->forwarding && sllao)
2486 addr_flags |= IFA_F_OPTIMISTIC;
2487#endif
2488
2489 /* Do not allow to create too much of autoconfigured
2490 * addresses; this would be too easy way to crash kernel.
2491 */
2492 if (!max_addresses ||
2493 ipv6_count_addresses(in6_dev) < max_addresses)
2494 ifp = ipv6_add_addr(in6_dev, addr, NULL,
2495 pinfo->prefix_len,
2496 addr_type&IPV6_ADDR_SCOPE_MASK,
2497 addr_flags, valid_lft,
David Ahernde95e042017-10-18 09:56:54 -07002498 prefered_lft, false, NULL);
Alexander Aring4f672232016-06-15 21:20:22 +02002499
2500 if (IS_ERR_OR_NULL(ifp))
2501 return -1;
2502
2503 update_lft = 0;
2504 create = 1;
2505 spin_lock_bh(&ifp->lock);
2506 ifp->flags |= IFA_F_MANAGETEMPADDR;
2507 ifp->cstamp = jiffies;
2508 ifp->tokenized = tokenized;
2509 spin_unlock_bh(&ifp->lock);
2510 addrconf_dad_start(ifp);
2511 }
2512
2513 if (ifp) {
2514 u32 flags;
2515 unsigned long now;
2516 u32 stored_lft;
2517
2518 /* update lifetime (RFC2462 5.5.3 e) */
2519 spin_lock_bh(&ifp->lock);
2520 now = jiffies;
2521 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2522 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2523 else
2524 stored_lft = 0;
2525 if (!update_lft && !create && stored_lft) {
2526 const u32 minimum_lft = min_t(u32,
2527 stored_lft, MIN_VALID_LIFETIME);
2528 valid_lft = max(valid_lft, minimum_lft);
2529
2530 /* RFC4862 Section 5.5.3e:
2531 * "Note that the preferred lifetime of the
2532 * corresponding address is always reset to
2533 * the Preferred Lifetime in the received
2534 * Prefix Information option, regardless of
2535 * whether the valid lifetime is also reset or
2536 * ignored."
2537 *
2538 * So we should always update prefered_lft here.
2539 */
2540 update_lft = 1;
2541 }
2542
2543 if (update_lft) {
2544 ifp->valid_lft = valid_lft;
2545 ifp->prefered_lft = prefered_lft;
2546 ifp->tstamp = now;
2547 flags = ifp->flags;
2548 ifp->flags &= ~IFA_F_DEPRECATED;
2549 spin_unlock_bh(&ifp->lock);
2550
2551 if (!(flags&IFA_F_TENTATIVE))
2552 ipv6_ifa_notify(0, ifp);
2553 } else
2554 spin_unlock_bh(&ifp->lock);
2555
2556 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2557 create, now);
2558
2559 in6_ifa_put(ifp);
2560 addrconf_verify();
2561 }
2562
2563 return 0;
2564}
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002565EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
Alexander Aring4f672232016-06-15 21:20:22 +02002566
Neil Hormane6bff992012-01-04 10:49:15 +00002567void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
2569 struct prefix_info *pinfo;
2570 __u32 valid_lft;
2571 __u32 prefered_lft;
Alexander Aring4f672232016-06-15 21:20:22 +02002572 int addr_type, err;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002573 u32 addr_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07002575 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 if (len < sizeof(struct prefix_info)) {
dingtianhongba3542e2013-08-17 10:27:04 +08002580 ADBG("addrconf: prefix option too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 return;
2582 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002583
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 /*
2585 * Validation checks ([ADDRCONF], page 19)
2586 */
2587
2588 addr_type = ipv6_addr_type(&pinfo->prefix);
2589
2590 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2591 return;
2592
2593 valid_lft = ntohl(pinfo->valid);
2594 prefered_lft = ntohl(pinfo->prefered);
2595
2596 if (prefered_lft > valid_lft) {
Joe Perchese87cc472012-05-13 21:56:26 +00002597 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 return;
2599 }
2600
2601 in6_dev = in6_dev_get(dev);
2602
Ian Morris63159f22015-03-29 14:00:04 +01002603 if (!in6_dev) {
Joe Perchese87cc472012-05-13 21:56:26 +00002604 net_dbg_ratelimited("addrconf: device %s not configured\n",
2605 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 return;
2607 }
2608
2609 /*
2610 * Two things going on here:
2611 * 1) Add routes for on-link prefixes
2612 * 2) Configure prefixes with the auto flag set
2613 */
2614
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002615 if (pinfo->onlink) {
2616 struct rt6_info *rt;
2617 unsigned long rt_expires;
2618
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002619 /* Avoid arithmetic overflow. Really, we could
2620 * save rt_expires in seconds, likely valid_lft,
2621 * but it would require division in fib gc, that it
2622 * not good.
2623 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002624 if (HZ > USER_HZ)
2625 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2626 else
2627 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08002628
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002629 if (addrconf_finite_timeout(rt_expires))
2630 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002632 rt = addrconf_get_prefix_route(&pinfo->prefix,
2633 pinfo->prefix_len,
2634 dev,
2635 RTF_ADDRCONF | RTF_PREFIX_RT,
2636 RTF_GATEWAY | RTF_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002638 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002639 /* Autoconf prefix route */
2640 if (valid_lft == 0) {
2641 ip6_del_rt(rt);
2642 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002643 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002644 /* not infinity */
Gao feng1716a962012-04-06 00:13:10 +00002645 rt6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002646 } else {
Gao feng1716a962012-04-06 00:13:10 +00002647 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 }
2649 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002650 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002651 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2652 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002653 /* not infinity */
2654 flags |= RTF_EXPIRES;
2655 expires = jiffies_to_clock_t(rt_expires);
2656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002658 dev, expires, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00002660 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 }
2662
2663 /* Try to figure out our local address for this prefix */
2664
2665 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 struct in6_addr addr;
Alexander Aringf997c552016-06-15 21:20:23 +02002667 bool tokenized = false, dev_addr_generated = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
2669 if (pinfo->prefix_len == 64) {
2670 memcpy(&addr, &pinfo->prefix, 8);
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002671
2672 if (!ipv6_addr_any(&in6_dev->token)) {
2673 read_lock_bh(&in6_dev->lock);
2674 memcpy(addr.s6_addr + 8,
2675 in6_dev->token.s6_addr + 8, 8);
2676 read_unlock_bh(&in6_dev->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00002677 tokenized = true;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002678 } else if (is_addr_mode_generate_stable(in6_dev) &&
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01002679 !ipv6_generate_stable_address(&addr, 0,
2680 in6_dev)) {
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002681 addr_flags |= IFA_F_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01002682 goto ok;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002683 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2684 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
Alexander Aring4f672232016-06-15 21:20:22 +02002685 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002686 } else {
2687 dev_addr_generated = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 }
2689 goto ok;
2690 }
Joe Perchese87cc472012-05-13 21:56:26 +00002691 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2692 pinfo->prefix_len);
Alexander Aring4f672232016-06-15 21:20:22 +02002693 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
2695ok:
Alexander Aring4f672232016-06-15 21:20:22 +02002696 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2697 &addr, addr_type,
2698 addr_flags, sllao,
2699 tokenized, valid_lft,
2700 prefered_lft);
2701 if (err)
2702 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002703
2704 /* Ignore error case here because previous prefix add addr was
2705 * successful which will be notified.
2706 */
2707 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2708 addr_type, addr_flags, sllao,
2709 tokenized, valid_lft,
2710 prefered_lft,
2711 dev_addr_generated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 }
2713 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
Alexander Aring4f672232016-06-15 21:20:22 +02002714put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 in6_dev_put(in6_dev);
2716}
2717
2718/*
2719 * Set destination address.
2720 * Special case for SIT interfaces where we create a new "virtual"
2721 * device.
2722 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002723int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
2725 struct in6_ifreq ireq;
2726 struct net_device *dev;
2727 int err = -EINVAL;
2728
2729 rtnl_lock();
2730
2731 err = -EFAULT;
2732 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2733 goto err_exit;
2734
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002735 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
2737 err = -ENODEV;
Ian Morris63159f22015-03-29 14:00:04 +01002738 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 goto err_exit;
2740
Amerigo Wang07a93622012-10-29 16:23:10 +00002741#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002743 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 struct ip_tunnel_parm p;
2746
2747 err = -EADDRNOTAVAIL;
2748 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2749 goto err_exit;
2750
2751 memset(&p, 0, sizeof(p));
2752 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2753 p.iph.saddr = 0;
2754 p.iph.version = 4;
2755 p.iph.ihl = 5;
2756 p.iph.protocol = IPPROTO_IPV6;
2757 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002758 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002760 if (ops->ndo_do_ioctl) {
2761 mm_segment_t oldfs = get_fs();
2762
2763 set_fs(KERNEL_DS);
2764 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2765 set_fs(oldfs);
2766 } else
2767 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
2769 if (err == 0) {
2770 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002771 dev = __dev_get_by_name(net, p.name);
2772 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 goto err_exit;
2774 err = dev_open(dev);
2775 }
2776 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002777#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779err_exit:
2780 rtnl_unlock();
2781 return err;
2782}
2783
Madhu Challa93a714d2015-02-25 09:58:35 -08002784static int ipv6_mc_config(struct sock *sk, bool join,
2785 const struct in6_addr *addr, int ifindex)
2786{
2787 int ret;
2788
2789 ASSERT_RTNL();
2790
2791 lock_sock(sk);
2792 if (join)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002793 ret = ipv6_sock_mc_join(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002794 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002795 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002796 release_sock(sk);
2797
2798 return ret;
2799}
2800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801/*
2802 * Manual configuration of address on an interface
2803 */
Jiri Pirko479840f2013-12-06 09:45:21 +01002804static int inet6_addr_add(struct net *net, int ifindex,
2805 const struct in6_addr *pfx,
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002806 const struct in6_addr *peer_pfx,
Jiri Pirko479840f2013-12-06 09:45:21 +01002807 unsigned int plen, __u32 ifa_flags,
David Ahernde95e042017-10-18 09:56:54 -07002808 __u32 prefered_lft, __u32 valid_lft,
2809 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
2811 struct inet6_ifaddr *ifp;
2812 struct inet6_dev *idev;
2813 struct net_device *dev;
Madhu Challa93a714d2015-02-25 09:58:35 -08002814 unsigned long timeout;
2815 clock_t expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002817 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
2819 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002820
Thomas Graf24ef0da2008-05-28 16:54:22 +02002821 if (plen > 128)
2822 return -EINVAL;
2823
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09002824 /* check the lifetime */
2825 if (!valid_lft || prefered_lft > valid_lft)
2826 return -EINVAL;
2827
Jiri Pirko53bd6742013-12-06 09:45:22 +01002828 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2829 return -EINVAL;
2830
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002831 dev = __dev_get_by_index(net, ifindex);
2832 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002834
Brian Haley64e724f2010-07-20 10:34:30 +00002835 idev = addrconf_add_dev(dev);
2836 if (IS_ERR(idev))
2837 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Madhu Challa93a714d2015-02-25 09:58:35 -08002839 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2840 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2841 true, pfx, ifindex);
2842
2843 if (ret < 0)
2844 return ret;
2845 }
2846
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 scope = ipv6_addr_scope(pfx);
2848
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002849 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2850 if (addrconf_finite_timeout(timeout)) {
2851 expires = jiffies_to_clock_t(timeout * HZ);
2852 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002853 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002854 } else {
2855 expires = 0;
2856 flags = 0;
2857 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002858 }
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09002859
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002860 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2861 if (addrconf_finite_timeout(timeout)) {
2862 if (timeout == 0)
2863 ifa_flags |= IFA_F_DEPRECATED;
2864 prefered_lft = timeout;
2865 }
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09002866
Jiri Benc8a226b22013-08-01 10:41:28 +02002867 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
David Ahernde95e042017-10-18 09:56:54 -07002868 valid_lft, prefered_lft, true, extack);
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09002869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 if (!IS_ERR(ifp)) {
Thomas Haller761aac72014-01-15 15:36:58 +01002871 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2872 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2873 expires, flags);
2874 }
2875
Neil Horman95c385b2007-04-25 17:08:10 -07002876 /*
2877 * Note that section 3.1 of RFC 4429 indicates
2878 * that the Optimistic flag should not be set for
2879 * manually configured addresses
2880 */
David S. Millercf22f9a2012-04-14 21:37:40 -04002881 addrconf_dad_start(ifp);
Jiri Pirko53bd6742013-12-06 09:45:22 +01002882 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2883 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2884 true, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002886 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 return 0;
Madhu Challa93a714d2015-02-25 09:58:35 -08002888 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2889 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2890 false, pfx, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 }
2892
2893 return PTR_ERR(ifp);
2894}
2895
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002896static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2897 const struct in6_addr *pfx, unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898{
2899 struct inet6_ifaddr *ifp;
2900 struct inet6_dev *idev;
2901 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002902
Thomas Graf24ef0da2008-05-28 16:54:22 +02002903 if (plen > 128)
2904 return -EINVAL;
2905
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002906 dev = __dev_get_by_index(net, ifindex);
2907 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 return -ENODEV;
2909
Ian Morrise5d08d72014-11-23 21:28:43 +00002910 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01002911 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 return -ENXIO;
2913
2914 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002915 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 if (ifp->prefix_len == plen &&
2917 ipv6_addr_equal(pfx, &ifp->addr)) {
2918 in6_ifa_hold(ifp);
2919 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002920
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002921 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2922 (ifa_flags & IFA_F_MANAGETEMPADDR))
2923 manage_tempaddrs(idev, ifp, 0, 0, false,
2924 jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 ipv6_del_addr(ifp);
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002926 addrconf_verify_rtnl();
Madhu Challa93a714d2015-02-25 09:58:35 -08002927 if (ipv6_addr_is_multicast(pfx)) {
2928 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2929 false, pfx, dev->ifindex);
2930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 return 0;
2932 }
2933 }
2934 read_unlock_bh(&idev->lock);
2935 return -EADDRNOTAVAIL;
2936}
2937
2938
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002939int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
2941 struct in6_ifreq ireq;
2942 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002943
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002944 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2948 return -EFAULT;
2949
2950 rtnl_lock();
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002951 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002952 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
David Ahernde95e042017-10-18 09:56:54 -07002953 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 rtnl_unlock();
2955 return err;
2956}
2957
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002958int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
2960 struct in6_ifreq ireq;
2961 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002962
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002963 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 return -EPERM;
2965
2966 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2967 return -EFAULT;
2968
2969 rtnl_lock();
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002970 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002971 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 rtnl_unlock();
2973 return err;
2974}
2975
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002976static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2977 int plen, int scope)
2978{
2979 struct inet6_ifaddr *ifp;
2980
Jiri Benc8a226b22013-08-01 10:41:28 +02002981 ifp = ipv6_add_addr(idev, addr, NULL, plen,
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01002982 scope, IFA_F_PERMANENT,
David Ahernf3d98322017-10-18 09:56:52 -07002983 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME,
David Ahernde95e042017-10-18 09:56:54 -07002984 true, NULL);
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002985 if (!IS_ERR(ifp)) {
2986 spin_lock_bh(&ifp->lock);
2987 ifp->flags &= ~IFA_F_TENTATIVE;
2988 spin_unlock_bh(&ifp->lock);
Paolo Abeni764d3be2016-11-22 16:57:40 +01002989 rt_genid_bump_ipv6(dev_net(idev->dev));
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002990 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2991 in6_ifa_put(ifp);
2992 }
2993}
2994
Amerigo Wang07a93622012-10-29 16:23:10 +00002995#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996static void sit_add_v4_addrs(struct inet6_dev *idev)
2997{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 struct in6_addr addr;
2999 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003000 struct net *net = dev_net(idev->dev);
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01003001 int scope, plen;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01003002 u32 pflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
3004 ASSERT_RTNL();
3005
3006 memset(&addr, 0, sizeof(struct in6_addr));
3007 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
3008
3009 if (idev->dev->flags&IFF_POINTOPOINT) {
3010 addr.s6_addr32[0] = htonl(0xfe800000);
3011 scope = IFA_LINK;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01003012 plen = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 } else {
3014 scope = IPV6_ADDR_COMPATv4;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01003015 plen = 96;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01003016 pflags |= RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 }
3018
3019 if (addr.s6_addr32[3]) {
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01003020 add_addr(idev, &addr, plen, scope);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01003021 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 return;
3023 }
3024
Benjamin Thery6fda7352008-03-05 10:47:47 -08003025 for_each_netdev(net, dev) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003026 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 if (in_dev && (dev->flags & IFF_UP)) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003028 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
3030 int flag = scope;
3031
3032 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
3034 addr.s6_addr32[3] = ifa->ifa_local;
3035
3036 if (ifa->ifa_scope == RT_SCOPE_LINK)
3037 continue;
3038 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
3039 if (idev->dev->flags&IFF_POINTOPOINT)
3040 continue;
3041 flag |= IFA_HOST;
3042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003044 add_addr(idev, &addr, plen, flag);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01003045 addrconf_prefix_route(&addr, plen, idev->dev, 0,
3046 pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 }
3048 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003051#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
3053static void init_loopback(struct net_device *dev)
3054{
3055 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
3057 /* ::1 */
3058
3059 ASSERT_RTNL();
3060
Ian Morrise5d08d72014-11-23 21:28:43 +00003061 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003062 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003063 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 return;
3065 }
3066
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003067 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068}
3069
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003070void addrconf_add_linklocal(struct inet6_dev *idev,
3071 const struct in6_addr *addr, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003073 struct inet6_ifaddr *ifp;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003074 u32 addr_flags = flags | IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Neil Horman95c385b2007-04-25 17:08:10 -07003076#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Matteo Croce35e015e2017-09-12 17:46:37 +02003077 if ((dev_net(idev->dev)->ipv6.devconf_all->optimistic_dad ||
3078 idev->cnf.optimistic_dad) &&
David Miller702beb82008-07-20 18:17:02 -07003079 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
Neil Horman95c385b2007-04-25 17:08:10 -07003080 addr_flags |= IFA_F_OPTIMISTIC;
3081#endif
3082
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01003083 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
David Ahernde95e042017-10-18 09:56:54 -07003084 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME, true, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003086 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
David S. Millercf22f9a2012-04-14 21:37:40 -04003087 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 in6_ifa_put(ifp);
3089 }
3090}
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003091EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003093static bool ipv6_reserved_interfaceid(struct in6_addr address)
3094{
3095 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3096 return true;
3097
3098 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3099 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3100 return true;
3101
3102 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3103 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3104 return true;
3105
3106 return false;
3107}
3108
3109static int ipv6_generate_stable_address(struct in6_addr *address,
3110 u8 dad_count,
3111 const struct inet6_dev *idev)
3112{
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003113 static DEFINE_SPINLOCK(lock);
3114 static __u32 digest[SHA_DIGEST_WORDS];
3115 static __u32 workspace[SHA_WORKSPACE_WORDS];
3116
3117 static union {
3118 char __data[SHA_MESSAGE_BYTES];
3119 struct {
3120 struct in6_addr secret;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003121 __be32 prefix[2];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003122 unsigned char hwaddr[MAX_ADDR_LEN];
3123 u8 dad_count;
3124 } __packed;
3125 } data;
3126
3127 struct in6_addr secret;
3128 struct in6_addr temp;
3129 struct net *net = dev_net(idev->dev);
3130
3131 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3132
3133 if (idev->cnf.stable_secret.initialized)
3134 secret = idev->cnf.stable_secret.secret;
3135 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3136 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3137 else
3138 return -1;
3139
3140retry:
3141 spin_lock_bh(&lock);
3142
3143 sha_init(digest);
3144 memset(&data, 0, sizeof(data));
3145 memset(workspace, 0, sizeof(workspace));
3146 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003147 data.prefix[0] = address->s6_addr32[0];
3148 data.prefix[1] = address->s6_addr32[1];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003149 data.secret = secret;
3150 data.dad_count = dad_count;
3151
3152 sha_transform(digest, data.__data, workspace);
3153
3154 temp = *address;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003155 temp.s6_addr32[2] = (__force __be32)digest[0];
3156 temp.s6_addr32[3] = (__force __be32)digest[1];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003157
3158 spin_unlock_bh(&lock);
3159
3160 if (ipv6_reserved_interfaceid(temp)) {
3161 dad_count++;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01003162 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003163 return -1;
3164 goto retry;
3165 }
3166
3167 *address = temp;
3168 return 0;
3169}
3170
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003171static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3172{
3173 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3174
3175 if (s->initialized)
3176 return;
3177 s = &idev->cnf.stable_secret;
3178 get_random_bytes(&s->secret, sizeof(s->secret));
3179 s->initialized = true;
3180}
3181
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003182static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3183{
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003184 struct in6_addr addr;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003185
David Ahernca254492015-10-12 11:47:10 -07003186 /* no link local addresses on L3 master devices */
3187 if (netif_is_l3_master(idev->dev))
3188 return;
3189
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003190 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3191
Felix Jiad35a00b2017-01-26 16:59:17 +13003192 switch (idev->cnf.addr_gen_mode) {
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003193 case IN6_ADDR_GEN_MODE_RANDOM:
3194 ipv6_gen_mode_random_init(idev);
3195 /* fallthrough */
3196 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003197 if (!ipv6_generate_stable_address(&addr, 0, idev))
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003198 addrconf_add_linklocal(idev, &addr,
3199 IFA_F_STABLE_PRIVACY);
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003200 else if (prefix_route)
3201 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003202 break;
3203 case IN6_ADDR_GEN_MODE_EUI64:
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003204 /* addrconf_add_linklocal also adds a prefix_route and we
3205 * only need to care about prefix routes if ipv6_generate_eui64
3206 * couldn't generate one.
3207 */
3208 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003209 addrconf_add_linklocal(idev, &addr, 0);
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003210 else if (prefix_route)
3211 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003212 break;
3213 case IN6_ADDR_GEN_MODE_NONE:
3214 default:
3215 /* will not add any link local address */
3216 break;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003217 }
3218}
3219
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220static void addrconf_dev_config(struct net_device *dev)
3221{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003222 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
3224 ASSERT_RTNL();
3225
Herbert Xu74235a22007-06-14 13:02:55 -07003226 if ((dev->type != ARPHRD_ETHER) &&
3227 (dev->type != ARPHRD_FDDI) &&
Herbert Xu74235a22007-06-14 13:02:55 -07003228 (dev->type != ARPHRD_ARCNET) &&
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00003229 (dev->type != ARPHRD_INFINIBAND) &&
Nicolas Dichtele8377352013-08-20 12:16:06 +02003230 (dev->type != ARPHRD_IEEE1394) &&
Jukka Rissanene74bccb82013-12-11 17:05:36 +02003231 (dev->type != ARPHRD_TUNNEL6) &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003232 (dev->type != ARPHRD_6LOWPAN) &&
Felix Jia45ce0fd2017-01-26 16:59:18 +13003233 (dev->type != ARPHRD_IP6GRE) &&
3234 (dev->type != ARPHRD_IPGRE) &&
3235 (dev->type != ARPHRD_TUNNEL) &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003236 (dev->type != ARPHRD_NONE)) {
Herbert Xu74235a22007-06-14 13:02:55 -07003237 /* Alas, we support only Ethernet autoconfiguration. */
3238 return;
3239 }
3240
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00003242 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 return;
3244
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003245 /* this device type has no EUI support */
3246 if (dev->type == ARPHRD_NONE &&
Felix Jiad35a00b2017-01-26 16:59:17 +13003247 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3248 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003249
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003250 addrconf_addr_gen(idev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251}
3252
Amerigo Wang07a93622012-10-29 16:23:10 +00003253#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254static void addrconf_sit_config(struct net_device *dev)
3255{
3256 struct inet6_dev *idev;
3257
3258 ASSERT_RTNL();
3259
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003260 /*
3261 * Configure the tunnel with one of our IPv4
3262 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 * our v4 addrs in the tunnel
3264 */
3265
Ian Morrise5d08d72014-11-23 21:28:43 +00003266 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003267 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003268 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 return;
3270 }
3271
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003272 if (dev->priv_flags & IFF_ISATAP) {
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003273 addrconf_addr_gen(idev, false);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003274 return;
3275 }
3276
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 sit_add_v4_addrs(idev);
3278
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00003279 if (dev->flags&IFF_POINTOPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003282#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
Amerigo Wang07a93622012-10-29 16:23:10 +00003284#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003285static void addrconf_gre_config(struct net_device *dev)
3286{
3287 struct inet6_dev *idev;
stephen hemmingeraee80b52011-06-08 10:44:30 +00003288
stephen hemmingeraee80b52011-06-08 10:44:30 +00003289 ASSERT_RTNL();
3290
Ian Morrise5d08d72014-11-23 21:28:43 +00003291 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003292 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003293 pr_debug("%s: add_dev failed\n", __func__);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003294 return;
3295 }
3296
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003297 addrconf_addr_gen(idev, true);
Hannes Frederic Sowad9e4ce62015-10-08 18:19:39 +02003298 if (dev->flags & IFF_POINTOPOINT)
3299 addrconf_add_mroute(dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003300}
3301#endif
3302
David Ahernf1705ec2016-02-24 09:25:37 -08003303static int fixup_permanent_addr(struct inet6_dev *idev,
3304 struct inet6_ifaddr *ifp)
3305{
Ido Schimmelfc882fc2017-08-03 13:28:24 +02003306 /* !rt6i_node means the host route was removed from the
David Ahern8048ced2017-04-25 09:17:29 -07003307 * FIB, for example, if 'lo' device is taken down. In that
3308 * case regenerate the host route.
3309 */
Ido Schimmelfc882fc2017-08-03 13:28:24 +02003310 if (!ifp->rt || !ifp->rt->rt6i_node) {
David Ahern8048ced2017-04-25 09:17:29 -07003311 struct rt6_info *rt, *prev;
David Ahernf1705ec2016-02-24 09:25:37 -08003312
3313 rt = addrconf_dst_alloc(idev, &ifp->addr, false);
Tobias Klauser63a4e802017-09-26 11:22:31 +02003314 if (IS_ERR(rt))
David Ahernf1705ec2016-02-24 09:25:37 -08003315 return PTR_ERR(rt);
3316
David Ahern8048ced2017-04-25 09:17:29 -07003317 /* ifp->rt can be accessed outside of rtnl */
3318 spin_lock(&ifp->lock);
3319 prev = ifp->rt;
David Ahernf1705ec2016-02-24 09:25:37 -08003320 ifp->rt = rt;
David Ahern8048ced2017-04-25 09:17:29 -07003321 spin_unlock(&ifp->lock);
3322
3323 ip6_rt_put(prev);
David Ahernf1705ec2016-02-24 09:25:37 -08003324 }
3325
3326 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3327 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3328 idev->dev, 0, 0);
3329 }
3330
David Ahern6d717132017-05-02 14:43:44 -07003331 if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3332 addrconf_dad_start(ifp);
David Ahernf1705ec2016-02-24 09:25:37 -08003333
3334 return 0;
3335}
3336
3337static void addrconf_permanent_addr(struct net_device *dev)
3338{
3339 struct inet6_ifaddr *ifp, *tmp;
3340 struct inet6_dev *idev;
3341
3342 idev = __in6_dev_get(dev);
3343 if (!idev)
3344 return;
3345
3346 write_lock_bh(&idev->lock);
3347
3348 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3349 if ((ifp->flags & IFA_F_PERMANENT) &&
3350 fixup_permanent_addr(idev, ifp) < 0) {
3351 write_unlock_bh(&idev->lock);
3352 ipv6_del_addr(ifp);
3353 write_lock_bh(&idev->lock);
3354
3355 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3356 idev->dev->name, &ifp->addr);
3357 }
3358 }
3359
3360 write_unlock_bh(&idev->lock);
3361}
3362
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003363static int addrconf_notify(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00003364 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365{
Jiri Pirko351638e2013-05-28 01:30:21 +00003366 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
David Ahern4f7f34e2016-04-07 11:10:41 -07003367 struct netdev_notifier_changeupper_info *info;
Eric Dumazet748e2d92012-08-22 21:50:59 +00003368 struct inet6_dev *idev = __in6_dev_get(dev);
WANG Cong60abc0b2017-06-21 14:34:58 -07003369 struct net *net = dev_net(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003370 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07003371 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003373 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003374 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07003375 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003376 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -07003377 if (IS_ERR(idev))
3378 return notifier_from_errno(PTR_ERR(idev));
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003379 }
3380 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003381
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003382 case NETDEV_CHANGEMTU:
3383 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3384 if (dev->mtu < IPV6_MIN_MTU) {
WANG Cong60abc0b2017-06-21 14:34:58 -07003385 addrconf_ifdown(dev, dev != net->loopback_dev);
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003386 break;
3387 }
3388
3389 if (idev) {
3390 rt6_mtu_change(dev, dev->mtu);
3391 idev->cnf.mtu6 = dev->mtu;
3392 break;
3393 }
3394
3395 /* allocate new idev */
3396 idev = ipv6_add_dev(dev);
3397 if (IS_ERR(idev))
3398 break;
3399
3400 /* device is still not ready */
3401 if (!(idev->if_flags & IF_READY))
3402 break;
3403
3404 run_pending = 1;
3405
3406 /* fall through */
3407
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003409 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07003410 if (dev->flags & IFF_SLAVE)
3411 break;
3412
WANG Cong3ce62a82014-09-11 15:07:16 -07003413 if (idev && idev->cnf.disable_ipv6)
3414 break;
3415
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003416 if (event == NETDEV_UP) {
David Ahern38bd10c2016-04-21 20:56:12 -07003417 /* restore routes for permanent addresses */
3418 addrconf_permanent_addr(dev);
3419
Mike Manning1f372c72017-09-25 22:01:36 +01003420 if (!addrconf_link_ready(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003421 /* device is not ready yet. */
Joe Perchesf3213832012-05-15 14:11:53 +00003422 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003423 dev->name);
3424 break;
3425 }
Kristian Slavov99081042006-02-08 16:10:53 -08003426
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11003427 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3428 idev = ipv6_add_dev(dev);
3429
WANG Conga317a2f2014-07-25 15:25:09 -07003430 if (!IS_ERR_OR_NULL(idev)) {
Kristian Slavov99081042006-02-08 16:10:53 -08003431 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08003432 run_pending = 1;
3433 }
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003434 } else if (event == NETDEV_CHANGE) {
Mike Manning1f372c72017-09-25 22:01:36 +01003435 if (!addrconf_link_ready(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003436 /* device is still not ready. */
3437 break;
3438 }
3439
3440 if (idev) {
Linus Lüssinga088d1d2017-02-03 08:11:03 +01003441 if (idev->if_flags & IF_READY) {
3442 /* device is already configured -
3443 * but resend MLD reports, we might
3444 * have roamed and need to update
3445 * multicast snooping switches
3446 */
3447 ipv6_mc_up(idev);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003448 break;
Linus Lüssinga088d1d2017-02-03 08:11:03 +01003449 }
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003450 idev->if_flags |= IF_READY;
3451 }
3452
Joe Perchesf3213832012-05-15 14:11:53 +00003453 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3454 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003455
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003456 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003457 }
3458
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003459 switch (dev->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +00003460#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 case ARPHRD_SIT:
3462 addrconf_sit_config(dev);
3463 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07003464#endif
Amerigo Wang07a93622012-10-29 16:23:10 +00003465#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003466 case ARPHRD_IPGRE:
3467 addrconf_gre_config(dev);
3468 break;
3469#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 case ARPHRD_LOOPBACK:
3471 init_loopback(dev);
3472 break;
3473
3474 default:
3475 addrconf_dev_config(dev);
3476 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003477 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003478
WANG Conga317a2f2014-07-25 15:25:09 -07003479 if (!IS_ERR_OR_NULL(idev)) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003480 if (run_pending)
3481 addrconf_dad_run(idev);
3482
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003483 /*
3484 * If the MTU changed during the interface down,
3485 * when the interface up, the changed MTU must be
3486 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003488 if (idev->cnf.mtu6 != dev->mtu &&
3489 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 rt6_mtu_change(dev, dev->mtu);
3491 idev->cnf.mtu6 = dev->mtu;
3492 }
3493 idev->tstamp = jiffies;
3494 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003495
3496 /*
3497 * If the changed mtu during down is lower than
3498 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 */
3500 if (dev->mtu < IPV6_MIN_MTU)
WANG Cong60abc0b2017-06-21 14:34:58 -07003501 addrconf_ifdown(dev, dev != net->loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 }
3503 break;
3504
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 case NETDEV_DOWN:
3506 case NETDEV_UNREGISTER:
3507 /*
3508 * Remove all addresses from this interface.
3509 */
3510 addrconf_ifdown(dev, event != NETDEV_DOWN);
3511 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003512
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07003515 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003516 addrconf_sysctl_unregister(idev);
WANG Conga317a2f2014-07-25 15:25:09 -07003517 err = addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07003518 if (err)
3519 return notifier_from_errno(err);
WANG Conga317a2f2014-07-25 15:25:09 -07003520 err = snmp6_register_dev(idev);
3521 if (err) {
3522 addrconf_sysctl_unregister(idev);
3523 return notifier_from_errno(err);
3524 }
Stephen Hemminger5632c512007-04-28 21:16:39 -07003525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003527
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003528 case NETDEV_PRE_TYPE_CHANGE:
3529 case NETDEV_POST_TYPE_CHANGE:
Andrew Lunn3ef09522015-12-03 21:12:32 +01003530 if (idev)
3531 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07003532 break;
David Ahern4f7f34e2016-04-07 11:10:41 -07003533
3534 case NETDEV_CHANGEUPPER:
3535 info = ptr;
3536
3537 /* flush all routes if dev is linked to or unlinked from
3538 * an L3 master device (e.g., VRF)
3539 */
3540 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3541 addrconf_ifdown(dev, 0);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543
3544 return NOTIFY_OK;
3545}
3546
3547/*
3548 * addrconf module should be notified of a device going up
3549 */
3550static struct notifier_block ipv6_dev_notf = {
3551 .notifier_call = addrconf_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07003552 .priority = ADDRCONF_NOTIFY_PRIORITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553};
3554
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003555static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07003556{
3557 struct inet6_dev *idev;
3558 ASSERT_RTNL();
3559
3560 idev = __in6_dev_get(dev);
3561
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003562 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003563 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003564 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003565 ipv6_mc_unmap(idev);
3566}
3567
David Ahern70af9212016-04-08 12:01:21 -07003568static bool addr_is_local(const struct in6_addr *addr)
3569{
3570 return ipv6_addr_type(addr) &
3571 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3572}
3573
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574static int addrconf_ifdown(struct net_device *dev, int how)
3575{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003576 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003577 struct inet6_dev *idev;
David Ahernf1705ec2016-02-24 09:25:37 -08003578 struct inet6_ifaddr *ifa, *tmp;
David Ahernf1705ec2016-02-24 09:25:37 -08003579 int _keep_addr;
3580 bool keep_addr;
David S. Miller73a8bd72011-01-23 23:27:15 -08003581 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
3583 ASSERT_RTNL();
3584
David S. Miller73a8bd72011-01-23 23:27:15 -08003585 rt6_ifdown(net, dev);
3586 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
3588 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003589 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 return -ENODEV;
3591
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003592 /*
3593 * Step 1: remove reference to ipv6 device from parent device.
3594 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003596 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003598
3599 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003600 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
3602 /* Step 1.5: remove snmp6 entry */
3603 snmp6_unregister_dev(idev);
3604
3605 }
3606
David Ahernf1705ec2016-02-24 09:25:37 -08003607 /* aggregate the system setting and interface setting */
3608 _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3609 if (!_keep_addr)
3610 _keep_addr = idev->cnf.keep_addr_on_down;
3611
3612 /* combine the user config with event to determine if permanent
3613 * addresses are to be removed from address hash table
3614 */
Mike Manningbc561632016-08-12 12:02:38 +01003615 keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003616
David S. Miller73a8bd72011-01-23 23:27:15 -08003617 /* Step 2: clear hash table */
3618 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3619 struct hlist_head *h = &inet6_addr_lst[i];
David S. Miller73a8bd72011-01-23 23:27:15 -08003620
3621 spin_lock_bh(&addrconf_hash_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01003622restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08003623 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
David S. Miller73a8bd72011-01-23 23:27:15 -08003624 if (ifa->idev == idev) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003625 addrconf_del_dad_work(ifa);
David Ahernf1705ec2016-02-24 09:25:37 -08003626 /* combined flag + permanent flag decide if
3627 * address is retained on a down event
3628 */
3629 if (!keep_addr ||
David Ahern70af9212016-04-08 12:01:21 -07003630 !(ifa->flags & IFA_F_PERMANENT) ||
3631 addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003632 hlist_del_init_rcu(&ifa->addr_lst);
3633 goto restart;
3634 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003635 }
3636 }
3637 spin_unlock_bh(&addrconf_hash_lock);
3638 }
3639
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 write_lock_bh(&idev->lock);
3641
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003642 addrconf_del_rs_timer(idev);
3643
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003644 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003645 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003646 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003648 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00003649 while (!list_empty(&idev->tempaddr_list)) {
3650 ifa = list_first_entry(&idev->tempaddr_list,
3651 struct inet6_ifaddr, tmp_list);
3652 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 write_unlock_bh(&idev->lock);
3654 spin_lock_bh(&ifa->lock);
3655
3656 if (ifa->ifpub) {
3657 in6_ifa_put(ifa->ifpub);
3658 ifa->ifpub = NULL;
3659 }
3660 spin_unlock_bh(&ifa->lock);
3661 in6_ifa_put(ifa);
3662 write_lock_bh(&idev->lock);
3663 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003664
David Ahernf1705ec2016-02-24 09:25:37 -08003665 /* re-combine the user config with event to determine if permanent
3666 * addresses are to be removed from the interface list
3667 */
Mike Manningbc561632016-08-12 12:02:38 +01003668 keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003669
David Ahernf1705ec2016-02-24 09:25:37 -08003670 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
David Ahern38bd10c2016-04-21 20:56:12 -07003671 struct rt6_info *rt = NULL;
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003672 bool keep;
David Ahern38bd10c2016-04-21 20:56:12 -07003673
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003674 addrconf_del_dad_work(ifa);
stephen hemminger84e8b802010-03-02 13:32:46 +00003675
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003676 keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3677 !addr_is_local(&ifa->addr);
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003678
David S. Miller73a8bd72011-01-23 23:27:15 -08003679 write_unlock_bh(&idev->lock);
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003680 spin_lock_bh(&ifa->lock);
David Ahernf1705ec2016-02-24 09:25:37 -08003681
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003682 if (keep) {
David Ahernf1705ec2016-02-24 09:25:37 -08003683 /* set state to skip the notifier below */
3684 state = INET6_IFADDR_STATE_DEAD;
David Ahern6d717132017-05-02 14:43:44 -07003685 ifa->state = INET6_IFADDR_STATE_PREDAD;
David Ahernf1705ec2016-02-24 09:25:37 -08003686 if (!(ifa->flags & IFA_F_NODAD))
3687 ifa->flags |= IFA_F_TENTATIVE;
David Ahern38bd10c2016-04-21 20:56:12 -07003688
3689 rt = ifa->rt;
3690 ifa->rt = NULL;
David Ahernf1705ec2016-02-24 09:25:37 -08003691 } else {
3692 state = ifa->state;
3693 ifa->state = INET6_IFADDR_STATE_DEAD;
David Ahernf1705ec2016-02-24 09:25:37 -08003694 }
3695
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003696 spin_unlock_bh(&ifa->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003697
David Ahern38bd10c2016-04-21 20:56:12 -07003698 if (rt)
3699 ip6_del_rt(rt);
3700
David S. Miller73a8bd72011-01-23 23:27:15 -08003701 if (state != INET6_IFADDR_STATE_DEAD) {
3702 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Cong Wangf88c91d2013-04-14 23:18:43 +08003703 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
Mike Manningea06f712016-07-22 18:32:11 +01003704 } else {
3705 if (idev->cnf.forwarding)
3706 addrconf_leave_anycast(ifa);
3707 addrconf_leave_solict(ifa->idev, &ifa->addr);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00003708 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003709
David S. Miller73a8bd72011-01-23 23:27:15 -08003710 write_lock_bh(&idev->lock);
Eric Dumazet8ef802a2017-10-07 19:30:23 -07003711 if (!keep) {
3712 list_del_rcu(&ifa->if_list);
3713 in6_ifa_put(ifa);
3714 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003715 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003716
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 write_unlock_bh(&idev->lock);
3718
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003719 /* Step 5: Discard anycast and multicast list */
3720 if (how) {
3721 ipv6_ac_destroy_dev(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 ipv6_mc_destroy_dev(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003723 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 ipv6_mc_down(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003728
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003729 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003730 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003731 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 neigh_parms_release(&nd_tbl, idev->nd_parms);
3733 neigh_ifdown(&nd_tbl, dev);
3734 in6_dev_put(idev);
3735 }
3736 return 0;
3737}
3738
3739static void addrconf_rs_timer(unsigned long data)
3740{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003741 struct inet6_dev *idev = (struct inet6_dev *)data;
Cong Wangcaf92bc2013-08-31 13:44:32 +08003742 struct net_device *dev = idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003743 struct in6_addr lladdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003745 write_lock(&idev->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00003746 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 goto out;
3748
Shmulik Ladkani9ba2add2012-12-02 01:44:53 +00003749 if (!ipv6_accept_ra(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003751
3752 /* Announcement received after solicitation was sent */
3753 if (idev->if_flags & IF_RA_RCVD)
3754 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003756 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
Cong Wangcaf92bc2013-08-31 13:44:32 +08003757 write_unlock(&idev->lock);
3758 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3759 ndisc_send_rs(dev, &lladdr,
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003760 &in6addr_linklocal_allrouters);
3761 else
Cong Wangcaf92bc2013-08-31 13:44:32 +08003762 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
Cong Wangcaf92bc2013-08-31 13:44:32 +08003764 write_lock(&idev->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003765 idev->rs_interval = rfc3315_s14_backoff_update(
3766 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003767 /* The wait after the last probe can be shorter */
3768 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3769 idev->cnf.rtr_solicits) ?
3770 idev->cnf.rtr_solicit_delay :
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003771 idev->rs_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 /*
3774 * Note: we do not support deprecated "all on-link"
3775 * assumption any longer.
3776 */
Joe Perches91df42b2012-05-15 14:11:54 +00003777 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 }
3779
3780out:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003781 write_unlock(&idev->lock);
Cong Wangcaf92bc2013-08-31 13:44:32 +08003782put:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003783 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784}
3785
3786/*
3787 * Duplicate Address Detection
3788 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003789static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3790{
3791 unsigned long rand_num;
3792 struct inet6_dev *idev = ifp->idev;
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003793 u64 nonce;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003794
Neil Horman95c385b2007-04-25 17:08:10 -07003795 if (ifp->flags & IFA_F_OPTIMISTIC)
3796 rand_num = 0;
3797 else
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003798 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
Neil Horman95c385b2007-04-25 17:08:10 -07003799
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003800 nonce = 0;
3801 if (idev->cnf.enhanced_dad ||
3802 dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
3803 do
3804 get_random_bytes(&nonce, 6);
3805 while (nonce == 0);
3806 }
3807 ifp->dad_nonce = nonce;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003808 ifp->dad_probes = idev->cnf.dad_transmits;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003809 addrconf_mod_dad_work(ifp, rand_num);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003810}
3811
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003812static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813{
3814 struct inet6_dev *idev = ifp->idev;
3815 struct net_device *dev = idev->dev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003816 bool bump_id, notify = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
3818 addrconf_join_solict(dev, &ifp->addr);
3819
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003820 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
3822 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003823 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003824 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
3827 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
Matteo Crocea2d3f3e2017-10-05 19:03:05 +02003828 (dev_net(dev)->ipv6.devconf_all->accept_dad < 1 &&
3829 idev->cnf.accept_dad < 1) ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003830 !(ifp->flags&IFA_F_TENTATIVE) ||
3831 ifp->flags & IFA_F_NODAD) {
Paolo Abeni764d3be2016-11-22 16:57:40 +01003832 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003833 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003834 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 read_unlock_bh(&idev->lock);
3836
Paolo Abeni764d3be2016-11-22 16:57:40 +01003837 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 return;
3839 }
3840
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003841 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003842 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003843 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003844 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003845 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003846 * - keep it tentative if it is a permanent address.
3847 * - otherwise, kill it.
3848 */
3849 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003850 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003851 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003852 }
Neil Horman95c385b2007-04-25 17:08:10 -07003853
3854 /*
3855 * Optimistic nodes can start receiving
3856 * Frames right away
3857 */
Erik Kline7fd25612014-10-28 18:11:14 +09003858 if (ifp->flags & IFA_F_OPTIMISTIC) {
Neil Horman95c385b2007-04-25 17:08:10 -07003859 ip6_ins_rt(ifp->rt);
Matteo Croce35e015e2017-09-12 17:46:37 +02003860 if (ipv6_use_optimistic_addr(dev_net(dev), idev)) {
Erik Kline7fd25612014-10-28 18:11:14 +09003861 /* Because optimistic nodes can use this address,
3862 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3863 */
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003864 notify = true;
Erik Kline7fd25612014-10-28 18:11:14 +09003865 }
3866 }
Neil Horman95c385b2007-04-25 17:08:10 -07003867
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003868 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003870 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 read_unlock_bh(&idev->lock);
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003872 if (notify)
3873 ipv6_ifa_notify(RTM_NEWADDR, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874}
3875
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003876static void addrconf_dad_start(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003878 bool begin_dad = false;
3879
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003880 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003881 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3882 ifp->state = INET6_IFADDR_STATE_PREDAD;
3883 begin_dad = true;
3884 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003885 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003886
3887 if (begin_dad)
3888 addrconf_mod_dad_work(ifp, 0);
3889}
3890
3891static void addrconf_dad_work(struct work_struct *w)
3892{
3893 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3894 struct inet6_ifaddr,
3895 dad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 struct inet6_dev *idev = ifp->idev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003897 bool bump_id, disable_ipv6 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 struct in6_addr mcaddr;
3899
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003900 enum {
3901 DAD_PROCESS,
3902 DAD_BEGIN,
3903 DAD_ABORT,
3904 } action = DAD_PROCESS;
3905
3906 rtnl_lock();
3907
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003908 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003909 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3910 action = DAD_BEGIN;
3911 ifp->state = INET6_IFADDR_STATE_DAD;
3912 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3913 action = DAD_ABORT;
3914 ifp->state = INET6_IFADDR_STATE_POSTDAD;
Mike Manning85b51b12016-08-18 14:39:40 +01003915
Matteo Croce35e015e2017-09-12 17:46:37 +02003916 if ((dev_net(idev->dev)->ipv6.devconf_all->accept_dad > 1 ||
3917 idev->cnf.accept_dad > 1) &&
3918 !idev->cnf.disable_ipv6 &&
Mike Manning85b51b12016-08-18 14:39:40 +01003919 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
3920 struct in6_addr addr;
3921
3922 addr.s6_addr32[0] = htonl(0xfe800000);
3923 addr.s6_addr32[1] = 0;
3924
3925 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
3926 ipv6_addr_equal(&ifp->addr, &addr)) {
3927 /* DAD failed for link-local based on MAC */
3928 idev->cnf.disable_ipv6 = 1;
3929
3930 pr_info("%s: IPv6 being disabled!\n",
3931 ifp->idev->dev->name);
3932 disable_ipv6 = true;
3933 }
3934 }
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003935 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003936 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003937
3938 if (action == DAD_BEGIN) {
3939 addrconf_dad_begin(ifp);
3940 goto out;
3941 } else if (action == DAD_ABORT) {
Wei Yongjun751eb6b2016-09-05 16:06:31 +08003942 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003943 addrconf_dad_stop(ifp, 1);
Mike Manning85b51b12016-08-18 14:39:40 +01003944 if (disable_ipv6)
3945 addrconf_ifdown(idev->dev, 0);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003946 goto out;
3947 }
3948
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003949 if (!ifp->dad_probes && addrconf_dad_end(ifp))
Herbert Xuf2344a12010-05-18 15:55:27 -07003950 goto out;
3951
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003952 write_lock_bh(&idev->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003953 if (idev->dead || !(idev->if_flags & IF_READY)) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003954 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 goto out;
3956 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003957
3958 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003959 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3960 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003961 write_unlock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003962 goto out;
3963 }
3964
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003965 if (ifp->dad_probes == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 /*
3967 * DAD was successful
3968 */
3969
Paolo Abeni764d3be2016-11-22 16:57:40 +01003970 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003971 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003972 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003973 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974
Paolo Abeni764d3be2016-11-22 16:57:40 +01003975 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976
3977 goto out;
3978 }
3979
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003980 ifp->dad_probes--;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003981 addrconf_mod_dad_work(ifp,
3982 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
stephen hemminger21809fa2010-02-08 19:48:52 +00003983 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003984 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
3986 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003988 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
3989 ifp->dad_nonce);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990out:
3991 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003992 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993}
3994
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003995/* ifp->idev must be at least read locked */
3996static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3997{
3998 struct inet6_ifaddr *ifpiter;
3999 struct inet6_dev *idev = ifp->idev;
4000
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01004001 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
4002 if (ifpiter->scope > IFA_LINK)
4003 break;
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004004 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
4005 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
4006 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
4007 IFA_F_PERMANENT)
4008 return false;
4009 }
4010 return true;
4011}
4012
Paolo Abeni764d3be2016-11-22 16:57:40 +01004013static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004015 struct net_device *dev = ifp->idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004016 struct in6_addr lladdr;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004017 bool send_rs, send_mld;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004018
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004019 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
4021 /*
4022 * Configure the address for reception. Now it is valid.
4023 */
4024
4025 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4026
Tore Anderson026359b2011-08-28 23:47:33 +00004027 /* If added prefix is link local and we are prepared to process
4028 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 */
4030
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004031 read_lock_bh(&ifp->idev->lock);
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004032 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004033 send_rs = send_mld &&
4034 ipv6_accept_ra(ifp->idev) &&
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004035 ifp->idev->cnf.rtr_solicits != 0 &&
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004036 (dev->flags&IFF_LOOPBACK) == 0;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004037 read_unlock_bh(&ifp->idev->lock);
4038
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004039 /* While dad is in progress mld report's source address is in6_addrany.
4040 * Resend with proper ll now.
4041 */
4042 if (send_mld)
4043 ipv6_mc_dad_complete(ifp->idev);
4044
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004045 if (send_rs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 /*
4047 * If a host as already performed a random delay
4048 * [...] as part of DAD [...] there is no need
4049 * to delay again before sending the first RS
4050 */
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004051 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004052 return;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004053 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004055 write_lock_bh(&ifp->idev->lock);
4056 spin_lock(&ifp->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004057 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4058 ifp->idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004059 ifp->idev->rs_probes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 ifp->idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004061 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004062 spin_unlock(&ifp->lock);
4063 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 }
Paolo Abeni764d3be2016-11-22 16:57:40 +01004065
4066 if (bump_id)
4067 rt_genid_bump_ipv6(dev_net(dev));
Marcus Huewea11a7f72017-02-06 18:34:56 +01004068
4069 /* Make sure that a new temporary address will be created
4070 * before this temporary address becomes deprecated.
4071 */
4072 if (ifp->flags & IFA_F_TEMPORARY)
4073 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074}
4075
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004076static void addrconf_dad_run(struct inet6_dev *idev)
4077{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004078 struct inet6_ifaddr *ifp;
4079
4080 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00004081 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00004082 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07004083 if (ifp->flags & IFA_F_TENTATIVE &&
4084 ifp->state == INET6_IFADDR_STATE_DAD)
4085 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00004086 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004087 }
4088 read_unlock_bh(&idev->lock);
4089}
4090
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091#ifdef CONFIG_PROC_FS
4092struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08004093 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004095 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096};
4097
Mihai Maruseac1d578302012-01-03 23:31:35 +00004098static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099{
4100 struct inet6_ifaddr *ifa = NULL;
4101 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004102 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00004103 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
Mihai Maruseac1d578302012-01-03 23:31:35 +00004105 /* initial bucket if pos is 0 */
4106 if (pos == 0) {
4107 state->bucket = 0;
4108 state->offset = 0;
4109 }
4110
4111 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08004112 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00004113 addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004114 if (!net_eq(dev_net(ifa->idev->dev), net))
4115 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004116 /* sync with offset */
4117 if (p < state->offset) {
4118 p++;
4119 continue;
4120 }
4121 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004122 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004123 }
4124
4125 /* prepare for next bucket */
4126 state->offset = 0;
4127 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004129 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130}
4131
stephen hemmingerc2e21292010-03-17 20:31:10 +00004132static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4133 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134{
4135 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004136 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137
Sasha Levinb67bfe02013-02-27 17:06:00 -08004138 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004139 if (!net_eq(dev_net(ifa->idev->dev), net))
4140 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004141 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004142 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004143 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004144
4145 while (++state->bucket < IN6_ADDR_HSIZE) {
Mihai Maruseac1d578302012-01-03 23:31:35 +00004146 state->offset = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004147 hlist_for_each_entry_rcu_bh(ifa,
stephen hemmingerc2e21292010-03-17 20:31:10 +00004148 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004149 if (!net_eq(dev_net(ifa->idev->dev), net))
4150 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004151 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004152 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08004153 }
4154 }
4155
stephen hemmingerc2e21292010-03-17 20:31:10 +00004156 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157}
4158
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004160 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161{
stephen hemminger5c578aed2010-03-17 20:31:11 +00004162 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00004163 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164}
4165
4166static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4167{
4168 struct inet6_ifaddr *ifa;
4169
4170 ifa = if6_get_next(seq, v);
4171 ++*pos;
4172 return ifa;
4173}
4174
4175static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004176 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177{
stephen hemminger5c578aed2010-03-17 20:31:11 +00004178 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179}
4180
4181static int if6_seq_show(struct seq_file *seq, void *v)
4182{
4183 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Jiri Pirko971a3512013-12-10 13:56:29 +01004184 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07004185 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 ifp->idev->dev->ifindex,
4187 ifp->prefix_len,
4188 ifp->scope,
Jiri Pirko971a3512013-12-10 13:56:29 +01004189 (u8) ifp->flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 ifp->idev->dev->name);
4191 return 0;
4192}
4193
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004194static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 .start = if6_seq_start,
4196 .next = if6_seq_next,
4197 .show = if6_seq_show,
4198 .stop = if6_seq_stop,
4199};
4200
4201static int if6_seq_open(struct inode *inode, struct file *file)
4202{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004203 return seq_open_net(inode, file, &if6_seq_ops,
4204 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205}
4206
Arjan van de Ven9a321442007-02-12 00:55:35 -08004207static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 .owner = THIS_MODULE,
4209 .open = if6_seq_open,
4210 .read = seq_read,
4211 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004212 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213};
4214
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004215static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216{
Gao fengd4beaa62013-02-18 01:34:54 +00004217 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 return -ENOMEM;
4219 return 0;
4220}
4221
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004222static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08004223{
Gao fengece31ff2013-02-18 01:34:56 +00004224 remove_proc_entry("if_inet6", net->proc_net);
Daniel Lezcano3c400902008-01-10 22:42:49 -08004225}
4226
4227static struct pernet_operations if6_proc_net_ops = {
Ian Morris67ba4152014-08-24 21:53:10 +01004228 .init = if6_proc_net_init,
4229 .exit = if6_proc_net_exit,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004230};
4231
4232int __init if6_proc_init(void)
4233{
4234 return register_pernet_subsys(&if6_proc_net_ops);
4235}
4236
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237void if6_proc_exit(void)
4238{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004239 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240}
4241#endif /* CONFIG_PROC_FS */
4242
Amerigo Wang07a93622012-10-29 16:23:10 +00004243#if IS_ENABLED(CONFIG_IPV6_MIP6)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004244/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004245int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004246{
Eric Dumazet3f27fb22017-10-23 16:17:47 -07004247 unsigned int hash = inet6_addr_hash(net, addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004248 struct inet6_ifaddr *ifp = NULL;
Eric Dumazet3f27fb22017-10-23 16:17:47 -07004249 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004250
stephen hemminger5c578aed2010-03-17 20:31:11 +00004251 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08004252 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09004253 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08004254 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09004255 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004256 (ifp->flags & IFA_F_HOMEADDRESS)) {
4257 ret = 1;
4258 break;
4259 }
4260 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00004261 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004262 return ret;
4263}
4264#endif
4265
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266/*
4267 * Periodic address status verification
4268 */
4269
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004270static void addrconf_verify_rtnl(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004272 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 int i;
4275
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004276 ASSERT_RTNL();
4277
stephen hemminger5c578aed2010-03-17 20:31:11 +00004278 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004280 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004282 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004284 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285restart:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004286 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
Yasushi Asanofad8da32013-12-31 12:04:19 +09004289 /* When setting preferred_lft to a value not zero or
4290 * infinity, while valid_lft is infinity
4291 * IFA_F_PERMANENT has a non-infinity life time.
4292 */
4293 if ((ifp->flags & IFA_F_PERMANENT) &&
4294 (ifp->prefered_lft == INFINITY_LIFE_TIME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 continue;
4296
4297 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004298 /* We try to batch several events at once. */
4299 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004301 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4302 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 spin_unlock(&ifp->lock);
4304 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 ipv6_del_addr(ifp);
4306 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004307 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4308 spin_unlock(&ifp->lock);
4309 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00004311 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 int deprecate = 0;
4313
4314 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4315 deprecate = 1;
4316 ifp->flags |= IFA_F_DEPRECATED;
4317 }
4318
Yasushi Asanofad8da32013-12-31 12:04:19 +09004319 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4320 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 next = ifp->tstamp + ifp->valid_lft * HZ;
4322
4323 spin_unlock(&ifp->lock);
4324
4325 if (deprecate) {
4326 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
4328 ipv6_ifa_notify(0, ifp);
4329 in6_ifa_put(ifp);
4330 goto restart;
4331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4333 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00004334 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4335 ifp->idev->cnf.dad_transmits *
Jiri Pirko1f9248e2013-12-07 19:26:53 +01004336 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
stephen hemminger88949cf2010-03-17 20:31:17 +00004337
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 if (age >= ifp->prefered_lft - regen_advance) {
4339 struct inet6_ifaddr *ifpub = ifp->ifpub;
4340 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4341 next = ifp->tstamp + ifp->prefered_lft * HZ;
4342 if (!ifp->regen_count && ifpub) {
4343 ifp->regen_count++;
4344 in6_ifa_hold(ifp);
4345 in6_ifa_hold(ifpub);
4346 spin_unlock(&ifp->lock);
stephen hemminger5c578aed2010-03-17 20:31:11 +00004347
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08004348 spin_lock(&ifpub->lock);
4349 ifpub->regen_count = 0;
4350 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 ipv6_create_tempaddr(ifpub, ifp);
4352 in6_ifa_put(ifpub);
4353 in6_ifa_put(ifp);
4354 goto restart;
4355 }
4356 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4357 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4358 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 } else {
4360 /* ifp->prefered_lft <= ifp->valid_lft */
4361 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4362 next = ifp->tstamp + ifp->prefered_lft * HZ;
4363 spin_unlock(&ifp->lock);
4364 }
4365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 }
4367
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004368 next_sec = round_jiffies_up(next);
4369 next_sched = next;
4370
4371 /* If rounded timeout is accurate enough, accept it. */
4372 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4373 next_sched = next_sec;
4374
4375 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4376 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4377 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4378
dingtianhongba3542e2013-08-17 10:27:04 +08004379 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4380 now, next, next_sec, next_sched);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004381 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
stephen hemminger5c578aed2010-03-17 20:31:11 +00004382 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383}
4384
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004385static void addrconf_verify_work(struct work_struct *w)
4386{
4387 rtnl_lock();
4388 addrconf_verify_rtnl();
4389 rtnl_unlock();
4390}
4391
4392static void addrconf_verify(void)
4393{
4394 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4395}
4396
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004397static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4398 struct in6_addr **peer_pfx)
Thomas Graf461d8832006-09-18 00:09:49 -07004399{
4400 struct in6_addr *pfx = NULL;
4401
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004402 *peer_pfx = NULL;
4403
Thomas Graf461d8832006-09-18 00:09:49 -07004404 if (addr)
4405 pfx = nla_data(addr);
4406
4407 if (local) {
4408 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004409 *peer_pfx = pfx;
4410 pfx = nla_data(local);
Thomas Graf461d8832006-09-18 00:09:49 -07004411 }
4412
4413 return pfx;
4414}
4415
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004416static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07004417 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4418 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4419 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
Jiri Pirko479840f2013-12-06 09:45:21 +01004420 [IFA_FLAGS] = { .len = sizeof(u32) },
Thomas Graf461d8832006-09-18 00:09:49 -07004421};
4422
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423static int
David Ahernc21ef3e2017-04-16 09:48:24 -07004424inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
4425 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004427 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07004428 struct ifaddrmsg *ifm;
4429 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004430 struct in6_addr *pfx, *peer_pfx;
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004431 u32 ifa_flags;
Thomas Grafb933f712006-09-18 00:10:19 -07004432 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
Johannes Bergfceb6432017-04-12 14:34:07 +02004434 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07004435 extack);
Thomas Grafb933f712006-09-18 00:10:19 -07004436 if (err < 0)
4437 return err;
4438
4439 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004440 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004441 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 return -EINVAL;
4443
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004444 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4445
4446 /* We ignore other flags so far. */
4447 ifa_flags &= IFA_F_MANAGETEMPADDR;
4448
4449 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4450 ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451}
4452
Jiri Pirko479840f2013-12-06 09:45:21 +01004453static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004454 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004455{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004456 u32 flags;
4457 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004458 unsigned long timeout;
Jiri Pirko53bd6742013-12-06 09:45:22 +01004459 bool was_managetempaddr;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004460 bool had_prefixroute;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09004461
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004462 ASSERT_RTNL();
4463
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004464 if (!valid_lft || (prefered_lft > valid_lft))
4465 return -EINVAL;
4466
Jiri Pirko53bd6742013-12-06 09:45:22 +01004467 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4468 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4469 return -EINVAL;
4470
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004471 timeout = addrconf_timeout_fixup(valid_lft, HZ);
4472 if (addrconf_finite_timeout(timeout)) {
4473 expires = jiffies_to_clock_t(timeout * HZ);
4474 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004475 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004476 } else {
4477 expires = 0;
4478 flags = 0;
4479 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004480 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004481
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004482 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4483 if (addrconf_finite_timeout(timeout)) {
4484 if (timeout == 0)
4485 ifa_flags |= IFA_F_DEPRECATED;
4486 prefered_lft = timeout;
4487 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004488
4489 spin_lock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004490 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004491 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4492 !(ifp->flags & IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004493 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
Thomas Haller761aac72014-01-15 15:36:58 +01004494 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4495 IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004496 ifp->flags |= ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004497 ifp->tstamp = jiffies;
4498 ifp->valid_lft = valid_lft;
4499 ifp->prefered_lft = prefered_lft;
4500
4501 spin_unlock_bh(&ifp->lock);
4502 if (!(ifp->flags&IFA_F_TENTATIVE))
4503 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004504
Thomas Haller761aac72014-01-15 15:36:58 +01004505 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4506 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4507 expires, flags);
Thomas Haller5b84efe2014-01-15 15:36:59 +01004508 } else if (had_prefixroute) {
4509 enum cleanup_prefix_rt_t action;
4510 unsigned long rt_expires;
4511
4512 write_lock_bh(&ifp->idev->lock);
4513 action = check_cleanup_prefix_route(ifp, &rt_expires);
4514 write_unlock_bh(&ifp->idev->lock);
4515
4516 if (action != CLEANUP_PREFIX_RT_NOP) {
4517 cleanup_prefix_route(ifp, rt_expires,
4518 action == CLEANUP_PREFIX_RT_DEL);
4519 }
Thomas Haller761aac72014-01-15 15:36:58 +01004520 }
Jiri Pirko53bd6742013-12-06 09:45:22 +01004521
4522 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4523 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4524 valid_lft = prefered_lft = 0;
4525 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4526 !was_managetempaddr, jiffies);
4527 }
4528
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004529 addrconf_verify_rtnl();
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004530
4531 return 0;
4532}
4533
4534static int
David Ahernc21ef3e2017-04-16 09:48:24 -07004535inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
4536 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004538 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07004539 struct ifaddrmsg *ifm;
4540 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004541 struct in6_addr *pfx, *peer_pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07004542 struct inet6_ifaddr *ifa;
4543 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004544 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
Jiri Pirko479840f2013-12-06 09:45:21 +01004545 u32 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07004546 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
Johannes Bergfceb6432017-04-12 14:34:07 +02004548 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07004549 extack);
Thomas Graf461d8832006-09-18 00:09:49 -07004550 if (err < 0)
4551 return err;
4552
4553 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004554 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004555 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 return -EINVAL;
4557
Thomas Graf461d8832006-09-18 00:09:49 -07004558 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09004559 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07004560
4561 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09004562 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07004563 preferred_lft = ci->ifa_prefered;
4564 } else {
4565 preferred_lft = INFINITY_LIFE_TIME;
4566 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09004567 }
4568
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004569 dev = __dev_get_by_index(net, ifm->ifa_index);
Ian Morris63159f22015-03-29 14:00:04 +01004570 if (!dev)
Thomas Graf7198f8c2006-09-18 00:13:46 -07004571 return -ENODEV;
4572
Jiri Pirko479840f2013-12-06 09:45:21 +01004573 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4574
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004575 /* We ignore other flags so far. */
Thomas Haller761aac72014-01-15 15:36:58 +01004576 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
Madhu Challa93a714d2015-02-25 09:58:35 -08004577 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004578
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08004579 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Ian Morris63159f22015-03-29 14:00:04 +01004580 if (!ifa) {
Thomas Graf7198f8c2006-09-18 00:13:46 -07004581 /*
4582 * It would be best to check for !NLM_F_CREATE here but
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08004583 * userspace already relies on not having to provide this.
Thomas Graf7198f8c2006-09-18 00:13:46 -07004584 */
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004585 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004586 ifm->ifa_prefixlen, ifa_flags,
David Ahernde95e042017-10-18 09:56:54 -07004587 preferred_lft, valid_lft, extack);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004588 }
4589
Thomas Graf7198f8c2006-09-18 00:13:46 -07004590 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4591 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4592 err = -EEXIST;
4593 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004594 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07004595
4596 in6_ifa_put(ifa);
4597
4598 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599}
4600
Jiri Pirko479840f2013-12-06 09:45:21 +01004601static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
Thomas Graf101bb222006-09-18 00:11:52 -07004602 u8 scope, int ifindex)
4603{
4604 struct ifaddrmsg *ifm;
4605
4606 ifm = nlmsg_data(nlh);
4607 ifm->ifa_family = AF_INET6;
4608 ifm->ifa_prefixlen = prefixlen;
4609 ifm->ifa_flags = flags;
4610 ifm->ifa_scope = scope;
4611 ifm->ifa_index = ifindex;
4612}
4613
Thomas Graf85486af2006-09-18 00:11:24 -07004614static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4615 unsigned long tstamp, u32 preferred, u32 valid)
4616{
4617 struct ifa_cacheinfo ci;
4618
Thomas Graf18a31e12010-11-17 04:12:02 +00004619 ci.cstamp = cstamp_delta(cstamp);
4620 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07004621 ci.ifa_prefered = preferred;
4622 ci.ifa_valid = valid;
4623
4624 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4625}
4626
Thomas Graf101bb222006-09-18 00:11:52 -07004627static inline int rt_scope(int ifa_scope)
4628{
4629 if (ifa_scope & IFA_HOST)
4630 return RT_SCOPE_HOST;
4631 else if (ifa_scope & IFA_LINK)
4632 return RT_SCOPE_LINK;
4633 else if (ifa_scope & IFA_SITE)
4634 return RT_SCOPE_SITE;
4635 else
4636 return RT_SCOPE_UNIVERSE;
4637}
4638
Thomas Graf0ab68032006-09-18 00:12:35 -07004639static inline int inet6_ifaddr_msgsize(void)
4640{
Thomas Graf339bf982006-11-10 14:10:15 -08004641 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004642 + nla_total_size(16) /* IFA_LOCAL */
Thomas Graf339bf982006-11-10 14:10:15 -08004643 + nla_total_size(16) /* IFA_ADDRESS */
Jiri Pirko479840f2013-12-06 09:45:21 +01004644 + nla_total_size(sizeof(struct ifa_cacheinfo))
4645 + nla_total_size(4) /* IFA_FLAGS */;
Thomas Graf0ab68032006-09-18 00:12:35 -07004646}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004647
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004649 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07004652 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
Eric W. Biederman15e47302012-09-07 20:12:54 +00004654 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004655 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004656 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004657
Thomas Graf101bb222006-09-18 00:11:52 -07004658 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4659 ifa->idev->dev->ifindex);
4660
Yasushi Asanofad8da32013-12-31 12:04:19 +09004661 if (!((ifa->flags&IFA_F_PERMANENT) &&
4662 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
Thomas Graf85486af2006-09-18 00:11:24 -07004663 preferred = ifa->prefered_lft;
4664 valid = ifa->valid_lft;
4665 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00004667 if (preferred > tval)
4668 preferred -= tval;
4669 else
4670 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00004671 if (valid != INFINITY_LIFE_TIME) {
4672 if (valid > tval)
4673 valid -= tval;
4674 else
4675 valid = 0;
4676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 }
4678 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07004679 preferred = INFINITY_LIFE_TIME;
4680 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 }
Thomas Graf85486af2006-09-18 00:11:24 -07004682
Cong Wang7996c792013-05-22 05:41:06 +00004683 if (!ipv6_addr_any(&ifa->peer_addr)) {
Jiri Benc930345e2015-03-29 16:59:25 +02004684 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4685 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004686 goto error;
4687 } else
Jiri Benc930345e2015-03-29 16:59:25 +02004688 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004689 goto error;
4690
4691 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4692 goto error;
Thomas Graf85486af2006-09-18 00:11:24 -07004693
Jiri Pirko479840f2013-12-06 09:45:21 +01004694 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4695 goto error;
4696
Johannes Berg053c0952015-01-16 22:09:00 +01004697 nlmsg_end(skb, nlh);
4698 return 0;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004699
4700error:
4701 nlmsg_cancel(skb, nlh);
4702 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703}
4704
4705static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004706 u32 portid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004709 u8 scope = RT_SCOPE_UNIVERSE;
4710 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711
Thomas Graf101bb222006-09-18 00:11:52 -07004712 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4713 scope = RT_SCOPE_SITE;
4714
Eric W. Biederman15e47302012-09-07 20:12:54 +00004715 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004716 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004717 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004718
Thomas Graf101bb222006-09-18 00:11:52 -07004719 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004720 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004721 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004722 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4723 nlmsg_cancel(skb, nlh);
4724 return -EMSGSIZE;
4725 }
Thomas Graf85486af2006-09-18 00:11:24 -07004726
Johannes Berg053c0952015-01-16 22:09:00 +01004727 nlmsg_end(skb, nlh);
4728 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729}
4730
4731static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004732 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004735 u8 scope = RT_SCOPE_UNIVERSE;
4736 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737
Thomas Graf101bb222006-09-18 00:11:52 -07004738 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4739 scope = RT_SCOPE_SITE;
4740
Eric W. Biederman15e47302012-09-07 20:12:54 +00004741 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004742 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004743 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004744
Thomas Graf101bb222006-09-18 00:11:52 -07004745 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004746 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004747 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004748 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4749 nlmsg_cancel(skb, nlh);
4750 return -EMSGSIZE;
4751 }
Thomas Graf85486af2006-09-18 00:11:24 -07004752
Johannes Berg053c0952015-01-16 22:09:00 +01004753 nlmsg_end(skb, nlh);
4754 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755}
4756
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004757enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 UNICAST_ADDR,
4759 MULTICAST_ADDR,
4760 ANYCAST_ADDR,
4761};
4762
Eric Dumazet234b27c2009-11-12 04:11:50 +00004763/* called with rcu_read_lock() */
4764static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4765 struct netlink_callback *cb, enum addr_type_t type,
4766 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 struct ifmcaddr6 *ifmca;
4769 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004770 int err = 1;
4771 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772
Eric Dumazet234b27c2009-11-12 04:11:50 +00004773 read_lock_bh(&idev->lock);
4774 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00004775 case UNICAST_ADDR: {
4776 struct inet6_ifaddr *ifa;
4777
Eric Dumazet234b27c2009-11-12 04:11:50 +00004778 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00004779 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4780 if (++ip_idx < s_ip_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004781 continue;
4782 err = inet6_fill_ifaddr(skb, ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004783 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004784 cb->nlh->nlmsg_seq,
4785 RTM_NEWADDR,
4786 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004787 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004788 break;
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004789 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004791 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00004792 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004793 case MULTICAST_ADDR:
4794 /* multicast address */
4795 for (ifmca = idev->mc_list; ifmca;
4796 ifmca = ifmca->next, ip_idx++) {
4797 if (ip_idx < s_ip_idx)
4798 continue;
4799 err = inet6_fill_ifmcaddr(skb, ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004800 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004801 cb->nlh->nlmsg_seq,
4802 RTM_GETMULTICAST,
4803 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004804 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004805 break;
4806 }
4807 break;
4808 case ANYCAST_ADDR:
4809 /* anycast address */
4810 for (ifaca = idev->ac_list; ifaca;
4811 ifaca = ifaca->aca_next, ip_idx++) {
4812 if (ip_idx < s_ip_idx)
4813 continue;
4814 err = inet6_fill_ifacaddr(skb, ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004815 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004816 cb->nlh->nlmsg_seq,
4817 RTM_GETANYCAST,
4818 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004819 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004820 break;
4821 }
4822 break;
4823 default:
4824 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004826 read_unlock_bh(&idev->lock);
4827 *p_ip_idx = ip_idx;
4828 return err;
4829}
4830
4831static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4832 enum addr_type_t type)
4833{
4834 struct net *net = sock_net(skb->sk);
4835 int h, s_h;
4836 int idx, ip_idx;
4837 int s_idx, s_ip_idx;
4838 struct net_device *dev;
4839 struct inet6_dev *idev;
4840 struct hlist_head *head;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004841
4842 s_h = cb->args[0];
4843 s_idx = idx = cb->args[1];
4844 s_ip_idx = ip_idx = cb->args[2];
4845
4846 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004847 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004848 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4849 idx = 0;
4850 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08004851 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet234b27c2009-11-12 04:11:50 +00004852 if (idx < s_idx)
4853 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07004854 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004855 s_ip_idx = 0;
4856 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004857 idev = __in6_dev_get(dev);
4858 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004859 goto cont;
4860
4861 if (in6_dump_addrs(idev, skb, cb, type,
David S. Miller7b46a642015-01-18 23:36:08 -05004862 s_ip_idx, &ip_idx) < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004863 goto done;
4864cont:
4865 idx++;
4866 }
4867 }
4868done:
4869 rcu_read_unlock();
4870 cb->args[0] = h;
4871 cb->args[1] = idx;
4872 cb->args[2] = ip_idx;
4873
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 return skb->len;
4875}
4876
4877static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4878{
4879 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004880
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 return inet6_dump_addr(skb, cb, type);
4882}
4883
4884static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4885{
4886 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004887
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 return inet6_dump_addr(skb, cb, type);
4889}
4890
4891
4892static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4893{
4894 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004895
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 return inet6_dump_addr(skb, cb, type);
4897}
4898
David Ahernc21ef3e2017-04-16 09:48:24 -07004899static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4900 struct netlink_ext_ack *extack)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004901{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004902 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004903 struct ifaddrmsg *ifm;
4904 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004905 struct in6_addr *addr = NULL, *peer;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004906 struct net_device *dev = NULL;
4907 struct inet6_ifaddr *ifa;
4908 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004909 int err;
4910
Johannes Bergfceb6432017-04-12 14:34:07 +02004911 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07004912 extack);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004913 if (err < 0)
Florian Westphalc24675f2017-10-11 10:28:01 +02004914 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004915
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004916 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
Florian Westphalc24675f2017-10-11 10:28:01 +02004917 if (!addr)
4918 return -EINVAL;
Thomas Graf1b29fc22006-09-18 00:10:50 -07004919
4920 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004921 if (ifm->ifa_index)
Florian Westphalc24675f2017-10-11 10:28:01 +02004922 dev = dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004923
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004924 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4925 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004926 err = -EADDRNOTAVAIL;
4927 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004928 }
4929
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004930 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4931 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004932 err = -ENOBUFS;
4933 goto errout_ifa;
4934 }
4935
Eric W. Biederman15e47302012-09-07 20:12:54 +00004936 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004937 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004938 if (err < 0) {
4939 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4940 WARN_ON(err == -EMSGSIZE);
4941 kfree_skb(skb);
4942 goto errout_ifa;
4943 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00004944 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004945errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004946 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004947errout:
Florian Westphalc24675f2017-10-11 10:28:01 +02004948 if (dev)
4949 dev_put(dev);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004950 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004951}
4952
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4954{
4955 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004956 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07004957 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958
Mahesh Bandewarf784ad32017-01-04 15:01:01 -08004959 /* Don't send DELADDR notification for TENTATIVE address,
4960 * since NEWADDR notification is sent only after removing
Mike Manning6819a142017-09-04 15:52:55 +01004961 * TENTATIVE flag, if DAD has not failed.
Mahesh Bandewarf784ad32017-01-04 15:01:01 -08004962 */
Mike Manning6819a142017-09-04 15:52:55 +01004963 if (ifa->flags & IFA_F_TENTATIVE && !(ifa->flags & IFA_F_DADFAILED) &&
4964 event == RTM_DELADDR)
Mahesh Bandewarf784ad32017-01-04 15:01:01 -08004965 return;
4966
Thomas Graf0ab68032006-09-18 00:12:35 -07004967 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01004968 if (!skb)
Thomas Graf5d620262006-08-15 00:35:02 -07004969 goto errout;
4970
4971 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004972 if (err < 0) {
4973 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4974 WARN_ON(err == -EMSGSIZE);
4975 kfree_skb(skb);
4976 goto errout;
4977 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004978 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4979 return;
Thomas Graf5d620262006-08-15 00:35:02 -07004980errout:
4981 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004982 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983}
4984
Dave Jonesb6f99a22007-03-22 12:27:49 -07004985static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 __s32 *array, int bytes)
4987{
Thomas Graf04561c12006-11-14 19:53:58 -08004988 BUG_ON(bytes < (DEVCONF_MAX * 4));
4989
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 memset(array, 0, bytes);
4991 array[DEVCONF_FORWARDING] = cnf->forwarding;
4992 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4993 array[DEVCONF_MTU6] = cnf->mtu6;
4994 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4995 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4996 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4997 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4998 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00004999 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
5000 jiffies_to_msecs(cnf->rtr_solicit_interval);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005001 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
5002 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
Thomas Graf93908d12010-11-17 01:44:24 +00005003 array[DEVCONF_RTR_SOLICIT_DELAY] =
5004 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02005006 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
5007 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
5008 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
5009 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
5011 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
5012 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
5013 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
5014 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08005016 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
Hangbin Liu8013d1d72015-07-30 14:28:42 +08005017 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08005018 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005019#ifdef CONFIG_IPV6_ROUTER_PREF
5020 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00005021 array[DEVCONF_RTR_PROBE_INTERVAL] =
5022 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08005023#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzbbea1242017-03-22 18:19:04 +09005024 array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08005025 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
5026#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005027#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07005028 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07005029 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07005030#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5031 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
Erik Kline7fd25612014-10-28 18:11:14 +09005032 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
Neil Horman95c385b2007-04-25 17:08:10 -07005033#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005034#ifdef CONFIG_IPV6_MROUTE
5035 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
5036#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09005037 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09005038 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00005039 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00005040 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02005041 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
Ben Greeard9333192014-06-25 14:44:53 -07005042 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
Harout Hedeshianc2943f12015-01-20 10:06:05 -07005043 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
Andy Gospodarek35103d12015-08-13 10:39:01 -04005044 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005045 /* we omit DEVCONF_STABLE_SECRET for now */
Erik Kline3985e8a2015-07-22 16:38:25 +09005046 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
Johannes Bergabbc3042016-02-04 13:31:19 +01005047 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
Johannes Berg7a02bf82016-02-04 13:31:20 +01005048 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
David Ahernf1705ec2016-02-24 09:25:37 -08005049 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
David Lebrun1ababeb2016-11-08 14:57:39 +01005050 array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
David Lebrunbf355b82016-11-08 14:57:42 +01005051#ifdef CONFIG_IPV6_SEG6_HMAC
5052 array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5053#endif
Erik Nordmarkadc176c2016-12-02 14:00:08 -08005054 array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
Felix Jiad35a00b2017-01-26 16:59:17 +13005055 array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
David Forsterdf789fe2017-02-23 16:27:18 +00005056 array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057}
5058
Thomas Grafb382b192010-11-16 04:33:57 +00005059static inline size_t inet6_ifla6_size(void)
5060{
5061 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5062 + nla_total_size(sizeof(struct ifla_cacheinfo))
5063 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5064 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005065 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5066 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
Thomas Grafb382b192010-11-16 04:33:57 +00005067}
5068
Thomas Graf339bf982006-11-10 14:10:15 -08005069static inline size_t inet6_if_nlmsg_size(void)
5070{
5071 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5072 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5073 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5074 + nla_total_size(4) /* IFLA_MTU */
5075 + nla_total_size(4) /* IFLA_LINK */
Andy Gospodarek03443382015-08-13 15:26:35 -04005076 + nla_total_size(1) /* IFLA_OPERSTATE */
Thomas Grafb382b192010-11-16 04:33:57 +00005077 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08005078}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005079
Eric Dumazetbe281e52011-05-19 01:14:23 +00005080static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Jia Heaca05672016-09-30 11:29:03 +08005081 int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005082{
5083 int i;
Jia Heaca05672016-09-30 11:29:03 +08005084 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005085 BUG_ON(pad < 0);
5086
5087 /* Use put_unaligned() because stats may not be aligned for u64. */
Jia Heaca05672016-09-30 11:29:03 +08005088 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5089 for (i = 1; i < ICMP6_MIB_MAX; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00005090 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005091
Jia Heaca05672016-09-30 11:29:03 +08005092 memset(&stats[ICMP6_MIB_MAX], 0, pad);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005093}
5094
WANG Cong698365f2014-05-05 15:55:55 -07005095static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305096 int bytes, size_t syncpoff)
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005097{
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305098 int i, c;
5099 u64 buff[IPSTATS_MIB_MAX];
5100 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5101
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005102 BUG_ON(pad < 0);
5103
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305104 memset(buff, 0, sizeof(buff));
5105 buff[0] = IPSTATS_MIB_MAX;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005106
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305107 for_each_possible_cpu(c) {
5108 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5109 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5110 }
5111
5112 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5113 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005114}
5115
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005116static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5117 int bytes)
5118{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005119 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005120 case IFLA_INET6_STATS:
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305121 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5122 offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005123 break;
5124 case IFLA_INET6_ICMP6STATS:
Jia Heaca05672016-09-30 11:29:03 +08005125 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005126 break;
5127 }
5128}
5129
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005130static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5131 u32 ext_filter_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005133 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08005134 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135
David S. Millerc78679e2012-04-01 20:27:33 -04005136 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5137 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08005139 ci.tstamp = cstamp_delta(idev->tstamp);
5140 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
Jiri Pirko1f9248e2013-12-07 19:26:53 +01005141 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
David S. Millerc78679e2012-04-01 20:27:33 -04005142 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5143 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005144 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
Ian Morris63159f22015-03-29 14:00:04 +01005145 if (!nla)
Thomas Graf04561c12006-11-14 19:53:58 -08005146 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005147 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005149 /* XXX - MC not implemented */
5150
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005151 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5152 return 0;
5153
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005154 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005155 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005156 goto nla_put_failure;
5157 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5158
5159 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005160 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005161 goto nla_put_failure;
5162 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005164 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
Ian Morris63159f22015-03-29 14:00:04 +01005165 if (!nla)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005166 goto nla_put_failure;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005167
Felix Jiad35a00b2017-01-26 16:59:17 +13005168 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005169 goto nla_put_failure;
5170
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005171 read_lock_bh(&idev->lock);
5172 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5173 read_unlock_bh(&idev->lock);
5174
Thomas Grafb382b192010-11-16 04:33:57 +00005175 return 0;
5176
5177nla_put_failure:
5178 return -EMSGSIZE;
5179}
5180
Arad, Ronenb1974ed2015-10-19 09:23:28 -07005181static size_t inet6_get_link_af_size(const struct net_device *dev,
5182 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005183{
5184 if (!__in6_dev_get(dev))
5185 return 0;
5186
5187 return inet6_ifla6_size();
5188}
5189
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005190static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5191 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005192{
5193 struct inet6_dev *idev = __in6_dev_get(dev);
5194
5195 if (!idev)
5196 return -ENODATA;
5197
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005198 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005199 return -EMSGSIZE;
5200
5201 return 0;
5202}
5203
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005204static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5205{
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005206 struct inet6_ifaddr *ifp;
5207 struct net_device *dev = idev->dev;
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005208 bool clear_token, update_rs = false;
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005209 struct in6_addr ll_addr;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005210
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005211 ASSERT_RTNL();
5212
Ian Morris63159f22015-03-29 14:00:04 +01005213 if (!token)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005214 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005215 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5216 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005217 if (!ipv6_accept_ra(idev))
5218 return -EINVAL;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005219 if (idev->cnf.rtr_solicits == 0)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005220 return -EINVAL;
5221
5222 write_lock_bh(&idev->lock);
5223
5224 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5225 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5226
5227 write_unlock_bh(&idev->lock);
5228
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005229 clear_token = ipv6_addr_any(token);
5230 if (clear_token)
5231 goto update_lft;
5232
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005233 if (!idev->dead && (idev->if_flags & IF_READY) &&
5234 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5235 IFA_F_OPTIMISTIC)) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005236 /* If we're not ready, then normal ifup will take care
5237 * of this. Otherwise, we need to request our rs here.
5238 */
5239 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5240 update_rs = true;
5241 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005242
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005243update_lft:
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005244 write_lock_bh(&idev->lock);
Daniel Borkmannfc403832013-04-09 03:47:15 +00005245
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005246 if (update_rs) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005247 idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005248 idev->rs_interval = rfc3315_s14_backoff_init(
5249 idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005250 idev->rs_probes = 1;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005251 addrconf_mod_rs_timer(idev, idev->rs_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005252 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005253
5254 /* Well, that's kinda nasty ... */
5255 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5256 spin_lock(&ifp->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00005257 if (ifp->tokenized) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005258 ifp->valid_lft = 0;
5259 ifp->prefered_lft = 0;
5260 }
5261 spin_unlock(&ifp->lock);
5262 }
5263
5264 write_unlock_bh(&idev->lock);
Lubomir Rintelb2ed64a2014-10-27 17:39:16 +01005265 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005266 addrconf_verify_rtnl();
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005267 return 0;
5268}
5269
Daniel Borkmann11b1f822015-02-05 14:39:11 +01005270static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5271 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5272 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5273};
5274
5275static int inet6_validate_link_af(const struct net_device *dev,
5276 const struct nlattr *nla)
5277{
5278 struct nlattr *tb[IFLA_INET6_MAX + 1];
5279
5280 if (dev && !__in6_dev_get(dev))
5281 return -EAFNOSUPPORT;
5282
Johannes Bergfceb6432017-04-12 14:34:07 +02005283 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy,
5284 NULL);
Daniel Borkmann11b1f822015-02-05 14:39:11 +01005285}
5286
Felix Jiad35a00b2017-01-26 16:59:17 +13005287static int check_addr_gen_mode(int mode)
5288{
5289 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5290 mode != IN6_ADDR_GEN_MODE_NONE &&
5291 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5292 mode != IN6_ADDR_GEN_MODE_RANDOM)
5293 return -EINVAL;
5294 return 1;
5295}
5296
5297static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5298 int mode)
5299{
5300 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5301 !idev->cnf.stable_secret.initialized &&
5302 !net->ipv6.devconf_dflt->stable_secret.initialized)
5303 return -EINVAL;
5304 return 1;
5305}
5306
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005307static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5308{
5309 int err = -EINVAL;
5310 struct inet6_dev *idev = __in6_dev_get(dev);
5311 struct nlattr *tb[IFLA_INET6_MAX + 1];
5312
5313 if (!idev)
5314 return -EAFNOSUPPORT;
5315
Johannes Bergfceb6432017-04-12 14:34:07 +02005316 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005317 BUG();
5318
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005319 if (tb[IFLA_INET6_TOKEN]) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005320 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005321 if (err)
5322 return err;
5323 }
5324
5325 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5326 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5327
Felix Jiad35a00b2017-01-26 16:59:17 +13005328 if (check_addr_gen_mode(mode) < 0 ||
5329 check_stable_privacy(idev, dev_net(dev), mode) < 0)
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005330 return -EINVAL;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005331
Felix Jiad35a00b2017-01-26 16:59:17 +13005332 idev->cnf.addr_gen_mode = mode;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005333 err = 0;
5334 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005335
5336 return err;
5337}
5338
Thomas Grafb382b192010-11-16 04:33:57 +00005339static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005340 u32 portid, u32 seq, int event, unsigned int flags)
Thomas Grafb382b192010-11-16 04:33:57 +00005341{
5342 struct net_device *dev = idev->dev;
5343 struct ifinfomsg *hdr;
5344 struct nlmsghdr *nlh;
5345 void *protoinfo;
5346
Eric W. Biederman15e47302012-09-07 20:12:54 +00005347 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005348 if (!nlh)
Thomas Grafb382b192010-11-16 04:33:57 +00005349 return -EMSGSIZE;
5350
5351 hdr = nlmsg_data(nlh);
5352 hdr->ifi_family = AF_INET6;
5353 hdr->__ifi_pad = 0;
5354 hdr->ifi_type = dev->type;
5355 hdr->ifi_index = dev->ifindex;
5356 hdr->ifi_flags = dev_get_flags(dev);
5357 hdr->ifi_change = 0;
5358
David S. Millerc78679e2012-04-01 20:27:33 -04005359 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5360 (dev->addr_len &&
5361 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5362 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02005363 (dev->ifindex != dev_get_iflink(dev) &&
Andy Gospodarek03443382015-08-13 15:26:35 -04005364 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5365 nla_put_u8(skb, IFLA_OPERSTATE,
5366 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
David S. Millerc78679e2012-04-01 20:27:33 -04005367 goto nla_put_failure;
Thomas Grafb382b192010-11-16 04:33:57 +00005368 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
Ian Morris63159f22015-03-29 14:00:04 +01005369 if (!protoinfo)
Thomas Grafb382b192010-11-16 04:33:57 +00005370 goto nla_put_failure;
5371
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005372 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005373 goto nla_put_failure;
5374
Thomas Graf04561c12006-11-14 19:53:58 -08005375 nla_nest_end(skb, protoinfo);
Johannes Berg053c0952015-01-16 22:09:00 +01005376 nlmsg_end(skb, nlh);
5377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
Thomas Graf04561c12006-11-14 19:53:58 -08005379nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005380 nlmsg_cancel(skb, nlh);
5381 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382}
5383
5384static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5385{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005386 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00005387 int h, s_h;
David S. Miller434a8a52009-11-11 18:53:00 -08005388 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 struct net_device *dev;
5390 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00005391 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392
Eric Dumazet84d26972009-11-09 12:11:28 +00005393 s_h = cb->args[0];
5394 s_idx = cb->args[1];
5395
5396 rcu_read_lock();
5397 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5398 idx = 0;
5399 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08005400 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet84d26972009-11-09 12:11:28 +00005401 if (idx < s_idx)
5402 goto cont;
5403 idev = __in6_dev_get(dev);
5404 if (!idev)
5405 goto cont;
5406 if (inet6_fill_ifinfo(skb, idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005407 NETLINK_CB(cb->skb).portid,
Eric Dumazet84d26972009-11-09 12:11:28 +00005408 cb->nlh->nlmsg_seq,
Johannes Berg053c0952015-01-16 22:09:00 +01005409 RTM_NEWLINK, NLM_F_MULTI) < 0)
Eric Dumazet84d26972009-11-09 12:11:28 +00005410 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07005411cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00005412 idx++;
5413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 }
Eric Dumazet84d26972009-11-09 12:11:28 +00005415out:
5416 rcu_read_unlock();
5417 cb->args[1] = idx;
5418 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419
5420 return skb->len;
5421}
5422
5423void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5424{
5425 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005426 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005427 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005428
Thomas Graf339bf982006-11-10 14:10:15 -08005429 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005430 if (!skb)
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005431 goto errout;
5432
5433 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005434 if (err < 0) {
5435 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5436 WARN_ON(err == -EMSGSIZE);
5437 kfree_skb(skb);
5438 goto errout;
5439 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005440 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005441 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005442errout:
5443 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005444 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445}
5446
Thomas Graf339bf982006-11-10 14:10:15 -08005447static inline size_t inet6_prefix_nlmsg_size(void)
5448{
5449 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5450 + nla_total_size(sizeof(struct in6_addr))
5451 + nla_total_size(sizeof(struct prefix_cacheinfo));
5452}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005453
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005455 struct prefix_info *pinfo, u32 portid, u32 seq,
Thomas Graf6051e2f2006-11-14 19:54:19 -08005456 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457{
Thomas Graf6051e2f2006-11-14 19:54:19 -08005458 struct prefixmsg *pmsg;
5459 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 struct prefix_cacheinfo ci;
5461
Eric W. Biederman15e47302012-09-07 20:12:54 +00005462 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005463 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005464 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08005465
5466 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07005468 pmsg->prefix_pad1 = 0;
5469 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 pmsg->prefix_ifindex = idev->dev->ifindex;
5471 pmsg->prefix_len = pinfo->prefix_len;
5472 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07005473 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 pmsg->prefix_flags = 0;
5475 if (pinfo->onlink)
5476 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5477 if (pinfo->autoconf)
5478 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5479
David S. Millerc78679e2012-04-01 20:27:33 -04005480 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5481 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 ci.preferred_time = ntohl(pinfo->prefered);
5483 ci.valid_time = ntohl(pinfo->valid);
David S. Millerc78679e2012-04-01 20:27:33 -04005484 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5485 goto nla_put_failure;
Johannes Berg053c0952015-01-16 22:09:00 +01005486 nlmsg_end(skb, nlh);
5487 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488
Thomas Graf6051e2f2006-11-14 19:54:19 -08005489nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005490 nlmsg_cancel(skb, nlh);
5491 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492}
5493
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005494static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 struct prefix_info *pinfo)
5496{
5497 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005498 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005499 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500
Thomas Graf339bf982006-11-10 14:10:15 -08005501 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005502 if (!skb)
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005503 goto errout;
5504
5505 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005506 if (err < 0) {
5507 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5508 WARN_ON(err == -EMSGSIZE);
5509 kfree_skb(skb);
5510 goto errout;
5511 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005512 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5513 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005514errout:
5515 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08005516 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517}
5518
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5520{
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005521 struct net *net = dev_net(ifp->idev->dev);
5522
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005523 if (event)
5524 ASSERT_RTNL();
5525
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5527
5528 switch (event) {
5529 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07005530 /*
5531 * If the address was optimistic
5532 * we inserted the route at the start of
5533 * our DAD process, so we don't need
5534 * to do it again
5535 */
Wei Wang4e587ea2017-08-25 15:03:10 -07005536 if (!rcu_access_pointer(ifp->rt->rt6i_node))
Neil Horman95c385b2007-04-25 17:08:10 -07005537 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 if (ifp->idev->cnf.forwarding)
5539 addrconf_join_anycast(ifp);
Cong Wang7996c792013-05-22 05:41:06 +00005540 if (!ipv6_addr_any(&ifp->peer_addr))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005541 addrconf_prefix_route(&ifp->peer_addr, 128,
5542 ifp->idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 break;
5544 case RTM_DELADDR:
5545 if (ifp->idev->cnf.forwarding)
5546 addrconf_leave_anycast(ifp);
5547 addrconf_leave_solict(ifp->idev, &ifp->addr);
Cong Wang7996c792013-05-22 05:41:06 +00005548 if (!ipv6_addr_any(&ifp->peer_addr)) {
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005549 struct rt6_info *rt;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005550
Nicolas Dichtele7478df2014-09-03 23:59:22 +02005551 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5552 ifp->idev->dev, 0, 0);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07005553 if (rt)
5554 ip6_del_rt(rt);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005555 }
David Ahern38bd10c2016-04-21 20:56:12 -07005556 if (ifp->rt) {
Wei Wangad65a2f2017-06-17 10:42:35 -07005557 if (dst_hold_safe(&ifp->rt->dst))
5558 ip6_del_rt(ifp->rt);
David Ahern38bd10c2016-04-21 20:56:12 -07005559 }
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02005560 rt_genid_bump_ipv6(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 break;
5562 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005563 atomic_inc(&net->ipv6.dev_addr_genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564}
5565
5566static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5567{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005568 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 if (likely(ifp->idev->dead == 0))
5570 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005571 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572}
5573
5574#ifdef CONFIG_SYSCTL
5575
5576static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005577int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 void __user *buffer, size_t *lenp, loff_t *ppos)
5579{
5580 int *valp = ctl->data;
5581 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005582 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005583 struct ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 int ret;
5585
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005586 /*
5587 * ctl->data points to idev->cnf.forwarding, we should
5588 * not modify it until we get the rtnl lock.
5589 */
5590 lctl = *ctl;
5591 lctl.data = &val;
5592
5593 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08005595 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00005596 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005597 if (ret)
5598 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005599 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600}
5601
Marcelo Leitner77751422015-02-23 11:17:13 -03005602static
5603int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5604 void __user *buffer, size_t *lenp, loff_t *ppos)
5605{
5606 struct inet6_dev *idev = ctl->extra1;
5607 int min_mtu = IPV6_MIN_MTU;
5608 struct ctl_table lctl;
5609
5610 lctl = *ctl;
5611 lctl.extra1 = &min_mtu;
5612 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5613
5614 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5615}
5616
Brian Haley56d417b2009-06-01 03:07:33 -07005617static void dev_disable_change(struct inet6_dev *idev)
5618{
Cong Wang75538c22013-05-29 11:30:50 +08005619 struct netdev_notifier_info info;
5620
Brian Haley56d417b2009-06-01 03:07:33 -07005621 if (!idev || !idev->dev)
5622 return;
5623
Cong Wang75538c22013-05-29 11:30:50 +08005624 netdev_notifier_info_init(&info, idev->dev);
Brian Haley56d417b2009-06-01 03:07:33 -07005625 if (idev->cnf.disable_ipv6)
Cong Wang75538c22013-05-29 11:30:50 +08005626 addrconf_notify(NULL, NETDEV_DOWN, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005627 else
Cong Wang75538c22013-05-29 11:30:50 +08005628 addrconf_notify(NULL, NETDEV_UP, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005629}
5630
5631static void addrconf_disable_change(struct net *net, __s32 newf)
5632{
5633 struct net_device *dev;
5634 struct inet6_dev *idev;
5635
Kefeng Wang03e4def2017-01-19 16:26:21 +08005636 for_each_netdev(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07005637 idev = __in6_dev_get(dev);
5638 if (idev) {
5639 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5640 idev->cnf.disable_ipv6 = newf;
5641 if (changed)
5642 dev_disable_change(idev);
5643 }
Brian Haley56d417b2009-06-01 03:07:33 -07005644 }
Brian Haley56d417b2009-06-01 03:07:33 -07005645}
5646
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005647static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07005648{
5649 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005650 int old;
5651
5652 if (!rtnl_trylock())
5653 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07005654
5655 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005656 old = *p;
5657 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07005658
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005659 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5660 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07005661 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005662 }
Brian Haley56d417b2009-06-01 03:07:33 -07005663
5664 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07005665 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5666 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005667 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07005668 dev_disable_change((struct inet6_dev *)table->extra1);
5669
5670 rtnl_unlock();
5671 return 0;
5672}
5673
5674static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005675int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07005676 void __user *buffer, size_t *lenp, loff_t *ppos)
5677{
5678 int *valp = ctl->data;
5679 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005680 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005681 struct ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07005682 int ret;
5683
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005684 /*
5685 * ctl->data points to idev->cnf.disable_ipv6, we should
5686 * not modify it until we get the rtnl lock.
5687 */
5688 lctl = *ctl;
5689 lctl.data = &val;
5690
5691 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07005692
5693 if (write)
5694 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005695 if (ret)
5696 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07005697 return ret;
5698}
5699
stephen hemmingerc92d5492013-12-17 22:37:14 -08005700static
5701int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5702 void __user *buffer, size_t *lenp, loff_t *ppos)
5703{
5704 int *valp = ctl->data;
5705 int ret;
5706 int old, new;
5707
5708 old = *valp;
5709 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5710 new = *valp;
5711
5712 if (write && old != new) {
5713 struct net *net = ctl->extra2;
5714
5715 if (!rtnl_trylock())
5716 return restart_syscall();
5717
5718 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
David Ahern85b3daa2017-03-28 14:28:04 -07005719 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
5720 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08005721 NETCONFA_IFINDEX_DEFAULT,
5722 net->ipv6.devconf_dflt);
5723 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
David Ahern85b3daa2017-03-28 14:28:04 -07005724 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
5725 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08005726 NETCONFA_IFINDEX_ALL,
5727 net->ipv6.devconf_all);
5728 else {
5729 struct inet6_dev *idev = ctl->extra1;
5730
David Ahern85b3daa2017-03-28 14:28:04 -07005731 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
5732 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08005733 idev->dev->ifindex,
5734 &idev->cnf);
5735 }
5736 rtnl_unlock();
5737 }
5738
5739 return ret;
5740}
5741
Felix Jiad35a00b2017-01-26 16:59:17 +13005742static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
5743 void __user *buffer, size_t *lenp,
5744 loff_t *ppos)
5745{
5746 int ret = 0;
5747 int new_val;
5748 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
5749 struct net *net = (struct net *)ctl->extra2;
5750
Felix Jia8c171d62017-02-27 12:41:23 +13005751 if (!rtnl_trylock())
5752 return restart_syscall();
5753
Felix Jiad35a00b2017-01-26 16:59:17 +13005754 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5755
5756 if (write) {
5757 new_val = *((int *)ctl->data);
5758
Felix Jia8c171d62017-02-27 12:41:23 +13005759 if (check_addr_gen_mode(new_val) < 0) {
5760 ret = -EINVAL;
5761 goto out;
5762 }
Felix Jiad35a00b2017-01-26 16:59:17 +13005763
5764 /* request for default */
5765 if (&net->ipv6.devconf_dflt->addr_gen_mode == ctl->data) {
5766 ipv6_devconf_dflt.addr_gen_mode = new_val;
5767
5768 /* request for individual net device */
5769 } else {
5770 if (!idev)
Felix Jia8c171d62017-02-27 12:41:23 +13005771 goto out;
Felix Jiad35a00b2017-01-26 16:59:17 +13005772
Felix Jia8c171d62017-02-27 12:41:23 +13005773 if (check_stable_privacy(idev, net, new_val) < 0) {
5774 ret = -EINVAL;
5775 goto out;
5776 }
Felix Jiad35a00b2017-01-26 16:59:17 +13005777
5778 if (idev->cnf.addr_gen_mode != new_val) {
5779 idev->cnf.addr_gen_mode = new_val;
Felix Jiad35a00b2017-01-26 16:59:17 +13005780 addrconf_dev_config(idev->dev);
Felix Jiad35a00b2017-01-26 16:59:17 +13005781 }
5782 }
5783 }
5784
Felix Jia8c171d62017-02-27 12:41:23 +13005785out:
5786 rtnl_unlock();
5787
Felix Jiad35a00b2017-01-26 16:59:17 +13005788 return ret;
5789}
5790
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005791static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5792 void __user *buffer, size_t *lenp,
5793 loff_t *ppos)
5794{
5795 int err;
5796 struct in6_addr addr;
5797 char str[IPV6_MAX_STRLEN];
5798 struct ctl_table lctl = *ctl;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005799 struct net *net = ctl->extra2;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005800 struct ipv6_stable_secret *secret = ctl->data;
5801
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005802 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5803 return -EIO;
5804
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005805 lctl.maxlen = IPV6_MAX_STRLEN;
5806 lctl.data = str;
5807
5808 if (!rtnl_trylock())
5809 return restart_syscall();
5810
5811 if (!write && !secret->initialized) {
5812 err = -EIO;
5813 goto out;
5814 }
5815
WANG Cong5449a5c2015-12-21 10:55:45 -08005816 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
5817 if (err >= sizeof(str)) {
5818 err = -EIO;
5819 goto out;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005820 }
5821
5822 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5823 if (err || !write)
5824 goto out;
5825
5826 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5827 err = -EIO;
5828 goto out;
5829 }
5830
5831 secret->initialized = true;
5832 secret->secret = addr;
5833
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005834 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5835 struct net_device *dev;
5836
5837 for_each_netdev(net, dev) {
5838 struct inet6_dev *idev = __in6_dev_get(dev);
5839
5840 if (idev) {
Felix Jiad35a00b2017-01-26 16:59:17 +13005841 idev->cnf.addr_gen_mode =
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005842 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5843 }
5844 }
5845 } else {
5846 struct inet6_dev *idev = ctl->extra1;
5847
Felix Jiad35a00b2017-01-26 16:59:17 +13005848 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005849 }
5850
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005851out:
5852 rtnl_unlock();
5853
5854 return err;
5855}
stephen hemmingerc92d5492013-12-17 22:37:14 -08005856
Andy Gospodarek35103d12015-08-13 10:39:01 -04005857static
5858int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
5859 int write,
5860 void __user *buffer,
5861 size_t *lenp,
5862 loff_t *ppos)
5863{
5864 int *valp = ctl->data;
5865 int val = *valp;
5866 loff_t pos = *ppos;
5867 struct ctl_table lctl;
5868 int ret;
5869
5870 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
5871 * we should not modify it until we get the rtnl lock.
5872 */
5873 lctl = *ctl;
5874 lctl.data = &val;
5875
5876 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5877
5878 if (write)
5879 ret = addrconf_fixup_linkdown(ctl, valp, val);
5880 if (ret)
5881 *ppos = pos;
5882 return ret;
5883}
5884
David Forsterdf789fe2017-02-23 16:27:18 +00005885static
5886void addrconf_set_nopolicy(struct rt6_info *rt, int action)
5887{
5888 if (rt) {
5889 if (action)
5890 rt->dst.flags |= DST_NOPOLICY;
5891 else
5892 rt->dst.flags &= ~DST_NOPOLICY;
5893 }
5894}
5895
5896static
5897void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
5898{
5899 struct inet6_ifaddr *ifa;
5900
5901 read_lock_bh(&idev->lock);
5902 list_for_each_entry(ifa, &idev->addr_list, if_list) {
5903 spin_lock(&ifa->lock);
5904 if (ifa->rt) {
5905 struct rt6_info *rt = ifa->rt;
David Forsterdf789fe2017-02-23 16:27:18 +00005906 int cpu;
5907
Wei Wang66f5d6c2017-10-06 12:06:10 -07005908 rcu_read_lock();
David Forsterdf789fe2017-02-23 16:27:18 +00005909 addrconf_set_nopolicy(ifa->rt, val);
5910 if (rt->rt6i_pcpu) {
5911 for_each_possible_cpu(cpu) {
5912 struct rt6_info **rtp;
5913
5914 rtp = per_cpu_ptr(rt->rt6i_pcpu, cpu);
5915 addrconf_set_nopolicy(*rtp, val);
5916 }
5917 }
Wei Wang66f5d6c2017-10-06 12:06:10 -07005918 rcu_read_unlock();
David Forsterdf789fe2017-02-23 16:27:18 +00005919 }
5920 spin_unlock(&ifa->lock);
5921 }
5922 read_unlock_bh(&idev->lock);
5923}
5924
5925static
5926int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
5927{
5928 struct inet6_dev *idev;
5929 struct net *net;
5930
5931 if (!rtnl_trylock())
5932 return restart_syscall();
5933
5934 *valp = val;
5935
5936 net = (struct net *)ctl->extra2;
5937 if (valp == &net->ipv6.devconf_dflt->disable_policy) {
5938 rtnl_unlock();
5939 return 0;
5940 }
5941
5942 if (valp == &net->ipv6.devconf_all->disable_policy) {
5943 struct net_device *dev;
5944
5945 for_each_netdev(net, dev) {
5946 idev = __in6_dev_get(dev);
5947 if (idev)
5948 addrconf_disable_policy_idev(idev, val);
5949 }
5950 } else {
5951 idev = (struct inet6_dev *)ctl->extra1;
5952 addrconf_disable_policy_idev(idev, val);
5953 }
5954
5955 rtnl_unlock();
5956 return 0;
5957}
5958
5959static
5960int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
5961 void __user *buffer, size_t *lenp,
5962 loff_t *ppos)
5963{
5964 int *valp = ctl->data;
5965 int val = *valp;
5966 loff_t pos = *ppos;
5967 struct ctl_table lctl;
5968 int ret;
5969
5970 lctl = *ctl;
5971 lctl.data = &val;
5972 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5973
5974 if (write && (*valp != val))
5975 ret = addrconf_disable_policy(ctl, valp, val);
5976
5977 if (ret)
5978 *ppos = pos;
5979
5980 return ret;
5981}
5982
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07005983static int minus_one = -1;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07005984static const int one = 1;
5985static const int two_five_five = 255;
5986
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03005987static const struct ctl_table addrconf_sysctl[] = {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005988 {
5989 .procname = "forwarding",
5990 .data = &ipv6_devconf.forwarding,
5991 .maxlen = sizeof(int),
5992 .mode = 0644,
5993 .proc_handler = addrconf_sysctl_forward,
5994 },
5995 {
5996 .procname = "hop_limit",
5997 .data = &ipv6_devconf.hop_limit,
5998 .maxlen = sizeof(int),
5999 .mode = 0644,
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006000 .proc_handler = proc_dointvec_minmax,
6001 .extra1 = (void *)&one,
6002 .extra2 = (void *)&two_five_five,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006003 },
6004 {
6005 .procname = "mtu",
6006 .data = &ipv6_devconf.mtu6,
6007 .maxlen = sizeof(int),
6008 .mode = 0644,
6009 .proc_handler = addrconf_sysctl_mtu,
6010 },
6011 {
6012 .procname = "accept_ra",
6013 .data = &ipv6_devconf.accept_ra,
6014 .maxlen = sizeof(int),
6015 .mode = 0644,
6016 .proc_handler = proc_dointvec,
6017 },
6018 {
6019 .procname = "accept_redirects",
6020 .data = &ipv6_devconf.accept_redirects,
6021 .maxlen = sizeof(int),
6022 .mode = 0644,
6023 .proc_handler = proc_dointvec,
6024 },
6025 {
6026 .procname = "autoconf",
6027 .data = &ipv6_devconf.autoconf,
6028 .maxlen = sizeof(int),
6029 .mode = 0644,
6030 .proc_handler = proc_dointvec,
6031 },
6032 {
6033 .procname = "dad_transmits",
6034 .data = &ipv6_devconf.dad_transmits,
6035 .maxlen = sizeof(int),
6036 .mode = 0644,
6037 .proc_handler = proc_dointvec,
6038 },
6039 {
6040 .procname = "router_solicitations",
6041 .data = &ipv6_devconf.rtr_solicits,
6042 .maxlen = sizeof(int),
6043 .mode = 0644,
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07006044 .proc_handler = proc_dointvec_minmax,
6045 .extra1 = &minus_one,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006046 },
6047 {
6048 .procname = "router_solicitation_interval",
6049 .data = &ipv6_devconf.rtr_solicit_interval,
6050 .maxlen = sizeof(int),
6051 .mode = 0644,
6052 .proc_handler = proc_dointvec_jiffies,
6053 },
6054 {
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07006055 .procname = "router_solicitation_max_interval",
6056 .data = &ipv6_devconf.rtr_solicit_max_interval,
6057 .maxlen = sizeof(int),
6058 .mode = 0644,
6059 .proc_handler = proc_dointvec_jiffies,
6060 },
6061 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006062 .procname = "router_solicitation_delay",
6063 .data = &ipv6_devconf.rtr_solicit_delay,
6064 .maxlen = sizeof(int),
6065 .mode = 0644,
6066 .proc_handler = proc_dointvec_jiffies,
6067 },
6068 {
6069 .procname = "force_mld_version",
6070 .data = &ipv6_devconf.force_mld_version,
6071 .maxlen = sizeof(int),
6072 .mode = 0644,
6073 .proc_handler = proc_dointvec,
6074 },
6075 {
6076 .procname = "mldv1_unsolicited_report_interval",
6077 .data =
6078 &ipv6_devconf.mldv1_unsolicited_report_interval,
6079 .maxlen = sizeof(int),
6080 .mode = 0644,
6081 .proc_handler = proc_dointvec_ms_jiffies,
6082 },
6083 {
6084 .procname = "mldv2_unsolicited_report_interval",
6085 .data =
6086 &ipv6_devconf.mldv2_unsolicited_report_interval,
6087 .maxlen = sizeof(int),
6088 .mode = 0644,
6089 .proc_handler = proc_dointvec_ms_jiffies,
6090 },
6091 {
6092 .procname = "use_tempaddr",
6093 .data = &ipv6_devconf.use_tempaddr,
6094 .maxlen = sizeof(int),
6095 .mode = 0644,
6096 .proc_handler = proc_dointvec,
6097 },
6098 {
6099 .procname = "temp_valid_lft",
6100 .data = &ipv6_devconf.temp_valid_lft,
6101 .maxlen = sizeof(int),
6102 .mode = 0644,
6103 .proc_handler = proc_dointvec,
6104 },
6105 {
6106 .procname = "temp_prefered_lft",
6107 .data = &ipv6_devconf.temp_prefered_lft,
6108 .maxlen = sizeof(int),
6109 .mode = 0644,
6110 .proc_handler = proc_dointvec,
6111 },
6112 {
6113 .procname = "regen_max_retry",
6114 .data = &ipv6_devconf.regen_max_retry,
6115 .maxlen = sizeof(int),
6116 .mode = 0644,
6117 .proc_handler = proc_dointvec,
6118 },
6119 {
6120 .procname = "max_desync_factor",
6121 .data = &ipv6_devconf.max_desync_factor,
6122 .maxlen = sizeof(int),
6123 .mode = 0644,
6124 .proc_handler = proc_dointvec,
6125 },
6126 {
6127 .procname = "max_addresses",
6128 .data = &ipv6_devconf.max_addresses,
6129 .maxlen = sizeof(int),
6130 .mode = 0644,
6131 .proc_handler = proc_dointvec,
6132 },
6133 {
6134 .procname = "accept_ra_defrtr",
6135 .data = &ipv6_devconf.accept_ra_defrtr,
6136 .maxlen = sizeof(int),
6137 .mode = 0644,
6138 .proc_handler = proc_dointvec,
6139 },
6140 {
6141 .procname = "accept_ra_min_hop_limit",
6142 .data = &ipv6_devconf.accept_ra_min_hop_limit,
6143 .maxlen = sizeof(int),
6144 .mode = 0644,
6145 .proc_handler = proc_dointvec,
6146 },
6147 {
6148 .procname = "accept_ra_pinfo",
6149 .data = &ipv6_devconf.accept_ra_pinfo,
6150 .maxlen = sizeof(int),
6151 .mode = 0644,
6152 .proc_handler = proc_dointvec,
6153 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08006154#ifdef CONFIG_IPV6_ROUTER_PREF
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006155 {
6156 .procname = "accept_ra_rtr_pref",
6157 .data = &ipv6_devconf.accept_ra_rtr_pref,
6158 .maxlen = sizeof(int),
6159 .mode = 0644,
6160 .proc_handler = proc_dointvec,
6161 },
6162 {
6163 .procname = "router_probe_interval",
6164 .data = &ipv6_devconf.rtr_probe_interval,
6165 .maxlen = sizeof(int),
6166 .mode = 0644,
6167 .proc_handler = proc_dointvec_jiffies,
6168 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08006169#ifdef CONFIG_IPV6_ROUTE_INFO
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006170 {
Joel Scherpelzbbea1242017-03-22 18:19:04 +09006171 .procname = "accept_ra_rt_info_min_plen",
6172 .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
6173 .maxlen = sizeof(int),
6174 .mode = 0644,
6175 .proc_handler = proc_dointvec,
6176 },
6177 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006178 .procname = "accept_ra_rt_info_max_plen",
6179 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
6180 .maxlen = sizeof(int),
6181 .mode = 0644,
6182 .proc_handler = proc_dointvec,
6183 },
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08006184#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08006185#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006186 {
6187 .procname = "proxy_ndp",
6188 .data = &ipv6_devconf.proxy_ndp,
6189 .maxlen = sizeof(int),
6190 .mode = 0644,
6191 .proc_handler = addrconf_sysctl_proxy_ndp,
6192 },
6193 {
6194 .procname = "accept_source_route",
6195 .data = &ipv6_devconf.accept_source_route,
6196 .maxlen = sizeof(int),
6197 .mode = 0644,
6198 .proc_handler = proc_dointvec,
6199 },
Neil Horman95c385b2007-04-25 17:08:10 -07006200#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006201 {
6202 .procname = "optimistic_dad",
6203 .data = &ipv6_devconf.optimistic_dad,
6204 .maxlen = sizeof(int),
6205 .mode = 0644,
6206 .proc_handler = proc_dointvec,
6207 },
6208 {
6209 .procname = "use_optimistic",
6210 .data = &ipv6_devconf.use_optimistic,
6211 .maxlen = sizeof(int),
6212 .mode = 0644,
6213 .proc_handler = proc_dointvec,
6214 },
Neil Horman95c385b2007-04-25 17:08:10 -07006215#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006216#ifdef CONFIG_IPV6_MROUTE
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006217 {
6218 .procname = "mc_forwarding",
6219 .data = &ipv6_devconf.mc_forwarding,
6220 .maxlen = sizeof(int),
6221 .mode = 0444,
6222 .proc_handler = proc_dointvec,
6223 },
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006224#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006225 {
6226 .procname = "disable_ipv6",
6227 .data = &ipv6_devconf.disable_ipv6,
6228 .maxlen = sizeof(int),
6229 .mode = 0644,
6230 .proc_handler = addrconf_sysctl_disable,
6231 },
6232 {
6233 .procname = "accept_dad",
6234 .data = &ipv6_devconf.accept_dad,
6235 .maxlen = sizeof(int),
6236 .mode = 0644,
6237 .proc_handler = proc_dointvec,
6238 },
6239 {
6240 .procname = "force_tllao",
6241 .data = &ipv6_devconf.force_tllao,
6242 .maxlen = sizeof(int),
6243 .mode = 0644,
6244 .proc_handler = proc_dointvec
6245 },
6246 {
6247 .procname = "ndisc_notify",
6248 .data = &ipv6_devconf.ndisc_notify,
6249 .maxlen = sizeof(int),
6250 .mode = 0644,
6251 .proc_handler = proc_dointvec
6252 },
6253 {
6254 .procname = "suppress_frag_ndisc",
6255 .data = &ipv6_devconf.suppress_frag_ndisc,
6256 .maxlen = sizeof(int),
6257 .mode = 0644,
6258 .proc_handler = proc_dointvec
6259 },
6260 {
6261 .procname = "accept_ra_from_local",
6262 .data = &ipv6_devconf.accept_ra_from_local,
6263 .maxlen = sizeof(int),
6264 .mode = 0644,
6265 .proc_handler = proc_dointvec,
6266 },
6267 {
6268 .procname = "accept_ra_mtu",
6269 .data = &ipv6_devconf.accept_ra_mtu,
6270 .maxlen = sizeof(int),
6271 .mode = 0644,
6272 .proc_handler = proc_dointvec,
6273 },
6274 {
6275 .procname = "stable_secret",
6276 .data = &ipv6_devconf.stable_secret,
6277 .maxlen = IPV6_MAX_STRLEN,
6278 .mode = 0600,
6279 .proc_handler = addrconf_sysctl_stable_secret,
6280 },
6281 {
6282 .procname = "use_oif_addrs_only",
6283 .data = &ipv6_devconf.use_oif_addrs_only,
6284 .maxlen = sizeof(int),
6285 .mode = 0644,
6286 .proc_handler = proc_dointvec,
6287 },
6288 {
6289 .procname = "ignore_routes_with_linkdown",
6290 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6291 .maxlen = sizeof(int),
6292 .mode = 0644,
6293 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6294 },
6295 {
6296 .procname = "drop_unicast_in_l2_multicast",
6297 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6298 .maxlen = sizeof(int),
6299 .mode = 0644,
6300 .proc_handler = proc_dointvec,
6301 },
6302 {
6303 .procname = "drop_unsolicited_na",
6304 .data = &ipv6_devconf.drop_unsolicited_na,
6305 .maxlen = sizeof(int),
6306 .mode = 0644,
6307 .proc_handler = proc_dointvec,
6308 },
6309 {
6310 .procname = "keep_addr_on_down",
6311 .data = &ipv6_devconf.keep_addr_on_down,
6312 .maxlen = sizeof(int),
6313 .mode = 0644,
6314 .proc_handler = proc_dointvec,
David Ahernf1705ec2016-02-24 09:25:37 -08006315
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006316 },
6317 {
David Lebrun1ababeb2016-11-08 14:57:39 +01006318 .procname = "seg6_enabled",
6319 .data = &ipv6_devconf.seg6_enabled,
6320 .maxlen = sizeof(int),
6321 .mode = 0644,
6322 .proc_handler = proc_dointvec,
6323 },
David Lebrunbf355b82016-11-08 14:57:42 +01006324#ifdef CONFIG_IPV6_SEG6_HMAC
6325 {
6326 .procname = "seg6_require_hmac",
6327 .data = &ipv6_devconf.seg6_require_hmac,
6328 .maxlen = sizeof(int),
6329 .mode = 0644,
6330 .proc_handler = proc_dointvec,
6331 },
6332#endif
David Lebrun1ababeb2016-11-08 14:57:39 +01006333 {
Erik Nordmarkadc176c2016-12-02 14:00:08 -08006334 .procname = "enhanced_dad",
6335 .data = &ipv6_devconf.enhanced_dad,
6336 .maxlen = sizeof(int),
6337 .mode = 0644,
6338 .proc_handler = proc_dointvec,
6339 },
6340 {
Felix Jiad35a00b2017-01-26 16:59:17 +13006341 .procname = "addr_gen_mode",
6342 .data = &ipv6_devconf.addr_gen_mode,
6343 .maxlen = sizeof(int),
6344 .mode = 0644,
6345 .proc_handler = addrconf_sysctl_addr_gen_mode,
6346 },
6347 {
David Forsterdf789fe2017-02-23 16:27:18 +00006348 .procname = "disable_policy",
6349 .data = &ipv6_devconf.disable_policy,
6350 .maxlen = sizeof(int),
6351 .mode = 0644,
6352 .proc_handler = addrconf_sysctl_disable_policy,
6353 },
6354 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006355 /* sentinel */
6356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357};
6358
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08006359static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006360 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361{
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006362 int i, ifindex;
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006363 struct ctl_table *table;
Eric W. Biederman6105e292012-04-19 13:41:24 +00006364 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11006365
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006366 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6367 if (!table)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006368 goto out;
6369
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006370 for (i = 0; table[i].data; i++) {
6371 table[i].data += (char *)p - (char *)&ipv6_devconf;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006372 /* If one of these is already set, then it is not safe to
6373 * overwrite either of them: this makes proc_dointvec_minmax
6374 * usable.
6375 */
6376 if (!table[i].extra1 && !table[i].extra2) {
6377 table[i].extra1 = idev; /* embedded; no ref */
6378 table[i].extra2 = net;
6379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381
Eric W. Biederman6105e292012-04-19 13:41:24 +00006382 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006384 p->sysctl_header = register_net_sysctl(net, path, table);
6385 if (!p->sysctl_header)
Eric W. Biederman6105e292012-04-19 13:41:24 +00006386 goto free;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006387
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006388 if (!strcmp(dev_name, "all"))
6389 ifindex = NETCONFA_IFINDEX_ALL;
6390 else if (!strcmp(dev_name, "default"))
6391 ifindex = NETCONFA_IFINDEX_DEFAULT;
6392 else
6393 ifindex = idev->dev->ifindex;
David Ahern85b3daa2017-03-28 14:28:04 -07006394 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
6395 ifindex, p);
Pavel Emelyanov95897312008-01-10 17:41:45 -08006396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006398free:
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006399 kfree(table);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006400out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08006401 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402}
6403
David Ahern23452172017-03-28 14:28:05 -07006404static void __addrconf_sysctl_unregister(struct net *net,
6405 struct ipv6_devconf *p, int ifindex)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006406{
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006407 struct ctl_table *table;
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006408
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006409 if (!p->sysctl_header)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006410 return;
6411
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006412 table = p->sysctl_header->ctl_table_arg;
6413 unregister_net_sysctl_table(p->sysctl_header);
6414 p->sysctl_header = NULL;
6415 kfree(table);
David Ahern23452172017-03-28 14:28:05 -07006416
6417 inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006418}
6419
WANG Conga317a2f2014-07-25 15:25:09 -07006420static int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006421{
WANG Conga317a2f2014-07-25 15:25:09 -07006422 int err;
6423
6424 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6425 return -EINVAL;
6426
6427 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6428 &ndisc_ifinfo_sysctl_change);
6429 if (err)
6430 return err;
6431 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6432 idev, &idev->cnf);
6433 if (err)
6434 neigh_sysctl_unregister(idev->nd_parms);
6435
6436 return err;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006437}
6438
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006439static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440{
David Ahern23452172017-03-28 14:28:05 -07006441 __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
6442 idev->dev->ifindex);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006443 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444}
6445
6446
6447#endif
6448
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006449static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006450{
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006451 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006452 struct ipv6_devconf *all, *dflt;
6453
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006454 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006455 if (!all)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006456 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006457
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006458 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006459 if (!dflt)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006460 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006461
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006462 /* these will be inherited by all namespaces */
6463 dflt->autoconf = ipv6_defaults.autoconf;
6464 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006465
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006466 dflt->stable_secret.initialized = false;
6467 all->stable_secret.initialized = false;
6468
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006469 net->ipv6.devconf_all = all;
6470 net->ipv6.devconf_dflt = dflt;
6471
6472#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006473 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006474 if (err < 0)
6475 goto err_reg_all;
6476
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006477 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006478 if (err < 0)
6479 goto err_reg_dflt;
6480#endif
6481 return 0;
6482
6483#ifdef CONFIG_SYSCTL
6484err_reg_dflt:
David Ahern23452172017-03-28 14:28:05 -07006485 __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006486err_reg_all:
6487 kfree(dflt);
6488#endif
6489err_alloc_dflt:
6490 kfree(all);
6491err_alloc_all:
6492 return err;
6493}
6494
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006495static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006496{
6497#ifdef CONFIG_SYSCTL
David Ahern23452172017-03-28 14:28:05 -07006498 __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
6499 NETCONFA_IFINDEX_DEFAULT);
6500 __addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
6501 NETCONFA_IFINDEX_ALL);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006502#endif
zhuyj73cf0e922014-11-26 10:25:58 +08006503 kfree(net->ipv6.devconf_dflt);
6504 kfree(net->ipv6.devconf_all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006505}
6506
6507static struct pernet_operations addrconf_ops = {
6508 .init = addrconf_init_net,
6509 .exit = addrconf_exit_net,
6510};
6511
Daniel Borkmann207895fd32015-01-29 12:15:03 +01006512static struct rtnl_af_ops inet6_ops __read_mostly = {
Thomas Grafb382b192010-11-16 04:33:57 +00006513 .family = AF_INET6,
6514 .fill_link_af = inet6_fill_link_af,
6515 .get_link_af_size = inet6_get_link_af_size,
Daniel Borkmann11b1f822015-02-05 14:39:11 +01006516 .validate_link_af = inet6_validate_link_af,
Daniel Borkmannf53adae2013-04-08 04:01:30 +00006517 .set_link_af = inet6_set_link_af,
Thomas Grafb382b192010-11-16 04:33:57 +00006518};
6519
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520/*
6521 * Init / cleanup code
6522 */
6523
6524int __init addrconf_init(void)
6525{
WANG Conga317a2f2014-07-25 15:25:09 -07006526 struct inet6_dev *idev;
stephen hemmingerc2e21292010-03-17 20:31:10 +00006527 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006528
Stephen Hemmingere21e8462010-03-20 16:09:01 -07006529 err = ipv6_addr_label_init();
6530 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00006531 pr_crit("%s: cannot initialize default policy table: %d\n",
6532 __func__, err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006533 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006536 err = register_pernet_subsys(&addrconf_ops);
6537 if (err < 0)
6538 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006539
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006540 addrconf_wq = create_workqueue("ipv6_addrconf");
6541 if (!addrconf_wq) {
6542 err = -ENOMEM;
6543 goto out_nowq;
6544 }
6545
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546 /* The addrconf netdev notifier requires that loopback_dev
6547 * has it's ipv6 private information allocated and setup
6548 * before it can bring up and give link-local addresses
6549 * to other devices which are up.
6550 *
6551 * Unfortunately, loopback_dev is not necessarily the first
6552 * entry in the global dev_base list of net devices. In fact,
6553 * it is likely to be the very last entry on that list.
6554 * So this causes the notifier registry below to try and
6555 * give link-local addresses to all devices besides loopback_dev
6556 * first, then loopback_dev, which cases all the non-loopback_dev
6557 * devices to fail to get a link-local address.
6558 *
6559 * So, as a temporary fix, allocate the ipv6 structure for
6560 * loopback_dev first by hand.
6561 * Longer term, all of the dependencies ipv6 has upon the loopback
6562 * device and it being up should be removed.
6563 */
6564 rtnl_lock();
WANG Conga317a2f2014-07-25 15:25:09 -07006565 idev = ipv6_add_dev(init_net.loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566 rtnl_unlock();
WANG Conga317a2f2014-07-25 15:25:09 -07006567 if (IS_ERR(idev)) {
6568 err = PTR_ERR(idev);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006569 goto errlo;
WANG Conga317a2f2014-07-25 15:25:09 -07006570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571
WANG Cong2f460932017-05-03 22:07:31 -07006572 ip6_route_init_special_entries();
6573
stephen hemmingerc2e21292010-03-17 20:31:10 +00006574 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6575 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6576
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577 register_netdevice_notifier(&ipv6_dev_notf);
6578
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006579 addrconf_verify();
Thomas Grafc127ea22007-03-22 11:58:32 -07006580
stephen hemminger3678a9d2013-12-30 10:41:32 -08006581 rtnl_af_register(&inet6_ops);
Thomas Grafb382b192010-11-16 04:33:57 +00006582
Greg Rosec7ac8672011-06-10 01:27:09 +00006583 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
Florian Westphalb97bac62017-08-09 20:41:48 +02006584 0);
Thomas Grafc127ea22007-03-22 11:58:32 -07006585 if (err < 0)
6586 goto errout;
6587
6588 /* Only the first call to __rtnl_register can fail */
Florian Westphalb97bac62017-08-09 20:41:48 +02006589 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, 0);
6590 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, 0);
Greg Rosec7ac8672011-06-10 01:27:09 +00006591 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
Florian Westphalc24675f2017-10-11 10:28:01 +02006592 inet6_dump_ifaddr, RTNL_FLAG_DOIT_UNLOCKED);
Greg Rosec7ac8672011-06-10 01:27:09 +00006593 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
Florian Westphalb97bac62017-08-09 20:41:48 +02006594 inet6_dump_ifmcaddr, 0);
Greg Rosec7ac8672011-06-10 01:27:09 +00006595 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
Florian Westphalb97bac62017-08-09 20:41:48 +02006596 inet6_dump_ifacaddr, 0);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +00006597 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
Florian Westphal4ea26072017-10-11 10:28:00 +02006598 inet6_netconf_dump_devconf, RTNL_FLAG_DOIT_UNLOCKED);
Thomas Grafc127ea22007-03-22 11:58:32 -07006599
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006600 ipv6_addr_label_rtnl_register();
6601
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07006603errout:
Thomas Grafb382b192010-11-16 04:33:57 +00006604 rtnl_af_unregister(&inet6_ops);
Thomas Grafc127ea22007-03-22 11:58:32 -07006605 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006606errlo:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006607 destroy_workqueue(addrconf_wq);
6608out_nowq:
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006609 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006610out_addrlabel:
6611 ipv6_addr_label_cleanup();
6612out:
Thomas Grafc127ea22007-03-22 11:58:32 -07006613 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614}
6615
Daniel Lezcano09f77092007-12-13 05:34:58 -08006616void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006618 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619 int i;
6620
6621 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006622 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006623 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624
Florian Westphal5c451212017-10-04 15:58:49 +02006625 rtnl_af_unregister(&inet6_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626
Florian Westphal5c451212017-10-04 15:58:49 +02006627 rtnl_lock();
Thomas Grafb382b192010-11-16 04:33:57 +00006628
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006629 /* clean dev list */
6630 for_each_netdev(&init_net, dev) {
6631 if (__in6_dev_get(dev) == NULL)
6632 continue;
6633 addrconf_ifdown(dev, 1);
6634 }
6635 addrconf_ifdown(init_net.loopback_dev, 2);
6636
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638 * Check hash table.
6639 */
stephen hemminger5c578aed2010-03-17 20:31:11 +00006640 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00006641 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6642 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578aed2010-03-17 20:31:11 +00006643 spin_unlock_bh(&addrconf_hash_lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006644 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645 rtnl_unlock();
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006646
6647 destroy_workqueue(addrconf_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648}