blob: 1d2dbace42ffadda98d6b324170101bd31d7c9a7 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static int ipv6_count_addresses(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);
David S. Miller3e81c6d2010-03-20 16:18:00 -0700195static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
196 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Adrian Bunk888c8482008-07-22 14:21:58 -0700198static struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 .forwarding = 0,
200 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
201 .mtu6 = IPV6_MIN_MTU,
202 .accept_ra = 1,
203 .accept_redirects = 1,
204 .autoconf = 1,
205 .force_mld_version = 0,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200206 .mldv1_unsolicited_report_interval = 10 * HZ,
207 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 .dad_transmits = 1,
209 .rtr_solicits = MAX_RTR_SOLICITATIONS,
210 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700211 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Ian Morris67ba4152014-08-24 21:53:10 +0100213 .use_tempaddr = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 .temp_valid_lft = TEMP_VALID_LIFETIME,
215 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
216 .regen_max_retry = REGEN_MAX_RETRY,
217 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800219 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700220 .accept_ra_from_local = 0,
Hangbin Liu8013d1d2015-07-30 14:28:42 +0800221 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800222 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800223#ifdef CONFIG_IPV6_ROUTER_PREF
224 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800225 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800226#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzbbea1242017-03-22 18:19:04 +0900227 .accept_ra_rt_info_min_plen = 0,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800228 .accept_ra_rt_info_max_plen = 0,
229#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800230#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700231 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700232 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900233 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900234 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200235 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700236 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100237 .stable_secret = {
238 .initialized = false,
Erik Kline3985e8a2015-07-22 16:38:25 +0900239 },
240 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400241 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800242 .keep_addr_on_down = 0,
David Lebrun1ababeb2016-11-08 14:57:39 +0100243 .seg6_enabled = 0,
David Lebrunbf355b82016-11-08 14:57:42 +0100244#ifdef CONFIG_IPV6_SEG6_HMAC
245 .seg6_require_hmac = 0,
246#endif
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800247 .enhanced_dad = 1,
Felix Jiad35a00b2017-01-26 16:59:17 +1300248 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
David Forsterdf789fe2017-02-23 16:27:18 +0000249 .disable_policy = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250};
251
Brian Haleyab32ea52006-09-22 14:15:41 -0700252static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 .forwarding = 0,
254 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
255 .mtu6 = IPV6_MIN_MTU,
256 .accept_ra = 1,
257 .accept_redirects = 1,
258 .autoconf = 1,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200259 .force_mld_version = 0,
260 .mldv1_unsolicited_report_interval = 10 * HZ,
261 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 .dad_transmits = 1,
263 .rtr_solicits = MAX_RTR_SOLICITATIONS,
264 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700265 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 .use_tempaddr = 0,
268 .temp_valid_lft = TEMP_VALID_LIFETIME,
269 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
270 .regen_max_retry = REGEN_MAX_RETRY,
271 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800273 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700274 .accept_ra_from_local = 0,
Hangbin Liu8013d1d2015-07-30 14:28:42 +0800275 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800276 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800277#ifdef CONFIG_IPV6_ROUTER_PREF
278 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800279 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800280#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzbbea1242017-03-22 18:19:04 +0900281 .accept_ra_rt_info_min_plen = 0,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800282 .accept_ra_rt_info_max_plen = 0,
283#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800284#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700285 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700286 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900287 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900288 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200289 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700290 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100291 .stable_secret = {
292 .initialized = false,
293 },
Erik Kline3985e8a2015-07-22 16:38:25 +0900294 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400295 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800296 .keep_addr_on_down = 0,
David Lebrun1ababeb2016-11-08 14:57:39 +0100297 .seg6_enabled = 0,
David Lebrunbf355b82016-11-08 14:57:42 +0100298#ifdef CONFIG_IPV6_SEG6_HMAC
299 .seg6_require_hmac = 0,
300#endif
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800301 .enhanced_dad = 1,
Felix Jiad35a00b2017-01-26 16:59:17 +1300302 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
David Forsterdf789fe2017-02-23 16:27:18 +0000303 .disable_policy = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304};
305
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700306/* Check if a valid qdisc is available */
David S. Miller05297942008-07-08 23:01:27 -0700307static inline bool addrconf_qdisc_ok(const struct net_device *dev)
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700308{
David S. Miller05297942008-07-08 23:01:27 -0700309 return !qdisc_tx_is_noop(dev);
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700310}
311
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200312static void addrconf_del_rs_timer(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200314 if (del_timer(&idev->rs_timer))
315 __in6_dev_put(idev);
316}
317
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100318static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200319{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100320 if (cancel_delayed_work(&ifp->dad_work))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 __in6_ifa_put(ifp);
322}
323
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200324static void addrconf_mod_rs_timer(struct inet6_dev *idev,
325 unsigned long when)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200327 if (!timer_pending(&idev->rs_timer))
328 in6_dev_hold(idev);
329 mod_timer(&idev->rs_timer, jiffies + when);
330}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100332static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
333 unsigned long delay)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200334{
Xin Longf8a894b2017-06-15 16:33:58 +0800335 in6_ifa_hold(ifp);
336 if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay))
337 in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700340static int snmp6_alloc_dev(struct inet6_dev *idev)
341{
John Stultz827da442013-10-07 15:51:58 -0700342 int i;
343
WANG Cong698365f2014-05-05 15:55:55 -0700344 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
345 if (!idev->stats.ipv6)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700346 goto err_ip;
John Stultz827da442013-10-07 15:51:58 -0700347
348 for_each_possible_cpu(i) {
349 struct ipstats_mib *addrconf_stats;
WANG Cong698365f2014-05-05 15:55:55 -0700350 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
John Stultz827da442013-10-07 15:51:58 -0700351 u64_stats_init(&addrconf_stats->syncp);
John Stultz827da442013-10-07 15:51:58 -0700352 }
353
354
Eric Dumazetbe281e52011-05-19 01:14:23 +0000355 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
356 GFP_KERNEL);
357 if (!idev->stats.icmpv6dev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700358 goto err_icmp;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000359 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
360 GFP_KERNEL);
361 if (!idev->stats.icmpv6msgdev)
David L Stevens14878f72007-09-16 16:52:35 -0700362 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700363
364 return 0;
365
David L Stevens14878f72007-09-16 16:52:35 -0700366err_icmpmsg:
Eric Dumazetbe281e52011-05-19 01:14:23 +0000367 kfree(idev->stats.icmpv6dev);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700368err_icmp:
WANG Cong698365f2014-05-05 15:55:55 -0700369 free_percpu(idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700370err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700371 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700372}
373
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000374static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 struct inet6_dev *ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700377 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 ASSERT_RTNL();
380
381 if (dev->mtu < IPV6_MIN_MTU)
WANG Conga317a2f2014-07-25 15:25:09 -0700382 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900384 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100385 if (!ndev)
WANG Conga317a2f2014-07-25 15:25:09 -0700386 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Ingo Oeser322f74a2006-03-20 23:01:47 -0800388 rwlock_init(&ndev->lock);
389 ndev->dev = dev;
stephen hemminger502a2ff2010-03-17 20:31:13 +0000390 INIT_LIST_HEAD(&ndev->addr_list);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200391 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
392 (unsigned long)ndev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900393 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100394
395 if (ndev->cnf.stable_secret.initialized)
Felix Jiad35a00b2017-01-26 16:59:17 +1300396 ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100397 else
Felix Jiad35a00b2017-01-26 16:59:17 +1300398 ndev->cnf.addr_gen_mode = ipv6_devconf_dflt.addr_gen_mode;
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100399
Ingo Oeser322f74a2006-03-20 23:01:47 -0800400 ndev->cnf.mtu6 = dev->mtu;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800401 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
Ian Morris63159f22015-03-29 14:00:04 +0100402 if (!ndev->nd_parms) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800403 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700404 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800405 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700406 if (ndev->cnf.forwarding)
407 dev_disable_lro(dev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800408 /* We refer to the device */
409 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Ingo Oeser322f74a2006-03-20 23:01:47 -0800411 if (snmp6_alloc_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800412 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000413 "%s: cannot allocate memory for statistics; dev=%s.\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800414 __func__, dev->name);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800415 neigh_parms_release(&nd_tbl, ndev->nd_parms);
Roy Li8603e332011-09-20 15:10:16 -0400416 dev_put(dev);
417 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700418 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Ingo Oeser322f74a2006-03-20 23:01:47 -0800421 if (snmp6_register_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800422 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000423 "%s: cannot create /proc/net/dev_snmp6/%s\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800424 __func__, dev->name);
WANG Conga317a2f2014-07-25 15:25:09 -0700425 goto err_release;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200428 /* One reference from device. */
Ingo Oeser322f74a2006-03-20 23:01:47 -0800429 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900431 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
432 ndev->cnf.accept_dad = -1;
433
Amerigo Wang07a93622012-10-29 16:23:10 +0000434#if IS_ENABLED(CONFIG_IPV6_SIT)
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700435 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
Joe Perchesf3213832012-05-15 14:11:53 +0000436 pr_info("%s: Disabled Multicast RS\n", dev->name);
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700437 ndev->cnf.rtr_solicits = 0;
438 }
439#endif
440
stephen hemminger372e6c82010-03-17 20:31:09 +0000441 INIT_LIST_HEAD(&ndev->tempaddr_list);
Jiri Bohac76506a92016-10-13 18:52:15 +0200442 ndev->desync_factor = U32_MAX;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800443 if ((dev->flags&IFF_LOOPBACK) ||
444 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700445 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700446 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700447 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800448 ndev->cnf.use_tempaddr = -1;
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200449 } else
450 ipv6_regen_rndid(ndev);
David S. Miller5d9efa72013-10-28 20:07:50 -0400451
Daniel Borkmann914faa12013-04-09 03:47:14 +0000452 ndev->token = in6addr_any;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800453
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700454 if (netif_running(dev) && addrconf_qdisc_ok(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700455 ndev->if_flags |= IF_READY;
456
Ingo Oeser322f74a2006-03-20 23:01:47 -0800457 ipv6_mc_init_dev(ndev);
458 ndev->tstamp = jiffies;
WANG Conga317a2f2014-07-25 15:25:09 -0700459 err = addrconf_sysctl_register(ndev);
460 if (err) {
461 ipv6_mc_destroy_dev(ndev);
Sabrina Dubroca2a189f92015-11-04 14:47:53 +0100462 snmp6_unregister_dev(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700463 goto err_release;
464 }
David L Stevens30c4cf52007-01-04 12:31:14 -0800465 /* protected by rtnl_lock */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000466 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800467
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000468 /* Join interface-local all-node multicast group */
469 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
470
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800471 /* Join all-node multicast group */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900472 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800473
Li Weid6ddef92012-03-05 14:45:17 +0000474 /* Join all-router multicast group if forwarding is set */
Li Wei8b2aaed2012-03-07 14:58:07 +0000475 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
Li Weid6ddef92012-03-05 14:45:17 +0000476 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700479
480err_release:
481 neigh_parms_release(&nd_tbl, ndev->nd_parms);
482 ndev->dead = 1;
483 in6_dev_finish_destroy(ndev);
484 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000487static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct inet6_dev *idev;
490
491 ASSERT_RTNL();
492
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700493 idev = __in6_dev_get(dev);
494 if (!idev) {
495 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -0700496 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return NULL;
498 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (dev->flags&IFF_UP)
501 ipv6_mc_up(idev);
502 return idev;
503}
504
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000505static int inet6_netconf_msgsize_devconf(int type)
506{
507 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
508 + nla_total_size(4); /* NETCONFA_IFINDEX */
Zhang Shengju136ba622016-03-10 08:55:50 +0000509 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000510
Zhang Shengju136ba622016-03-10 08:55:50 +0000511 if (type == NETCONFA_ALL)
512 all = true;
513
514 if (all || type == NETCONFA_FORWARDING)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000515 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500516#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000517 if (all || type == NETCONFA_MC_FORWARDING)
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000518 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500519#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000520 if (all || type == NETCONFA_PROXY_NEIGH)
stephen hemmingerc92d5492013-12-17 22:37:14 -0800521 size += nla_total_size(4);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000522
Zhang Shengju136ba622016-03-10 08:55:50 +0000523 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
Andy Gospodarek35103d12015-08-13 10:39:01 -0400524 size += nla_total_size(4);
525
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000526 return size;
527}
528
529static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
530 struct ipv6_devconf *devconf, u32 portid,
531 u32 seq, int event, unsigned int flags,
532 int type)
533{
534 struct nlmsghdr *nlh;
535 struct netconfmsg *ncm;
Zhang Shengju136ba622016-03-10 08:55:50 +0000536 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000537
538 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
539 flags);
Ian Morris63159f22015-03-29 14:00:04 +0100540 if (!nlh)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000541 return -EMSGSIZE;
542
Zhang Shengju136ba622016-03-10 08:55:50 +0000543 if (type == NETCONFA_ALL)
544 all = true;
545
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000546 ncm = nlmsg_data(nlh);
547 ncm->ncm_family = AF_INET6;
548
549 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
550 goto nla_put_failure;
551
David Ahern23452172017-03-28 14:28:05 -0700552 if (!devconf)
553 goto out;
554
Zhang Shengju136ba622016-03-10 08:55:50 +0000555 if ((all || type == NETCONFA_FORWARDING) &&
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000556 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
557 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500558#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000559 if ((all || type == NETCONFA_MC_FORWARDING) &&
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000560 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
561 devconf->mc_forwarding) < 0)
562 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500563#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000564 if ((all || type == NETCONFA_PROXY_NEIGH) &&
stephen hemmingerc92d5492013-12-17 22:37:14 -0800565 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
566 goto nla_put_failure;
567
Zhang Shengju136ba622016-03-10 08:55:50 +0000568 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
Andy Gospodarek35103d12015-08-13 10:39:01 -0400569 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
570 devconf->ignore_routes_with_linkdown) < 0)
571 goto nla_put_failure;
572
David Ahern23452172017-03-28 14:28:05 -0700573out:
Johannes Berg053c0952015-01-16 22:09:00 +0100574 nlmsg_end(skb, nlh);
575 return 0;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000576
577nla_put_failure:
578 nlmsg_cancel(skb, nlh);
579 return -EMSGSIZE;
580}
581
David Ahern85b3daa2017-03-28 14:28:04 -0700582void inet6_netconf_notify_devconf(struct net *net, int event, int type,
583 int ifindex, struct ipv6_devconf *devconf)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000584{
585 struct sk_buff *skb;
586 int err = -ENOBUFS;
587
Eric Dumazet927265b2016-07-08 05:46:04 +0200588 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100589 if (!skb)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000590 goto errout;
591
592 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
David Ahern85b3daa2017-03-28 14:28:04 -0700593 event, 0, type);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000594 if (err < 0) {
595 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
596 WARN_ON(err == -EMSGSIZE);
597 kfree_skb(skb);
598 goto errout;
599 }
Eric Dumazet927265b2016-07-08 05:46:04 +0200600 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000601 return;
602errout:
Cong Dingbd779022012-12-18 12:08:56 +0000603 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000604}
605
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000606static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
607 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
608 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
stephen hemmingerc92d5492013-12-17 22:37:14 -0800609 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
Andy Gospodarek35103d12015-08-13 10:39:01 -0400610 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000611};
612
613static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
David Ahernc21ef3e2017-04-16 09:48:24 -0700614 struct nlmsghdr *nlh,
615 struct netlink_ext_ack *extack)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000616{
617 struct net *net = sock_net(in_skb->sk);
618 struct nlattr *tb[NETCONFA_MAX+1];
619 struct netconfmsg *ncm;
620 struct sk_buff *skb;
621 struct ipv6_devconf *devconf;
622 struct inet6_dev *in6_dev;
623 struct net_device *dev;
624 int ifindex;
625 int err;
626
627 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
David Ahernc21ef3e2017-04-16 09:48:24 -0700628 devconf_ipv6_policy, extack);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000629 if (err < 0)
630 goto errout;
631
Anton Protopopova97eb332016-02-16 21:43:16 -0500632 err = -EINVAL;
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000633 if (!tb[NETCONFA_IFINDEX])
634 goto errout;
635
636 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
637 switch (ifindex) {
638 case NETCONFA_IFINDEX_ALL:
639 devconf = net->ipv6.devconf_all;
640 break;
641 case NETCONFA_IFINDEX_DEFAULT:
642 devconf = net->ipv6.devconf_dflt;
643 break;
644 default:
645 dev = __dev_get_by_index(net, ifindex);
Ian Morris63159f22015-03-29 14:00:04 +0100646 if (!dev)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000647 goto errout;
648 in6_dev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +0100649 if (!in6_dev)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000650 goto errout;
651 devconf = &in6_dev->cnf;
652 break;
653 }
654
655 err = -ENOBUFS;
Zhang Shengju136ba622016-03-10 08:55:50 +0000656 skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +0100657 if (!skb)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000658 goto errout;
659
660 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
661 NETLINK_CB(in_skb).portid,
662 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
Zhang Shengju136ba622016-03-10 08:55:50 +0000663 NETCONFA_ALL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000664 if (err < 0) {
665 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
666 WARN_ON(err == -EMSGSIZE);
667 kfree_skb(skb);
668 goto errout;
669 }
670 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
671errout:
672 return err;
673}
674
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000675static int inet6_netconf_dump_devconf(struct sk_buff *skb,
676 struct netlink_callback *cb)
677{
678 struct net *net = sock_net(skb->sk);
679 int h, s_h;
680 int idx, s_idx;
681 struct net_device *dev;
682 struct inet6_dev *idev;
683 struct hlist_head *head;
684
685 s_h = cb->args[0];
686 s_idx = idx = cb->args[1];
687
688 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
689 idx = 0;
690 head = &net->dev_index_head[h];
691 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000692 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
693 net->dev_base_seq;
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000694 hlist_for_each_entry_rcu(dev, head, index_hlist) {
695 if (idx < s_idx)
696 goto cont;
697 idev = __in6_dev_get(dev);
698 if (!idev)
699 goto cont;
700
701 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
702 &idev->cnf,
703 NETLINK_CB(cb->skb).portid,
704 cb->nlh->nlmsg_seq,
705 RTM_NEWNETCONF,
706 NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000707 NETCONFA_ALL) < 0) {
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000708 rcu_read_unlock();
709 goto done;
710 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000711 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000712cont:
713 idx++;
714 }
715 rcu_read_unlock();
716 }
717 if (h == NETDEV_HASHENTRIES) {
718 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
719 net->ipv6.devconf_all,
720 NETLINK_CB(cb->skb).portid,
721 cb->nlh->nlmsg_seq,
722 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000723 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000724 goto done;
725 else
726 h++;
727 }
728 if (h == NETDEV_HASHENTRIES + 1) {
729 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
730 net->ipv6.devconf_dflt,
731 NETLINK_CB(cb->skb).portid,
732 cb->nlh->nlmsg_seq,
733 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000734 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000735 goto done;
736 else
737 h++;
738 }
739done:
740 cb->args[0] = h;
741 cb->args[1] = idx;
742
743 return skb->len;
744}
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746#ifdef CONFIG_SYSCTL
747static void dev_forward_change(struct inet6_dev *idev)
748{
749 struct net_device *dev;
750 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 if (!idev)
753 return;
754 dev = idev->dev;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700755 if (idev->cnf.forwarding)
756 dev_disable_lro(dev);
Amerigo Wang1a940832012-10-28 17:43:53 +0000757 if (dev->flags & IFF_MULTICAST) {
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000758 if (idev->cnf.forwarding) {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900759 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000760 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
761 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
762 } else {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900763 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000764 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
765 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
stephen hemminger502a2ff2010-03-17 20:31:13 +0000768
769 list_for_each_entry(ifa, &idev->addr_list, if_list) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800770 if (ifa->flags&IFA_F_TENTATIVE)
771 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (idev->cnf.forwarding)
773 addrconf_join_anycast(ifa);
774 else
775 addrconf_leave_anycast(ifa);
776 }
David Ahern85b3daa2017-03-28 14:28:04 -0700777 inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
778 NETCONFA_FORWARDING,
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000779 dev->ifindex, &idev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782
Pavel Emelyanove1869322008-01-10 17:43:50 -0800783static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 struct net_device *dev;
786 struct inet6_dev *idev;
787
Ben Hutchings4acd4942012-08-14 08:54:51 +0000788 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 idev = __in6_dev_get(dev);
790 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800791 int changed = (!idev->cnf.forwarding) ^ (!newf);
792 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (changed)
794 dev_forward_change(idev);
795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800798
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000799static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800800{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800801 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000802 int old;
803
804 if (!rtnl_trylock())
805 return restart_syscall();
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800806
807 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000808 old = *p;
809 *p = newf;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800810
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000811 if (p == &net->ipv6.devconf_dflt->forwarding) {
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000812 if ((!newf) ^ (!old))
David Ahern85b3daa2017-03-28 14:28:04 -0700813 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
814 NETCONFA_FORWARDING,
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000815 NETCONFA_IFINDEX_DEFAULT,
816 net->ipv6.devconf_dflt);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000817 rtnl_unlock();
818 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +0000819 }
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000820
Pavel Emelyanove1869322008-01-10 17:43:50 -0800821 if (p == &net->ipv6.devconf_all->forwarding) {
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200822 int old_dflt = net->ipv6.devconf_dflt->forwarding;
823
Pavel Emelyanove1869322008-01-10 17:43:50 -0800824 net->ipv6.devconf_dflt->forwarding = newf;
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200825 if ((!newf) ^ (!old_dflt))
David Ahern85b3daa2017-03-28 14:28:04 -0700826 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
827 NETCONFA_FORWARDING,
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200828 NETCONFA_IFINDEX_DEFAULT,
829 net->ipv6.devconf_dflt);
830
Pavel Emelyanove1869322008-01-10 17:43:50 -0800831 addrconf_forward_change(net, newf);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000832 if ((!newf) ^ (!old))
David Ahern85b3daa2017-03-28 14:28:04 -0700833 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
834 NETCONFA_FORWARDING,
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000835 NETCONFA_IFINDEX_ALL,
836 net->ipv6.devconf_all);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000837 } else if ((!newf) ^ (!old))
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800838 dev_forward_change((struct inet6_dev *)table->extra1);
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700839 rtnl_unlock();
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800840
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000841 if (newf)
Daniel Lezcano7b4da532008-03-04 13:47:14 -0800842 rt6_purge_dflt_routers(net);
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000843 return 1;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800844}
Andy Gospodarek35103d12015-08-13 10:39:01 -0400845
846static void addrconf_linkdown_change(struct net *net, __s32 newf)
847{
848 struct net_device *dev;
849 struct inet6_dev *idev;
850
851 for_each_netdev(net, dev) {
852 idev = __in6_dev_get(dev);
853 if (idev) {
854 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
855
856 idev->cnf.ignore_routes_with_linkdown = newf;
857 if (changed)
858 inet6_netconf_notify_devconf(dev_net(dev),
David Ahern85b3daa2017-03-28 14:28:04 -0700859 RTM_NEWNETCONF,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400860 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
861 dev->ifindex,
862 &idev->cnf);
863 }
864 }
865}
866
867static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
868{
869 struct net *net;
870 int old;
871
872 if (!rtnl_trylock())
873 return restart_syscall();
874
875 net = (struct net *)table->extra2;
876 old = *p;
877 *p = newf;
878
879 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
880 if ((!newf) ^ (!old))
881 inet6_netconf_notify_devconf(net,
David Ahern85b3daa2017-03-28 14:28:04 -0700882 RTM_NEWNETCONF,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400883 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
884 NETCONFA_IFINDEX_DEFAULT,
885 net->ipv6.devconf_dflt);
886 rtnl_unlock();
887 return 0;
888 }
889
890 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
891 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
892 addrconf_linkdown_change(net, newf);
893 if ((!newf) ^ (!old))
894 inet6_netconf_notify_devconf(net,
David Ahern85b3daa2017-03-28 14:28:04 -0700895 RTM_NEWNETCONF,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400896 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
897 NETCONFA_IFINDEX_ALL,
898 net->ipv6.devconf_all);
899 }
900 rtnl_unlock();
901
902 return 1;
903}
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905#endif
906
stephen hemminger5c578aed2010-03-17 20:31:11 +0000907/* Nobody refers to this ifaddr, destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
909{
stephen hemmingerc2e21292010-03-17 20:31:10 +0000910 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912#ifdef NET_REFCNT_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +0000913 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914#endif
915
916 in6_dev_put(ifp->idev);
917
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100918 if (cancel_delayed_work(&ifp->dad_work))
919 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
920 ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Herbert Xue9d3e082010-05-18 15:36:06 -0700922 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
Joe Perchesf3213832012-05-15 14:11:53 +0000923 pr_warn("Freeing alive inet6 address %p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return;
925 }
Amerigo Wang94e187c2012-10-29 00:13:19 +0000926 ip6_rt_put(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Lai Jiangshane5785982011-03-15 18:00:14 +0800928 kfree_rcu(ifp, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Brian Haleye55ffac2006-07-10 15:25:51 -0700931static void
932ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
933{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000934 struct list_head *p;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700935 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700936
937 /*
938 * Each device address list is sorted in order of scope -
939 * global before linklocal.
940 */
stephen hemminger502a2ff2010-03-17 20:31:13 +0000941 list_for_each(p, &idev->addr_list) {
942 struct inet6_ifaddr *ifa
943 = list_entry(p, struct inet6_ifaddr, if_list);
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700944 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700945 break;
946 }
947
David S. Millerb54c9b92010-03-25 21:25:30 -0700948 list_add_tail(&ifp->if_list, p);
Brian Haleye55ffac2006-07-10 15:25:51 -0700949}
950
Eric Dumazetddbe5032012-07-18 08:11:12 +0000951static u32 inet6_addr_hash(const struct in6_addr *addr)
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900952{
Eric Dumazetddbe5032012-07-18 08:11:12 +0000953 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900954}
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956/* On success it returns ifp with increased reference count */
957
958static struct inet6_ifaddr *
Jiri Pirko3f8f5292013-08-01 10:41:27 +0200959ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
960 const struct in6_addr *peer_addr, int pfxlen,
Jiri Benc8a226b22013-08-01 10:41:28 +0200961 int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
David Forsterdf789fe2017-02-23 16:27:18 +0000963 struct net *net = dev_net(idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 struct inet6_ifaddr *ifa = NULL;
965 struct rt6_info *rt;
stephen hemminger3a88a812010-03-17 20:31:12 +0000966 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 int err = 0;
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +0900968 int addr_type = ipv6_addr_type(addr);
969
970 if (addr_type == IPV6_ADDR_ANY ||
971 addr_type & IPV6_ADDR_MULTICAST ||
972 (!(idev->dev->flags & IFF_LOOPBACK) &&
973 addr_type & IPV6_ADDR_LOOPBACK))
974 return ERR_PTR(-EADDRNOTAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700976 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (idev->dead) {
978 err = -ENODEV; /*XXX*/
979 goto out2;
980 }
981
Brian Haley56d417b2009-06-01 03:07:33 -0700982 if (idev->cnf.disable_ipv6) {
Brian Haley9bdd8d42009-03-18 18:22:48 -0700983 err = -EACCES;
984 goto out2;
985 }
986
stephen hemminger5c578aed2010-03-17 20:31:11 +0000987 spin_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 /* Ignore adding duplicate addresses on an interface */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900990 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
dingtianhongba3542e2013-08-17 10:27:04 +0800991 ADBG("ipv6_add_addr: already assigned\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 err = -EEXIST;
993 goto out;
994 }
995
Ingo Oeser322f74a2006-03-20 23:01:47 -0800996 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Ian Morris63159f22015-03-29 14:00:04 +0100998 if (!ifa) {
dingtianhongba3542e2013-08-17 10:27:04 +0800999 ADBG("ipv6_add_addr: malloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 err = -ENOBUFS;
1001 goto out;
1002 }
1003
David S. Miller8f031512011-12-06 16:48:14 -05001004 rt = addrconf_dst_alloc(idev, addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (IS_ERR(rt)) {
1006 err = PTR_ERR(rt);
1007 goto out;
1008 }
1009
David Forsterdf789fe2017-02-23 16:27:18 +00001010 if (net->ipv6.devconf_all->disable_policy ||
1011 idev->cnf.disable_policy)
1012 rt->dst.flags |= DST_NOPOLICY;
1013
Jiri Pirkobba24892013-12-07 19:26:57 +01001014 neigh_parms_data_state_setall(idev->nd_parms);
1015
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001016 ifa->addr = *addr;
Jiri Pirko3f8f5292013-08-01 10:41:27 +02001017 if (peer_addr)
1018 ifa->peer_addr = *peer_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 spin_lock_init(&ifa->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001021 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001022 INIT_HLIST_NODE(&ifa->addr_lst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 ifa->scope = scope;
1024 ifa->prefix_len = pfxlen;
Mahesh Bandewar66eb9f82017-05-12 17:03:39 -07001025 ifa->flags = flags;
1026 /* No need to add the TENTATIVE flag for addresses with NODAD */
1027 if (!(flags & IFA_F_NODAD))
1028 ifa->flags |= IFA_F_TENTATIVE;
Jiri Benc8a226b22013-08-01 10:41:28 +02001029 ifa->valid_lft = valid_lft;
1030 ifa->prefered_lft = prefered_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 ifa->cstamp = ifa->tstamp = jiffies;
Daniel Borkmann617fe292013-04-09 03:47:16 +00001032 ifa->tokenized = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Keir Fraser57f5f542006-08-29 02:43:49 -07001034 ifa->rt = rt;
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 ifa->idev = idev;
1037 in6_dev_hold(idev);
1038 /* For caller */
1039 in6_ifa_hold(ifa);
1040
1041 /* Add to big hash table */
Eric Dumazetddbe5032012-07-18 08:11:12 +00001042 hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
stephen hemminger5c578aed2010-03-17 20:31:11 +00001044 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
stephen hemminger5c578aed2010-03-17 20:31:11 +00001045 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 write_lock(&idev->lock);
1048 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -07001049 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (ifa->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001052 list_add(&ifa->tmp_list, &idev->tempaddr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 in6_ifa_hold(ifa);
1054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 in6_ifa_hold(ifa);
1057 write_unlock(&idev->lock);
1058out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001059 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -07001061 if (likely(err == 0))
Cong Wangf88c91d2013-04-14 23:18:43 +08001062 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 else {
1064 kfree(ifa);
1065 ifa = ERR_PTR(err);
1066 }
1067
1068 return ifa;
1069out:
stephen hemminger5c578aed2010-03-17 20:31:11 +00001070 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 goto out2;
1072}
1073
Thomas Haller5b84efe2014-01-15 15:36:59 +01001074enum cleanup_prefix_rt_t {
1075 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1076 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1077 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1078};
1079
1080/*
1081 * Check, whether the prefix for ifp would still need a prefix route
1082 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1083 * constants.
1084 *
1085 * 1) we don't purge prefix if address was not permanent.
1086 * prefix is managed by its own lifetime.
1087 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1088 * 3) if there are no addresses, delete prefix.
1089 * 4) if there are still other permanent address(es),
1090 * corresponding prefix is still permanent.
1091 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1092 * don't purge the prefix, assume user space is managing it.
1093 * 6) otherwise, update prefix lifetime to the
1094 * longest valid lifetime among the corresponding
1095 * addresses on the device.
1096 * Note: subsequent RA will update lifetime.
1097 **/
1098static enum cleanup_prefix_rt_t
1099check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1100{
1101 struct inet6_ifaddr *ifa;
1102 struct inet6_dev *idev = ifp->idev;
1103 unsigned long lifetime;
1104 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1105
1106 *expires = jiffies;
1107
1108 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1109 if (ifa == ifp)
1110 continue;
1111 if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
1112 ifp->prefix_len))
1113 continue;
1114 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1115 return CLEANUP_PREFIX_RT_NOP;
1116
1117 action = CLEANUP_PREFIX_RT_EXPIRE;
1118
1119 spin_lock(&ifa->lock);
1120
1121 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1122 /*
1123 * Note: Because this address is
1124 * not permanent, lifetime <
1125 * LONG_MAX / HZ here.
1126 */
1127 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1128 *expires = ifa->tstamp + lifetime * HZ;
1129 spin_unlock(&ifa->lock);
1130 }
1131
1132 return action;
1133}
1134
1135static void
1136cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
1137{
1138 struct rt6_info *rt;
1139
1140 rt = addrconf_get_prefix_route(&ifp->addr,
1141 ifp->prefix_len,
1142 ifp->idev->dev,
1143 0, RTF_GATEWAY | RTF_DEFAULT);
1144 if (rt) {
1145 if (del_rt)
1146 ip6_del_rt(rt);
1147 else {
1148 if (!(rt->rt6i_flags & RTF_EXPIRES))
1149 rt6_set_expires(rt, expires);
1150 ip6_rt_put(rt);
1151 }
1152 }
1153}
1154
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156/* This function wants to get referenced ifp and releases it before return */
1157
1158static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1159{
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001160 int state;
Thomas Haller5b84efe2014-01-15 15:36:59 +01001161 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1162 unsigned long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001164 ASSERT_RTNL();
1165
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001166 spin_lock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001167 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -07001168 ifp->state = INET6_IFADDR_STATE_DEAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001169 spin_unlock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001170
1171 if (state == INET6_IFADDR_STATE_DEAD)
1172 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
stephen hemminger5c578aed2010-03-17 20:31:11 +00001174 spin_lock_bh(&addrconf_hash_lock);
1175 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578aed2010-03-17 20:31:11 +00001176 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Thomas Haller5b84efe2014-01-15 15:36:59 +01001178 write_lock_bh(&ifp->idev->lock);
David S. Miller5d9efa72013-10-28 20:07:50 -04001179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001181 list_del(&ifp->tmp_list);
1182 if (ifp->ifpub) {
1183 in6_ifa_put(ifp->ifpub);
1184 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
stephen hemminger372e6c82010-03-17 20:31:09 +00001186 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Thomas Haller5b84efe2014-01-15 15:36:59 +01001189 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1190 action = check_cleanup_prefix_route(ifp, &expires);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001191
Thomas Haller5b84efe2014-01-15 15:36:59 +01001192 list_del_init(&ifp->if_list);
1193 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Thomas Haller5b84efe2014-01-15 15:36:59 +01001195 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001197 addrconf_del_dad_work(ifp);
Andrey Vaginb2238562008-07-08 15:13:31 -07001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 ipv6_ifa_notify(RTM_DELADDR, ifp);
1200
Cong Wangf88c91d2013-04-14 23:18:43 +08001201 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Thomas Haller5b84efe2014-01-15 15:36:59 +01001203 if (action != CLEANUP_PREFIX_RT_NOP) {
1204 cleanup_prefix_route(ifp, expires,
1205 action == CLEANUP_PREFIX_RT_DEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207
Daniel Walterc3968a82011-04-13 21:10:57 +00001208 /* clean up prefsrc entries */
1209 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001210out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 in6_ifa_put(ifp);
1212}
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1215{
1216 struct inet6_dev *idev = ifp->idev;
1217 struct in6_addr addr, *tmpaddr;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001218 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001219 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 int tmp_plen;
1221 int ret = 0;
Neil Horman95c385b2007-04-25 17:08:10 -07001222 u32 addr_flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001223 unsigned long now = jiffies;
Jiri Bohac76506a92016-10-13 18:52:15 +02001224 long max_desync_factor;
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001225 s32 cnf_temp_preferred_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Jiri Pirko53bd6742013-12-06 09:45:22 +01001227 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (ift) {
1229 spin_lock_bh(&ift->lock);
1230 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1231 spin_unlock_bh(&ift->lock);
1232 tmpaddr = &addr;
1233 } else {
1234 tmpaddr = NULL;
1235 }
1236retry:
1237 in6_dev_hold(idev);
1238 if (idev->cnf.use_tempaddr <= 0) {
Jiri Pirko53bd6742013-12-06 09:45:22 +01001239 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001240 pr_info("%s: use_tempaddr is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 in6_dev_put(idev);
1242 ret = -1;
1243 goto out;
1244 }
1245 spin_lock_bh(&ifp->lock);
1246 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1247 idev->cnf.use_tempaddr = -1; /*XXX*/
1248 spin_unlock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01001249 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001250 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1251 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 in6_dev_put(idev);
1253 ret = -1;
1254 goto out;
1255 }
1256 in6_ifa_hold(ifp);
1257 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
Jiri Bohac9d6280d2016-10-13 18:50:02 +02001258 ipv6_try_regen_rndid(idev, tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001260 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001262 regen_advance = idev->cnf.regen_max_retry *
Ian Morris67ba4152014-08-24 21:53:10 +01001263 idev->cnf.dad_transmits *
1264 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
Jiri Bohac76506a92016-10-13 18:52:15 +02001265
1266 /* recalculate max_desync_factor each time and update
1267 * idev->desync_factor if it's larger
1268 */
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001269 cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
Jiri Bohac76506a92016-10-13 18:52:15 +02001270 max_desync_factor = min_t(__u32,
1271 idev->cnf.max_desync_factor,
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001272 cnf_temp_preferred_lft - regen_advance);
Jiri Bohac76506a92016-10-13 18:52:15 +02001273
1274 if (unlikely(idev->desync_factor > max_desync_factor)) {
1275 if (max_desync_factor > 0) {
1276 get_random_bytes(&idev->desync_factor,
1277 sizeof(idev->desync_factor));
1278 idev->desync_factor %= max_desync_factor;
1279 } else {
1280 idev->desync_factor = 0;
1281 }
1282 }
1283
1284 tmp_valid_lft = min_t(__u32,
1285 ifp->valid_lft,
1286 idev->cnf.temp_valid_lft + age);
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001287 tmp_prefered_lft = cnf_temp_preferred_lft + age -
Jiri Bohac76506a92016-10-13 18:52:15 +02001288 idev->desync_factor;
Jiri Bohac76506a92016-10-13 18:52:15 +02001289 tmp_prefered_lft = min_t(__u32, ifp->prefered_lft, tmp_prefered_lft);
1290 tmp_plen = ifp->prefix_len;
1291 tmp_tstamp = ifp->tstamp;
1292 spin_unlock_bh(&ifp->lock);
1293
Jiri Pirko53bd6742013-12-06 09:45:22 +01001294 write_unlock_bh(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -07001295
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001296 /* A temporary address is created only if this calculated Preferred
1297 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1298 * an implementation must not create a temporary address with a zero
1299 * Preferred Lifetime.
Heiner Kallweitecab6702014-03-12 22:13:19 +01001300 * Use age calculation as in addrconf_verify to avoid unnecessary
1301 * temporary addresses being generated.
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001302 */
Heiner Kallweitecab6702014-03-12 22:13:19 +01001303 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1304 if (tmp_prefered_lft <= regen_advance + age) {
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001305 in6_ifa_put(ifp);
1306 in6_dev_put(idev);
1307 ret = -1;
1308 goto out;
1309 }
1310
Neil Horman95c385b2007-04-25 17:08:10 -07001311 addr_flags = IFA_F_TEMPORARY;
1312 /* set in addrconf_prefix_rcv() */
1313 if (ifp->flags & IFA_F_OPTIMISTIC)
1314 addr_flags |= IFA_F_OPTIMISTIC;
1315
Hannes Frederic Sowa4b08a8f2013-08-16 13:02:27 +02001316 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1317 ipv6_addr_scope(&addr), addr_flags,
1318 tmp_valid_lft, tmp_prefered_lft);
1319 if (IS_ERR(ift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 in6_ifa_put(ifp);
1321 in6_dev_put(idev);
Joe Perchesf3213832012-05-15 14:11:53 +00001322 pr_info("%s: retry temporary address regeneration\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 tmpaddr = &addr;
Jiri Pirko53bd6742013-12-06 09:45:22 +01001324 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 goto retry;
1326 }
1327
1328 spin_lock_bh(&ift->lock);
1329 ift->ifpub = ifp;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001330 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 ift->tstamp = tmp_tstamp;
1332 spin_unlock_bh(&ift->lock);
1333
David S. Millercf22f9a2012-04-14 21:37:40 -04001334 addrconf_dad_start(ift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 in6_ifa_put(ift);
1336 in6_dev_put(idev);
1337out:
1338 return ret;
1339}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001342 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001344enum {
1345 IPV6_SADDR_RULE_INIT = 0,
1346 IPV6_SADDR_RULE_LOCAL,
1347 IPV6_SADDR_RULE_SCOPE,
1348 IPV6_SADDR_RULE_PREFERRED,
1349#ifdef CONFIG_IPV6_MIP6
1350 IPV6_SADDR_RULE_HOA,
1351#endif
1352 IPV6_SADDR_RULE_OIF,
1353 IPV6_SADDR_RULE_LABEL,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001354 IPV6_SADDR_RULE_PRIVACY,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001355 IPV6_SADDR_RULE_ORCHID,
1356 IPV6_SADDR_RULE_PREFIX,
Erik Kline7fd25612014-10-28 18:11:14 +09001357#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1358 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1359#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001360 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001361};
1362
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001363struct ipv6_saddr_score {
1364 int rule;
1365 int addr_type;
1366 struct inet6_ifaddr *ifa;
1367 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1368 int scopedist;
1369 int matchlen;
1370};
1371
1372struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001373 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001374 int ifindex;
1375 int scope;
1376 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001377 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001378};
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001379
Dave Jonesb6f99a22007-03-22 12:27:49 -07001380static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Ulrich Weber45bb0062010-02-25 23:28:58 +00001382 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001383 return 1;
1384 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
1386
Erik Kline7fd25612014-10-28 18:11:14 +09001387static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
1388{
1389#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1390 return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
1391#else
1392 return false;
1393#endif
1394}
1395
Benjamin Thery3de23252008-05-28 14:51:24 +02001396static int ipv6_get_saddr_eval(struct net *net,
1397 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001398 struct ipv6_saddr_dst *dst,
1399 int i)
1400{
1401 int ret;
1402
1403 if (i <= score->rule) {
1404 switch (i) {
1405 case IPV6_SADDR_RULE_SCOPE:
1406 ret = score->scopedist;
1407 break;
1408 case IPV6_SADDR_RULE_PREFIX:
1409 ret = score->matchlen;
1410 break;
1411 default:
1412 ret = !!test_bit(i, score->scorebits);
1413 }
1414 goto out;
1415 }
1416
1417 switch (i) {
1418 case IPV6_SADDR_RULE_INIT:
1419 /* Rule 0: remember if hiscore is not ready yet */
1420 ret = !!score->ifa;
1421 break;
1422 case IPV6_SADDR_RULE_LOCAL:
1423 /* Rule 1: Prefer same address */
1424 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1425 break;
1426 case IPV6_SADDR_RULE_SCOPE:
1427 /* Rule 2: Prefer appropriate scope
1428 *
1429 * ret
1430 * ^
1431 * -1 | d 15
1432 * ---+--+-+---> scope
1433 * |
1434 * | d is scope of the destination.
1435 * B-d | \
1436 * | \ <- smaller scope is better if
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08001437 * B-15 | \ if scope is enough for destination.
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001438 * | ret = B - scope (-1 <= scope >= d <= 15).
1439 * d-C-1 | /
1440 * |/ <- greater is better
1441 * -C / if scope is not enough for destination.
1442 * /| ret = scope - C (-1 <= d < scope <= 15).
1443 *
1444 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1445 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1446 * Assume B = 0 and we get C > 29.
1447 */
1448 ret = __ipv6_addr_src_scope(score->addr_type);
1449 if (ret >= dst->scope)
1450 ret = -ret;
1451 else
1452 ret -= 128; /* 30 is enough */
1453 score->scopedist = ret;
1454 break;
1455 case IPV6_SADDR_RULE_PREFERRED:
Erik Kline7fd25612014-10-28 18:11:14 +09001456 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001457 /* Rule 3: Avoid deprecated and optimistic addresses */
Erik Kline7fd25612014-10-28 18:11:14 +09001458 u8 avoid = IFA_F_DEPRECATED;
1459
1460 if (!ipv6_use_optimistic_addr(score->ifa->idev))
1461 avoid |= IFA_F_OPTIMISTIC;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001462 ret = ipv6_saddr_preferred(score->addr_type) ||
Erik Kline7fd25612014-10-28 18:11:14 +09001463 !(score->ifa->flags & avoid);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001464 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001465 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001466#ifdef CONFIG_IPV6_MIP6
1467 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001468 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001469 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001470 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1471 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001472 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001473 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001474#endif
1475 case IPV6_SADDR_RULE_OIF:
1476 /* Rule 5: Prefer outgoing interface */
1477 ret = (!dst->ifindex ||
1478 dst->ifindex == score->ifa->idev->dev->ifindex);
1479 break;
1480 case IPV6_SADDR_RULE_LABEL:
1481 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001482 ret = ipv6_addr_label(net,
1483 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001484 score->ifa->idev->dev->ifindex) == dst->label;
1485 break;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001486 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001487 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001488 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001489 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001490 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001491 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1492 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1493 score->ifa->idev->cnf.use_tempaddr >= 2;
1494 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001495 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001496 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001497 case IPV6_SADDR_RULE_ORCHID:
1498 /* Rule 8-: Prefer ORCHID vs ORCHID or
1499 * non-ORCHID vs non-ORCHID
1500 */
1501 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1502 ipv6_addr_orchid(dst->addr));
1503 break;
1504 case IPV6_SADDR_RULE_PREFIX:
1505 /* Rule 8: Use longest matching prefix */
YOSHIFUJI Hideaki / 吉藤英明91b4b042012-09-10 18:41:07 +00001506 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1507 if (ret > score->ifa->prefix_len)
1508 ret = score->ifa->prefix_len;
1509 score->matchlen = ret;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001510 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001511#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1512 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1513 /* Optimistic addresses still have lower precedence than other
1514 * preferred addresses.
1515 */
1516 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1517 break;
1518#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001519 default:
1520 ret = 0;
1521 }
1522
1523 if (ret)
1524 __set_bit(i, score->scorebits);
1525 score->rule = i;
1526out:
1527 return ret;
1528}
1529
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001530static int __ipv6_dev_get_saddr(struct net *net,
1531 struct ipv6_saddr_dst *dst,
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001532 struct inet6_dev *idev,
1533 struct ipv6_saddr_score *scores,
1534 int hiscore_idx)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001535{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001536 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001537
1538 read_lock_bh(&idev->lock);
1539 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1540 int i;
1541
1542 /*
1543 * - Tentative Address (RFC2462 section 5.4)
1544 * - A tentative address is not considered
1545 * "assigned to an interface" in the traditional
1546 * sense, unless it is also flagged as optimistic.
1547 * - Candidate Source Address (section 4)
1548 * - In any case, anycast addresses, multicast
1549 * addresses, and the unspecified address MUST
1550 * NOT be included in a candidate set.
1551 */
1552 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1553 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1554 continue;
1555
1556 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1557
1558 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1559 score->addr_type & IPV6_ADDR_MULTICAST)) {
1560 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1561 idev->dev->name);
1562 continue;
1563 }
1564
1565 score->rule = -1;
1566 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1567
1568 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1569 int minihiscore, miniscore;
1570
1571 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1572 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1573
1574 if (minihiscore > miniscore) {
1575 if (i == IPV6_SADDR_RULE_SCOPE &&
1576 score->scopedist > 0) {
1577 /*
1578 * special case:
1579 * each remaining entry
1580 * has too small (not enough)
1581 * scope, because ifa entries
1582 * are sorted by their scope
1583 * values.
1584 */
1585 goto out;
1586 }
1587 break;
1588 } else if (minihiscore < miniscore) {
1589 if (hiscore->ifa)
1590 in6_ifa_put(hiscore->ifa);
1591
1592 in6_ifa_hold(score->ifa);
1593
1594 swap(hiscore, score);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001595 hiscore_idx = 1 - hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001596
1597 /* restore our iterator */
1598 score->ifa = hiscore->ifa;
1599
1600 break;
1601 }
1602 }
1603 }
1604out:
1605 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001606 return hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001607}
1608
David Ahernafbac6012016-06-16 16:24:26 -07001609static int ipv6_get_saddr_master(struct net *net,
1610 const struct net_device *dst_dev,
1611 const struct net_device *master,
1612 struct ipv6_saddr_dst *dst,
1613 struct ipv6_saddr_score *scores,
1614 int hiscore_idx)
1615{
1616 struct inet6_dev *idev;
1617
1618 idev = __in6_dev_get(dst_dev);
1619 if (idev)
1620 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1621 scores, hiscore_idx);
1622
1623 idev = __in6_dev_get(master);
1624 if (idev)
1625 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1626 scores, hiscore_idx);
1627
1628 return hiscore_idx;
1629}
1630
Patrick McHardyb3f644f2012-08-26 19:14:14 +02001631int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001632 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001633 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001635 struct ipv6_saddr_score scores[2], *hiscore;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001636 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001637 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001638 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001639 int dst_type;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001640 bool use_oif_addr = false;
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001641 int hiscore_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001643 dst_type = __ipv6_addr_type(daddr);
1644 dst.addr = daddr;
1645 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1646 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001647 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001648 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001649
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001650 scores[hiscore_idx].rule = -1;
1651 scores[hiscore_idx].ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001653 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001654
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001655 /* Candidate Source Address (section 4)
1656 * - multicast and link-local destination address,
1657 * the set of candidate source address MUST only
1658 * include addresses assigned to interfaces
1659 * belonging to the same link as the outgoing
1660 * interface.
1661 * (- For site-local destination addresses, the
1662 * set of candidate source addresses MUST only
1663 * include addresses assigned to interfaces
1664 * belonging to the same site as the outgoing
1665 * interface.)
Erik Kline3985e8a2015-07-22 16:38:25 +09001666 * - "It is RECOMMENDED that the candidate source addresses
1667 * be the set of unicast addresses assigned to the
1668 * interface that will be used to send to the destination
1669 * (the 'outgoing' interface)." (RFC 6724)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001670 */
1671 if (dst_dev) {
Erik Kline3985e8a2015-07-22 16:38:25 +09001672 idev = __in6_dev_get(dst_dev);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001673 if ((dst_type & IPV6_ADDR_MULTICAST) ||
Erik Kline3985e8a2015-07-22 16:38:25 +09001674 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1675 (idev && idev->cnf.use_oif_addrs_only)) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001676 use_oif_addr = true;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001677 }
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001678 }
1679
1680 if (use_oif_addr) {
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001681 if (idev)
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001682 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001683 } else {
David Ahernafbac6012016-06-16 16:24:26 -07001684 const struct net_device *master;
1685 int master_idx = 0;
1686
1687 /* if dst_dev exists and is enslaved to an L3 device, then
1688 * prefer addresses from dst_dev and then the master over
1689 * any other enslaved devices in the L3 domain.
1690 */
1691 master = l3mdev_master_dev_rcu(dst_dev);
1692 if (master) {
1693 master_idx = master->ifindex;
1694
1695 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1696 master, &dst,
1697 scores, hiscore_idx);
1698
1699 if (scores[hiscore_idx].ifa)
1700 goto out;
1701 }
1702
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001703 for_each_netdev_rcu(net, dev) {
David Ahernafbac6012016-06-16 16:24:26 -07001704 /* only consider addresses on devices in the
1705 * same L3 domain
1706 */
1707 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1708 continue;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001709 idev = __in6_dev_get(dev);
1710 if (!idev)
1711 continue;
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001712 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001713 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001714 }
David Ahernafbac6012016-06-16 16:24:26 -07001715
1716out:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001717 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001719 hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001720 if (!hiscore->ifa)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001721 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001722
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001723 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001724 in6_ifa_put(hiscore->ifa);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001725 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001727EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Amerigo Wang89657792013-06-29 21:30:49 +08001729int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001730 u32 banned_flags)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001731{
1732 struct inet6_ifaddr *ifp;
1733 int err = -EADDRNOTAVAIL;
1734
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01001735 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1736 if (ifp->scope > IFA_LINK)
1737 break;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001738 if (ifp->scope == IFA_LINK &&
1739 !(ifp->flags & banned_flags)) {
1740 *addr = ifp->addr;
1741 err = 0;
1742 break;
1743 }
1744 }
1745 return err;
1746}
1747
Neil Horman95c385b2007-04-25 17:08:10 -07001748int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001749 u32 banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
1751 struct inet6_dev *idev;
1752 int err = -EADDRNOTAVAIL;
1753
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001754 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001755 idev = __in6_dev_get(dev);
1756 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 read_lock_bh(&idev->lock);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001758 err = __ipv6_get_lladdr(idev, addr, banned_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 read_unlock_bh(&idev->lock);
1760 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001761 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return err;
1763}
1764
1765static int ipv6_count_addresses(struct inet6_dev *idev)
1766{
1767 int cnt = 0;
1768 struct inet6_ifaddr *ifp;
1769
1770 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001771 list_for_each_entry(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 cnt++;
1773 read_unlock_bh(&idev->lock);
1774 return cnt;
1775}
1776
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001777int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Florian Westphal2a7851b2013-05-17 03:56:10 +00001778 const struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Erik Klinec58da4c2015-02-04 20:01:23 +09001780 return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1781}
1782EXPORT_SYMBOL(ipv6_chk_addr);
1783
1784int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1785 const struct net_device *dev, int strict,
1786 u32 banned_flags)
1787{
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001788 struct inet6_ifaddr *ifp;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001789 unsigned int hash = inet6_addr_hash(addr);
Erik Klinec58da4c2015-02-04 20:01:23 +09001790 u32 ifp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
stephen hemminger5c578aed2010-03-17 20:31:11 +00001792 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001793 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001794 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001795 continue;
Erik Klinec58da4c2015-02-04 20:01:23 +09001796 /* Decouple optimistic from tentative for evaluation here.
1797 * Ban optimistic addresses explicitly, when required.
1798 */
1799 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1800 ? (ifp->flags&~IFA_F_TENTATIVE)
1801 : ifp->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (ipv6_addr_equal(&ifp->addr, addr) &&
Erik Klinec58da4c2015-02-04 20:01:23 +09001803 !(ifp_flags&banned_flags) &&
Ian Morris63159f22015-03-29 14:00:04 +01001804 (!dev || ifp->idev->dev == dev ||
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001805 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1806 rcu_read_unlock_bh();
1807 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 }
1809 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00001810
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001811 rcu_read_unlock_bh();
1812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
Erik Klinec58da4c2015-02-04 20:01:23 +09001814EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001815
David S. Miller3e81c6d2010-03-20 16:18:00 -07001816static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1817 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
Eric Dumazetddbe5032012-07-18 08:11:12 +00001819 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001820 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Sasha Levinb67bfe02013-02-27 17:06:00 -08001822 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001823 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001824 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001826 if (!dev || ifp->idev->dev == dev)
David S. Miller3e81c6d2010-03-20 16:18:00 -07001827 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 }
1829 }
David S. Miller3e81c6d2010-03-20 16:18:00 -07001830 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001833/* Compares an address/prefix_len with addresses on device @dev.
1834 * If one is found it returns true.
1835 */
1836bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1837 const unsigned int prefix_len, struct net_device *dev)
1838{
1839 struct inet6_dev *idev;
1840 struct inet6_ifaddr *ifa;
1841 bool ret = false;
1842
1843 rcu_read_lock();
1844 idev = __in6_dev_get(dev);
1845 if (idev) {
1846 read_lock_bh(&idev->lock);
1847 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1848 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1849 if (ret)
1850 break;
1851 }
1852 read_unlock_bh(&idev->lock);
1853 }
1854 rcu_read_unlock();
1855
1856 return ret;
1857}
1858EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1859
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001860int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001861{
1862 struct inet6_dev *idev;
1863 struct inet6_ifaddr *ifa;
1864 int onlink;
1865
1866 onlink = 0;
1867 rcu_read_lock();
1868 idev = __in6_dev_get(dev);
1869 if (idev) {
1870 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001871 list_for_each_entry(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001872 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1873 ifa->prefix_len);
1874 if (onlink)
1875 break;
1876 }
1877 read_unlock_bh(&idev->lock);
1878 }
1879 rcu_read_unlock();
1880 return onlink;
1881}
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001882EXPORT_SYMBOL(ipv6_chk_prefix);
1883
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001884struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001885 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
David S. Millerb79d1d52010-03-25 21:39:21 -07001887 struct inet6_ifaddr *ifp, *result = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001888 unsigned int hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
stephen hemminger5c578aed2010-03-17 20:31:11 +00001890 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001891 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001892 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001893 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001895 if (!dev || ifp->idev->dev == dev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07001897 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 in6_ifa_hold(ifp);
1899 break;
1900 }
1901 }
1902 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00001903 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
David S. Millerb79d1d52010-03-25 21:39:21 -07001905 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908/* Gets referenced address, destroys ifaddr */
1909
Brian Haleycc411d02009-09-09 14:41:32 +00001910static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
Lubomir Rintel3d171f32016-01-08 13:47:23 +01001912 if (dad_failed)
1913 ifp->flags |= IFA_F_DADFAILED;
1914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (ifp->flags&IFA_F_PERMANENT) {
1916 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001917 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 ifp->flags |= IFA_F_TENTATIVE;
1919 spin_unlock_bh(&ifp->lock);
Herbert Xue2577a02010-03-13 12:23:29 -08001920 if (dad_failed)
1921 ipv6_ifa_notify(0, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 } else if (ifp->flags&IFA_F_TEMPORARY) {
1924 struct inet6_ifaddr *ifpub;
1925 spin_lock_bh(&ifp->lock);
1926 ifpub = ifp->ifpub;
1927 if (ifpub) {
1928 in6_ifa_hold(ifpub);
1929 spin_unlock_bh(&ifp->lock);
1930 ipv6_create_tempaddr(ifpub, ifp);
1931 in6_ifa_put(ifpub);
1932 } else {
1933 spin_unlock_bh(&ifp->lock);
1934 }
1935 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001936 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
Herbert Xuf2344a12010-05-18 15:55:27 -07001941static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1942{
1943 int err = -ENOENT;
1944
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001945 spin_lock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001946 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1947 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1948 err = 0;
1949 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001950 spin_unlock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001951
1952 return err;
1953}
1954
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001955void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1956{
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001957 struct inet6_dev *idev = ifp->idev;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001958 struct net *net = dev_net(ifp->idev->dev);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001959
Ursula Braun853dc2e2010-10-24 23:06:43 +00001960 if (addrconf_dad_end(ifp)) {
1961 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07001962 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00001963 }
Herbert Xuf2344a12010-05-18 15:55:27 -07001964
Joe Perchese87cc472012-05-13 21:56:26 +00001965 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1966 ifp->idev->dev->name, &ifp->addr);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001967
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001968 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001969
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001970 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
1971 int scope = ifp->scope;
1972 u32 flags = ifp->flags;
1973 struct in6_addr new_addr;
1974 struct inet6_ifaddr *ifp2;
1975 u32 valid_lft, preferred_lft;
1976 int pfxlen = ifp->prefix_len;
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001977 int retries = ifp->stable_privacy_retry + 1;
1978
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001979 if (retries > net->ipv6.sysctl.idgen_retries) {
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001980 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
1981 ifp->idev->dev->name);
1982 goto errdad;
1983 }
1984
1985 new_addr = ifp->addr;
1986 if (ipv6_generate_stable_address(&new_addr, retries,
1987 idev))
1988 goto errdad;
1989
1990 valid_lft = ifp->valid_lft;
1991 preferred_lft = ifp->prefered_lft;
1992
1993 spin_unlock_bh(&ifp->lock);
1994
1995 if (idev->cnf.max_addresses &&
1996 ipv6_count_addresses(idev) >=
1997 idev->cnf.max_addresses)
1998 goto lock_errdad;
1999
2000 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
2001 ifp->idev->dev->name);
2002
2003 ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
2004 scope, flags, valid_lft,
2005 preferred_lft);
2006 if (IS_ERR(ifp2))
2007 goto lock_errdad;
2008
2009 spin_lock_bh(&ifp2->lock);
2010 ifp2->stable_privacy_retry = retries;
2011 ifp2->state = INET6_IFADDR_STATE_PREDAD;
2012 spin_unlock_bh(&ifp2->lock);
2013
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01002014 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002015 in6_ifa_put(ifp2);
2016lock_errdad:
2017 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09002018 }
2019
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002020errdad:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002021 /* transition from _POSTDAD to _ERRDAD */
2022 ifp->state = INET6_IFADDR_STATE_ERRDAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01002023 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002024
2025 addrconf_mod_dad_work(ifp, 0);
Wei Yongjun751eb6b2016-09-05 16:06:31 +08002026 in6_ifa_put(ifp);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002027}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002029/* Join to solicited addr multicast group.
2030 * caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002031void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 struct in6_addr maddr;
2034
2035 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2036 return;
2037
2038 addrconf_addr_solict_mult(addr, &maddr);
2039 ipv6_dev_mc_inc(dev, &maddr);
2040}
2041
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002042/* caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002043void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
2045 struct in6_addr maddr;
2046
2047 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2048 return;
2049
2050 addrconf_addr_solict_mult(addr, &maddr);
2051 __ipv6_dev_mc_dec(idev, &maddr);
2052}
2053
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002054/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002055static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
2057 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002058
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002059 if (ifp->prefix_len >= 127) /* RFC 6164 */
Bjørn Mork2bda8a02011-07-05 23:04:13 +00002060 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2062 if (ipv6_addr_any(&addr))
2063 return;
WANG Cong013b4d92014-09-11 15:35:11 -07002064 __ipv6_dev_ac_inc(ifp->idev, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065}
2066
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002067/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002068static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
2070 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002071
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002072 if (ifp->prefix_len >= 127) /* RFC 6164 */
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00002073 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2075 if (ipv6_addr_any(&addr))
2076 return;
2077 __ipv6_dev_ac_dec(ifp->idev, &addr);
2078}
2079
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002080static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002081{
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002082 switch (dev->addr_len) {
2083 case ETH_ALEN:
Luiz Augusto von Dentz9dae2e02017-03-12 10:19:38 +02002084 memcpy(eui, dev->dev_addr, 3);
2085 eui[3] = 0xFF;
2086 eui[4] = 0xFE;
2087 memcpy(eui + 5, dev->dev_addr + 3, 3);
2088 break;
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002089 case EUI64_ADDR_LEN:
2090 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
2091 eui[0] ^= 2;
2092 break;
2093 default:
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002094 return -1;
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002095 }
2096
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002097 return 0;
2098}
2099
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002100static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2101{
2102 union fwnet_hwaddr *ha;
2103
2104 if (dev->addr_len != FWNET_ALEN)
2105 return -1;
2106
2107 ha = (union fwnet_hwaddr *)dev->dev_addr;
2108
2109 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2110 eui[0] ^= 2;
2111 return 0;
2112}
2113
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002114static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2115{
2116 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2117 if (dev->addr_len != ARCNET_ALEN)
2118 return -1;
2119 memset(eui, 0, 7);
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002120 eui[7] = *(u8 *)dev->dev_addr;
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002121 return 0;
2122}
2123
2124static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2125{
2126 if (dev->addr_len != INFINIBAND_ALEN)
2127 return -1;
2128 memcpy(eui, dev->dev_addr + 12, 8);
2129 eui[0] |= 2;
2130 return 0;
2131}
2132
stephen hemmingerc61393e2010-10-04 20:17:53 +00002133static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002134{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00002135 if (addr == 0)
2136 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002137 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2138 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2139 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2140 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2141 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2142 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2143 eui[1] = 0;
2144 eui[2] = 0x5E;
2145 eui[3] = 0xFE;
2146 memcpy(eui + 4, &addr, 4);
2147 return 0;
2148}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002149
2150static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2151{
2152 if (dev->priv_flags & IFF_ISATAP)
2153 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2154 return -1;
2155}
2156
stephen hemmingeraee80b52011-06-08 10:44:30 +00002157static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2158{
2159 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2160}
2161
Nicolas Dichtele8377352013-08-20 12:16:06 +02002162static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2163{
2164 memcpy(eui, dev->perm_addr, 3);
2165 memcpy(eui + 5, dev->perm_addr + 3, 3);
2166 eui[3] = 0xFF;
2167 eui[4] = 0xFE;
2168 eui[0] ^= 2;
2169 return 0;
2170}
2171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2173{
2174 switch (dev->type) {
2175 case ARPHRD_ETHER:
2176 case ARPHRD_FDDI:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002177 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002179 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002181 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002182 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002183 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002184 case ARPHRD_IPGRE:
Felix Jia45ce0fd2017-01-26 16:59:18 +13002185 case ARPHRD_TUNNEL:
stephen hemmingeraee80b52011-06-08 10:44:30 +00002186 return addrconf_ifid_gre(eui, dev);
Jukka Rissanene74bccb82013-12-11 17:05:36 +02002187 case ARPHRD_6LOWPAN:
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002188 return addrconf_ifid_6lowpan(eui, dev);
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002189 case ARPHRD_IEEE1394:
2190 return addrconf_ifid_ieee1394(eui, dev);
Nicolas Dichtele8377352013-08-20 12:16:06 +02002191 case ARPHRD_TUNNEL6:
Felix Jia45ce0fd2017-01-26 16:59:18 +13002192 case ARPHRD_IP6GRE:
Nicolas Dichtele8377352013-08-20 12:16:06 +02002193 return addrconf_ifid_ip6tnl(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
2195 return -1;
2196}
2197
2198static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2199{
2200 int err = -1;
2201 struct inet6_ifaddr *ifp;
2202
2203 read_lock_bh(&idev->lock);
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01002204 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2205 if (ifp->scope > IFA_LINK)
2206 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2208 memcpy(eui, ifp->addr.s6_addr+8, 8);
2209 err = 0;
2210 break;
2211 }
2212 }
2213 read_unlock_bh(&idev->lock);
2214 return err;
2215}
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002218static void ipv6_regen_rndid(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08002221 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
2224 /*
2225 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2226 * check if generated address is not inappropriate
2227 *
2228 * - Reserved subnet anycast (RFC 2526)
2229 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002230 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 * 00-00-5E-FE-xx-xx-xx-xx
2232 * - value 0
2233 * - XXX: already assigned to an address on the device
2234 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002235 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2237 (idev->rndid[7]&0x80))
2238 goto regen;
2239 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2240 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2241 goto regen;
2242 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2243 goto regen;
2244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}
2246
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002247static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002248{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002250 ipv6_regen_rndid(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253/*
2254 * Add prefix route.
2255 */
2256
2257static void
2258addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002259 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
Thomas Graf86872cb2006-08-22 00:01:08 -07002261 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002262 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
Thomas Graf86872cb2006-08-22 00:01:08 -07002263 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2264 .fc_ifindex = dev->ifindex,
2265 .fc_expires = expires,
2266 .fc_dst_len = plen,
2267 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002268 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07002269 .fc_protocol = RTPROT_KERNEL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002270 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002272 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
2274 /* Prevent useless cloning on PtP SIT.
2275 This thing is done here expecting that the whole
2276 class of non-broadcast devices need not cloning.
2277 */
Amerigo Wang07a93622012-10-29 16:23:10 +00002278#if IS_ENABLED(CONFIG_IPV6_SIT)
Thomas Graf86872cb2006-08-22 00:01:08 -07002279 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2280 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002281#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Thomas Graf86872cb2006-08-22 00:01:08 -07002283 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284}
2285
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002286
2287static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2288 int plen,
2289 const struct net_device *dev,
2290 u32 flags, u32 noflags)
2291{
2292 struct fib6_node *fn;
2293 struct rt6_info *rt = NULL;
2294 struct fib6_table *table;
David Ahernca254492015-10-12 11:47:10 -07002295 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002296
David Ahernca254492015-10-12 11:47:10 -07002297 table = fib6_get_table(dev_net(dev), tb_id);
Ian Morris63159f22015-03-29 14:00:04 +01002298 if (!table)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002299 return NULL;
2300
Li RongQing5744dd92012-09-11 21:59:01 +00002301 read_lock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002302 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2303 if (!fn)
2304 goto out;
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07002305
2306 noflags |= RTF_CACHE;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002307 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002308 if (rt->dst.dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002309 continue;
2310 if ((rt->rt6i_flags & flags) != flags)
2311 continue;
Romain Kuntz85da53b2013-01-09 15:02:26 +01002312 if ((rt->rt6i_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002313 continue;
2314 dst_hold(&rt->dst);
2315 break;
2316 }
2317out:
Li RongQing5744dd92012-09-11 21:59:01 +00002318 read_unlock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002319 return rt;
2320}
2321
2322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323/* Create "default" multicast route to the interface */
2324
2325static void addrconf_add_mroute(struct net_device *dev)
2326{
Thomas Graf86872cb2006-08-22 00:01:08 -07002327 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002328 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002329 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2330 .fc_ifindex = dev->ifindex,
2331 .fc_dst_len = 8,
2332 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002333 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002334 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Thomas Graf86872cb2006-08-22 00:01:08 -07002336 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2337
2338 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
2340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2342{
2343 struct inet6_dev *idev;
2344
2345 ASSERT_RTNL();
2346
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002347 idev = ipv6_find_idev(dev);
2348 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00002349 return ERR_PTR(-ENOBUFS);
2350
2351 if (idev->cnf.disable_ipv6)
2352 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
2354 /* Add default multicast route */
David Ahernba46ee42016-06-13 13:44:18 -07002355 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
Li Wei4af04ab2011-12-06 21:23:45 +00002356 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 return idev;
2359}
2360
Jiri Pirko53bd6742013-12-06 09:45:22 +01002361static void manage_tempaddrs(struct inet6_dev *idev,
2362 struct inet6_ifaddr *ifp,
2363 __u32 valid_lft, __u32 prefered_lft,
2364 bool create, unsigned long now)
2365{
2366 u32 flags;
2367 struct inet6_ifaddr *ift;
2368
2369 read_lock_bh(&idev->lock);
2370 /* update all temporary addresses in the list */
2371 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2372 int age, max_valid, max_prefered;
2373
2374 if (ifp != ift->ifpub)
2375 continue;
2376
2377 /* RFC 4941 section 3.3:
2378 * If a received option will extend the lifetime of a public
2379 * address, the lifetimes of temporary addresses should
2380 * be extended, subject to the overall constraint that no
2381 * temporary addresses should ever remain "valid" or "preferred"
2382 * for a time longer than (TEMP_VALID_LIFETIME) or
2383 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2384 */
2385 age = (now - ift->cstamp) / HZ;
2386 max_valid = idev->cnf.temp_valid_lft - age;
2387 if (max_valid < 0)
2388 max_valid = 0;
2389
2390 max_prefered = idev->cnf.temp_prefered_lft -
Jiri Bohac76506a92016-10-13 18:52:15 +02002391 idev->desync_factor - age;
Jiri Pirko53bd6742013-12-06 09:45:22 +01002392 if (max_prefered < 0)
2393 max_prefered = 0;
2394
2395 if (valid_lft > max_valid)
2396 valid_lft = max_valid;
2397
2398 if (prefered_lft > max_prefered)
2399 prefered_lft = max_prefered;
2400
2401 spin_lock(&ift->lock);
2402 flags = ift->flags;
2403 ift->valid_lft = valid_lft;
2404 ift->prefered_lft = prefered_lft;
2405 ift->tstamp = now;
2406 if (prefered_lft > 0)
2407 ift->flags &= ~IFA_F_DEPRECATED;
2408
2409 spin_unlock(&ift->lock);
2410 if (!(flags&IFA_F_TENTATIVE))
2411 ipv6_ifa_notify(0, ift);
2412 }
2413
2414 if ((create || list_empty(&idev->tempaddr_list)) &&
2415 idev->cnf.use_tempaddr > 0) {
2416 /* When a new public address is created as described
2417 * in [ADDRCONF], also create a new temporary address.
2418 * Also create a temporary address if it's enabled but
2419 * no temporary address currently exists.
2420 */
2421 read_unlock_bh(&idev->lock);
2422 ipv6_create_tempaddr(ifp, NULL);
2423 } else {
2424 read_unlock_bh(&idev->lock);
2425 }
2426}
2427
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002428static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2429{
Felix Jiad35a00b2017-01-26 16:59:17 +13002430 return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2431 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002432}
2433
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002434int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2435 const struct prefix_info *pinfo,
2436 struct inet6_dev *in6_dev,
2437 const struct in6_addr *addr, int addr_type,
2438 u32 addr_flags, bool sllao, bool tokenized,
2439 __u32 valid_lft, u32 prefered_lft)
Alexander Aring4f672232016-06-15 21:20:22 +02002440{
2441 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2442 int create = 0, update_lft = 0;
2443
2444 if (!ifp && valid_lft) {
2445 int max_addresses = in6_dev->cnf.max_addresses;
2446
2447#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2448 if (in6_dev->cnf.optimistic_dad &&
2449 !net->ipv6.devconf_all->forwarding && sllao)
2450 addr_flags |= IFA_F_OPTIMISTIC;
2451#endif
2452
2453 /* Do not allow to create too much of autoconfigured
2454 * addresses; this would be too easy way to crash kernel.
2455 */
2456 if (!max_addresses ||
2457 ipv6_count_addresses(in6_dev) < max_addresses)
2458 ifp = ipv6_add_addr(in6_dev, addr, NULL,
2459 pinfo->prefix_len,
2460 addr_type&IPV6_ADDR_SCOPE_MASK,
2461 addr_flags, valid_lft,
2462 prefered_lft);
2463
2464 if (IS_ERR_OR_NULL(ifp))
2465 return -1;
2466
2467 update_lft = 0;
2468 create = 1;
2469 spin_lock_bh(&ifp->lock);
2470 ifp->flags |= IFA_F_MANAGETEMPADDR;
2471 ifp->cstamp = jiffies;
2472 ifp->tokenized = tokenized;
2473 spin_unlock_bh(&ifp->lock);
2474 addrconf_dad_start(ifp);
2475 }
2476
2477 if (ifp) {
2478 u32 flags;
2479 unsigned long now;
2480 u32 stored_lft;
2481
2482 /* update lifetime (RFC2462 5.5.3 e) */
2483 spin_lock_bh(&ifp->lock);
2484 now = jiffies;
2485 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2486 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2487 else
2488 stored_lft = 0;
2489 if (!update_lft && !create && stored_lft) {
2490 const u32 minimum_lft = min_t(u32,
2491 stored_lft, MIN_VALID_LIFETIME);
2492 valid_lft = max(valid_lft, minimum_lft);
2493
2494 /* RFC4862 Section 5.5.3e:
2495 * "Note that the preferred lifetime of the
2496 * corresponding address is always reset to
2497 * the Preferred Lifetime in the received
2498 * Prefix Information option, regardless of
2499 * whether the valid lifetime is also reset or
2500 * ignored."
2501 *
2502 * So we should always update prefered_lft here.
2503 */
2504 update_lft = 1;
2505 }
2506
2507 if (update_lft) {
2508 ifp->valid_lft = valid_lft;
2509 ifp->prefered_lft = prefered_lft;
2510 ifp->tstamp = now;
2511 flags = ifp->flags;
2512 ifp->flags &= ~IFA_F_DEPRECATED;
2513 spin_unlock_bh(&ifp->lock);
2514
2515 if (!(flags&IFA_F_TENTATIVE))
2516 ipv6_ifa_notify(0, ifp);
2517 } else
2518 spin_unlock_bh(&ifp->lock);
2519
2520 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2521 create, now);
2522
2523 in6_ifa_put(ifp);
2524 addrconf_verify();
2525 }
2526
2527 return 0;
2528}
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002529EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
Alexander Aring4f672232016-06-15 21:20:22 +02002530
Neil Hormane6bff992012-01-04 10:49:15 +00002531void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532{
2533 struct prefix_info *pinfo;
2534 __u32 valid_lft;
2535 __u32 prefered_lft;
Alexander Aring4f672232016-06-15 21:20:22 +02002536 int addr_type, err;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002537 u32 addr_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07002539 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 if (len < sizeof(struct prefix_info)) {
dingtianhongba3542e2013-08-17 10:27:04 +08002544 ADBG("addrconf: prefix option too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 return;
2546 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 /*
2549 * Validation checks ([ADDRCONF], page 19)
2550 */
2551
2552 addr_type = ipv6_addr_type(&pinfo->prefix);
2553
2554 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2555 return;
2556
2557 valid_lft = ntohl(pinfo->valid);
2558 prefered_lft = ntohl(pinfo->prefered);
2559
2560 if (prefered_lft > valid_lft) {
Joe Perchese87cc472012-05-13 21:56:26 +00002561 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 return;
2563 }
2564
2565 in6_dev = in6_dev_get(dev);
2566
Ian Morris63159f22015-03-29 14:00:04 +01002567 if (!in6_dev) {
Joe Perchese87cc472012-05-13 21:56:26 +00002568 net_dbg_ratelimited("addrconf: device %s not configured\n",
2569 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 return;
2571 }
2572
2573 /*
2574 * Two things going on here:
2575 * 1) Add routes for on-link prefixes
2576 * 2) Configure prefixes with the auto flag set
2577 */
2578
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002579 if (pinfo->onlink) {
2580 struct rt6_info *rt;
2581 unsigned long rt_expires;
2582
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002583 /* Avoid arithmetic overflow. Really, we could
2584 * save rt_expires in seconds, likely valid_lft,
2585 * but it would require division in fib gc, that it
2586 * not good.
2587 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002588 if (HZ > USER_HZ)
2589 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2590 else
2591 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08002592
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002593 if (addrconf_finite_timeout(rt_expires))
2594 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002596 rt = addrconf_get_prefix_route(&pinfo->prefix,
2597 pinfo->prefix_len,
2598 dev,
2599 RTF_ADDRCONF | RTF_PREFIX_RT,
2600 RTF_GATEWAY | RTF_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002602 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002603 /* Autoconf prefix route */
2604 if (valid_lft == 0) {
2605 ip6_del_rt(rt);
2606 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002607 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002608 /* not infinity */
Gao feng1716a962012-04-06 00:13:10 +00002609 rt6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002610 } else {
Gao feng1716a962012-04-06 00:13:10 +00002611 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 }
2613 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002614 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002615 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2616 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002617 /* not infinity */
2618 flags |= RTF_EXPIRES;
2619 expires = jiffies_to_clock_t(rt_expires);
2620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002622 dev, expires, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00002624 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 }
2626
2627 /* Try to figure out our local address for this prefix */
2628
2629 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 struct in6_addr addr;
Alexander Aringf997c552016-06-15 21:20:23 +02002631 bool tokenized = false, dev_addr_generated = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
2633 if (pinfo->prefix_len == 64) {
2634 memcpy(&addr, &pinfo->prefix, 8);
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002635
2636 if (!ipv6_addr_any(&in6_dev->token)) {
2637 read_lock_bh(&in6_dev->lock);
2638 memcpy(addr.s6_addr + 8,
2639 in6_dev->token.s6_addr + 8, 8);
2640 read_unlock_bh(&in6_dev->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00002641 tokenized = true;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002642 } else if (is_addr_mode_generate_stable(in6_dev) &&
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01002643 !ipv6_generate_stable_address(&addr, 0,
2644 in6_dev)) {
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002645 addr_flags |= IFA_F_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01002646 goto ok;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002647 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2648 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
Alexander Aring4f672232016-06-15 21:20:22 +02002649 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002650 } else {
2651 dev_addr_generated = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 }
2653 goto ok;
2654 }
Joe Perchese87cc472012-05-13 21:56:26 +00002655 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2656 pinfo->prefix_len);
Alexander Aring4f672232016-06-15 21:20:22 +02002657 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
2659ok:
Alexander Aring4f672232016-06-15 21:20:22 +02002660 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2661 &addr, addr_type,
2662 addr_flags, sllao,
2663 tokenized, valid_lft,
2664 prefered_lft);
2665 if (err)
2666 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002667
2668 /* Ignore error case here because previous prefix add addr was
2669 * successful which will be notified.
2670 */
2671 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2672 addr_type, addr_flags, sllao,
2673 tokenized, valid_lft,
2674 prefered_lft,
2675 dev_addr_generated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 }
2677 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
Alexander Aring4f672232016-06-15 21:20:22 +02002678put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 in6_dev_put(in6_dev);
2680}
2681
2682/*
2683 * Set destination address.
2684 * Special case for SIT interfaces where we create a new "virtual"
2685 * device.
2686 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002687int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688{
2689 struct in6_ifreq ireq;
2690 struct net_device *dev;
2691 int err = -EINVAL;
2692
2693 rtnl_lock();
2694
2695 err = -EFAULT;
2696 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2697 goto err_exit;
2698
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002699 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
2701 err = -ENODEV;
Ian Morris63159f22015-03-29 14:00:04 +01002702 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 goto err_exit;
2704
Amerigo Wang07a93622012-10-29 16:23:10 +00002705#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002707 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 struct ip_tunnel_parm p;
2710
2711 err = -EADDRNOTAVAIL;
2712 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2713 goto err_exit;
2714
2715 memset(&p, 0, sizeof(p));
2716 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2717 p.iph.saddr = 0;
2718 p.iph.version = 4;
2719 p.iph.ihl = 5;
2720 p.iph.protocol = IPPROTO_IPV6;
2721 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002722 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002724 if (ops->ndo_do_ioctl) {
2725 mm_segment_t oldfs = get_fs();
2726
2727 set_fs(KERNEL_DS);
2728 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2729 set_fs(oldfs);
2730 } else
2731 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
2733 if (err == 0) {
2734 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002735 dev = __dev_get_by_name(net, p.name);
2736 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 goto err_exit;
2738 err = dev_open(dev);
2739 }
2740 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002741#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
2743err_exit:
2744 rtnl_unlock();
2745 return err;
2746}
2747
Madhu Challa93a714d2015-02-25 09:58:35 -08002748static int ipv6_mc_config(struct sock *sk, bool join,
2749 const struct in6_addr *addr, int ifindex)
2750{
2751 int ret;
2752
2753 ASSERT_RTNL();
2754
2755 lock_sock(sk);
2756 if (join)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002757 ret = ipv6_sock_mc_join(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002758 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002759 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002760 release_sock(sk);
2761
2762 return ret;
2763}
2764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765/*
2766 * Manual configuration of address on an interface
2767 */
Jiri Pirko479840f2013-12-06 09:45:21 +01002768static int inet6_addr_add(struct net *net, int ifindex,
2769 const struct in6_addr *pfx,
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002770 const struct in6_addr *peer_pfx,
Jiri Pirko479840f2013-12-06 09:45:21 +01002771 unsigned int plen, __u32 ifa_flags,
2772 __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
2774 struct inet6_ifaddr *ifp;
2775 struct inet6_dev *idev;
2776 struct net_device *dev;
Madhu Challa93a714d2015-02-25 09:58:35 -08002777 unsigned long timeout;
2778 clock_t expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002780 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
2782 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002783
Thomas Graf24ef0da2008-05-28 16:54:22 +02002784 if (plen > 128)
2785 return -EINVAL;
2786
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002787 /* check the lifetime */
2788 if (!valid_lft || prefered_lft > valid_lft)
2789 return -EINVAL;
2790
Jiri Pirko53bd6742013-12-06 09:45:22 +01002791 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2792 return -EINVAL;
2793
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002794 dev = __dev_get_by_index(net, ifindex);
2795 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002797
Brian Haley64e724f2010-07-20 10:34:30 +00002798 idev = addrconf_add_dev(dev);
2799 if (IS_ERR(idev))
2800 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
Madhu Challa93a714d2015-02-25 09:58:35 -08002802 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2803 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2804 true, pfx, ifindex);
2805
2806 if (ret < 0)
2807 return ret;
2808 }
2809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 scope = ipv6_addr_scope(pfx);
2811
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002812 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2813 if (addrconf_finite_timeout(timeout)) {
2814 expires = jiffies_to_clock_t(timeout * HZ);
2815 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002816 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002817 } else {
2818 expires = 0;
2819 flags = 0;
2820 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002821 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002822
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002823 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2824 if (addrconf_finite_timeout(timeout)) {
2825 if (timeout == 0)
2826 ifa_flags |= IFA_F_DEPRECATED;
2827 prefered_lft = timeout;
2828 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002829
Jiri Benc8a226b22013-08-01 10:41:28 +02002830 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2831 valid_lft, prefered_lft);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 if (!IS_ERR(ifp)) {
Thomas Haller761aac72014-01-15 15:36:58 +01002834 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2835 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2836 expires, flags);
2837 }
2838
Neil Horman95c385b2007-04-25 17:08:10 -07002839 /*
2840 * Note that section 3.1 of RFC 4429 indicates
2841 * that the Optimistic flag should not be set for
2842 * manually configured addresses
2843 */
David S. Millercf22f9a2012-04-14 21:37:40 -04002844 addrconf_dad_start(ifp);
Jiri Pirko53bd6742013-12-06 09:45:22 +01002845 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2846 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2847 true, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002849 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 return 0;
Madhu Challa93a714d2015-02-25 09:58:35 -08002851 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2852 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2853 false, pfx, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 }
2855
2856 return PTR_ERR(ifp);
2857}
2858
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002859static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2860 const struct in6_addr *pfx, unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861{
2862 struct inet6_ifaddr *ifp;
2863 struct inet6_dev *idev;
2864 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002865
Thomas Graf24ef0da2008-05-28 16:54:22 +02002866 if (plen > 128)
2867 return -EINVAL;
2868
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002869 dev = __dev_get_by_index(net, ifindex);
2870 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 return -ENODEV;
2872
Ian Morrise5d08d72014-11-23 21:28:43 +00002873 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01002874 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 return -ENXIO;
2876
2877 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002878 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 if (ifp->prefix_len == plen &&
2880 ipv6_addr_equal(pfx, &ifp->addr)) {
2881 in6_ifa_hold(ifp);
2882 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002883
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002884 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2885 (ifa_flags & IFA_F_MANAGETEMPADDR))
2886 manage_tempaddrs(idev, ifp, 0, 0, false,
2887 jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 ipv6_del_addr(ifp);
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002889 addrconf_verify_rtnl();
Madhu Challa93a714d2015-02-25 09:58:35 -08002890 if (ipv6_addr_is_multicast(pfx)) {
2891 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2892 false, pfx, dev->ifindex);
2893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 return 0;
2895 }
2896 }
2897 read_unlock_bh(&idev->lock);
2898 return -EADDRNOTAVAIL;
2899}
2900
2901
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002902int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
2904 struct in6_ifreq ireq;
2905 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002906
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002907 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2911 return -EFAULT;
2912
2913 rtnl_lock();
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002914 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002915 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2916 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 rtnl_unlock();
2918 return err;
2919}
2920
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002921int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922{
2923 struct in6_ifreq ireq;
2924 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002925
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002926 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 return -EPERM;
2928
2929 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2930 return -EFAULT;
2931
2932 rtnl_lock();
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002933 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002934 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 rtnl_unlock();
2936 return err;
2937}
2938
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002939static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2940 int plen, int scope)
2941{
2942 struct inet6_ifaddr *ifp;
2943
Jiri Benc8a226b22013-08-01 10:41:28 +02002944 ifp = ipv6_add_addr(idev, addr, NULL, plen,
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01002945 scope, IFA_F_PERMANENT,
2946 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002947 if (!IS_ERR(ifp)) {
2948 spin_lock_bh(&ifp->lock);
2949 ifp->flags &= ~IFA_F_TENTATIVE;
2950 spin_unlock_bh(&ifp->lock);
Paolo Abeni764d3be2016-11-22 16:57:40 +01002951 rt_genid_bump_ipv6(dev_net(idev->dev));
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002952 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2953 in6_ifa_put(ifp);
2954 }
2955}
2956
Amerigo Wang07a93622012-10-29 16:23:10 +00002957#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958static void sit_add_v4_addrs(struct inet6_dev *idev)
2959{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 struct in6_addr addr;
2961 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002962 struct net *net = dev_net(idev->dev);
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002963 int scope, plen;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002964 u32 pflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
2966 ASSERT_RTNL();
2967
2968 memset(&addr, 0, sizeof(struct in6_addr));
2969 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2970
2971 if (idev->dev->flags&IFF_POINTOPOINT) {
2972 addr.s6_addr32[0] = htonl(0xfe800000);
2973 scope = IFA_LINK;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002974 plen = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 } else {
2976 scope = IPV6_ADDR_COMPATv4;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002977 plen = 96;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002978 pflags |= RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 }
2980
2981 if (addr.s6_addr32[3]) {
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002982 add_addr(idev, &addr, plen, scope);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002983 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 return;
2985 }
2986
Benjamin Thery6fda7352008-03-05 10:47:47 -08002987 for_each_netdev(net, dev) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002988 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 if (in_dev && (dev->flags & IFF_UP)) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002990 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
2992 int flag = scope;
2993
2994 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
2996 addr.s6_addr32[3] = ifa->ifa_local;
2997
2998 if (ifa->ifa_scope == RT_SCOPE_LINK)
2999 continue;
3000 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
3001 if (idev->dev->flags&IFF_POINTOPOINT)
3002 continue;
3003 flag |= IFA_HOST;
3004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003006 add_addr(idev, &addr, plen, flag);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01003007 addrconf_prefix_route(&addr, plen, idev->dev, 0,
3008 pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 }
3010 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003013#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
3015static void init_loopback(struct net_device *dev)
3016{
3017 struct inet6_dev *idev;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303018 struct net_device *sp_dev;
3019 struct inet6_ifaddr *sp_ifa;
3020 struct rt6_info *sp_rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
3022 /* ::1 */
3023
3024 ASSERT_RTNL();
3025
Ian Morrise5d08d72014-11-23 21:28:43 +00003026 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003027 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003028 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 return;
3030 }
3031
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003032 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303033
3034 /* Add routes to other interface's IPv6 addresses */
3035 for_each_netdev(dev_net(dev), sp_dev) {
3036 if (!strcmp(sp_dev->name, dev->name))
3037 continue;
3038
3039 idev = __in6_dev_get(sp_dev);
3040 if (!idev)
3041 continue;
3042
3043 read_lock_bh(&idev->lock);
3044 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
3045
3046 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
3047 continue;
3048
Gao feng33d99112014-01-24 16:29:11 +08003049 if (sp_ifa->rt) {
3050 /* This dst has been added to garbage list when
3051 * lo device down, release this obsolete dst and
3052 * reallocate a new router for ifa.
3053 */
Nicolas Dichtela2204452016-10-12 10:10:40 +02003054 if (!atomic_read(&sp_ifa->rt->rt6i_ref)) {
Gao feng33d99112014-01-24 16:29:11 +08003055 ip6_rt_put(sp_ifa->rt);
3056 sp_ifa->rt = NULL;
3057 } else {
3058 continue;
3059 }
3060 }
Gao fenga881ae12013-06-16 11:14:30 +08003061
François-Xavier Le Bail57ec0af2013-12-02 11:28:49 +01003062 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303063
3064 /* Failure cases are ignored */
Gao feng534c8772013-06-02 22:16:21 +00003065 if (!IS_ERR(sp_rt)) {
3066 sp_ifa->rt = sp_rt;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303067 ip6_ins_rt(sp_rt);
Gao feng534c8772013-06-02 22:16:21 +00003068 }
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303069 }
3070 read_unlock_bh(&idev->lock);
3071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072}
3073
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003074void addrconf_add_linklocal(struct inet6_dev *idev,
3075 const struct in6_addr *addr, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003077 struct inet6_ifaddr *ifp;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003078 u32 addr_flags = flags | IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Neil Horman95c385b2007-04-25 17:08:10 -07003080#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3081 if (idev->cnf.optimistic_dad &&
David Miller702beb82008-07-20 18:17:02 -07003082 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
Neil Horman95c385b2007-04-25 17:08:10 -07003083 addr_flags |= IFA_F_OPTIMISTIC;
3084#endif
3085
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01003086 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
3087 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003089 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
David S. Millercf22f9a2012-04-14 21:37:40 -04003090 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 in6_ifa_put(ifp);
3092 }
3093}
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003094EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003096static bool ipv6_reserved_interfaceid(struct in6_addr address)
3097{
3098 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3099 return true;
3100
3101 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3102 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3103 return true;
3104
3105 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3106 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3107 return true;
3108
3109 return false;
3110}
3111
3112static int ipv6_generate_stable_address(struct in6_addr *address,
3113 u8 dad_count,
3114 const struct inet6_dev *idev)
3115{
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003116 static DEFINE_SPINLOCK(lock);
3117 static __u32 digest[SHA_DIGEST_WORDS];
3118 static __u32 workspace[SHA_WORKSPACE_WORDS];
3119
3120 static union {
3121 char __data[SHA_MESSAGE_BYTES];
3122 struct {
3123 struct in6_addr secret;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003124 __be32 prefix[2];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003125 unsigned char hwaddr[MAX_ADDR_LEN];
3126 u8 dad_count;
3127 } __packed;
3128 } data;
3129
3130 struct in6_addr secret;
3131 struct in6_addr temp;
3132 struct net *net = dev_net(idev->dev);
3133
3134 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3135
3136 if (idev->cnf.stable_secret.initialized)
3137 secret = idev->cnf.stable_secret.secret;
3138 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3139 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3140 else
3141 return -1;
3142
3143retry:
3144 spin_lock_bh(&lock);
3145
3146 sha_init(digest);
3147 memset(&data, 0, sizeof(data));
3148 memset(workspace, 0, sizeof(workspace));
3149 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003150 data.prefix[0] = address->s6_addr32[0];
3151 data.prefix[1] = address->s6_addr32[1];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003152 data.secret = secret;
3153 data.dad_count = dad_count;
3154
3155 sha_transform(digest, data.__data, workspace);
3156
3157 temp = *address;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003158 temp.s6_addr32[2] = (__force __be32)digest[0];
3159 temp.s6_addr32[3] = (__force __be32)digest[1];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003160
3161 spin_unlock_bh(&lock);
3162
3163 if (ipv6_reserved_interfaceid(temp)) {
3164 dad_count++;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01003165 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003166 return -1;
3167 goto retry;
3168 }
3169
3170 *address = temp;
3171 return 0;
3172}
3173
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003174static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3175{
3176 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3177
3178 if (s->initialized)
3179 return;
3180 s = &idev->cnf.stable_secret;
3181 get_random_bytes(&s->secret, sizeof(s->secret));
3182 s->initialized = true;
3183}
3184
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003185static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3186{
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003187 struct in6_addr addr;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003188
David Ahernca254492015-10-12 11:47:10 -07003189 /* no link local addresses on L3 master devices */
3190 if (netif_is_l3_master(idev->dev))
3191 return;
3192
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003193 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3194
Felix Jiad35a00b2017-01-26 16:59:17 +13003195 switch (idev->cnf.addr_gen_mode) {
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003196 case IN6_ADDR_GEN_MODE_RANDOM:
3197 ipv6_gen_mode_random_init(idev);
3198 /* fallthrough */
3199 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003200 if (!ipv6_generate_stable_address(&addr, 0, idev))
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003201 addrconf_add_linklocal(idev, &addr,
3202 IFA_F_STABLE_PRIVACY);
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003203 else if (prefix_route)
3204 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003205 break;
3206 case IN6_ADDR_GEN_MODE_EUI64:
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003207 /* addrconf_add_linklocal also adds a prefix_route and we
3208 * only need to care about prefix routes if ipv6_generate_eui64
3209 * couldn't generate one.
3210 */
3211 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003212 addrconf_add_linklocal(idev, &addr, 0);
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003213 else if (prefix_route)
3214 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003215 break;
3216 case IN6_ADDR_GEN_MODE_NONE:
3217 default:
3218 /* will not add any link local address */
3219 break;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003220 }
3221}
3222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223static void addrconf_dev_config(struct net_device *dev)
3224{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003225 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
3227 ASSERT_RTNL();
3228
Herbert Xu74235a22007-06-14 13:02:55 -07003229 if ((dev->type != ARPHRD_ETHER) &&
3230 (dev->type != ARPHRD_FDDI) &&
Herbert Xu74235a22007-06-14 13:02:55 -07003231 (dev->type != ARPHRD_ARCNET) &&
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00003232 (dev->type != ARPHRD_INFINIBAND) &&
Nicolas Dichtele8377352013-08-20 12:16:06 +02003233 (dev->type != ARPHRD_IEEE1394) &&
Jukka Rissanene74bccb82013-12-11 17:05:36 +02003234 (dev->type != ARPHRD_TUNNEL6) &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003235 (dev->type != ARPHRD_6LOWPAN) &&
Felix Jia45ce0fd2017-01-26 16:59:18 +13003236 (dev->type != ARPHRD_IP6GRE) &&
3237 (dev->type != ARPHRD_IPGRE) &&
3238 (dev->type != ARPHRD_TUNNEL) &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003239 (dev->type != ARPHRD_NONE)) {
Herbert Xu74235a22007-06-14 13:02:55 -07003240 /* Alas, we support only Ethernet autoconfiguration. */
3241 return;
3242 }
3243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00003245 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 return;
3247
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003248 /* this device type has no EUI support */
3249 if (dev->type == ARPHRD_NONE &&
Felix Jiad35a00b2017-01-26 16:59:17 +13003250 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3251 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003252
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003253 addrconf_addr_gen(idev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254}
3255
Amerigo Wang07a93622012-10-29 16:23:10 +00003256#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257static void addrconf_sit_config(struct net_device *dev)
3258{
3259 struct inet6_dev *idev;
3260
3261 ASSERT_RTNL();
3262
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003263 /*
3264 * Configure the tunnel with one of our IPv4
3265 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 * our v4 addrs in the tunnel
3267 */
3268
Ian Morrise5d08d72014-11-23 21:28:43 +00003269 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003270 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003271 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 return;
3273 }
3274
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003275 if (dev->priv_flags & IFF_ISATAP) {
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003276 addrconf_addr_gen(idev, false);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003277 return;
3278 }
3279
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 sit_add_v4_addrs(idev);
3281
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00003282 if (dev->flags&IFF_POINTOPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003285#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Amerigo Wang07a93622012-10-29 16:23:10 +00003287#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003288static void addrconf_gre_config(struct net_device *dev)
3289{
3290 struct inet6_dev *idev;
stephen hemmingeraee80b52011-06-08 10:44:30 +00003291
stephen hemmingeraee80b52011-06-08 10:44:30 +00003292 ASSERT_RTNL();
3293
Ian Morrise5d08d72014-11-23 21:28:43 +00003294 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003295 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003296 pr_debug("%s: add_dev failed\n", __func__);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003297 return;
3298 }
3299
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003300 addrconf_addr_gen(idev, true);
Hannes Frederic Sowad9e4ce62015-10-08 18:19:39 +02003301 if (dev->flags & IFF_POINTOPOINT)
3302 addrconf_add_mroute(dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003303}
3304#endif
3305
David Ahernf1705ec2016-02-24 09:25:37 -08003306static int fixup_permanent_addr(struct inet6_dev *idev,
3307 struct inet6_ifaddr *ifp)
3308{
David Ahern8048ced2017-04-25 09:17:29 -07003309 /* rt6i_ref == 0 means the host route was removed from the
3310 * FIB, for example, if 'lo' device is taken down. In that
3311 * case regenerate the host route.
3312 */
3313 if (!ifp->rt || !atomic_read(&ifp->rt->rt6i_ref)) {
3314 struct rt6_info *rt, *prev;
David Ahernf1705ec2016-02-24 09:25:37 -08003315
3316 rt = addrconf_dst_alloc(idev, &ifp->addr, false);
3317 if (unlikely(IS_ERR(rt)))
3318 return PTR_ERR(rt);
3319
David Ahern8048ced2017-04-25 09:17:29 -07003320 /* ifp->rt can be accessed outside of rtnl */
3321 spin_lock(&ifp->lock);
3322 prev = ifp->rt;
David Ahernf1705ec2016-02-24 09:25:37 -08003323 ifp->rt = rt;
David Ahern8048ced2017-04-25 09:17:29 -07003324 spin_unlock(&ifp->lock);
3325
3326 ip6_rt_put(prev);
David Ahernf1705ec2016-02-24 09:25:37 -08003327 }
3328
3329 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3330 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3331 idev->dev, 0, 0);
3332 }
3333
David Ahern6d717132017-05-02 14:43:44 -07003334 if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3335 addrconf_dad_start(ifp);
David Ahernf1705ec2016-02-24 09:25:37 -08003336
3337 return 0;
3338}
3339
3340static void addrconf_permanent_addr(struct net_device *dev)
3341{
3342 struct inet6_ifaddr *ifp, *tmp;
3343 struct inet6_dev *idev;
3344
3345 idev = __in6_dev_get(dev);
3346 if (!idev)
3347 return;
3348
3349 write_lock_bh(&idev->lock);
3350
3351 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3352 if ((ifp->flags & IFA_F_PERMANENT) &&
3353 fixup_permanent_addr(idev, ifp) < 0) {
3354 write_unlock_bh(&idev->lock);
3355 ipv6_del_addr(ifp);
3356 write_lock_bh(&idev->lock);
3357
3358 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3359 idev->dev->name, &ifp->addr);
3360 }
3361 }
3362
3363 write_unlock_bh(&idev->lock);
3364}
3365
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003366static int addrconf_notify(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00003367 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368{
Jiri Pirko351638e2013-05-28 01:30:21 +00003369 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
David Ahern4f7f34e2016-04-07 11:10:41 -07003370 struct netdev_notifier_changeupper_info *info;
Eric Dumazet748e2d92012-08-22 21:50:59 +00003371 struct inet6_dev *idev = __in6_dev_get(dev);
WANG Cong60abc0b2017-06-21 14:34:58 -07003372 struct net *net = dev_net(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003373 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07003374 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003376 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003377 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07003378 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003379 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -07003380 if (IS_ERR(idev))
3381 return notifier_from_errno(PTR_ERR(idev));
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003382 }
3383 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003384
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003385 case NETDEV_CHANGEMTU:
3386 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3387 if (dev->mtu < IPV6_MIN_MTU) {
WANG Cong60abc0b2017-06-21 14:34:58 -07003388 addrconf_ifdown(dev, dev != net->loopback_dev);
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003389 break;
3390 }
3391
3392 if (idev) {
3393 rt6_mtu_change(dev, dev->mtu);
3394 idev->cnf.mtu6 = dev->mtu;
3395 break;
3396 }
3397
3398 /* allocate new idev */
3399 idev = ipv6_add_dev(dev);
3400 if (IS_ERR(idev))
3401 break;
3402
3403 /* device is still not ready */
3404 if (!(idev->if_flags & IF_READY))
3405 break;
3406
3407 run_pending = 1;
3408
3409 /* fall through */
3410
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003412 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07003413 if (dev->flags & IFF_SLAVE)
3414 break;
3415
WANG Cong3ce62a82014-09-11 15:07:16 -07003416 if (idev && idev->cnf.disable_ipv6)
3417 break;
3418
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003419 if (event == NETDEV_UP) {
David Ahern38bd10c2016-04-21 20:56:12 -07003420 /* restore routes for permanent addresses */
3421 addrconf_permanent_addr(dev);
3422
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07003423 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003424 /* device is not ready yet. */
Joe Perchesf3213832012-05-15 14:11:53 +00003425 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003426 dev->name);
3427 break;
3428 }
Kristian Slavov99081042006-02-08 16:10:53 -08003429
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11003430 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3431 idev = ipv6_add_dev(dev);
3432
WANG Conga317a2f2014-07-25 15:25:09 -07003433 if (!IS_ERR_OR_NULL(idev)) {
Kristian Slavov99081042006-02-08 16:10:53 -08003434 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08003435 run_pending = 1;
3436 }
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003437 } else if (event == NETDEV_CHANGE) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07003438 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003439 /* device is still not ready. */
3440 break;
3441 }
3442
3443 if (idev) {
Linus Lüssinga088d1d2017-02-03 08:11:03 +01003444 if (idev->if_flags & IF_READY) {
3445 /* device is already configured -
3446 * but resend MLD reports, we might
3447 * have roamed and need to update
3448 * multicast snooping switches
3449 */
3450 ipv6_mc_up(idev);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003451 break;
Linus Lüssinga088d1d2017-02-03 08:11:03 +01003452 }
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003453 idev->if_flags |= IF_READY;
3454 }
3455
Joe Perchesf3213832012-05-15 14:11:53 +00003456 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3457 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003458
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003459 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003460 }
3461
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003462 switch (dev->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +00003463#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 case ARPHRD_SIT:
3465 addrconf_sit_config(dev);
3466 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07003467#endif
Amerigo Wang07a93622012-10-29 16:23:10 +00003468#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003469 case ARPHRD_IPGRE:
3470 addrconf_gre_config(dev);
3471 break;
3472#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 case ARPHRD_LOOPBACK:
3474 init_loopback(dev);
3475 break;
3476
3477 default:
3478 addrconf_dev_config(dev);
3479 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003480 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003481
WANG Conga317a2f2014-07-25 15:25:09 -07003482 if (!IS_ERR_OR_NULL(idev)) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003483 if (run_pending)
3484 addrconf_dad_run(idev);
3485
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003486 /*
3487 * If the MTU changed during the interface down,
3488 * when the interface up, the changed MTU must be
3489 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003491 if (idev->cnf.mtu6 != dev->mtu &&
3492 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 rt6_mtu_change(dev, dev->mtu);
3494 idev->cnf.mtu6 = dev->mtu;
3495 }
3496 idev->tstamp = jiffies;
3497 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003498
3499 /*
3500 * If the changed mtu during down is lower than
3501 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 */
3503 if (dev->mtu < IPV6_MIN_MTU)
WANG Cong60abc0b2017-06-21 14:34:58 -07003504 addrconf_ifdown(dev, dev != net->loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 }
3506 break;
3507
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 case NETDEV_DOWN:
3509 case NETDEV_UNREGISTER:
3510 /*
3511 * Remove all addresses from this interface.
3512 */
3513 addrconf_ifdown(dev, event != NETDEV_DOWN);
3514 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003515
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07003518 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003519 addrconf_sysctl_unregister(idev);
WANG Conga317a2f2014-07-25 15:25:09 -07003520 err = addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07003521 if (err)
3522 return notifier_from_errno(err);
WANG Conga317a2f2014-07-25 15:25:09 -07003523 err = snmp6_register_dev(idev);
3524 if (err) {
3525 addrconf_sysctl_unregister(idev);
3526 return notifier_from_errno(err);
3527 }
Stephen Hemminger5632c512007-04-28 21:16:39 -07003528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003530
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003531 case NETDEV_PRE_TYPE_CHANGE:
3532 case NETDEV_POST_TYPE_CHANGE:
Andrew Lunn3ef09522015-12-03 21:12:32 +01003533 if (idev)
3534 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07003535 break;
David Ahern4f7f34e2016-04-07 11:10:41 -07003536
3537 case NETDEV_CHANGEUPPER:
3538 info = ptr;
3539
3540 /* flush all routes if dev is linked to or unlinked from
3541 * an L3 master device (e.g., VRF)
3542 */
3543 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3544 addrconf_ifdown(dev, 0);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
3547 return NOTIFY_OK;
3548}
3549
3550/*
3551 * addrconf module should be notified of a device going up
3552 */
3553static struct notifier_block ipv6_dev_notf = {
3554 .notifier_call = addrconf_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07003555 .priority = ADDRCONF_NOTIFY_PRIORITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556};
3557
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003558static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07003559{
3560 struct inet6_dev *idev;
3561 ASSERT_RTNL();
3562
3563 idev = __in6_dev_get(dev);
3564
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003565 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003566 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003567 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003568 ipv6_mc_unmap(idev);
3569}
3570
David Ahern70af9212016-04-08 12:01:21 -07003571static bool addr_is_local(const struct in6_addr *addr)
3572{
3573 return ipv6_addr_type(addr) &
3574 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3575}
3576
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577static int addrconf_ifdown(struct net_device *dev, int how)
3578{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003579 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003580 struct inet6_dev *idev;
David Ahernf1705ec2016-02-24 09:25:37 -08003581 struct inet6_ifaddr *ifa, *tmp;
3582 struct list_head del_list;
3583 int _keep_addr;
3584 bool keep_addr;
David S. Miller73a8bd72011-01-23 23:27:15 -08003585 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
3587 ASSERT_RTNL();
3588
David S. Miller73a8bd72011-01-23 23:27:15 -08003589 rt6_ifdown(net, dev);
3590 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
3592 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003593 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 return -ENODEV;
3595
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003596 /*
3597 * Step 1: remove reference to ipv6 device from parent device.
3598 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003600 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003602
3603 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003604 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
3606 /* Step 1.5: remove snmp6 entry */
3607 snmp6_unregister_dev(idev);
3608
3609 }
3610
David Ahernf1705ec2016-02-24 09:25:37 -08003611 /* aggregate the system setting and interface setting */
3612 _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3613 if (!_keep_addr)
3614 _keep_addr = idev->cnf.keep_addr_on_down;
3615
3616 /* combine the user config with event to determine if permanent
3617 * addresses are to be removed from address hash table
3618 */
Mike Manningbc561632016-08-12 12:02:38 +01003619 keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003620
David S. Miller73a8bd72011-01-23 23:27:15 -08003621 /* Step 2: clear hash table */
3622 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3623 struct hlist_head *h = &inet6_addr_lst[i];
David S. Miller73a8bd72011-01-23 23:27:15 -08003624
3625 spin_lock_bh(&addrconf_hash_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01003626restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08003627 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
David S. Miller73a8bd72011-01-23 23:27:15 -08003628 if (ifa->idev == idev) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003629 addrconf_del_dad_work(ifa);
David Ahernf1705ec2016-02-24 09:25:37 -08003630 /* combined flag + permanent flag decide if
3631 * address is retained on a down event
3632 */
3633 if (!keep_addr ||
David Ahern70af9212016-04-08 12:01:21 -07003634 !(ifa->flags & IFA_F_PERMANENT) ||
3635 addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003636 hlist_del_init_rcu(&ifa->addr_lst);
3637 goto restart;
3638 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003639 }
3640 }
3641 spin_unlock_bh(&addrconf_hash_lock);
3642 }
3643
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 write_lock_bh(&idev->lock);
3645
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003646 addrconf_del_rs_timer(idev);
3647
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003648 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003649 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003650 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003652 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00003653 while (!list_empty(&idev->tempaddr_list)) {
3654 ifa = list_first_entry(&idev->tempaddr_list,
3655 struct inet6_ifaddr, tmp_list);
3656 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 write_unlock_bh(&idev->lock);
3658 spin_lock_bh(&ifa->lock);
3659
3660 if (ifa->ifpub) {
3661 in6_ifa_put(ifa->ifpub);
3662 ifa->ifpub = NULL;
3663 }
3664 spin_unlock_bh(&ifa->lock);
3665 in6_ifa_put(ifa);
3666 write_lock_bh(&idev->lock);
3667 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003668
David Ahernf1705ec2016-02-24 09:25:37 -08003669 /* re-combine the user config with event to determine if permanent
3670 * addresses are to be removed from the interface list
3671 */
Mike Manningbc561632016-08-12 12:02:38 +01003672 keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003673
3674 INIT_LIST_HEAD(&del_list);
3675 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
David Ahern38bd10c2016-04-21 20:56:12 -07003676 struct rt6_info *rt = NULL;
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003677 bool keep;
David Ahern38bd10c2016-04-21 20:56:12 -07003678
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003679 addrconf_del_dad_work(ifa);
stephen hemminger84e8b802010-03-02 13:32:46 +00003680
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003681 keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3682 !addr_is_local(&ifa->addr);
3683 if (!keep)
3684 list_move(&ifa->if_list, &del_list);
3685
David S. Miller73a8bd72011-01-23 23:27:15 -08003686 write_unlock_bh(&idev->lock);
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003687 spin_lock_bh(&ifa->lock);
David Ahernf1705ec2016-02-24 09:25:37 -08003688
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003689 if (keep) {
David Ahernf1705ec2016-02-24 09:25:37 -08003690 /* set state to skip the notifier below */
3691 state = INET6_IFADDR_STATE_DEAD;
David Ahern6d717132017-05-02 14:43:44 -07003692 ifa->state = INET6_IFADDR_STATE_PREDAD;
David Ahernf1705ec2016-02-24 09:25:37 -08003693 if (!(ifa->flags & IFA_F_NODAD))
3694 ifa->flags |= IFA_F_TENTATIVE;
David Ahern38bd10c2016-04-21 20:56:12 -07003695
3696 rt = ifa->rt;
3697 ifa->rt = NULL;
David Ahernf1705ec2016-02-24 09:25:37 -08003698 } else {
3699 state = ifa->state;
3700 ifa->state = INET6_IFADDR_STATE_DEAD;
David Ahernf1705ec2016-02-24 09:25:37 -08003701 }
3702
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003703 spin_unlock_bh(&ifa->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003704
David Ahern38bd10c2016-04-21 20:56:12 -07003705 if (rt)
3706 ip6_del_rt(rt);
3707
David S. Miller73a8bd72011-01-23 23:27:15 -08003708 if (state != INET6_IFADDR_STATE_DEAD) {
3709 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Cong Wangf88c91d2013-04-14 23:18:43 +08003710 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
Mike Manningea06f712016-07-22 18:32:11 +01003711 } else {
3712 if (idev->cnf.forwarding)
3713 addrconf_leave_anycast(ifa);
3714 addrconf_leave_solict(ifa->idev, &ifa->addr);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00003715 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003716
David S. Miller73a8bd72011-01-23 23:27:15 -08003717 write_lock_bh(&idev->lock);
3718 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003719
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 write_unlock_bh(&idev->lock);
3721
David Ahernf1705ec2016-02-24 09:25:37 -08003722 /* now clean up addresses to be removed */
3723 while (!list_empty(&del_list)) {
3724 ifa = list_first_entry(&del_list,
3725 struct inet6_ifaddr, if_list);
3726 list_del(&ifa->if_list);
3727
3728 in6_ifa_put(ifa);
3729 }
3730
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003731 /* Step 5: Discard anycast and multicast list */
3732 if (how) {
3733 ipv6_ac_destroy_dev(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 ipv6_mc_destroy_dev(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003735 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 ipv6_mc_down(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003740
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003741 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003742 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003743 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 neigh_parms_release(&nd_tbl, idev->nd_parms);
3745 neigh_ifdown(&nd_tbl, dev);
3746 in6_dev_put(idev);
3747 }
3748 return 0;
3749}
3750
3751static void addrconf_rs_timer(unsigned long data)
3752{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003753 struct inet6_dev *idev = (struct inet6_dev *)data;
Cong Wangcaf92bc2013-08-31 13:44:32 +08003754 struct net_device *dev = idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003755 struct in6_addr lladdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003757 write_lock(&idev->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00003758 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 goto out;
3760
Shmulik Ladkani9ba2add2012-12-02 01:44:53 +00003761 if (!ipv6_accept_ra(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003763
3764 /* Announcement received after solicitation was sent */
3765 if (idev->if_flags & IF_RA_RCVD)
3766 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003768 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
Cong Wangcaf92bc2013-08-31 13:44:32 +08003769 write_unlock(&idev->lock);
3770 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3771 ndisc_send_rs(dev, &lladdr,
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003772 &in6addr_linklocal_allrouters);
3773 else
Cong Wangcaf92bc2013-08-31 13:44:32 +08003774 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775
Cong Wangcaf92bc2013-08-31 13:44:32 +08003776 write_lock(&idev->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003777 idev->rs_interval = rfc3315_s14_backoff_update(
3778 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003779 /* The wait after the last probe can be shorter */
3780 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3781 idev->cnf.rtr_solicits) ?
3782 idev->cnf.rtr_solicit_delay :
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003783 idev->rs_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 /*
3786 * Note: we do not support deprecated "all on-link"
3787 * assumption any longer.
3788 */
Joe Perches91df42b2012-05-15 14:11:54 +00003789 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 }
3791
3792out:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003793 write_unlock(&idev->lock);
Cong Wangcaf92bc2013-08-31 13:44:32 +08003794put:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003795 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796}
3797
3798/*
3799 * Duplicate Address Detection
3800 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003801static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3802{
3803 unsigned long rand_num;
3804 struct inet6_dev *idev = ifp->idev;
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003805 u64 nonce;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003806
Neil Horman95c385b2007-04-25 17:08:10 -07003807 if (ifp->flags & IFA_F_OPTIMISTIC)
3808 rand_num = 0;
3809 else
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003810 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
Neil Horman95c385b2007-04-25 17:08:10 -07003811
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003812 nonce = 0;
3813 if (idev->cnf.enhanced_dad ||
3814 dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
3815 do
3816 get_random_bytes(&nonce, 6);
3817 while (nonce == 0);
3818 }
3819 ifp->dad_nonce = nonce;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003820 ifp->dad_probes = idev->cnf.dad_transmits;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003821 addrconf_mod_dad_work(ifp, rand_num);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003822}
3823
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003824static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825{
3826 struct inet6_dev *idev = ifp->idev;
3827 struct net_device *dev = idev->dev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003828 bool bump_id, notify = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
3830 addrconf_join_solict(dev, &ifp->addr);
3831
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003832 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833
3834 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003835 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003836 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838
3839 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09003840 idev->cnf.accept_dad < 1 ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003841 !(ifp->flags&IFA_F_TENTATIVE) ||
3842 ifp->flags & IFA_F_NODAD) {
Paolo Abeni764d3be2016-11-22 16:57:40 +01003843 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003844 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003845 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 read_unlock_bh(&idev->lock);
3847
Paolo Abeni764d3be2016-11-22 16:57:40 +01003848 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 return;
3850 }
3851
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003852 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003853 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003854 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003855 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003856 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003857 * - keep it tentative if it is a permanent address.
3858 * - otherwise, kill it.
3859 */
3860 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003861 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003862 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003863 }
Neil Horman95c385b2007-04-25 17:08:10 -07003864
3865 /*
3866 * Optimistic nodes can start receiving
3867 * Frames right away
3868 */
Erik Kline7fd25612014-10-28 18:11:14 +09003869 if (ifp->flags & IFA_F_OPTIMISTIC) {
Neil Horman95c385b2007-04-25 17:08:10 -07003870 ip6_ins_rt(ifp->rt);
Erik Kline7fd25612014-10-28 18:11:14 +09003871 if (ipv6_use_optimistic_addr(idev)) {
3872 /* Because optimistic nodes can use this address,
3873 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3874 */
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003875 notify = true;
Erik Kline7fd25612014-10-28 18:11:14 +09003876 }
3877 }
Neil Horman95c385b2007-04-25 17:08:10 -07003878
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003879 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003881 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 read_unlock_bh(&idev->lock);
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003883 if (notify)
3884 ipv6_ifa_notify(RTM_NEWADDR, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885}
3886
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003887static void addrconf_dad_start(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003889 bool begin_dad = false;
3890
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003891 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003892 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3893 ifp->state = INET6_IFADDR_STATE_PREDAD;
3894 begin_dad = true;
3895 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003896 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003897
3898 if (begin_dad)
3899 addrconf_mod_dad_work(ifp, 0);
3900}
3901
3902static void addrconf_dad_work(struct work_struct *w)
3903{
3904 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3905 struct inet6_ifaddr,
3906 dad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 struct inet6_dev *idev = ifp->idev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003908 bool bump_id, disable_ipv6 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 struct in6_addr mcaddr;
3910
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003911 enum {
3912 DAD_PROCESS,
3913 DAD_BEGIN,
3914 DAD_ABORT,
3915 } action = DAD_PROCESS;
3916
3917 rtnl_lock();
3918
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003919 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003920 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3921 action = DAD_BEGIN;
3922 ifp->state = INET6_IFADDR_STATE_DAD;
3923 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3924 action = DAD_ABORT;
3925 ifp->state = INET6_IFADDR_STATE_POSTDAD;
Mike Manning85b51b12016-08-18 14:39:40 +01003926
3927 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6 &&
3928 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
3929 struct in6_addr addr;
3930
3931 addr.s6_addr32[0] = htonl(0xfe800000);
3932 addr.s6_addr32[1] = 0;
3933
3934 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
3935 ipv6_addr_equal(&ifp->addr, &addr)) {
3936 /* DAD failed for link-local based on MAC */
3937 idev->cnf.disable_ipv6 = 1;
3938
3939 pr_info("%s: IPv6 being disabled!\n",
3940 ifp->idev->dev->name);
3941 disable_ipv6 = true;
3942 }
3943 }
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003944 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003945 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003946
3947 if (action == DAD_BEGIN) {
3948 addrconf_dad_begin(ifp);
3949 goto out;
3950 } else if (action == DAD_ABORT) {
Wei Yongjun751eb6b2016-09-05 16:06:31 +08003951 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003952 addrconf_dad_stop(ifp, 1);
Mike Manning85b51b12016-08-18 14:39:40 +01003953 if (disable_ipv6)
3954 addrconf_ifdown(idev->dev, 0);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003955 goto out;
3956 }
3957
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003958 if (!ifp->dad_probes && addrconf_dad_end(ifp))
Herbert Xuf2344a12010-05-18 15:55:27 -07003959 goto out;
3960
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003961 write_lock_bh(&idev->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003962 if (idev->dead || !(idev->if_flags & IF_READY)) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003963 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 goto out;
3965 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003966
3967 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003968 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3969 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003970 write_unlock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003971 goto out;
3972 }
3973
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003974 if (ifp->dad_probes == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 /*
3976 * DAD was successful
3977 */
3978
Paolo Abeni764d3be2016-11-22 16:57:40 +01003979 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003980 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003981 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003982 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
Paolo Abeni764d3be2016-11-22 16:57:40 +01003984 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
3986 goto out;
3987 }
3988
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003989 ifp->dad_probes--;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003990 addrconf_mod_dad_work(ifp,
3991 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
stephen hemminger21809fa2010-02-08 19:48:52 +00003992 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003993 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
3995 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003997 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
3998 ifp->dad_nonce);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999out:
4000 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004001 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002}
4003
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004004/* ifp->idev must be at least read locked */
4005static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
4006{
4007 struct inet6_ifaddr *ifpiter;
4008 struct inet6_dev *idev = ifp->idev;
4009
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01004010 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
4011 if (ifpiter->scope > IFA_LINK)
4012 break;
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004013 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
4014 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
4015 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
4016 IFA_F_PERMANENT)
4017 return false;
4018 }
4019 return true;
4020}
4021
Paolo Abeni764d3be2016-11-22 16:57:40 +01004022static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004024 struct net_device *dev = ifp->idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004025 struct in6_addr lladdr;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004026 bool send_rs, send_mld;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004027
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004028 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
4030 /*
4031 * Configure the address for reception. Now it is valid.
4032 */
4033
4034 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4035
Tore Anderson026359b2011-08-28 23:47:33 +00004036 /* If added prefix is link local and we are prepared to process
4037 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 */
4039
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004040 read_lock_bh(&ifp->idev->lock);
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004041 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004042 send_rs = send_mld &&
4043 ipv6_accept_ra(ifp->idev) &&
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004044 ifp->idev->cnf.rtr_solicits != 0 &&
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004045 (dev->flags&IFF_LOOPBACK) == 0;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004046 read_unlock_bh(&ifp->idev->lock);
4047
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004048 /* While dad is in progress mld report's source address is in6_addrany.
4049 * Resend with proper ll now.
4050 */
4051 if (send_mld)
4052 ipv6_mc_dad_complete(ifp->idev);
4053
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004054 if (send_rs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 /*
4056 * If a host as already performed a random delay
4057 * [...] as part of DAD [...] there is no need
4058 * to delay again before sending the first RS
4059 */
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004060 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004061 return;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004062 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004064 write_lock_bh(&ifp->idev->lock);
4065 spin_lock(&ifp->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004066 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4067 ifp->idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004068 ifp->idev->rs_probes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 ifp->idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004070 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004071 spin_unlock(&ifp->lock);
4072 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 }
Paolo Abeni764d3be2016-11-22 16:57:40 +01004074
4075 if (bump_id)
4076 rt_genid_bump_ipv6(dev_net(dev));
Marcus Huewea11a7f72017-02-06 18:34:56 +01004077
4078 /* Make sure that a new temporary address will be created
4079 * before this temporary address becomes deprecated.
4080 */
4081 if (ifp->flags & IFA_F_TEMPORARY)
4082 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083}
4084
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004085static void addrconf_dad_run(struct inet6_dev *idev)
4086{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004087 struct inet6_ifaddr *ifp;
4088
4089 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00004090 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00004091 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07004092 if (ifp->flags & IFA_F_TENTATIVE &&
4093 ifp->state == INET6_IFADDR_STATE_DAD)
4094 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00004095 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004096 }
4097 read_unlock_bh(&idev->lock);
4098}
4099
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100#ifdef CONFIG_PROC_FS
4101struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08004102 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004104 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105};
4106
Mihai Maruseac1d578302012-01-03 23:31:35 +00004107static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108{
4109 struct inet6_ifaddr *ifa = NULL;
4110 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004111 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00004112 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113
Mihai Maruseac1d578302012-01-03 23:31:35 +00004114 /* initial bucket if pos is 0 */
4115 if (pos == 0) {
4116 state->bucket = 0;
4117 state->offset = 0;
4118 }
4119
4120 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08004121 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00004122 addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004123 if (!net_eq(dev_net(ifa->idev->dev), net))
4124 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004125 /* sync with offset */
4126 if (p < state->offset) {
4127 p++;
4128 continue;
4129 }
4130 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004131 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004132 }
4133
4134 /* prepare for next bucket */
4135 state->offset = 0;
4136 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004138 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139}
4140
stephen hemmingerc2e21292010-03-17 20:31:10 +00004141static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4142 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143{
4144 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004145 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
Sasha Levinb67bfe02013-02-27 17:06:00 -08004147 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004148 if (!net_eq(dev_net(ifa->idev->dev), net))
4149 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004150 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004151 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004152 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004153
4154 while (++state->bucket < IN6_ADDR_HSIZE) {
Mihai Maruseac1d578302012-01-03 23:31:35 +00004155 state->offset = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004156 hlist_for_each_entry_rcu_bh(ifa,
stephen hemmingerc2e21292010-03-17 20:31:10 +00004157 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004158 if (!net_eq(dev_net(ifa->idev->dev), net))
4159 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004160 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004161 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08004162 }
4163 }
4164
stephen hemmingerc2e21292010-03-17 20:31:10 +00004165 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166}
4167
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004169 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170{
stephen hemminger5c578aed2010-03-17 20:31:11 +00004171 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00004172 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173}
4174
4175static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4176{
4177 struct inet6_ifaddr *ifa;
4178
4179 ifa = if6_get_next(seq, v);
4180 ++*pos;
4181 return ifa;
4182}
4183
4184static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004185 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186{
stephen hemminger5c578aed2010-03-17 20:31:11 +00004187 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188}
4189
4190static int if6_seq_show(struct seq_file *seq, void *v)
4191{
4192 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Jiri Pirko971a3512013-12-10 13:56:29 +01004193 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07004194 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 ifp->idev->dev->ifindex,
4196 ifp->prefix_len,
4197 ifp->scope,
Jiri Pirko971a3512013-12-10 13:56:29 +01004198 (u8) ifp->flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 ifp->idev->dev->name);
4200 return 0;
4201}
4202
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004203static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 .start = if6_seq_start,
4205 .next = if6_seq_next,
4206 .show = if6_seq_show,
4207 .stop = if6_seq_stop,
4208};
4209
4210static int if6_seq_open(struct inode *inode, struct file *file)
4211{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004212 return seq_open_net(inode, file, &if6_seq_ops,
4213 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214}
4215
Arjan van de Ven9a321442007-02-12 00:55:35 -08004216static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 .owner = THIS_MODULE,
4218 .open = if6_seq_open,
4219 .read = seq_read,
4220 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004221 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222};
4223
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004224static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225{
Gao fengd4beaa62013-02-18 01:34:54 +00004226 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 return -ENOMEM;
4228 return 0;
4229}
4230
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004231static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08004232{
Gao fengece31ff2013-02-18 01:34:56 +00004233 remove_proc_entry("if_inet6", net->proc_net);
Daniel Lezcano3c400902008-01-10 22:42:49 -08004234}
4235
4236static struct pernet_operations if6_proc_net_ops = {
Ian Morris67ba4152014-08-24 21:53:10 +01004237 .init = if6_proc_net_init,
4238 .exit = if6_proc_net_exit,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004239};
4240
4241int __init if6_proc_init(void)
4242{
4243 return register_pernet_subsys(&if6_proc_net_ops);
4244}
4245
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246void if6_proc_exit(void)
4247{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004248 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249}
4250#endif /* CONFIG_PROC_FS */
4251
Amerigo Wang07a93622012-10-29 16:23:10 +00004252#if IS_ENABLED(CONFIG_IPV6_MIP6)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004253/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004254int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004255{
4256 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004257 struct inet6_ifaddr *ifp = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00004258 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004259
stephen hemminger5c578aed2010-03-17 20:31:11 +00004260 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08004261 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09004262 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08004263 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09004264 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004265 (ifp->flags & IFA_F_HOMEADDRESS)) {
4266 ret = 1;
4267 break;
4268 }
4269 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00004270 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004271 return ret;
4272}
4273#endif
4274
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275/*
4276 * Periodic address status verification
4277 */
4278
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004279static void addrconf_verify_rtnl(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004281 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 int i;
4284
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004285 ASSERT_RTNL();
4286
stephen hemminger5c578aed2010-03-17 20:31:11 +00004287 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004289 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004291 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004293 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294restart:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004295 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
Yasushi Asanofad8da32013-12-31 12:04:19 +09004298 /* When setting preferred_lft to a value not zero or
4299 * infinity, while valid_lft is infinity
4300 * IFA_F_PERMANENT has a non-infinity life time.
4301 */
4302 if ((ifp->flags & IFA_F_PERMANENT) &&
4303 (ifp->prefered_lft == INFINITY_LIFE_TIME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 continue;
4305
4306 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004307 /* We try to batch several events at once. */
4308 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004310 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4311 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 spin_unlock(&ifp->lock);
4313 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 ipv6_del_addr(ifp);
4315 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004316 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4317 spin_unlock(&ifp->lock);
4318 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00004320 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 int deprecate = 0;
4322
4323 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4324 deprecate = 1;
4325 ifp->flags |= IFA_F_DEPRECATED;
4326 }
4327
Yasushi Asanofad8da32013-12-31 12:04:19 +09004328 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4329 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 next = ifp->tstamp + ifp->valid_lft * HZ;
4331
4332 spin_unlock(&ifp->lock);
4333
4334 if (deprecate) {
4335 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
4337 ipv6_ifa_notify(0, ifp);
4338 in6_ifa_put(ifp);
4339 goto restart;
4340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4342 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00004343 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4344 ifp->idev->cnf.dad_transmits *
Jiri Pirko1f9248e2013-12-07 19:26:53 +01004345 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
stephen hemminger88949cf2010-03-17 20:31:17 +00004346
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 if (age >= ifp->prefered_lft - regen_advance) {
4348 struct inet6_ifaddr *ifpub = ifp->ifpub;
4349 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4350 next = ifp->tstamp + ifp->prefered_lft * HZ;
4351 if (!ifp->regen_count && ifpub) {
4352 ifp->regen_count++;
4353 in6_ifa_hold(ifp);
4354 in6_ifa_hold(ifpub);
4355 spin_unlock(&ifp->lock);
stephen hemminger5c578aed2010-03-17 20:31:11 +00004356
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08004357 spin_lock(&ifpub->lock);
4358 ifpub->regen_count = 0;
4359 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 ipv6_create_tempaddr(ifpub, ifp);
4361 in6_ifa_put(ifpub);
4362 in6_ifa_put(ifp);
4363 goto restart;
4364 }
4365 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4366 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4367 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 } else {
4369 /* ifp->prefered_lft <= ifp->valid_lft */
4370 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4371 next = ifp->tstamp + ifp->prefered_lft * HZ;
4372 spin_unlock(&ifp->lock);
4373 }
4374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 }
4376
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004377 next_sec = round_jiffies_up(next);
4378 next_sched = next;
4379
4380 /* If rounded timeout is accurate enough, accept it. */
4381 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4382 next_sched = next_sec;
4383
4384 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4385 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4386 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4387
dingtianhongba3542e2013-08-17 10:27:04 +08004388 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4389 now, next, next_sec, next_sched);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004390 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
stephen hemminger5c578aed2010-03-17 20:31:11 +00004391 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392}
4393
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004394static void addrconf_verify_work(struct work_struct *w)
4395{
4396 rtnl_lock();
4397 addrconf_verify_rtnl();
4398 rtnl_unlock();
4399}
4400
4401static void addrconf_verify(void)
4402{
4403 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4404}
4405
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004406static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4407 struct in6_addr **peer_pfx)
Thomas Graf461d8832006-09-18 00:09:49 -07004408{
4409 struct in6_addr *pfx = NULL;
4410
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004411 *peer_pfx = NULL;
4412
Thomas Graf461d8832006-09-18 00:09:49 -07004413 if (addr)
4414 pfx = nla_data(addr);
4415
4416 if (local) {
4417 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004418 *peer_pfx = pfx;
4419 pfx = nla_data(local);
Thomas Graf461d8832006-09-18 00:09:49 -07004420 }
4421
4422 return pfx;
4423}
4424
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004425static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07004426 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4427 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4428 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
Jiri Pirko479840f2013-12-06 09:45:21 +01004429 [IFA_FLAGS] = { .len = sizeof(u32) },
Thomas Graf461d8832006-09-18 00:09:49 -07004430};
4431
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432static int
David Ahernc21ef3e2017-04-16 09:48:24 -07004433inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
4434 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004436 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07004437 struct ifaddrmsg *ifm;
4438 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004439 struct in6_addr *pfx, *peer_pfx;
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004440 u32 ifa_flags;
Thomas Grafb933f712006-09-18 00:10:19 -07004441 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
Johannes Bergfceb6432017-04-12 14:34:07 +02004443 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07004444 extack);
Thomas Grafb933f712006-09-18 00:10:19 -07004445 if (err < 0)
4446 return err;
4447
4448 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004449 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004450 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 return -EINVAL;
4452
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004453 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4454
4455 /* We ignore other flags so far. */
4456 ifa_flags &= IFA_F_MANAGETEMPADDR;
4457
4458 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4459 ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460}
4461
Jiri Pirko479840f2013-12-06 09:45:21 +01004462static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004463 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004464{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004465 u32 flags;
4466 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004467 unsigned long timeout;
Jiri Pirko53bd6742013-12-06 09:45:22 +01004468 bool was_managetempaddr;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004469 bool had_prefixroute;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09004470
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004471 ASSERT_RTNL();
4472
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004473 if (!valid_lft || (prefered_lft > valid_lft))
4474 return -EINVAL;
4475
Jiri Pirko53bd6742013-12-06 09:45:22 +01004476 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4477 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4478 return -EINVAL;
4479
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004480 timeout = addrconf_timeout_fixup(valid_lft, HZ);
4481 if (addrconf_finite_timeout(timeout)) {
4482 expires = jiffies_to_clock_t(timeout * HZ);
4483 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004484 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004485 } else {
4486 expires = 0;
4487 flags = 0;
4488 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004489 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004490
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004491 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4492 if (addrconf_finite_timeout(timeout)) {
4493 if (timeout == 0)
4494 ifa_flags |= IFA_F_DEPRECATED;
4495 prefered_lft = timeout;
4496 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004497
4498 spin_lock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004499 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004500 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4501 !(ifp->flags & IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004502 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
Thomas Haller761aac72014-01-15 15:36:58 +01004503 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4504 IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004505 ifp->flags |= ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004506 ifp->tstamp = jiffies;
4507 ifp->valid_lft = valid_lft;
4508 ifp->prefered_lft = prefered_lft;
4509
4510 spin_unlock_bh(&ifp->lock);
4511 if (!(ifp->flags&IFA_F_TENTATIVE))
4512 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004513
Thomas Haller761aac72014-01-15 15:36:58 +01004514 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4515 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4516 expires, flags);
Thomas Haller5b84efe2014-01-15 15:36:59 +01004517 } else if (had_prefixroute) {
4518 enum cleanup_prefix_rt_t action;
4519 unsigned long rt_expires;
4520
4521 write_lock_bh(&ifp->idev->lock);
4522 action = check_cleanup_prefix_route(ifp, &rt_expires);
4523 write_unlock_bh(&ifp->idev->lock);
4524
4525 if (action != CLEANUP_PREFIX_RT_NOP) {
4526 cleanup_prefix_route(ifp, rt_expires,
4527 action == CLEANUP_PREFIX_RT_DEL);
4528 }
Thomas Haller761aac72014-01-15 15:36:58 +01004529 }
Jiri Pirko53bd6742013-12-06 09:45:22 +01004530
4531 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4532 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4533 valid_lft = prefered_lft = 0;
4534 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4535 !was_managetempaddr, jiffies);
4536 }
4537
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004538 addrconf_verify_rtnl();
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004539
4540 return 0;
4541}
4542
4543static int
David Ahernc21ef3e2017-04-16 09:48:24 -07004544inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
4545 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004547 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07004548 struct ifaddrmsg *ifm;
4549 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004550 struct in6_addr *pfx, *peer_pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07004551 struct inet6_ifaddr *ifa;
4552 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004553 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
Jiri Pirko479840f2013-12-06 09:45:21 +01004554 u32 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07004555 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556
Johannes Bergfceb6432017-04-12 14:34:07 +02004557 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07004558 extack);
Thomas Graf461d8832006-09-18 00:09:49 -07004559 if (err < 0)
4560 return err;
4561
4562 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004563 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004564 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 return -EINVAL;
4566
Thomas Graf461d8832006-09-18 00:09:49 -07004567 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004568 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07004569
4570 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004571 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07004572 preferred_lft = ci->ifa_prefered;
4573 } else {
4574 preferred_lft = INFINITY_LIFE_TIME;
4575 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004576 }
4577
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004578 dev = __dev_get_by_index(net, ifm->ifa_index);
Ian Morris63159f22015-03-29 14:00:04 +01004579 if (!dev)
Thomas Graf7198f8c2006-09-18 00:13:46 -07004580 return -ENODEV;
4581
Jiri Pirko479840f2013-12-06 09:45:21 +01004582 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4583
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004584 /* We ignore other flags so far. */
Thomas Haller761aac72014-01-15 15:36:58 +01004585 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
Madhu Challa93a714d2015-02-25 09:58:35 -08004586 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004587
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08004588 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Ian Morris63159f22015-03-29 14:00:04 +01004589 if (!ifa) {
Thomas Graf7198f8c2006-09-18 00:13:46 -07004590 /*
4591 * It would be best to check for !NLM_F_CREATE here but
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08004592 * userspace already relies on not having to provide this.
Thomas Graf7198f8c2006-09-18 00:13:46 -07004593 */
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004594 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004595 ifm->ifa_prefixlen, ifa_flags,
4596 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004597 }
4598
Thomas Graf7198f8c2006-09-18 00:13:46 -07004599 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4600 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4601 err = -EEXIST;
4602 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004603 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07004604
4605 in6_ifa_put(ifa);
4606
4607 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608}
4609
Jiri Pirko479840f2013-12-06 09:45:21 +01004610static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
Thomas Graf101bb222006-09-18 00:11:52 -07004611 u8 scope, int ifindex)
4612{
4613 struct ifaddrmsg *ifm;
4614
4615 ifm = nlmsg_data(nlh);
4616 ifm->ifa_family = AF_INET6;
4617 ifm->ifa_prefixlen = prefixlen;
4618 ifm->ifa_flags = flags;
4619 ifm->ifa_scope = scope;
4620 ifm->ifa_index = ifindex;
4621}
4622
Thomas Graf85486af2006-09-18 00:11:24 -07004623static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4624 unsigned long tstamp, u32 preferred, u32 valid)
4625{
4626 struct ifa_cacheinfo ci;
4627
Thomas Graf18a31e12010-11-17 04:12:02 +00004628 ci.cstamp = cstamp_delta(cstamp);
4629 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07004630 ci.ifa_prefered = preferred;
4631 ci.ifa_valid = valid;
4632
4633 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4634}
4635
Thomas Graf101bb222006-09-18 00:11:52 -07004636static inline int rt_scope(int ifa_scope)
4637{
4638 if (ifa_scope & IFA_HOST)
4639 return RT_SCOPE_HOST;
4640 else if (ifa_scope & IFA_LINK)
4641 return RT_SCOPE_LINK;
4642 else if (ifa_scope & IFA_SITE)
4643 return RT_SCOPE_SITE;
4644 else
4645 return RT_SCOPE_UNIVERSE;
4646}
4647
Thomas Graf0ab68032006-09-18 00:12:35 -07004648static inline int inet6_ifaddr_msgsize(void)
4649{
Thomas Graf339bf982006-11-10 14:10:15 -08004650 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004651 + nla_total_size(16) /* IFA_LOCAL */
Thomas Graf339bf982006-11-10 14:10:15 -08004652 + nla_total_size(16) /* IFA_ADDRESS */
Jiri Pirko479840f2013-12-06 09:45:21 +01004653 + nla_total_size(sizeof(struct ifa_cacheinfo))
4654 + nla_total_size(4) /* IFA_FLAGS */;
Thomas Graf0ab68032006-09-18 00:12:35 -07004655}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004656
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004658 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07004661 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
Eric W. Biederman15e47302012-09-07 20:12:54 +00004663 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004664 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004665 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004666
Thomas Graf101bb222006-09-18 00:11:52 -07004667 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4668 ifa->idev->dev->ifindex);
4669
Yasushi Asanofad8da32013-12-31 12:04:19 +09004670 if (!((ifa->flags&IFA_F_PERMANENT) &&
4671 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
Thomas Graf85486af2006-09-18 00:11:24 -07004672 preferred = ifa->prefered_lft;
4673 valid = ifa->valid_lft;
4674 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00004676 if (preferred > tval)
4677 preferred -= tval;
4678 else
4679 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00004680 if (valid != INFINITY_LIFE_TIME) {
4681 if (valid > tval)
4682 valid -= tval;
4683 else
4684 valid = 0;
4685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 }
4687 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07004688 preferred = INFINITY_LIFE_TIME;
4689 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 }
Thomas Graf85486af2006-09-18 00:11:24 -07004691
Cong Wang7996c792013-05-22 05:41:06 +00004692 if (!ipv6_addr_any(&ifa->peer_addr)) {
Jiri Benc930345e2015-03-29 16:59:25 +02004693 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4694 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004695 goto error;
4696 } else
Jiri Benc930345e2015-03-29 16:59:25 +02004697 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004698 goto error;
4699
4700 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4701 goto error;
Thomas Graf85486af2006-09-18 00:11:24 -07004702
Jiri Pirko479840f2013-12-06 09:45:21 +01004703 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4704 goto error;
4705
Johannes Berg053c0952015-01-16 22:09:00 +01004706 nlmsg_end(skb, nlh);
4707 return 0;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004708
4709error:
4710 nlmsg_cancel(skb, nlh);
4711 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712}
4713
4714static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004715 u32 portid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004718 u8 scope = RT_SCOPE_UNIVERSE;
4719 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
Thomas Graf101bb222006-09-18 00:11:52 -07004721 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4722 scope = RT_SCOPE_SITE;
4723
Eric W. Biederman15e47302012-09-07 20:12:54 +00004724 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004725 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004726 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004727
Thomas Graf101bb222006-09-18 00:11:52 -07004728 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004729 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004730 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004731 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4732 nlmsg_cancel(skb, nlh);
4733 return -EMSGSIZE;
4734 }
Thomas Graf85486af2006-09-18 00:11:24 -07004735
Johannes Berg053c0952015-01-16 22:09:00 +01004736 nlmsg_end(skb, nlh);
4737 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738}
4739
4740static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004741 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004744 u8 scope = RT_SCOPE_UNIVERSE;
4745 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746
Thomas Graf101bb222006-09-18 00:11:52 -07004747 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4748 scope = RT_SCOPE_SITE;
4749
Eric W. Biederman15e47302012-09-07 20:12:54 +00004750 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004751 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004752 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004753
Thomas Graf101bb222006-09-18 00:11:52 -07004754 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004755 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004756 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004757 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4758 nlmsg_cancel(skb, nlh);
4759 return -EMSGSIZE;
4760 }
Thomas Graf85486af2006-09-18 00:11:24 -07004761
Johannes Berg053c0952015-01-16 22:09:00 +01004762 nlmsg_end(skb, nlh);
4763 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764}
4765
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004766enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 UNICAST_ADDR,
4768 MULTICAST_ADDR,
4769 ANYCAST_ADDR,
4770};
4771
Eric Dumazet234b27c2009-11-12 04:11:50 +00004772/* called with rcu_read_lock() */
4773static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4774 struct netlink_callback *cb, enum addr_type_t type,
4775 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 struct ifmcaddr6 *ifmca;
4778 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004779 int err = 1;
4780 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781
Eric Dumazet234b27c2009-11-12 04:11:50 +00004782 read_lock_bh(&idev->lock);
4783 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00004784 case UNICAST_ADDR: {
4785 struct inet6_ifaddr *ifa;
4786
Eric Dumazet234b27c2009-11-12 04:11:50 +00004787 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00004788 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4789 if (++ip_idx < s_ip_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004790 continue;
4791 err = inet6_fill_ifaddr(skb, ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004792 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004793 cb->nlh->nlmsg_seq,
4794 RTM_NEWADDR,
4795 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004796 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004797 break;
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004798 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004800 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00004801 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004802 case MULTICAST_ADDR:
4803 /* multicast address */
4804 for (ifmca = idev->mc_list; ifmca;
4805 ifmca = ifmca->next, ip_idx++) {
4806 if (ip_idx < s_ip_idx)
4807 continue;
4808 err = inet6_fill_ifmcaddr(skb, ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004809 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004810 cb->nlh->nlmsg_seq,
4811 RTM_GETMULTICAST,
4812 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004813 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004814 break;
4815 }
4816 break;
4817 case ANYCAST_ADDR:
4818 /* anycast address */
4819 for (ifaca = idev->ac_list; ifaca;
4820 ifaca = ifaca->aca_next, ip_idx++) {
4821 if (ip_idx < s_ip_idx)
4822 continue;
4823 err = inet6_fill_ifacaddr(skb, ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004824 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004825 cb->nlh->nlmsg_seq,
4826 RTM_GETANYCAST,
4827 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004828 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004829 break;
4830 }
4831 break;
4832 default:
4833 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004835 read_unlock_bh(&idev->lock);
4836 *p_ip_idx = ip_idx;
4837 return err;
4838}
4839
4840static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4841 enum addr_type_t type)
4842{
4843 struct net *net = sock_net(skb->sk);
4844 int h, s_h;
4845 int idx, ip_idx;
4846 int s_idx, s_ip_idx;
4847 struct net_device *dev;
4848 struct inet6_dev *idev;
4849 struct hlist_head *head;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004850
4851 s_h = cb->args[0];
4852 s_idx = idx = cb->args[1];
4853 s_ip_idx = ip_idx = cb->args[2];
4854
4855 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004856 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004857 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4858 idx = 0;
4859 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08004860 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet234b27c2009-11-12 04:11:50 +00004861 if (idx < s_idx)
4862 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07004863 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004864 s_ip_idx = 0;
4865 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004866 idev = __in6_dev_get(dev);
4867 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004868 goto cont;
4869
4870 if (in6_dump_addrs(idev, skb, cb, type,
David S. Miller7b46a642015-01-18 23:36:08 -05004871 s_ip_idx, &ip_idx) < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004872 goto done;
4873cont:
4874 idx++;
4875 }
4876 }
4877done:
4878 rcu_read_unlock();
4879 cb->args[0] = h;
4880 cb->args[1] = idx;
4881 cb->args[2] = ip_idx;
4882
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 return skb->len;
4884}
4885
4886static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4887{
4888 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004889
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 return inet6_dump_addr(skb, cb, type);
4891}
4892
4893static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4894{
4895 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004896
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 return inet6_dump_addr(skb, cb, type);
4898}
4899
4900
4901static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4902{
4903 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004904
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 return inet6_dump_addr(skb, cb, type);
4906}
4907
David Ahernc21ef3e2017-04-16 09:48:24 -07004908static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4909 struct netlink_ext_ack *extack)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004910{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004911 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004912 struct ifaddrmsg *ifm;
4913 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004914 struct in6_addr *addr = NULL, *peer;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004915 struct net_device *dev = NULL;
4916 struct inet6_ifaddr *ifa;
4917 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004918 int err;
4919
Johannes Bergfceb6432017-04-12 14:34:07 +02004920 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07004921 extack);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004922 if (err < 0)
4923 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004924
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004925 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
Ian Morris63159f22015-03-29 14:00:04 +01004926 if (!addr) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004927 err = -EINVAL;
4928 goto errout;
4929 }
4930
4931 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004932 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004933 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004934
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004935 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4936 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004937 err = -EADDRNOTAVAIL;
4938 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004939 }
4940
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004941 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4942 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004943 err = -ENOBUFS;
4944 goto errout_ifa;
4945 }
4946
Eric W. Biederman15e47302012-09-07 20:12:54 +00004947 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004948 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004949 if (err < 0) {
4950 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4951 WARN_ON(err == -EMSGSIZE);
4952 kfree_skb(skb);
4953 goto errout_ifa;
4954 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00004955 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004956errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004957 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004958errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004959 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004960}
4961
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4963{
4964 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004965 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07004966 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
Mahesh Bandewarf784ad32017-01-04 15:01:01 -08004968 /* Don't send DELADDR notification for TENTATIVE address,
4969 * since NEWADDR notification is sent only after removing
4970 * TENTATIVE flag.
4971 */
4972 if (ifa->flags & IFA_F_TENTATIVE && event == RTM_DELADDR)
4973 return;
4974
Thomas Graf0ab68032006-09-18 00:12:35 -07004975 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01004976 if (!skb)
Thomas Graf5d620262006-08-15 00:35:02 -07004977 goto errout;
4978
4979 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004980 if (err < 0) {
4981 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4982 WARN_ON(err == -EMSGSIZE);
4983 kfree_skb(skb);
4984 goto errout;
4985 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004986 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4987 return;
Thomas Graf5d620262006-08-15 00:35:02 -07004988errout:
4989 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004990 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991}
4992
Dave Jonesb6f99a22007-03-22 12:27:49 -07004993static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 __s32 *array, int bytes)
4995{
Thomas Graf04561c12006-11-14 19:53:58 -08004996 BUG_ON(bytes < (DEVCONF_MAX * 4));
4997
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 memset(array, 0, bytes);
4999 array[DEVCONF_FORWARDING] = cnf->forwarding;
5000 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
5001 array[DEVCONF_MTU6] = cnf->mtu6;
5002 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
5003 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
5004 array[DEVCONF_AUTOCONF] = cnf->autoconf;
5005 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
5006 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00005007 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
5008 jiffies_to_msecs(cnf->rtr_solicit_interval);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005009 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
5010 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
Thomas Graf93908d12010-11-17 01:44:24 +00005011 array[DEVCONF_RTR_SOLICIT_DELAY] =
5012 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02005014 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
5015 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
5016 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
5017 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
5019 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
5020 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
5021 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
5022 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08005024 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
Hangbin Liu8013d1d2015-07-30 14:28:42 +08005025 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08005026 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005027#ifdef CONFIG_IPV6_ROUTER_PREF
5028 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00005029 array[DEVCONF_RTR_PROBE_INTERVAL] =
5030 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08005031#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzbbea1242017-03-22 18:19:04 +09005032 array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08005033 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
5034#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005035#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07005036 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07005037 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07005038#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5039 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
Erik Kline7fd25612014-10-28 18:11:14 +09005040 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
Neil Horman95c385b2007-04-25 17:08:10 -07005041#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005042#ifdef CONFIG_IPV6_MROUTE
5043 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
5044#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09005045 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09005046 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00005047 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00005048 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02005049 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
Ben Greeard9333192014-06-25 14:44:53 -07005050 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
Harout Hedeshianc2943f12015-01-20 10:06:05 -07005051 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
Andy Gospodarek35103d12015-08-13 10:39:01 -04005052 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005053 /* we omit DEVCONF_STABLE_SECRET for now */
Erik Kline3985e8a2015-07-22 16:38:25 +09005054 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
Johannes Bergabbc3042016-02-04 13:31:19 +01005055 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
Johannes Berg7a02bf82016-02-04 13:31:20 +01005056 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
David Ahernf1705ec2016-02-24 09:25:37 -08005057 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
David Lebrun1ababeb2016-11-08 14:57:39 +01005058 array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
David Lebrunbf355b82016-11-08 14:57:42 +01005059#ifdef CONFIG_IPV6_SEG6_HMAC
5060 array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5061#endif
Erik Nordmarkadc176c2016-12-02 14:00:08 -08005062 array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
Felix Jiad35a00b2017-01-26 16:59:17 +13005063 array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
David Forsterdf789fe2017-02-23 16:27:18 +00005064 array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065}
5066
Thomas Grafb382b192010-11-16 04:33:57 +00005067static inline size_t inet6_ifla6_size(void)
5068{
5069 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5070 + nla_total_size(sizeof(struct ifla_cacheinfo))
5071 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5072 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005073 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5074 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
Thomas Grafb382b192010-11-16 04:33:57 +00005075}
5076
Thomas Graf339bf982006-11-10 14:10:15 -08005077static inline size_t inet6_if_nlmsg_size(void)
5078{
5079 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5080 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5081 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5082 + nla_total_size(4) /* IFLA_MTU */
5083 + nla_total_size(4) /* IFLA_LINK */
Andy Gospodarek03443382015-08-13 15:26:35 -04005084 + nla_total_size(1) /* IFLA_OPERSTATE */
Thomas Grafb382b192010-11-16 04:33:57 +00005085 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08005086}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005087
Eric Dumazetbe281e52011-05-19 01:14:23 +00005088static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Jia Heaca05672016-09-30 11:29:03 +08005089 int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005090{
5091 int i;
Jia Heaca05672016-09-30 11:29:03 +08005092 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005093 BUG_ON(pad < 0);
5094
5095 /* Use put_unaligned() because stats may not be aligned for u64. */
Jia Heaca05672016-09-30 11:29:03 +08005096 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5097 for (i = 1; i < ICMP6_MIB_MAX; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00005098 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005099
Jia Heaca05672016-09-30 11:29:03 +08005100 memset(&stats[ICMP6_MIB_MAX], 0, pad);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005101}
5102
WANG Cong698365f2014-05-05 15:55:55 -07005103static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305104 int bytes, size_t syncpoff)
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005105{
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305106 int i, c;
5107 u64 buff[IPSTATS_MIB_MAX];
5108 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5109
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005110 BUG_ON(pad < 0);
5111
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305112 memset(buff, 0, sizeof(buff));
5113 buff[0] = IPSTATS_MIB_MAX;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005114
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305115 for_each_possible_cpu(c) {
5116 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5117 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5118 }
5119
5120 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5121 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005122}
5123
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005124static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5125 int bytes)
5126{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005127 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005128 case IFLA_INET6_STATS:
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305129 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5130 offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005131 break;
5132 case IFLA_INET6_ICMP6STATS:
Jia Heaca05672016-09-30 11:29:03 +08005133 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005134 break;
5135 }
5136}
5137
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005138static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5139 u32 ext_filter_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005141 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08005142 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143
David S. Millerc78679e2012-04-01 20:27:33 -04005144 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5145 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08005147 ci.tstamp = cstamp_delta(idev->tstamp);
5148 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
Jiri Pirko1f9248e2013-12-07 19:26:53 +01005149 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
David S. Millerc78679e2012-04-01 20:27:33 -04005150 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5151 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005152 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
Ian Morris63159f22015-03-29 14:00:04 +01005153 if (!nla)
Thomas Graf04561c12006-11-14 19:53:58 -08005154 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005155 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005157 /* XXX - MC not implemented */
5158
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005159 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5160 return 0;
5161
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005162 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005163 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005164 goto nla_put_failure;
5165 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5166
5167 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005168 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005169 goto nla_put_failure;
5170 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005172 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
Ian Morris63159f22015-03-29 14:00:04 +01005173 if (!nla)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005174 goto nla_put_failure;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005175
Felix Jiad35a00b2017-01-26 16:59:17 +13005176 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005177 goto nla_put_failure;
5178
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005179 read_lock_bh(&idev->lock);
5180 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5181 read_unlock_bh(&idev->lock);
5182
Thomas Grafb382b192010-11-16 04:33:57 +00005183 return 0;
5184
5185nla_put_failure:
5186 return -EMSGSIZE;
5187}
5188
Arad, Ronenb1974ed2015-10-19 09:23:28 -07005189static size_t inet6_get_link_af_size(const struct net_device *dev,
5190 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005191{
5192 if (!__in6_dev_get(dev))
5193 return 0;
5194
5195 return inet6_ifla6_size();
5196}
5197
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005198static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5199 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005200{
5201 struct inet6_dev *idev = __in6_dev_get(dev);
5202
5203 if (!idev)
5204 return -ENODATA;
5205
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005206 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005207 return -EMSGSIZE;
5208
5209 return 0;
5210}
5211
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005212static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5213{
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005214 struct inet6_ifaddr *ifp;
5215 struct net_device *dev = idev->dev;
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005216 bool clear_token, update_rs = false;
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005217 struct in6_addr ll_addr;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005218
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005219 ASSERT_RTNL();
5220
Ian Morris63159f22015-03-29 14:00:04 +01005221 if (!token)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005222 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005223 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5224 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005225 if (!ipv6_accept_ra(idev))
5226 return -EINVAL;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005227 if (idev->cnf.rtr_solicits == 0)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005228 return -EINVAL;
5229
5230 write_lock_bh(&idev->lock);
5231
5232 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5233 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5234
5235 write_unlock_bh(&idev->lock);
5236
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005237 clear_token = ipv6_addr_any(token);
5238 if (clear_token)
5239 goto update_lft;
5240
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005241 if (!idev->dead && (idev->if_flags & IF_READY) &&
5242 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5243 IFA_F_OPTIMISTIC)) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005244 /* If we're not ready, then normal ifup will take care
5245 * of this. Otherwise, we need to request our rs here.
5246 */
5247 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5248 update_rs = true;
5249 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005250
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005251update_lft:
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005252 write_lock_bh(&idev->lock);
Daniel Borkmannfc403832013-04-09 03:47:15 +00005253
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005254 if (update_rs) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005255 idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005256 idev->rs_interval = rfc3315_s14_backoff_init(
5257 idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005258 idev->rs_probes = 1;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005259 addrconf_mod_rs_timer(idev, idev->rs_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005260 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005261
5262 /* Well, that's kinda nasty ... */
5263 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5264 spin_lock(&ifp->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00005265 if (ifp->tokenized) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005266 ifp->valid_lft = 0;
5267 ifp->prefered_lft = 0;
5268 }
5269 spin_unlock(&ifp->lock);
5270 }
5271
5272 write_unlock_bh(&idev->lock);
Lubomir Rintelb2ed64a2014-10-27 17:39:16 +01005273 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005274 addrconf_verify_rtnl();
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005275 return 0;
5276}
5277
Daniel Borkmann11b1f822015-02-05 14:39:11 +01005278static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5279 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5280 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5281};
5282
5283static int inet6_validate_link_af(const struct net_device *dev,
5284 const struct nlattr *nla)
5285{
5286 struct nlattr *tb[IFLA_INET6_MAX + 1];
5287
5288 if (dev && !__in6_dev_get(dev))
5289 return -EAFNOSUPPORT;
5290
Johannes Bergfceb6432017-04-12 14:34:07 +02005291 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy,
5292 NULL);
Daniel Borkmann11b1f822015-02-05 14:39:11 +01005293}
5294
Felix Jiad35a00b2017-01-26 16:59:17 +13005295static int check_addr_gen_mode(int mode)
5296{
5297 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5298 mode != IN6_ADDR_GEN_MODE_NONE &&
5299 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5300 mode != IN6_ADDR_GEN_MODE_RANDOM)
5301 return -EINVAL;
5302 return 1;
5303}
5304
5305static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5306 int mode)
5307{
5308 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5309 !idev->cnf.stable_secret.initialized &&
5310 !net->ipv6.devconf_dflt->stable_secret.initialized)
5311 return -EINVAL;
5312 return 1;
5313}
5314
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005315static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5316{
5317 int err = -EINVAL;
5318 struct inet6_dev *idev = __in6_dev_get(dev);
5319 struct nlattr *tb[IFLA_INET6_MAX + 1];
5320
5321 if (!idev)
5322 return -EAFNOSUPPORT;
5323
Johannes Bergfceb6432017-04-12 14:34:07 +02005324 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005325 BUG();
5326
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005327 if (tb[IFLA_INET6_TOKEN]) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005328 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005329 if (err)
5330 return err;
5331 }
5332
5333 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5334 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5335
Felix Jiad35a00b2017-01-26 16:59:17 +13005336 if (check_addr_gen_mode(mode) < 0 ||
5337 check_stable_privacy(idev, dev_net(dev), mode) < 0)
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005338 return -EINVAL;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005339
Felix Jiad35a00b2017-01-26 16:59:17 +13005340 idev->cnf.addr_gen_mode = mode;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005341 err = 0;
5342 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005343
5344 return err;
5345}
5346
Thomas Grafb382b192010-11-16 04:33:57 +00005347static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005348 u32 portid, u32 seq, int event, unsigned int flags)
Thomas Grafb382b192010-11-16 04:33:57 +00005349{
5350 struct net_device *dev = idev->dev;
5351 struct ifinfomsg *hdr;
5352 struct nlmsghdr *nlh;
5353 void *protoinfo;
5354
Eric W. Biederman15e47302012-09-07 20:12:54 +00005355 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005356 if (!nlh)
Thomas Grafb382b192010-11-16 04:33:57 +00005357 return -EMSGSIZE;
5358
5359 hdr = nlmsg_data(nlh);
5360 hdr->ifi_family = AF_INET6;
5361 hdr->__ifi_pad = 0;
5362 hdr->ifi_type = dev->type;
5363 hdr->ifi_index = dev->ifindex;
5364 hdr->ifi_flags = dev_get_flags(dev);
5365 hdr->ifi_change = 0;
5366
David S. Millerc78679e2012-04-01 20:27:33 -04005367 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5368 (dev->addr_len &&
5369 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5370 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02005371 (dev->ifindex != dev_get_iflink(dev) &&
Andy Gospodarek03443382015-08-13 15:26:35 -04005372 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5373 nla_put_u8(skb, IFLA_OPERSTATE,
5374 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
David S. Millerc78679e2012-04-01 20:27:33 -04005375 goto nla_put_failure;
Thomas Grafb382b192010-11-16 04:33:57 +00005376 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
Ian Morris63159f22015-03-29 14:00:04 +01005377 if (!protoinfo)
Thomas Grafb382b192010-11-16 04:33:57 +00005378 goto nla_put_failure;
5379
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005380 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005381 goto nla_put_failure;
5382
Thomas Graf04561c12006-11-14 19:53:58 -08005383 nla_nest_end(skb, protoinfo);
Johannes Berg053c0952015-01-16 22:09:00 +01005384 nlmsg_end(skb, nlh);
5385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386
Thomas Graf04561c12006-11-14 19:53:58 -08005387nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005388 nlmsg_cancel(skb, nlh);
5389 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390}
5391
5392static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5393{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005394 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00005395 int h, s_h;
David S. Miller434a8a52009-11-11 18:53:00 -08005396 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 struct net_device *dev;
5398 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00005399 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400
Eric Dumazet84d26972009-11-09 12:11:28 +00005401 s_h = cb->args[0];
5402 s_idx = cb->args[1];
5403
5404 rcu_read_lock();
5405 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5406 idx = 0;
5407 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08005408 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet84d26972009-11-09 12:11:28 +00005409 if (idx < s_idx)
5410 goto cont;
5411 idev = __in6_dev_get(dev);
5412 if (!idev)
5413 goto cont;
5414 if (inet6_fill_ifinfo(skb, idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005415 NETLINK_CB(cb->skb).portid,
Eric Dumazet84d26972009-11-09 12:11:28 +00005416 cb->nlh->nlmsg_seq,
Johannes Berg053c0952015-01-16 22:09:00 +01005417 RTM_NEWLINK, NLM_F_MULTI) < 0)
Eric Dumazet84d26972009-11-09 12:11:28 +00005418 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07005419cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00005420 idx++;
5421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 }
Eric Dumazet84d26972009-11-09 12:11:28 +00005423out:
5424 rcu_read_unlock();
5425 cb->args[1] = idx;
5426 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427
5428 return skb->len;
5429}
5430
5431void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5432{
5433 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005434 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005435 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005436
Thomas Graf339bf982006-11-10 14:10:15 -08005437 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005438 if (!skb)
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005439 goto errout;
5440
5441 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005442 if (err < 0) {
5443 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5444 WARN_ON(err == -EMSGSIZE);
5445 kfree_skb(skb);
5446 goto errout;
5447 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005448 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005449 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005450errout:
5451 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005452 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453}
5454
Thomas Graf339bf982006-11-10 14:10:15 -08005455static inline size_t inet6_prefix_nlmsg_size(void)
5456{
5457 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5458 + nla_total_size(sizeof(struct in6_addr))
5459 + nla_total_size(sizeof(struct prefix_cacheinfo));
5460}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005461
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005463 struct prefix_info *pinfo, u32 portid, u32 seq,
Thomas Graf6051e2f2006-11-14 19:54:19 -08005464 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465{
Thomas Graf6051e2f2006-11-14 19:54:19 -08005466 struct prefixmsg *pmsg;
5467 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 struct prefix_cacheinfo ci;
5469
Eric W. Biederman15e47302012-09-07 20:12:54 +00005470 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005471 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005472 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08005473
5474 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07005476 pmsg->prefix_pad1 = 0;
5477 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 pmsg->prefix_ifindex = idev->dev->ifindex;
5479 pmsg->prefix_len = pinfo->prefix_len;
5480 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07005481 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 pmsg->prefix_flags = 0;
5483 if (pinfo->onlink)
5484 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5485 if (pinfo->autoconf)
5486 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5487
David S. Millerc78679e2012-04-01 20:27:33 -04005488 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5489 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 ci.preferred_time = ntohl(pinfo->prefered);
5491 ci.valid_time = ntohl(pinfo->valid);
David S. Millerc78679e2012-04-01 20:27:33 -04005492 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5493 goto nla_put_failure;
Johannes Berg053c0952015-01-16 22:09:00 +01005494 nlmsg_end(skb, nlh);
5495 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496
Thomas Graf6051e2f2006-11-14 19:54:19 -08005497nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005498 nlmsg_cancel(skb, nlh);
5499 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500}
5501
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005502static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 struct prefix_info *pinfo)
5504{
5505 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005506 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005507 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508
Thomas Graf339bf982006-11-10 14:10:15 -08005509 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005510 if (!skb)
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005511 goto errout;
5512
5513 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005514 if (err < 0) {
5515 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5516 WARN_ON(err == -EMSGSIZE);
5517 kfree_skb(skb);
5518 goto errout;
5519 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005520 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5521 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005522errout:
5523 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08005524 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525}
5526
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5528{
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005529 struct net *net = dev_net(ifp->idev->dev);
5530
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005531 if (event)
5532 ASSERT_RTNL();
5533
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5535
5536 switch (event) {
5537 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07005538 /*
5539 * If the address was optimistic
5540 * we inserted the route at the start of
5541 * our DAD process, so we don't need
5542 * to do it again
5543 */
5544 if (!(ifp->rt->rt6i_node))
5545 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 if (ifp->idev->cnf.forwarding)
5547 addrconf_join_anycast(ifp);
Cong Wang7996c792013-05-22 05:41:06 +00005548 if (!ipv6_addr_any(&ifp->peer_addr))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005549 addrconf_prefix_route(&ifp->peer_addr, 128,
5550 ifp->idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 break;
5552 case RTM_DELADDR:
5553 if (ifp->idev->cnf.forwarding)
5554 addrconf_leave_anycast(ifp);
5555 addrconf_leave_solict(ifp->idev, &ifp->addr);
Cong Wang7996c792013-05-22 05:41:06 +00005556 if (!ipv6_addr_any(&ifp->peer_addr)) {
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005557 struct rt6_info *rt;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005558
Nicolas Dichtele7478df2014-09-03 23:59:22 +02005559 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5560 ifp->idev->dev, 0, 0);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07005561 if (rt)
5562 ip6_del_rt(rt);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005563 }
David Ahern38bd10c2016-04-21 20:56:12 -07005564 if (ifp->rt) {
5565 dst_hold(&ifp->rt->dst);
5566 ip6_del_rt(ifp->rt);
5567 }
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02005568 rt_genid_bump_ipv6(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 break;
5570 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005571 atomic_inc(&net->ipv6.dev_addr_genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572}
5573
5574static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5575{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005576 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 if (likely(ifp->idev->dead == 0))
5578 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005579 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580}
5581
5582#ifdef CONFIG_SYSCTL
5583
5584static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005585int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 void __user *buffer, size_t *lenp, loff_t *ppos)
5587{
5588 int *valp = ctl->data;
5589 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005590 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005591 struct ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 int ret;
5593
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005594 /*
5595 * ctl->data points to idev->cnf.forwarding, we should
5596 * not modify it until we get the rtnl lock.
5597 */
5598 lctl = *ctl;
5599 lctl.data = &val;
5600
5601 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08005603 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00005604 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005605 if (ret)
5606 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005607 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608}
5609
Marcelo Leitner77751422015-02-23 11:17:13 -03005610static
5611int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5612 void __user *buffer, size_t *lenp, loff_t *ppos)
5613{
5614 struct inet6_dev *idev = ctl->extra1;
5615 int min_mtu = IPV6_MIN_MTU;
5616 struct ctl_table lctl;
5617
5618 lctl = *ctl;
5619 lctl.extra1 = &min_mtu;
5620 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5621
5622 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5623}
5624
Brian Haley56d417b2009-06-01 03:07:33 -07005625static void dev_disable_change(struct inet6_dev *idev)
5626{
Cong Wang75538c22013-05-29 11:30:50 +08005627 struct netdev_notifier_info info;
5628
Brian Haley56d417b2009-06-01 03:07:33 -07005629 if (!idev || !idev->dev)
5630 return;
5631
Cong Wang75538c22013-05-29 11:30:50 +08005632 netdev_notifier_info_init(&info, idev->dev);
Brian Haley56d417b2009-06-01 03:07:33 -07005633 if (idev->cnf.disable_ipv6)
Cong Wang75538c22013-05-29 11:30:50 +08005634 addrconf_notify(NULL, NETDEV_DOWN, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005635 else
Cong Wang75538c22013-05-29 11:30:50 +08005636 addrconf_notify(NULL, NETDEV_UP, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005637}
5638
5639static void addrconf_disable_change(struct net *net, __s32 newf)
5640{
5641 struct net_device *dev;
5642 struct inet6_dev *idev;
5643
Kefeng Wang03e4def2017-01-19 16:26:21 +08005644 for_each_netdev(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07005645 idev = __in6_dev_get(dev);
5646 if (idev) {
5647 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5648 idev->cnf.disable_ipv6 = newf;
5649 if (changed)
5650 dev_disable_change(idev);
5651 }
Brian Haley56d417b2009-06-01 03:07:33 -07005652 }
Brian Haley56d417b2009-06-01 03:07:33 -07005653}
5654
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005655static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07005656{
5657 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005658 int old;
5659
5660 if (!rtnl_trylock())
5661 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07005662
5663 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005664 old = *p;
5665 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07005666
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005667 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5668 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07005669 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005670 }
Brian Haley56d417b2009-06-01 03:07:33 -07005671
5672 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07005673 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5674 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005675 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07005676 dev_disable_change((struct inet6_dev *)table->extra1);
5677
5678 rtnl_unlock();
5679 return 0;
5680}
5681
5682static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005683int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07005684 void __user *buffer, size_t *lenp, loff_t *ppos)
5685{
5686 int *valp = ctl->data;
5687 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005688 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005689 struct ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07005690 int ret;
5691
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005692 /*
5693 * ctl->data points to idev->cnf.disable_ipv6, we should
5694 * not modify it until we get the rtnl lock.
5695 */
5696 lctl = *ctl;
5697 lctl.data = &val;
5698
5699 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07005700
5701 if (write)
5702 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005703 if (ret)
5704 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07005705 return ret;
5706}
5707
stephen hemmingerc92d5492013-12-17 22:37:14 -08005708static
5709int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5710 void __user *buffer, size_t *lenp, loff_t *ppos)
5711{
5712 int *valp = ctl->data;
5713 int ret;
5714 int old, new;
5715
5716 old = *valp;
5717 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5718 new = *valp;
5719
5720 if (write && old != new) {
5721 struct net *net = ctl->extra2;
5722
5723 if (!rtnl_trylock())
5724 return restart_syscall();
5725
5726 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
David Ahern85b3daa2017-03-28 14:28:04 -07005727 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
5728 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08005729 NETCONFA_IFINDEX_DEFAULT,
5730 net->ipv6.devconf_dflt);
5731 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
David Ahern85b3daa2017-03-28 14:28:04 -07005732 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
5733 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08005734 NETCONFA_IFINDEX_ALL,
5735 net->ipv6.devconf_all);
5736 else {
5737 struct inet6_dev *idev = ctl->extra1;
5738
David Ahern85b3daa2017-03-28 14:28:04 -07005739 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
5740 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08005741 idev->dev->ifindex,
5742 &idev->cnf);
5743 }
5744 rtnl_unlock();
5745 }
5746
5747 return ret;
5748}
5749
Felix Jiad35a00b2017-01-26 16:59:17 +13005750static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
5751 void __user *buffer, size_t *lenp,
5752 loff_t *ppos)
5753{
5754 int ret = 0;
5755 int new_val;
5756 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
5757 struct net *net = (struct net *)ctl->extra2;
5758
Felix Jia8c171d62017-02-27 12:41:23 +13005759 if (!rtnl_trylock())
5760 return restart_syscall();
5761
Felix Jiad35a00b2017-01-26 16:59:17 +13005762 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5763
5764 if (write) {
5765 new_val = *((int *)ctl->data);
5766
Felix Jia8c171d62017-02-27 12:41:23 +13005767 if (check_addr_gen_mode(new_val) < 0) {
5768 ret = -EINVAL;
5769 goto out;
5770 }
Felix Jiad35a00b2017-01-26 16:59:17 +13005771
5772 /* request for default */
5773 if (&net->ipv6.devconf_dflt->addr_gen_mode == ctl->data) {
5774 ipv6_devconf_dflt.addr_gen_mode = new_val;
5775
5776 /* request for individual net device */
5777 } else {
5778 if (!idev)
Felix Jia8c171d62017-02-27 12:41:23 +13005779 goto out;
Felix Jiad35a00b2017-01-26 16:59:17 +13005780
Felix Jia8c171d62017-02-27 12:41:23 +13005781 if (check_stable_privacy(idev, net, new_val) < 0) {
5782 ret = -EINVAL;
5783 goto out;
5784 }
Felix Jiad35a00b2017-01-26 16:59:17 +13005785
5786 if (idev->cnf.addr_gen_mode != new_val) {
5787 idev->cnf.addr_gen_mode = new_val;
Felix Jiad35a00b2017-01-26 16:59:17 +13005788 addrconf_dev_config(idev->dev);
Felix Jiad35a00b2017-01-26 16:59:17 +13005789 }
5790 }
5791 }
5792
Felix Jia8c171d62017-02-27 12:41:23 +13005793out:
5794 rtnl_unlock();
5795
Felix Jiad35a00b2017-01-26 16:59:17 +13005796 return ret;
5797}
5798
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005799static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5800 void __user *buffer, size_t *lenp,
5801 loff_t *ppos)
5802{
5803 int err;
5804 struct in6_addr addr;
5805 char str[IPV6_MAX_STRLEN];
5806 struct ctl_table lctl = *ctl;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005807 struct net *net = ctl->extra2;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005808 struct ipv6_stable_secret *secret = ctl->data;
5809
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005810 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5811 return -EIO;
5812
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005813 lctl.maxlen = IPV6_MAX_STRLEN;
5814 lctl.data = str;
5815
5816 if (!rtnl_trylock())
5817 return restart_syscall();
5818
5819 if (!write && !secret->initialized) {
5820 err = -EIO;
5821 goto out;
5822 }
5823
WANG Cong5449a5c2015-12-21 10:55:45 -08005824 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
5825 if (err >= sizeof(str)) {
5826 err = -EIO;
5827 goto out;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005828 }
5829
5830 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5831 if (err || !write)
5832 goto out;
5833
5834 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5835 err = -EIO;
5836 goto out;
5837 }
5838
5839 secret->initialized = true;
5840 secret->secret = addr;
5841
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005842 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5843 struct net_device *dev;
5844
5845 for_each_netdev(net, dev) {
5846 struct inet6_dev *idev = __in6_dev_get(dev);
5847
5848 if (idev) {
Felix Jiad35a00b2017-01-26 16:59:17 +13005849 idev->cnf.addr_gen_mode =
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005850 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5851 }
5852 }
5853 } else {
5854 struct inet6_dev *idev = ctl->extra1;
5855
Felix Jiad35a00b2017-01-26 16:59:17 +13005856 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005857 }
5858
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005859out:
5860 rtnl_unlock();
5861
5862 return err;
5863}
stephen hemmingerc92d5492013-12-17 22:37:14 -08005864
Andy Gospodarek35103d12015-08-13 10:39:01 -04005865static
5866int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
5867 int write,
5868 void __user *buffer,
5869 size_t *lenp,
5870 loff_t *ppos)
5871{
5872 int *valp = ctl->data;
5873 int val = *valp;
5874 loff_t pos = *ppos;
5875 struct ctl_table lctl;
5876 int ret;
5877
5878 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
5879 * we should not modify it until we get the rtnl lock.
5880 */
5881 lctl = *ctl;
5882 lctl.data = &val;
5883
5884 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5885
5886 if (write)
5887 ret = addrconf_fixup_linkdown(ctl, valp, val);
5888 if (ret)
5889 *ppos = pos;
5890 return ret;
5891}
5892
David Forsterdf789fe2017-02-23 16:27:18 +00005893static
5894void addrconf_set_nopolicy(struct rt6_info *rt, int action)
5895{
5896 if (rt) {
5897 if (action)
5898 rt->dst.flags |= DST_NOPOLICY;
5899 else
5900 rt->dst.flags &= ~DST_NOPOLICY;
5901 }
5902}
5903
5904static
5905void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
5906{
5907 struct inet6_ifaddr *ifa;
5908
5909 read_lock_bh(&idev->lock);
5910 list_for_each_entry(ifa, &idev->addr_list, if_list) {
5911 spin_lock(&ifa->lock);
5912 if (ifa->rt) {
5913 struct rt6_info *rt = ifa->rt;
5914 struct fib6_table *table = rt->rt6i_table;
5915 int cpu;
5916
5917 read_lock(&table->tb6_lock);
5918 addrconf_set_nopolicy(ifa->rt, val);
5919 if (rt->rt6i_pcpu) {
5920 for_each_possible_cpu(cpu) {
5921 struct rt6_info **rtp;
5922
5923 rtp = per_cpu_ptr(rt->rt6i_pcpu, cpu);
5924 addrconf_set_nopolicy(*rtp, val);
5925 }
5926 }
5927 read_unlock(&table->tb6_lock);
5928 }
5929 spin_unlock(&ifa->lock);
5930 }
5931 read_unlock_bh(&idev->lock);
5932}
5933
5934static
5935int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
5936{
5937 struct inet6_dev *idev;
5938 struct net *net;
5939
5940 if (!rtnl_trylock())
5941 return restart_syscall();
5942
5943 *valp = val;
5944
5945 net = (struct net *)ctl->extra2;
5946 if (valp == &net->ipv6.devconf_dflt->disable_policy) {
5947 rtnl_unlock();
5948 return 0;
5949 }
5950
5951 if (valp == &net->ipv6.devconf_all->disable_policy) {
5952 struct net_device *dev;
5953
5954 for_each_netdev(net, dev) {
5955 idev = __in6_dev_get(dev);
5956 if (idev)
5957 addrconf_disable_policy_idev(idev, val);
5958 }
5959 } else {
5960 idev = (struct inet6_dev *)ctl->extra1;
5961 addrconf_disable_policy_idev(idev, val);
5962 }
5963
5964 rtnl_unlock();
5965 return 0;
5966}
5967
5968static
5969int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
5970 void __user *buffer, size_t *lenp,
5971 loff_t *ppos)
5972{
5973 int *valp = ctl->data;
5974 int val = *valp;
5975 loff_t pos = *ppos;
5976 struct ctl_table lctl;
5977 int ret;
5978
5979 lctl = *ctl;
5980 lctl.data = &val;
5981 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5982
5983 if (write && (*valp != val))
5984 ret = addrconf_disable_policy(ctl, valp, val);
5985
5986 if (ret)
5987 *ppos = pos;
5988
5989 return ret;
5990}
5991
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07005992static int minus_one = -1;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07005993static const int one = 1;
5994static const int two_five_five = 255;
5995
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03005996static const struct ctl_table addrconf_sysctl[] = {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005997 {
5998 .procname = "forwarding",
5999 .data = &ipv6_devconf.forwarding,
6000 .maxlen = sizeof(int),
6001 .mode = 0644,
6002 .proc_handler = addrconf_sysctl_forward,
6003 },
6004 {
6005 .procname = "hop_limit",
6006 .data = &ipv6_devconf.hop_limit,
6007 .maxlen = sizeof(int),
6008 .mode = 0644,
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006009 .proc_handler = proc_dointvec_minmax,
6010 .extra1 = (void *)&one,
6011 .extra2 = (void *)&two_five_five,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006012 },
6013 {
6014 .procname = "mtu",
6015 .data = &ipv6_devconf.mtu6,
6016 .maxlen = sizeof(int),
6017 .mode = 0644,
6018 .proc_handler = addrconf_sysctl_mtu,
6019 },
6020 {
6021 .procname = "accept_ra",
6022 .data = &ipv6_devconf.accept_ra,
6023 .maxlen = sizeof(int),
6024 .mode = 0644,
6025 .proc_handler = proc_dointvec,
6026 },
6027 {
6028 .procname = "accept_redirects",
6029 .data = &ipv6_devconf.accept_redirects,
6030 .maxlen = sizeof(int),
6031 .mode = 0644,
6032 .proc_handler = proc_dointvec,
6033 },
6034 {
6035 .procname = "autoconf",
6036 .data = &ipv6_devconf.autoconf,
6037 .maxlen = sizeof(int),
6038 .mode = 0644,
6039 .proc_handler = proc_dointvec,
6040 },
6041 {
6042 .procname = "dad_transmits",
6043 .data = &ipv6_devconf.dad_transmits,
6044 .maxlen = sizeof(int),
6045 .mode = 0644,
6046 .proc_handler = proc_dointvec,
6047 },
6048 {
6049 .procname = "router_solicitations",
6050 .data = &ipv6_devconf.rtr_solicits,
6051 .maxlen = sizeof(int),
6052 .mode = 0644,
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07006053 .proc_handler = proc_dointvec_minmax,
6054 .extra1 = &minus_one,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006055 },
6056 {
6057 .procname = "router_solicitation_interval",
6058 .data = &ipv6_devconf.rtr_solicit_interval,
6059 .maxlen = sizeof(int),
6060 .mode = 0644,
6061 .proc_handler = proc_dointvec_jiffies,
6062 },
6063 {
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07006064 .procname = "router_solicitation_max_interval",
6065 .data = &ipv6_devconf.rtr_solicit_max_interval,
6066 .maxlen = sizeof(int),
6067 .mode = 0644,
6068 .proc_handler = proc_dointvec_jiffies,
6069 },
6070 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006071 .procname = "router_solicitation_delay",
6072 .data = &ipv6_devconf.rtr_solicit_delay,
6073 .maxlen = sizeof(int),
6074 .mode = 0644,
6075 .proc_handler = proc_dointvec_jiffies,
6076 },
6077 {
6078 .procname = "force_mld_version",
6079 .data = &ipv6_devconf.force_mld_version,
6080 .maxlen = sizeof(int),
6081 .mode = 0644,
6082 .proc_handler = proc_dointvec,
6083 },
6084 {
6085 .procname = "mldv1_unsolicited_report_interval",
6086 .data =
6087 &ipv6_devconf.mldv1_unsolicited_report_interval,
6088 .maxlen = sizeof(int),
6089 .mode = 0644,
6090 .proc_handler = proc_dointvec_ms_jiffies,
6091 },
6092 {
6093 .procname = "mldv2_unsolicited_report_interval",
6094 .data =
6095 &ipv6_devconf.mldv2_unsolicited_report_interval,
6096 .maxlen = sizeof(int),
6097 .mode = 0644,
6098 .proc_handler = proc_dointvec_ms_jiffies,
6099 },
6100 {
6101 .procname = "use_tempaddr",
6102 .data = &ipv6_devconf.use_tempaddr,
6103 .maxlen = sizeof(int),
6104 .mode = 0644,
6105 .proc_handler = proc_dointvec,
6106 },
6107 {
6108 .procname = "temp_valid_lft",
6109 .data = &ipv6_devconf.temp_valid_lft,
6110 .maxlen = sizeof(int),
6111 .mode = 0644,
6112 .proc_handler = proc_dointvec,
6113 },
6114 {
6115 .procname = "temp_prefered_lft",
6116 .data = &ipv6_devconf.temp_prefered_lft,
6117 .maxlen = sizeof(int),
6118 .mode = 0644,
6119 .proc_handler = proc_dointvec,
6120 },
6121 {
6122 .procname = "regen_max_retry",
6123 .data = &ipv6_devconf.regen_max_retry,
6124 .maxlen = sizeof(int),
6125 .mode = 0644,
6126 .proc_handler = proc_dointvec,
6127 },
6128 {
6129 .procname = "max_desync_factor",
6130 .data = &ipv6_devconf.max_desync_factor,
6131 .maxlen = sizeof(int),
6132 .mode = 0644,
6133 .proc_handler = proc_dointvec,
6134 },
6135 {
6136 .procname = "max_addresses",
6137 .data = &ipv6_devconf.max_addresses,
6138 .maxlen = sizeof(int),
6139 .mode = 0644,
6140 .proc_handler = proc_dointvec,
6141 },
6142 {
6143 .procname = "accept_ra_defrtr",
6144 .data = &ipv6_devconf.accept_ra_defrtr,
6145 .maxlen = sizeof(int),
6146 .mode = 0644,
6147 .proc_handler = proc_dointvec,
6148 },
6149 {
6150 .procname = "accept_ra_min_hop_limit",
6151 .data = &ipv6_devconf.accept_ra_min_hop_limit,
6152 .maxlen = sizeof(int),
6153 .mode = 0644,
6154 .proc_handler = proc_dointvec,
6155 },
6156 {
6157 .procname = "accept_ra_pinfo",
6158 .data = &ipv6_devconf.accept_ra_pinfo,
6159 .maxlen = sizeof(int),
6160 .mode = 0644,
6161 .proc_handler = proc_dointvec,
6162 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08006163#ifdef CONFIG_IPV6_ROUTER_PREF
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006164 {
6165 .procname = "accept_ra_rtr_pref",
6166 .data = &ipv6_devconf.accept_ra_rtr_pref,
6167 .maxlen = sizeof(int),
6168 .mode = 0644,
6169 .proc_handler = proc_dointvec,
6170 },
6171 {
6172 .procname = "router_probe_interval",
6173 .data = &ipv6_devconf.rtr_probe_interval,
6174 .maxlen = sizeof(int),
6175 .mode = 0644,
6176 .proc_handler = proc_dointvec_jiffies,
6177 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08006178#ifdef CONFIG_IPV6_ROUTE_INFO
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006179 {
Joel Scherpelzbbea1242017-03-22 18:19:04 +09006180 .procname = "accept_ra_rt_info_min_plen",
6181 .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
6182 .maxlen = sizeof(int),
6183 .mode = 0644,
6184 .proc_handler = proc_dointvec,
6185 },
6186 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006187 .procname = "accept_ra_rt_info_max_plen",
6188 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
6189 .maxlen = sizeof(int),
6190 .mode = 0644,
6191 .proc_handler = proc_dointvec,
6192 },
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08006193#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08006194#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006195 {
6196 .procname = "proxy_ndp",
6197 .data = &ipv6_devconf.proxy_ndp,
6198 .maxlen = sizeof(int),
6199 .mode = 0644,
6200 .proc_handler = addrconf_sysctl_proxy_ndp,
6201 },
6202 {
6203 .procname = "accept_source_route",
6204 .data = &ipv6_devconf.accept_source_route,
6205 .maxlen = sizeof(int),
6206 .mode = 0644,
6207 .proc_handler = proc_dointvec,
6208 },
Neil Horman95c385b2007-04-25 17:08:10 -07006209#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006210 {
6211 .procname = "optimistic_dad",
6212 .data = &ipv6_devconf.optimistic_dad,
6213 .maxlen = sizeof(int),
6214 .mode = 0644,
6215 .proc_handler = proc_dointvec,
6216 },
6217 {
6218 .procname = "use_optimistic",
6219 .data = &ipv6_devconf.use_optimistic,
6220 .maxlen = sizeof(int),
6221 .mode = 0644,
6222 .proc_handler = proc_dointvec,
6223 },
Neil Horman95c385b2007-04-25 17:08:10 -07006224#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006225#ifdef CONFIG_IPV6_MROUTE
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006226 {
6227 .procname = "mc_forwarding",
6228 .data = &ipv6_devconf.mc_forwarding,
6229 .maxlen = sizeof(int),
6230 .mode = 0444,
6231 .proc_handler = proc_dointvec,
6232 },
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006233#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006234 {
6235 .procname = "disable_ipv6",
6236 .data = &ipv6_devconf.disable_ipv6,
6237 .maxlen = sizeof(int),
6238 .mode = 0644,
6239 .proc_handler = addrconf_sysctl_disable,
6240 },
6241 {
6242 .procname = "accept_dad",
6243 .data = &ipv6_devconf.accept_dad,
6244 .maxlen = sizeof(int),
6245 .mode = 0644,
6246 .proc_handler = proc_dointvec,
6247 },
6248 {
6249 .procname = "force_tllao",
6250 .data = &ipv6_devconf.force_tllao,
6251 .maxlen = sizeof(int),
6252 .mode = 0644,
6253 .proc_handler = proc_dointvec
6254 },
6255 {
6256 .procname = "ndisc_notify",
6257 .data = &ipv6_devconf.ndisc_notify,
6258 .maxlen = sizeof(int),
6259 .mode = 0644,
6260 .proc_handler = proc_dointvec
6261 },
6262 {
6263 .procname = "suppress_frag_ndisc",
6264 .data = &ipv6_devconf.suppress_frag_ndisc,
6265 .maxlen = sizeof(int),
6266 .mode = 0644,
6267 .proc_handler = proc_dointvec
6268 },
6269 {
6270 .procname = "accept_ra_from_local",
6271 .data = &ipv6_devconf.accept_ra_from_local,
6272 .maxlen = sizeof(int),
6273 .mode = 0644,
6274 .proc_handler = proc_dointvec,
6275 },
6276 {
6277 .procname = "accept_ra_mtu",
6278 .data = &ipv6_devconf.accept_ra_mtu,
6279 .maxlen = sizeof(int),
6280 .mode = 0644,
6281 .proc_handler = proc_dointvec,
6282 },
6283 {
6284 .procname = "stable_secret",
6285 .data = &ipv6_devconf.stable_secret,
6286 .maxlen = IPV6_MAX_STRLEN,
6287 .mode = 0600,
6288 .proc_handler = addrconf_sysctl_stable_secret,
6289 },
6290 {
6291 .procname = "use_oif_addrs_only",
6292 .data = &ipv6_devconf.use_oif_addrs_only,
6293 .maxlen = sizeof(int),
6294 .mode = 0644,
6295 .proc_handler = proc_dointvec,
6296 },
6297 {
6298 .procname = "ignore_routes_with_linkdown",
6299 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6300 .maxlen = sizeof(int),
6301 .mode = 0644,
6302 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6303 },
6304 {
6305 .procname = "drop_unicast_in_l2_multicast",
6306 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6307 .maxlen = sizeof(int),
6308 .mode = 0644,
6309 .proc_handler = proc_dointvec,
6310 },
6311 {
6312 .procname = "drop_unsolicited_na",
6313 .data = &ipv6_devconf.drop_unsolicited_na,
6314 .maxlen = sizeof(int),
6315 .mode = 0644,
6316 .proc_handler = proc_dointvec,
6317 },
6318 {
6319 .procname = "keep_addr_on_down",
6320 .data = &ipv6_devconf.keep_addr_on_down,
6321 .maxlen = sizeof(int),
6322 .mode = 0644,
6323 .proc_handler = proc_dointvec,
David Ahernf1705ec2016-02-24 09:25:37 -08006324
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006325 },
6326 {
David Lebrun1ababeb2016-11-08 14:57:39 +01006327 .procname = "seg6_enabled",
6328 .data = &ipv6_devconf.seg6_enabled,
6329 .maxlen = sizeof(int),
6330 .mode = 0644,
6331 .proc_handler = proc_dointvec,
6332 },
David Lebrunbf355b82016-11-08 14:57:42 +01006333#ifdef CONFIG_IPV6_SEG6_HMAC
6334 {
6335 .procname = "seg6_require_hmac",
6336 .data = &ipv6_devconf.seg6_require_hmac,
6337 .maxlen = sizeof(int),
6338 .mode = 0644,
6339 .proc_handler = proc_dointvec,
6340 },
6341#endif
David Lebrun1ababeb2016-11-08 14:57:39 +01006342 {
Erik Nordmarkadc176c2016-12-02 14:00:08 -08006343 .procname = "enhanced_dad",
6344 .data = &ipv6_devconf.enhanced_dad,
6345 .maxlen = sizeof(int),
6346 .mode = 0644,
6347 .proc_handler = proc_dointvec,
6348 },
6349 {
Felix Jiad35a00b2017-01-26 16:59:17 +13006350 .procname = "addr_gen_mode",
6351 .data = &ipv6_devconf.addr_gen_mode,
6352 .maxlen = sizeof(int),
6353 .mode = 0644,
6354 .proc_handler = addrconf_sysctl_addr_gen_mode,
6355 },
6356 {
David Forsterdf789fe2017-02-23 16:27:18 +00006357 .procname = "disable_policy",
6358 .data = &ipv6_devconf.disable_policy,
6359 .maxlen = sizeof(int),
6360 .mode = 0644,
6361 .proc_handler = addrconf_sysctl_disable_policy,
6362 },
6363 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006364 /* sentinel */
6365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366};
6367
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08006368static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006369 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370{
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006371 int i, ifindex;
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006372 struct ctl_table *table;
Eric W. Biederman6105e292012-04-19 13:41:24 +00006373 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11006374
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006375 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6376 if (!table)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006377 goto out;
6378
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006379 for (i = 0; table[i].data; i++) {
6380 table[i].data += (char *)p - (char *)&ipv6_devconf;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006381 /* If one of these is already set, then it is not safe to
6382 * overwrite either of them: this makes proc_dointvec_minmax
6383 * usable.
6384 */
6385 if (!table[i].extra1 && !table[i].extra2) {
6386 table[i].extra1 = idev; /* embedded; no ref */
6387 table[i].extra2 = net;
6388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390
Eric W. Biederman6105e292012-04-19 13:41:24 +00006391 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006393 p->sysctl_header = register_net_sysctl(net, path, table);
6394 if (!p->sysctl_header)
Eric W. Biederman6105e292012-04-19 13:41:24 +00006395 goto free;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006396
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006397 if (!strcmp(dev_name, "all"))
6398 ifindex = NETCONFA_IFINDEX_ALL;
6399 else if (!strcmp(dev_name, "default"))
6400 ifindex = NETCONFA_IFINDEX_DEFAULT;
6401 else
6402 ifindex = idev->dev->ifindex;
David Ahern85b3daa2017-03-28 14:28:04 -07006403 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
6404 ifindex, p);
Pavel Emelyanov95897312008-01-10 17:41:45 -08006405 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006407free:
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006408 kfree(table);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006409out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08006410 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411}
6412
David Ahern23452172017-03-28 14:28:05 -07006413static void __addrconf_sysctl_unregister(struct net *net,
6414 struct ipv6_devconf *p, int ifindex)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006415{
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006416 struct ctl_table *table;
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006417
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006418 if (!p->sysctl_header)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006419 return;
6420
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006421 table = p->sysctl_header->ctl_table_arg;
6422 unregister_net_sysctl_table(p->sysctl_header);
6423 p->sysctl_header = NULL;
6424 kfree(table);
David Ahern23452172017-03-28 14:28:05 -07006425
6426 inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006427}
6428
WANG Conga317a2f2014-07-25 15:25:09 -07006429static int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006430{
WANG Conga317a2f2014-07-25 15:25:09 -07006431 int err;
6432
6433 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6434 return -EINVAL;
6435
6436 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6437 &ndisc_ifinfo_sysctl_change);
6438 if (err)
6439 return err;
6440 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6441 idev, &idev->cnf);
6442 if (err)
6443 neigh_sysctl_unregister(idev->nd_parms);
6444
6445 return err;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006446}
6447
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006448static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449{
David Ahern23452172017-03-28 14:28:05 -07006450 __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
6451 idev->dev->ifindex);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006452 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453}
6454
6455
6456#endif
6457
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006458static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006459{
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006460 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006461 struct ipv6_devconf *all, *dflt;
6462
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006463 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006464 if (!all)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006465 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006466
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006467 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006468 if (!dflt)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006469 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006470
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006471 /* these will be inherited by all namespaces */
6472 dflt->autoconf = ipv6_defaults.autoconf;
6473 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006474
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006475 dflt->stable_secret.initialized = false;
6476 all->stable_secret.initialized = false;
6477
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006478 net->ipv6.devconf_all = all;
6479 net->ipv6.devconf_dflt = dflt;
6480
6481#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006482 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006483 if (err < 0)
6484 goto err_reg_all;
6485
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006486 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006487 if (err < 0)
6488 goto err_reg_dflt;
6489#endif
6490 return 0;
6491
6492#ifdef CONFIG_SYSCTL
6493err_reg_dflt:
David Ahern23452172017-03-28 14:28:05 -07006494 __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006495err_reg_all:
6496 kfree(dflt);
6497#endif
6498err_alloc_dflt:
6499 kfree(all);
6500err_alloc_all:
6501 return err;
6502}
6503
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006504static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006505{
6506#ifdef CONFIG_SYSCTL
David Ahern23452172017-03-28 14:28:05 -07006507 __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
6508 NETCONFA_IFINDEX_DEFAULT);
6509 __addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
6510 NETCONFA_IFINDEX_ALL);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006511#endif
zhuyj73cf0e922014-11-26 10:25:58 +08006512 kfree(net->ipv6.devconf_dflt);
6513 kfree(net->ipv6.devconf_all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006514}
6515
6516static struct pernet_operations addrconf_ops = {
6517 .init = addrconf_init_net,
6518 .exit = addrconf_exit_net,
6519};
6520
Daniel Borkmann207895f2015-01-29 12:15:03 +01006521static struct rtnl_af_ops inet6_ops __read_mostly = {
Thomas Grafb382b192010-11-16 04:33:57 +00006522 .family = AF_INET6,
6523 .fill_link_af = inet6_fill_link_af,
6524 .get_link_af_size = inet6_get_link_af_size,
Daniel Borkmann11b1f822015-02-05 14:39:11 +01006525 .validate_link_af = inet6_validate_link_af,
Daniel Borkmannf53adae2013-04-08 04:01:30 +00006526 .set_link_af = inet6_set_link_af,
Thomas Grafb382b192010-11-16 04:33:57 +00006527};
6528
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529/*
6530 * Init / cleanup code
6531 */
6532
6533int __init addrconf_init(void)
6534{
WANG Conga317a2f2014-07-25 15:25:09 -07006535 struct inet6_dev *idev;
stephen hemmingerc2e21292010-03-17 20:31:10 +00006536 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006537
Stephen Hemmingere21e8462010-03-20 16:09:01 -07006538 err = ipv6_addr_label_init();
6539 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00006540 pr_crit("%s: cannot initialize default policy table: %d\n",
6541 __func__, err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006542 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006545 err = register_pernet_subsys(&addrconf_ops);
6546 if (err < 0)
6547 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006548
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006549 addrconf_wq = create_workqueue("ipv6_addrconf");
6550 if (!addrconf_wq) {
6551 err = -ENOMEM;
6552 goto out_nowq;
6553 }
6554
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555 /* The addrconf netdev notifier requires that loopback_dev
6556 * has it's ipv6 private information allocated and setup
6557 * before it can bring up and give link-local addresses
6558 * to other devices which are up.
6559 *
6560 * Unfortunately, loopback_dev is not necessarily the first
6561 * entry in the global dev_base list of net devices. In fact,
6562 * it is likely to be the very last entry on that list.
6563 * So this causes the notifier registry below to try and
6564 * give link-local addresses to all devices besides loopback_dev
6565 * first, then loopback_dev, which cases all the non-loopback_dev
6566 * devices to fail to get a link-local address.
6567 *
6568 * So, as a temporary fix, allocate the ipv6 structure for
6569 * loopback_dev first by hand.
6570 * Longer term, all of the dependencies ipv6 has upon the loopback
6571 * device and it being up should be removed.
6572 */
6573 rtnl_lock();
WANG Conga317a2f2014-07-25 15:25:09 -07006574 idev = ipv6_add_dev(init_net.loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575 rtnl_unlock();
WANG Conga317a2f2014-07-25 15:25:09 -07006576 if (IS_ERR(idev)) {
6577 err = PTR_ERR(idev);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006578 goto errlo;
WANG Conga317a2f2014-07-25 15:25:09 -07006579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580
WANG Cong2f460932017-05-03 22:07:31 -07006581 ip6_route_init_special_entries();
6582
stephen hemmingerc2e21292010-03-17 20:31:10 +00006583 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6584 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6585
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586 register_netdevice_notifier(&ipv6_dev_notf);
6587
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006588 addrconf_verify();
Thomas Grafc127ea22007-03-22 11:58:32 -07006589
stephen hemminger3678a9d2013-12-30 10:41:32 -08006590 rtnl_af_register(&inet6_ops);
Thomas Grafb382b192010-11-16 04:33:57 +00006591
Greg Rosec7ac8672011-06-10 01:27:09 +00006592 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6593 NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006594 if (err < 0)
6595 goto errout;
6596
6597 /* Only the first call to __rtnl_register can fail */
Greg Rosec7ac8672011-06-10 01:27:09 +00006598 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
6599 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
6600 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6601 inet6_dump_ifaddr, NULL);
6602 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6603 inet6_dump_ifmcaddr, NULL);
6604 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6605 inet6_dump_ifacaddr, NULL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +00006606 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
Nicolas Dichtel7a674202013-03-05 23:42:06 +00006607 inet6_netconf_dump_devconf, NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006608
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006609 ipv6_addr_label_rtnl_register();
6610
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07006612errout:
Thomas Grafb382b192010-11-16 04:33:57 +00006613 rtnl_af_unregister(&inet6_ops);
Thomas Grafc127ea22007-03-22 11:58:32 -07006614 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006615errlo:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006616 destroy_workqueue(addrconf_wq);
6617out_nowq:
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006618 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006619out_addrlabel:
6620 ipv6_addr_label_cleanup();
6621out:
Thomas Grafc127ea22007-03-22 11:58:32 -07006622 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623}
6624
Daniel Lezcano09f77092007-12-13 05:34:58 -08006625void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006627 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628 int i;
6629
6630 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006631 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006632 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633
6634 rtnl_lock();
6635
Thomas Grafb382b192010-11-16 04:33:57 +00006636 __rtnl_af_unregister(&inet6_ops);
6637
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006638 /* clean dev list */
6639 for_each_netdev(&init_net, dev) {
6640 if (__in6_dev_get(dev) == NULL)
6641 continue;
6642 addrconf_ifdown(dev, 1);
6643 }
6644 addrconf_ifdown(init_net.loopback_dev, 2);
6645
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647 * Check hash table.
6648 */
stephen hemminger5c578aed2010-03-17 20:31:11 +00006649 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00006650 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6651 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578aed2010-03-17 20:31:11 +00006652 spin_unlock_bh(&addrconf_hash_lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006653 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 rtnl_unlock();
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006655
6656 destroy_workqueue(addrconf_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657}