blob: 686c92375e81d50787adbfb423afba76903948ad [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 Liu8013d1d72015-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 Liu8013d1d72015-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 Dubroca2a189f9e2015-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 Hideaki072047e42005-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 Hideaki072047e42005-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 Hideaki072047e42005-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 Weber45bb00602010-02-25 23:28:58 +00001382 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e42005-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 Hideaki072047e42005-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 Hideaki072047e42005-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 Hideaki072047e42005-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 Hideaki072047e42005-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 Hideaki072047e42005-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 Hideaki072047e42005-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 Lau1f56a01f2015-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 Hormane6bff9952012-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 TAKAMIYA0778769d2006-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 TAKAMIYA0778769d2006-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 TAKAMIYA0778769d2006-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 TAKAMIYA0778769d2006-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);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003372 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07003373 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003375 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003376 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07003377 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003378 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -07003379 if (IS_ERR(idev))
3380 return notifier_from_errno(PTR_ERR(idev));
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003381 }
3382 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003383
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003384 case NETDEV_CHANGEMTU:
3385 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3386 if (dev->mtu < IPV6_MIN_MTU) {
3387 addrconf_ifdown(dev, 1);
3388 break;
3389 }
3390
3391 if (idev) {
3392 rt6_mtu_change(dev, dev->mtu);
3393 idev->cnf.mtu6 = dev->mtu;
3394 break;
3395 }
3396
3397 /* allocate new idev */
3398 idev = ipv6_add_dev(dev);
3399 if (IS_ERR(idev))
3400 break;
3401
3402 /* device is still not ready */
3403 if (!(idev->if_flags & IF_READY))
3404 break;
3405
3406 run_pending = 1;
3407
3408 /* fall through */
3409
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003411 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07003412 if (dev->flags & IFF_SLAVE)
3413 break;
3414
WANG Cong3ce62a82014-09-11 15:07:16 -07003415 if (idev && idev->cnf.disable_ipv6)
3416 break;
3417
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003418 if (event == NETDEV_UP) {
David Ahern38bd10c2016-04-21 20:56:12 -07003419 /* restore routes for permanent addresses */
3420 addrconf_permanent_addr(dev);
3421
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07003422 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003423 /* device is not ready yet. */
Joe Perchesf3213832012-05-15 14:11:53 +00003424 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003425 dev->name);
3426 break;
3427 }
Kristian Slavov99081042006-02-08 16:10:53 -08003428
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11003429 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3430 idev = ipv6_add_dev(dev);
3431
WANG Conga317a2f2014-07-25 15:25:09 -07003432 if (!IS_ERR_OR_NULL(idev)) {
Kristian Slavov99081042006-02-08 16:10:53 -08003433 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08003434 run_pending = 1;
3435 }
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003436 } else if (event == NETDEV_CHANGE) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07003437 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003438 /* device is still not ready. */
3439 break;
3440 }
3441
3442 if (idev) {
Linus Lüssinga088d1d2017-02-03 08:11:03 +01003443 if (idev->if_flags & IF_READY) {
3444 /* device is already configured -
3445 * but resend MLD reports, we might
3446 * have roamed and need to update
3447 * multicast snooping switches
3448 */
3449 ipv6_mc_up(idev);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003450 break;
Linus Lüssinga088d1d2017-02-03 08:11:03 +01003451 }
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003452 idev->if_flags |= IF_READY;
3453 }
3454
Joe Perchesf3213832012-05-15 14:11:53 +00003455 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3456 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003457
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003458 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003459 }
3460
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003461 switch (dev->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +00003462#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 case ARPHRD_SIT:
3464 addrconf_sit_config(dev);
3465 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07003466#endif
Amerigo Wang07a93622012-10-29 16:23:10 +00003467#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003468 case ARPHRD_IPGRE:
3469 addrconf_gre_config(dev);
3470 break;
3471#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 case ARPHRD_LOOPBACK:
3473 init_loopback(dev);
3474 break;
3475
3476 default:
3477 addrconf_dev_config(dev);
3478 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003479 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003480
WANG Conga317a2f2014-07-25 15:25:09 -07003481 if (!IS_ERR_OR_NULL(idev)) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003482 if (run_pending)
3483 addrconf_dad_run(idev);
3484
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003485 /*
3486 * If the MTU changed during the interface down,
3487 * when the interface up, the changed MTU must be
3488 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003490 if (idev->cnf.mtu6 != dev->mtu &&
3491 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 rt6_mtu_change(dev, dev->mtu);
3493 idev->cnf.mtu6 = dev->mtu;
3494 }
3495 idev->tstamp = jiffies;
3496 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003497
3498 /*
3499 * If the changed mtu during down is lower than
3500 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 */
3502 if (dev->mtu < IPV6_MIN_MTU)
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003503 addrconf_ifdown(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 }
3505 break;
3506
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 case NETDEV_DOWN:
3508 case NETDEV_UNREGISTER:
3509 /*
3510 * Remove all addresses from this interface.
3511 */
3512 addrconf_ifdown(dev, event != NETDEV_DOWN);
3513 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003514
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07003517 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003518 addrconf_sysctl_unregister(idev);
WANG Conga317a2f2014-07-25 15:25:09 -07003519 err = addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07003520 if (err)
3521 return notifier_from_errno(err);
WANG Conga317a2f2014-07-25 15:25:09 -07003522 err = snmp6_register_dev(idev);
3523 if (err) {
3524 addrconf_sysctl_unregister(idev);
3525 return notifier_from_errno(err);
3526 }
Stephen Hemminger5632c512007-04-28 21:16:39 -07003527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003529
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003530 case NETDEV_PRE_TYPE_CHANGE:
3531 case NETDEV_POST_TYPE_CHANGE:
Andrew Lunn3ef09522015-12-03 21:12:32 +01003532 if (idev)
3533 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07003534 break;
David Ahern4f7f34e2016-04-07 11:10:41 -07003535
3536 case NETDEV_CHANGEUPPER:
3537 info = ptr;
3538
3539 /* flush all routes if dev is linked to or unlinked from
3540 * an L3 master device (e.g., VRF)
3541 */
3542 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3543 addrconf_ifdown(dev, 0);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
3546 return NOTIFY_OK;
3547}
3548
3549/*
3550 * addrconf module should be notified of a device going up
3551 */
3552static struct notifier_block ipv6_dev_notf = {
3553 .notifier_call = addrconf_notify,
WANG Cong242d3a42017-05-08 10:12:13 -07003554 .priority = ADDRCONF_NOTIFY_PRIORITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555};
3556
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003557static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07003558{
3559 struct inet6_dev *idev;
3560 ASSERT_RTNL();
3561
3562 idev = __in6_dev_get(dev);
3563
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003564 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003565 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003566 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003567 ipv6_mc_unmap(idev);
3568}
3569
David Ahern70af9212016-04-08 12:01:21 -07003570static bool addr_is_local(const struct in6_addr *addr)
3571{
3572 return ipv6_addr_type(addr) &
3573 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3574}
3575
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576static int addrconf_ifdown(struct net_device *dev, int how)
3577{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003578 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003579 struct inet6_dev *idev;
David Ahernf1705ec2016-02-24 09:25:37 -08003580 struct inet6_ifaddr *ifa, *tmp;
3581 struct list_head del_list;
3582 int _keep_addr;
3583 bool keep_addr;
David S. Miller73a8bd72011-01-23 23:27:15 -08003584 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
3586 ASSERT_RTNL();
3587
David S. Miller73a8bd72011-01-23 23:27:15 -08003588 rt6_ifdown(net, dev);
3589 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
3591 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003592 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 return -ENODEV;
3594
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003595 /*
3596 * Step 1: remove reference to ipv6 device from parent device.
3597 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003599 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003601
3602 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003603 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
3605 /* Step 1.5: remove snmp6 entry */
3606 snmp6_unregister_dev(idev);
3607
3608 }
3609
David Ahernf1705ec2016-02-24 09:25:37 -08003610 /* aggregate the system setting and interface setting */
3611 _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3612 if (!_keep_addr)
3613 _keep_addr = idev->cnf.keep_addr_on_down;
3614
3615 /* combine the user config with event to determine if permanent
3616 * addresses are to be removed from address hash table
3617 */
Mike Manningbc561632016-08-12 12:02:38 +01003618 keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003619
David S. Miller73a8bd72011-01-23 23:27:15 -08003620 /* Step 2: clear hash table */
3621 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3622 struct hlist_head *h = &inet6_addr_lst[i];
David S. Miller73a8bd72011-01-23 23:27:15 -08003623
3624 spin_lock_bh(&addrconf_hash_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01003625restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08003626 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
David S. Miller73a8bd72011-01-23 23:27:15 -08003627 if (ifa->idev == idev) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003628 addrconf_del_dad_work(ifa);
David Ahernf1705ec2016-02-24 09:25:37 -08003629 /* combined flag + permanent flag decide if
3630 * address is retained on a down event
3631 */
3632 if (!keep_addr ||
David Ahern70af9212016-04-08 12:01:21 -07003633 !(ifa->flags & IFA_F_PERMANENT) ||
3634 addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003635 hlist_del_init_rcu(&ifa->addr_lst);
3636 goto restart;
3637 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003638 }
3639 }
3640 spin_unlock_bh(&addrconf_hash_lock);
3641 }
3642
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 write_lock_bh(&idev->lock);
3644
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003645 addrconf_del_rs_timer(idev);
3646
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003647 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003648 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003649 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003651 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00003652 while (!list_empty(&idev->tempaddr_list)) {
3653 ifa = list_first_entry(&idev->tempaddr_list,
3654 struct inet6_ifaddr, tmp_list);
3655 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 write_unlock_bh(&idev->lock);
3657 spin_lock_bh(&ifa->lock);
3658
3659 if (ifa->ifpub) {
3660 in6_ifa_put(ifa->ifpub);
3661 ifa->ifpub = NULL;
3662 }
3663 spin_unlock_bh(&ifa->lock);
3664 in6_ifa_put(ifa);
3665 write_lock_bh(&idev->lock);
3666 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003667
David Ahernf1705ec2016-02-24 09:25:37 -08003668 /* re-combine the user config with event to determine if permanent
3669 * addresses are to be removed from the interface list
3670 */
Mike Manningbc561632016-08-12 12:02:38 +01003671 keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003672
3673 INIT_LIST_HEAD(&del_list);
3674 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
David Ahern38bd10c2016-04-21 20:56:12 -07003675 struct rt6_info *rt = NULL;
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003676 bool keep;
David Ahern38bd10c2016-04-21 20:56:12 -07003677
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003678 addrconf_del_dad_work(ifa);
stephen hemminger84e8b802010-03-02 13:32:46 +00003679
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003680 keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3681 !addr_is_local(&ifa->addr);
3682 if (!keep)
3683 list_move(&ifa->if_list, &del_list);
3684
David S. Miller73a8bd72011-01-23 23:27:15 -08003685 write_unlock_bh(&idev->lock);
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003686 spin_lock_bh(&ifa->lock);
David Ahernf1705ec2016-02-24 09:25:37 -08003687
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003688 if (keep) {
David Ahernf1705ec2016-02-24 09:25:37 -08003689 /* set state to skip the notifier below */
3690 state = INET6_IFADDR_STATE_DEAD;
David Ahern6d717132017-05-02 14:43:44 -07003691 ifa->state = INET6_IFADDR_STATE_PREDAD;
David Ahernf1705ec2016-02-24 09:25:37 -08003692 if (!(ifa->flags & IFA_F_NODAD))
3693 ifa->flags |= IFA_F_TENTATIVE;
David Ahern38bd10c2016-04-21 20:56:12 -07003694
3695 rt = ifa->rt;
3696 ifa->rt = NULL;
David Ahernf1705ec2016-02-24 09:25:37 -08003697 } else {
3698 state = ifa->state;
3699 ifa->state = INET6_IFADDR_STATE_DEAD;
David Ahernf1705ec2016-02-24 09:25:37 -08003700 }
3701
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003702 spin_unlock_bh(&ifa->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003703
David Ahern38bd10c2016-04-21 20:56:12 -07003704 if (rt)
3705 ip6_del_rt(rt);
3706
David S. Miller73a8bd72011-01-23 23:27:15 -08003707 if (state != INET6_IFADDR_STATE_DEAD) {
3708 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Cong Wangf88c91d2013-04-14 23:18:43 +08003709 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
Mike Manningea06f712016-07-22 18:32:11 +01003710 } else {
3711 if (idev->cnf.forwarding)
3712 addrconf_leave_anycast(ifa);
3713 addrconf_leave_solict(ifa->idev, &ifa->addr);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00003714 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003715
David S. Miller73a8bd72011-01-23 23:27:15 -08003716 write_lock_bh(&idev->lock);
3717 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003718
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 write_unlock_bh(&idev->lock);
3720
David Ahernf1705ec2016-02-24 09:25:37 -08003721 /* now clean up addresses to be removed */
3722 while (!list_empty(&del_list)) {
3723 ifa = list_first_entry(&del_list,
3724 struct inet6_ifaddr, if_list);
3725 list_del(&ifa->if_list);
3726
3727 in6_ifa_put(ifa);
3728 }
3729
Sabrina Dubroca381f4dca2014-09-10 23:23:02 +02003730 /* Step 5: Discard anycast and multicast list */
3731 if (how) {
3732 ipv6_ac_destroy_dev(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 ipv6_mc_destroy_dev(idev);
Sabrina Dubroca381f4dca2014-09-10 23:23:02 +02003734 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 ipv6_mc_down(idev);
Sabrina Dubroca381f4dca2014-09-10 23:23:02 +02003736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003739
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003740 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003741 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003742 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 neigh_parms_release(&nd_tbl, idev->nd_parms);
3744 neigh_ifdown(&nd_tbl, dev);
3745 in6_dev_put(idev);
3746 }
3747 return 0;
3748}
3749
3750static void addrconf_rs_timer(unsigned long data)
3751{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003752 struct inet6_dev *idev = (struct inet6_dev *)data;
Cong Wangcaf92bc2013-08-31 13:44:32 +08003753 struct net_device *dev = idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003754 struct in6_addr lladdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003756 write_lock(&idev->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00003757 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 goto out;
3759
Shmulik Ladkani9ba2add2012-12-02 01:44:53 +00003760 if (!ipv6_accept_ra(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003762
3763 /* Announcement received after solicitation was sent */
3764 if (idev->if_flags & IF_RA_RCVD)
3765 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003767 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
Cong Wangcaf92bc2013-08-31 13:44:32 +08003768 write_unlock(&idev->lock);
3769 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3770 ndisc_send_rs(dev, &lladdr,
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003771 &in6addr_linklocal_allrouters);
3772 else
Cong Wangcaf92bc2013-08-31 13:44:32 +08003773 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774
Cong Wangcaf92bc2013-08-31 13:44:32 +08003775 write_lock(&idev->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003776 idev->rs_interval = rfc3315_s14_backoff_update(
3777 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003778 /* The wait after the last probe can be shorter */
3779 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3780 idev->cnf.rtr_solicits) ?
3781 idev->cnf.rtr_solicit_delay :
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003782 idev->rs_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 /*
3785 * Note: we do not support deprecated "all on-link"
3786 * assumption any longer.
3787 */
Joe Perches91df42b2012-05-15 14:11:54 +00003788 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 }
3790
3791out:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003792 write_unlock(&idev->lock);
Cong Wangcaf92bc2013-08-31 13:44:32 +08003793put:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003794 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795}
3796
3797/*
3798 * Duplicate Address Detection
3799 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003800static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3801{
3802 unsigned long rand_num;
3803 struct inet6_dev *idev = ifp->idev;
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003804 u64 nonce;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003805
Neil Horman95c385b2007-04-25 17:08:10 -07003806 if (ifp->flags & IFA_F_OPTIMISTIC)
3807 rand_num = 0;
3808 else
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003809 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
Neil Horman95c385b2007-04-25 17:08:10 -07003810
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003811 nonce = 0;
3812 if (idev->cnf.enhanced_dad ||
3813 dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
3814 do
3815 get_random_bytes(&nonce, 6);
3816 while (nonce == 0);
3817 }
3818 ifp->dad_nonce = nonce;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003819 ifp->dad_probes = idev->cnf.dad_transmits;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003820 addrconf_mod_dad_work(ifp, rand_num);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003821}
3822
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003823static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824{
3825 struct inet6_dev *idev = ifp->idev;
3826 struct net_device *dev = idev->dev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003827 bool bump_id, notify = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
3829 addrconf_join_solict(dev, &ifp->addr);
3830
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003831 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
3833 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003834 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003835 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837
3838 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09003839 idev->cnf.accept_dad < 1 ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003840 !(ifp->flags&IFA_F_TENTATIVE) ||
3841 ifp->flags & IFA_F_NODAD) {
Paolo Abeni764d3be2016-11-22 16:57:40 +01003842 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003843 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003844 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 read_unlock_bh(&idev->lock);
3846
Paolo Abeni764d3be2016-11-22 16:57:40 +01003847 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 return;
3849 }
3850
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003851 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003852 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003853 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003854 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003855 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003856 * - keep it tentative if it is a permanent address.
3857 * - otherwise, kill it.
3858 */
3859 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003860 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003861 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003862 }
Neil Horman95c385b2007-04-25 17:08:10 -07003863
3864 /*
3865 * Optimistic nodes can start receiving
3866 * Frames right away
3867 */
Erik Kline7fd25612014-10-28 18:11:14 +09003868 if (ifp->flags & IFA_F_OPTIMISTIC) {
Neil Horman95c385b2007-04-25 17:08:10 -07003869 ip6_ins_rt(ifp->rt);
Erik Kline7fd25612014-10-28 18:11:14 +09003870 if (ipv6_use_optimistic_addr(idev)) {
3871 /* Because optimistic nodes can use this address,
3872 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3873 */
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003874 notify = true;
Erik Kline7fd25612014-10-28 18:11:14 +09003875 }
3876 }
Neil Horman95c385b2007-04-25 17:08:10 -07003877
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003878 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003880 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 read_unlock_bh(&idev->lock);
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003882 if (notify)
3883 ipv6_ifa_notify(RTM_NEWADDR, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884}
3885
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003886static void addrconf_dad_start(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003888 bool begin_dad = false;
3889
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003890 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003891 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3892 ifp->state = INET6_IFADDR_STATE_PREDAD;
3893 begin_dad = true;
3894 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003895 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003896
3897 if (begin_dad)
3898 addrconf_mod_dad_work(ifp, 0);
3899}
3900
3901static void addrconf_dad_work(struct work_struct *w)
3902{
3903 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3904 struct inet6_ifaddr,
3905 dad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 struct inet6_dev *idev = ifp->idev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003907 bool bump_id, disable_ipv6 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 struct in6_addr mcaddr;
3909
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003910 enum {
3911 DAD_PROCESS,
3912 DAD_BEGIN,
3913 DAD_ABORT,
3914 } action = DAD_PROCESS;
3915
3916 rtnl_lock();
3917
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003918 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003919 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3920 action = DAD_BEGIN;
3921 ifp->state = INET6_IFADDR_STATE_DAD;
3922 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3923 action = DAD_ABORT;
3924 ifp->state = INET6_IFADDR_STATE_POSTDAD;
Mike Manning85b51b12016-08-18 14:39:40 +01003925
3926 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6 &&
3927 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
3928 struct in6_addr addr;
3929
3930 addr.s6_addr32[0] = htonl(0xfe800000);
3931 addr.s6_addr32[1] = 0;
3932
3933 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
3934 ipv6_addr_equal(&ifp->addr, &addr)) {
3935 /* DAD failed for link-local based on MAC */
3936 idev->cnf.disable_ipv6 = 1;
3937
3938 pr_info("%s: IPv6 being disabled!\n",
3939 ifp->idev->dev->name);
3940 disable_ipv6 = true;
3941 }
3942 }
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003943 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003944 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003945
3946 if (action == DAD_BEGIN) {
3947 addrconf_dad_begin(ifp);
3948 goto out;
3949 } else if (action == DAD_ABORT) {
Wei Yongjun751eb6b2016-09-05 16:06:31 +08003950 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003951 addrconf_dad_stop(ifp, 1);
Mike Manning85b51b12016-08-18 14:39:40 +01003952 if (disable_ipv6)
3953 addrconf_ifdown(idev->dev, 0);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003954 goto out;
3955 }
3956
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003957 if (!ifp->dad_probes && addrconf_dad_end(ifp))
Herbert Xuf2344a12010-05-18 15:55:27 -07003958 goto out;
3959
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003960 write_lock_bh(&idev->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003961 if (idev->dead || !(idev->if_flags & IF_READY)) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003962 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 goto out;
3964 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003965
3966 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003967 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3968 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003969 write_unlock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003970 goto out;
3971 }
3972
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003973 if (ifp->dad_probes == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 /*
3975 * DAD was successful
3976 */
3977
Paolo Abeni764d3be2016-11-22 16:57:40 +01003978 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003979 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003980 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003981 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
Paolo Abeni764d3be2016-11-22 16:57:40 +01003983 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984
3985 goto out;
3986 }
3987
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003988 ifp->dad_probes--;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003989 addrconf_mod_dad_work(ifp,
3990 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
stephen hemminger21809fa2010-02-08 19:48:52 +00003991 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003992 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
3994 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003996 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
3997 ifp->dad_nonce);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998out:
3999 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004000 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001}
4002
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004003/* ifp->idev must be at least read locked */
4004static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
4005{
4006 struct inet6_ifaddr *ifpiter;
4007 struct inet6_dev *idev = ifp->idev;
4008
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01004009 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
4010 if (ifpiter->scope > IFA_LINK)
4011 break;
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004012 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
4013 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
4014 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
4015 IFA_F_PERMANENT)
4016 return false;
4017 }
4018 return true;
4019}
4020
Paolo Abeni764d3be2016-11-22 16:57:40 +01004021static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004023 struct net_device *dev = ifp->idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004024 struct in6_addr lladdr;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004025 bool send_rs, send_mld;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004026
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004027 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
4029 /*
4030 * Configure the address for reception. Now it is valid.
4031 */
4032
4033 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4034
Tore Anderson026359b2011-08-28 23:47:33 +00004035 /* If added prefix is link local and we are prepared to process
4036 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 */
4038
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004039 read_lock_bh(&ifp->idev->lock);
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004040 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004041 send_rs = send_mld &&
4042 ipv6_accept_ra(ifp->idev) &&
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004043 ifp->idev->cnf.rtr_solicits != 0 &&
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004044 (dev->flags&IFF_LOOPBACK) == 0;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004045 read_unlock_bh(&ifp->idev->lock);
4046
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004047 /* While dad is in progress mld report's source address is in6_addrany.
4048 * Resend with proper ll now.
4049 */
4050 if (send_mld)
4051 ipv6_mc_dad_complete(ifp->idev);
4052
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004053 if (send_rs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 /*
4055 * If a host as already performed a random delay
4056 * [...] as part of DAD [...] there is no need
4057 * to delay again before sending the first RS
4058 */
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004059 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004060 return;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004061 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004063 write_lock_bh(&ifp->idev->lock);
4064 spin_lock(&ifp->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004065 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4066 ifp->idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004067 ifp->idev->rs_probes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 ifp->idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004069 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004070 spin_unlock(&ifp->lock);
4071 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 }
Paolo Abeni764d3be2016-11-22 16:57:40 +01004073
4074 if (bump_id)
4075 rt_genid_bump_ipv6(dev_net(dev));
Marcus Huewea11a7f72017-02-06 18:34:56 +01004076
4077 /* Make sure that a new temporary address will be created
4078 * before this temporary address becomes deprecated.
4079 */
4080 if (ifp->flags & IFA_F_TEMPORARY)
4081 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082}
4083
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004084static void addrconf_dad_run(struct inet6_dev *idev)
4085{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004086 struct inet6_ifaddr *ifp;
4087
4088 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00004089 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00004090 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07004091 if (ifp->flags & IFA_F_TENTATIVE &&
4092 ifp->state == INET6_IFADDR_STATE_DAD)
4093 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00004094 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004095 }
4096 read_unlock_bh(&idev->lock);
4097}
4098
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099#ifdef CONFIG_PROC_FS
4100struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08004101 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004103 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104};
4105
Mihai Maruseac1d578302012-01-03 23:31:35 +00004106static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107{
4108 struct inet6_ifaddr *ifa = NULL;
4109 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004110 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00004111 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
Mihai Maruseac1d578302012-01-03 23:31:35 +00004113 /* initial bucket if pos is 0 */
4114 if (pos == 0) {
4115 state->bucket = 0;
4116 state->offset = 0;
4117 }
4118
4119 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08004120 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00004121 addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004122 if (!net_eq(dev_net(ifa->idev->dev), net))
4123 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004124 /* sync with offset */
4125 if (p < state->offset) {
4126 p++;
4127 continue;
4128 }
4129 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004130 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004131 }
4132
4133 /* prepare for next bucket */
4134 state->offset = 0;
4135 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004137 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138}
4139
stephen hemmingerc2e21292010-03-17 20:31:10 +00004140static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4141 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142{
4143 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004144 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
Sasha Levinb67bfe02013-02-27 17:06:00 -08004146 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004147 if (!net_eq(dev_net(ifa->idev->dev), net))
4148 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004149 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004150 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004151 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004152
4153 while (++state->bucket < IN6_ADDR_HSIZE) {
Mihai Maruseac1d578302012-01-03 23:31:35 +00004154 state->offset = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004155 hlist_for_each_entry_rcu_bh(ifa,
stephen hemmingerc2e21292010-03-17 20:31:10 +00004156 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004157 if (!net_eq(dev_net(ifa->idev->dev), net))
4158 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004159 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004160 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08004161 }
4162 }
4163
stephen hemmingerc2e21292010-03-17 20:31:10 +00004164 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165}
4166
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004168 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169{
stephen hemminger5c578aed2010-03-17 20:31:11 +00004170 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00004171 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172}
4173
4174static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4175{
4176 struct inet6_ifaddr *ifa;
4177
4178 ifa = if6_get_next(seq, v);
4179 ++*pos;
4180 return ifa;
4181}
4182
4183static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004184 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185{
stephen hemminger5c578aed2010-03-17 20:31:11 +00004186 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187}
4188
4189static int if6_seq_show(struct seq_file *seq, void *v)
4190{
4191 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Jiri Pirko971a3512013-12-10 13:56:29 +01004192 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07004193 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 ifp->idev->dev->ifindex,
4195 ifp->prefix_len,
4196 ifp->scope,
Jiri Pirko971a3512013-12-10 13:56:29 +01004197 (u8) ifp->flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 ifp->idev->dev->name);
4199 return 0;
4200}
4201
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004202static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 .start = if6_seq_start,
4204 .next = if6_seq_next,
4205 .show = if6_seq_show,
4206 .stop = if6_seq_stop,
4207};
4208
4209static int if6_seq_open(struct inode *inode, struct file *file)
4210{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004211 return seq_open_net(inode, file, &if6_seq_ops,
4212 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213}
4214
Arjan van de Ven9a321442007-02-12 00:55:35 -08004215static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 .owner = THIS_MODULE,
4217 .open = if6_seq_open,
4218 .read = seq_read,
4219 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004220 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221};
4222
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004223static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224{
Gao fengd4beaa62013-02-18 01:34:54 +00004225 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 return -ENOMEM;
4227 return 0;
4228}
4229
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004230static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08004231{
Gao fengece31ff2013-02-18 01:34:56 +00004232 remove_proc_entry("if_inet6", net->proc_net);
Daniel Lezcano3c400902008-01-10 22:42:49 -08004233}
4234
4235static struct pernet_operations if6_proc_net_ops = {
Ian Morris67ba4152014-08-24 21:53:10 +01004236 .init = if6_proc_net_init,
4237 .exit = if6_proc_net_exit,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004238};
4239
4240int __init if6_proc_init(void)
4241{
4242 return register_pernet_subsys(&if6_proc_net_ops);
4243}
4244
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245void if6_proc_exit(void)
4246{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004247 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248}
4249#endif /* CONFIG_PROC_FS */
4250
Amerigo Wang07a93622012-10-29 16:23:10 +00004251#if IS_ENABLED(CONFIG_IPV6_MIP6)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004252/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004253int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004254{
4255 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004256 struct inet6_ifaddr *ifp = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00004257 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004258
stephen hemminger5c578aed2010-03-17 20:31:11 +00004259 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08004260 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09004261 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08004262 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09004263 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004264 (ifp->flags & IFA_F_HOMEADDRESS)) {
4265 ret = 1;
4266 break;
4267 }
4268 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00004269 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004270 return ret;
4271}
4272#endif
4273
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274/*
4275 * Periodic address status verification
4276 */
4277
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004278static void addrconf_verify_rtnl(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004280 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 int i;
4283
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004284 ASSERT_RTNL();
4285
stephen hemminger5c578aed2010-03-17 20:31:11 +00004286 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004288 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004290 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004292 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293restart:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004294 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296
Yasushi Asanofad8da32013-12-31 12:04:19 +09004297 /* When setting preferred_lft to a value not zero or
4298 * infinity, while valid_lft is infinity
4299 * IFA_F_PERMANENT has a non-infinity life time.
4300 */
4301 if ((ifp->flags & IFA_F_PERMANENT) &&
4302 (ifp->prefered_lft == INFINITY_LIFE_TIME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 continue;
4304
4305 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004306 /* We try to batch several events at once. */
4307 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004309 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4310 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 spin_unlock(&ifp->lock);
4312 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 ipv6_del_addr(ifp);
4314 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004315 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4316 spin_unlock(&ifp->lock);
4317 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00004319 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 int deprecate = 0;
4321
4322 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4323 deprecate = 1;
4324 ifp->flags |= IFA_F_DEPRECATED;
4325 }
4326
Yasushi Asanofad8da32013-12-31 12:04:19 +09004327 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4328 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 next = ifp->tstamp + ifp->valid_lft * HZ;
4330
4331 spin_unlock(&ifp->lock);
4332
4333 if (deprecate) {
4334 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
4336 ipv6_ifa_notify(0, ifp);
4337 in6_ifa_put(ifp);
4338 goto restart;
4339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4341 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00004342 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4343 ifp->idev->cnf.dad_transmits *
Jiri Pirko1f9248e2013-12-07 19:26:53 +01004344 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
stephen hemminger88949cf2010-03-17 20:31:17 +00004345
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 if (age >= ifp->prefered_lft - regen_advance) {
4347 struct inet6_ifaddr *ifpub = ifp->ifpub;
4348 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4349 next = ifp->tstamp + ifp->prefered_lft * HZ;
4350 if (!ifp->regen_count && ifpub) {
4351 ifp->regen_count++;
4352 in6_ifa_hold(ifp);
4353 in6_ifa_hold(ifpub);
4354 spin_unlock(&ifp->lock);
stephen hemminger5c578aed2010-03-17 20:31:11 +00004355
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08004356 spin_lock(&ifpub->lock);
4357 ifpub->regen_count = 0;
4358 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 ipv6_create_tempaddr(ifpub, ifp);
4360 in6_ifa_put(ifpub);
4361 in6_ifa_put(ifp);
4362 goto restart;
4363 }
4364 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4365 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4366 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 } else {
4368 /* ifp->prefered_lft <= ifp->valid_lft */
4369 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4370 next = ifp->tstamp + ifp->prefered_lft * HZ;
4371 spin_unlock(&ifp->lock);
4372 }
4373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 }
4375
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004376 next_sec = round_jiffies_up(next);
4377 next_sched = next;
4378
4379 /* If rounded timeout is accurate enough, accept it. */
4380 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4381 next_sched = next_sec;
4382
4383 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4384 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4385 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4386
dingtianhongba3542e2013-08-17 10:27:04 +08004387 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4388 now, next, next_sec, next_sched);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004389 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
stephen hemminger5c578aed2010-03-17 20:31:11 +00004390 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391}
4392
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004393static void addrconf_verify_work(struct work_struct *w)
4394{
4395 rtnl_lock();
4396 addrconf_verify_rtnl();
4397 rtnl_unlock();
4398}
4399
4400static void addrconf_verify(void)
4401{
4402 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4403}
4404
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004405static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4406 struct in6_addr **peer_pfx)
Thomas Graf461d8832006-09-18 00:09:49 -07004407{
4408 struct in6_addr *pfx = NULL;
4409
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004410 *peer_pfx = NULL;
4411
Thomas Graf461d8832006-09-18 00:09:49 -07004412 if (addr)
4413 pfx = nla_data(addr);
4414
4415 if (local) {
4416 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004417 *peer_pfx = pfx;
4418 pfx = nla_data(local);
Thomas Graf461d8832006-09-18 00:09:49 -07004419 }
4420
4421 return pfx;
4422}
4423
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004424static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07004425 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4426 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4427 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
Jiri Pirko479840f2013-12-06 09:45:21 +01004428 [IFA_FLAGS] = { .len = sizeof(u32) },
Thomas Graf461d8832006-09-18 00:09:49 -07004429};
4430
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431static int
David Ahernc21ef3e2017-04-16 09:48:24 -07004432inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
4433 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004435 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07004436 struct ifaddrmsg *ifm;
4437 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004438 struct in6_addr *pfx, *peer_pfx;
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004439 u32 ifa_flags;
Thomas Grafb933f712006-09-18 00:10:19 -07004440 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441
Johannes Bergfceb6432017-04-12 14:34:07 +02004442 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07004443 extack);
Thomas Grafb933f712006-09-18 00:10:19 -07004444 if (err < 0)
4445 return err;
4446
4447 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004448 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004449 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 return -EINVAL;
4451
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004452 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4453
4454 /* We ignore other flags so far. */
4455 ifa_flags &= IFA_F_MANAGETEMPADDR;
4456
4457 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4458 ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459}
4460
Jiri Pirko479840f2013-12-06 09:45:21 +01004461static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004462 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004463{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004464 u32 flags;
4465 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004466 unsigned long timeout;
Jiri Pirko53bd6742013-12-06 09:45:22 +01004467 bool was_managetempaddr;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004468 bool had_prefixroute;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09004469
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004470 ASSERT_RTNL();
4471
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004472 if (!valid_lft || (prefered_lft > valid_lft))
4473 return -EINVAL;
4474
Jiri Pirko53bd6742013-12-06 09:45:22 +01004475 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4476 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4477 return -EINVAL;
4478
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004479 timeout = addrconf_timeout_fixup(valid_lft, HZ);
4480 if (addrconf_finite_timeout(timeout)) {
4481 expires = jiffies_to_clock_t(timeout * HZ);
4482 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004483 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004484 } else {
4485 expires = 0;
4486 flags = 0;
4487 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004488 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004489
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004490 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4491 if (addrconf_finite_timeout(timeout)) {
4492 if (timeout == 0)
4493 ifa_flags |= IFA_F_DEPRECATED;
4494 prefered_lft = timeout;
4495 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004496
4497 spin_lock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004498 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004499 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4500 !(ifp->flags & IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004501 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
Thomas Haller761aac72014-01-15 15:36:58 +01004502 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4503 IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004504 ifp->flags |= ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004505 ifp->tstamp = jiffies;
4506 ifp->valid_lft = valid_lft;
4507 ifp->prefered_lft = prefered_lft;
4508
4509 spin_unlock_bh(&ifp->lock);
4510 if (!(ifp->flags&IFA_F_TENTATIVE))
4511 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004512
Thomas Haller761aac72014-01-15 15:36:58 +01004513 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4514 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4515 expires, flags);
Thomas Haller5b84efe2014-01-15 15:36:59 +01004516 } else if (had_prefixroute) {
4517 enum cleanup_prefix_rt_t action;
4518 unsigned long rt_expires;
4519
4520 write_lock_bh(&ifp->idev->lock);
4521 action = check_cleanup_prefix_route(ifp, &rt_expires);
4522 write_unlock_bh(&ifp->idev->lock);
4523
4524 if (action != CLEANUP_PREFIX_RT_NOP) {
4525 cleanup_prefix_route(ifp, rt_expires,
4526 action == CLEANUP_PREFIX_RT_DEL);
4527 }
Thomas Haller761aac72014-01-15 15:36:58 +01004528 }
Jiri Pirko53bd6742013-12-06 09:45:22 +01004529
4530 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4531 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4532 valid_lft = prefered_lft = 0;
4533 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4534 !was_managetempaddr, jiffies);
4535 }
4536
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004537 addrconf_verify_rtnl();
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004538
4539 return 0;
4540}
4541
4542static int
David Ahernc21ef3e2017-04-16 09:48:24 -07004543inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
4544 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004546 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07004547 struct ifaddrmsg *ifm;
4548 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004549 struct in6_addr *pfx, *peer_pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07004550 struct inet6_ifaddr *ifa;
4551 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004552 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
Jiri Pirko479840f2013-12-06 09:45:21 +01004553 u32 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07004554 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
Johannes Bergfceb6432017-04-12 14:34:07 +02004556 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07004557 extack);
Thomas Graf461d8832006-09-18 00:09:49 -07004558 if (err < 0)
4559 return err;
4560
4561 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004562 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004563 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 return -EINVAL;
4565
Thomas Graf461d8832006-09-18 00:09:49 -07004566 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09004567 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07004568
4569 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09004570 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07004571 preferred_lft = ci->ifa_prefered;
4572 } else {
4573 preferred_lft = INFINITY_LIFE_TIME;
4574 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09004575 }
4576
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004577 dev = __dev_get_by_index(net, ifm->ifa_index);
Ian Morris63159f22015-03-29 14:00:04 +01004578 if (!dev)
Thomas Graf7198f8c2006-09-18 00:13:46 -07004579 return -ENODEV;
4580
Jiri Pirko479840f2013-12-06 09:45:21 +01004581 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4582
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004583 /* We ignore other flags so far. */
Thomas Haller761aac72014-01-15 15:36:58 +01004584 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
Madhu Challa93a714d2015-02-25 09:58:35 -08004585 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004586
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08004587 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Ian Morris63159f22015-03-29 14:00:04 +01004588 if (!ifa) {
Thomas Graf7198f8c2006-09-18 00:13:46 -07004589 /*
4590 * It would be best to check for !NLM_F_CREATE here but
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08004591 * userspace already relies on not having to provide this.
Thomas Graf7198f8c2006-09-18 00:13:46 -07004592 */
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004593 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004594 ifm->ifa_prefixlen, ifa_flags,
4595 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004596 }
4597
Thomas Graf7198f8c2006-09-18 00:13:46 -07004598 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4599 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4600 err = -EEXIST;
4601 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004602 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07004603
4604 in6_ifa_put(ifa);
4605
4606 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607}
4608
Jiri Pirko479840f2013-12-06 09:45:21 +01004609static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
Thomas Graf101bb222006-09-18 00:11:52 -07004610 u8 scope, int ifindex)
4611{
4612 struct ifaddrmsg *ifm;
4613
4614 ifm = nlmsg_data(nlh);
4615 ifm->ifa_family = AF_INET6;
4616 ifm->ifa_prefixlen = prefixlen;
4617 ifm->ifa_flags = flags;
4618 ifm->ifa_scope = scope;
4619 ifm->ifa_index = ifindex;
4620}
4621
Thomas Graf85486af2006-09-18 00:11:24 -07004622static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4623 unsigned long tstamp, u32 preferred, u32 valid)
4624{
4625 struct ifa_cacheinfo ci;
4626
Thomas Graf18a31e12010-11-17 04:12:02 +00004627 ci.cstamp = cstamp_delta(cstamp);
4628 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07004629 ci.ifa_prefered = preferred;
4630 ci.ifa_valid = valid;
4631
4632 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4633}
4634
Thomas Graf101bb222006-09-18 00:11:52 -07004635static inline int rt_scope(int ifa_scope)
4636{
4637 if (ifa_scope & IFA_HOST)
4638 return RT_SCOPE_HOST;
4639 else if (ifa_scope & IFA_LINK)
4640 return RT_SCOPE_LINK;
4641 else if (ifa_scope & IFA_SITE)
4642 return RT_SCOPE_SITE;
4643 else
4644 return RT_SCOPE_UNIVERSE;
4645}
4646
Thomas Graf0ab68032006-09-18 00:12:35 -07004647static inline int inet6_ifaddr_msgsize(void)
4648{
Thomas Graf339bf982006-11-10 14:10:15 -08004649 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004650 + nla_total_size(16) /* IFA_LOCAL */
Thomas Graf339bf982006-11-10 14:10:15 -08004651 + nla_total_size(16) /* IFA_ADDRESS */
Jiri Pirko479840f2013-12-06 09:45:21 +01004652 + nla_total_size(sizeof(struct ifa_cacheinfo))
4653 + nla_total_size(4) /* IFA_FLAGS */;
Thomas Graf0ab68032006-09-18 00:12:35 -07004654}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004655
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004657 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07004660 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
Eric W. Biederman15e47302012-09-07 20:12:54 +00004662 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004663 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004664 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004665
Thomas Graf101bb222006-09-18 00:11:52 -07004666 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4667 ifa->idev->dev->ifindex);
4668
Yasushi Asanofad8da32013-12-31 12:04:19 +09004669 if (!((ifa->flags&IFA_F_PERMANENT) &&
4670 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
Thomas Graf85486af2006-09-18 00:11:24 -07004671 preferred = ifa->prefered_lft;
4672 valid = ifa->valid_lft;
4673 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00004675 if (preferred > tval)
4676 preferred -= tval;
4677 else
4678 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00004679 if (valid != INFINITY_LIFE_TIME) {
4680 if (valid > tval)
4681 valid -= tval;
4682 else
4683 valid = 0;
4684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 }
4686 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07004687 preferred = INFINITY_LIFE_TIME;
4688 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 }
Thomas Graf85486af2006-09-18 00:11:24 -07004690
Cong Wang7996c792013-05-22 05:41:06 +00004691 if (!ipv6_addr_any(&ifa->peer_addr)) {
Jiri Benc930345e2015-03-29 16:59:25 +02004692 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4693 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004694 goto error;
4695 } else
Jiri Benc930345e2015-03-29 16:59:25 +02004696 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004697 goto error;
4698
4699 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4700 goto error;
Thomas Graf85486af2006-09-18 00:11:24 -07004701
Jiri Pirko479840f2013-12-06 09:45:21 +01004702 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4703 goto error;
4704
Johannes Berg053c0952015-01-16 22:09:00 +01004705 nlmsg_end(skb, nlh);
4706 return 0;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004707
4708error:
4709 nlmsg_cancel(skb, nlh);
4710 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711}
4712
4713static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004714 u32 portid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004717 u8 scope = RT_SCOPE_UNIVERSE;
4718 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
Thomas Graf101bb222006-09-18 00:11:52 -07004720 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4721 scope = RT_SCOPE_SITE;
4722
Eric W. Biederman15e47302012-09-07 20:12:54 +00004723 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004724 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004725 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004726
Thomas Graf101bb222006-09-18 00:11:52 -07004727 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004728 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004729 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004730 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4731 nlmsg_cancel(skb, nlh);
4732 return -EMSGSIZE;
4733 }
Thomas Graf85486af2006-09-18 00:11:24 -07004734
Johannes Berg053c0952015-01-16 22:09:00 +01004735 nlmsg_end(skb, nlh);
4736 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737}
4738
4739static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004740 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004743 u8 scope = RT_SCOPE_UNIVERSE;
4744 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745
Thomas Graf101bb222006-09-18 00:11:52 -07004746 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4747 scope = RT_SCOPE_SITE;
4748
Eric W. Biederman15e47302012-09-07 20:12:54 +00004749 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004750 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004751 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004752
Thomas Graf101bb222006-09-18 00:11:52 -07004753 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004754 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004755 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004756 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4757 nlmsg_cancel(skb, nlh);
4758 return -EMSGSIZE;
4759 }
Thomas Graf85486af2006-09-18 00:11:24 -07004760
Johannes Berg053c0952015-01-16 22:09:00 +01004761 nlmsg_end(skb, nlh);
4762 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763}
4764
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004765enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 UNICAST_ADDR,
4767 MULTICAST_ADDR,
4768 ANYCAST_ADDR,
4769};
4770
Eric Dumazet234b27c2009-11-12 04:11:50 +00004771/* called with rcu_read_lock() */
4772static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4773 struct netlink_callback *cb, enum addr_type_t type,
4774 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 struct ifmcaddr6 *ifmca;
4777 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004778 int err = 1;
4779 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
Eric Dumazet234b27c2009-11-12 04:11:50 +00004781 read_lock_bh(&idev->lock);
4782 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00004783 case UNICAST_ADDR: {
4784 struct inet6_ifaddr *ifa;
4785
Eric Dumazet234b27c2009-11-12 04:11:50 +00004786 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00004787 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4788 if (++ip_idx < s_ip_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004789 continue;
4790 err = inet6_fill_ifaddr(skb, ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004791 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004792 cb->nlh->nlmsg_seq,
4793 RTM_NEWADDR,
4794 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004795 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004796 break;
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004797 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004799 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00004800 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004801 case MULTICAST_ADDR:
4802 /* multicast address */
4803 for (ifmca = idev->mc_list; ifmca;
4804 ifmca = ifmca->next, ip_idx++) {
4805 if (ip_idx < s_ip_idx)
4806 continue;
4807 err = inet6_fill_ifmcaddr(skb, ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004808 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004809 cb->nlh->nlmsg_seq,
4810 RTM_GETMULTICAST,
4811 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004812 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004813 break;
4814 }
4815 break;
4816 case ANYCAST_ADDR:
4817 /* anycast address */
4818 for (ifaca = idev->ac_list; ifaca;
4819 ifaca = ifaca->aca_next, ip_idx++) {
4820 if (ip_idx < s_ip_idx)
4821 continue;
4822 err = inet6_fill_ifacaddr(skb, ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004823 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004824 cb->nlh->nlmsg_seq,
4825 RTM_GETANYCAST,
4826 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004827 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004828 break;
4829 }
4830 break;
4831 default:
4832 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004834 read_unlock_bh(&idev->lock);
4835 *p_ip_idx = ip_idx;
4836 return err;
4837}
4838
4839static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4840 enum addr_type_t type)
4841{
4842 struct net *net = sock_net(skb->sk);
4843 int h, s_h;
4844 int idx, ip_idx;
4845 int s_idx, s_ip_idx;
4846 struct net_device *dev;
4847 struct inet6_dev *idev;
4848 struct hlist_head *head;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004849
4850 s_h = cb->args[0];
4851 s_idx = idx = cb->args[1];
4852 s_ip_idx = ip_idx = cb->args[2];
4853
4854 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004855 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004856 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4857 idx = 0;
4858 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08004859 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet234b27c2009-11-12 04:11:50 +00004860 if (idx < s_idx)
4861 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07004862 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004863 s_ip_idx = 0;
4864 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004865 idev = __in6_dev_get(dev);
4866 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004867 goto cont;
4868
4869 if (in6_dump_addrs(idev, skb, cb, type,
David S. Miller7b46a642015-01-18 23:36:08 -05004870 s_ip_idx, &ip_idx) < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004871 goto done;
4872cont:
4873 idx++;
4874 }
4875 }
4876done:
4877 rcu_read_unlock();
4878 cb->args[0] = h;
4879 cb->args[1] = idx;
4880 cb->args[2] = ip_idx;
4881
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 return skb->len;
4883}
4884
4885static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4886{
4887 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004888
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 return inet6_dump_addr(skb, cb, type);
4890}
4891
4892static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4893{
4894 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004895
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 return inet6_dump_addr(skb, cb, type);
4897}
4898
4899
4900static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4901{
4902 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004903
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 return inet6_dump_addr(skb, cb, type);
4905}
4906
David Ahernc21ef3e2017-04-16 09:48:24 -07004907static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4908 struct netlink_ext_ack *extack)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004909{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004910 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004911 struct ifaddrmsg *ifm;
4912 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004913 struct in6_addr *addr = NULL, *peer;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004914 struct net_device *dev = NULL;
4915 struct inet6_ifaddr *ifa;
4916 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004917 int err;
4918
Johannes Bergfceb6432017-04-12 14:34:07 +02004919 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07004920 extack);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004921 if (err < 0)
4922 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004923
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004924 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
Ian Morris63159f22015-03-29 14:00:04 +01004925 if (!addr) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004926 err = -EINVAL;
4927 goto errout;
4928 }
4929
4930 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004931 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004932 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004933
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004934 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4935 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004936 err = -EADDRNOTAVAIL;
4937 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004938 }
4939
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004940 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4941 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004942 err = -ENOBUFS;
4943 goto errout_ifa;
4944 }
4945
Eric W. Biederman15e47302012-09-07 20:12:54 +00004946 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004947 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004948 if (err < 0) {
4949 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4950 WARN_ON(err == -EMSGSIZE);
4951 kfree_skb(skb);
4952 goto errout_ifa;
4953 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00004954 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004955errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004956 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004957errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004958 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004959}
4960
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4962{
4963 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004964 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07004965 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966
Mahesh Bandewarf784ad32017-01-04 15:01:01 -08004967 /* Don't send DELADDR notification for TENTATIVE address,
4968 * since NEWADDR notification is sent only after removing
4969 * TENTATIVE flag.
4970 */
4971 if (ifa->flags & IFA_F_TENTATIVE && event == RTM_DELADDR)
4972 return;
4973
Thomas Graf0ab68032006-09-18 00:12:35 -07004974 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01004975 if (!skb)
Thomas Graf5d620262006-08-15 00:35:02 -07004976 goto errout;
4977
4978 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004979 if (err < 0) {
4980 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4981 WARN_ON(err == -EMSGSIZE);
4982 kfree_skb(skb);
4983 goto errout;
4984 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004985 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4986 return;
Thomas Graf5d620262006-08-15 00:35:02 -07004987errout:
4988 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004989 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990}
4991
Dave Jonesb6f99a22007-03-22 12:27:49 -07004992static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 __s32 *array, int bytes)
4994{
Thomas Graf04561c12006-11-14 19:53:58 -08004995 BUG_ON(bytes < (DEVCONF_MAX * 4));
4996
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 memset(array, 0, bytes);
4998 array[DEVCONF_FORWARDING] = cnf->forwarding;
4999 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
5000 array[DEVCONF_MTU6] = cnf->mtu6;
5001 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
5002 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
5003 array[DEVCONF_AUTOCONF] = cnf->autoconf;
5004 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
5005 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00005006 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
5007 jiffies_to_msecs(cnf->rtr_solicit_interval);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005008 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
5009 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
Thomas Graf93908d12010-11-17 01:44:24 +00005010 array[DEVCONF_RTR_SOLICIT_DELAY] =
5011 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02005013 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
5014 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
5015 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
5016 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
5018 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
5019 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
5020 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
5021 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08005023 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
Hangbin Liu8013d1d72015-07-30 14:28:42 +08005024 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08005025 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005026#ifdef CONFIG_IPV6_ROUTER_PREF
5027 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00005028 array[DEVCONF_RTR_PROBE_INTERVAL] =
5029 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08005030#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzbbea1242017-03-22 18:19:04 +09005031 array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08005032 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
5033#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005034#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07005035 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07005036 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07005037#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5038 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
Erik Kline7fd25612014-10-28 18:11:14 +09005039 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
Neil Horman95c385b2007-04-25 17:08:10 -07005040#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005041#ifdef CONFIG_IPV6_MROUTE
5042 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
5043#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09005044 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09005045 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00005046 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00005047 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02005048 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
Ben Greeard9333192014-06-25 14:44:53 -07005049 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
Harout Hedeshianc2943f12015-01-20 10:06:05 -07005050 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
Andy Gospodarek35103d12015-08-13 10:39:01 -04005051 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005052 /* we omit DEVCONF_STABLE_SECRET for now */
Erik Kline3985e8a2015-07-22 16:38:25 +09005053 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
Johannes Bergabbc3042016-02-04 13:31:19 +01005054 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
Johannes Berg7a02bf82016-02-04 13:31:20 +01005055 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
David Ahernf1705ec2016-02-24 09:25:37 -08005056 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
David Lebrun1ababeb2016-11-08 14:57:39 +01005057 array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
David Lebrunbf355b82016-11-08 14:57:42 +01005058#ifdef CONFIG_IPV6_SEG6_HMAC
5059 array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5060#endif
Erik Nordmarkadc176c2016-12-02 14:00:08 -08005061 array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
Felix Jiad35a00b2017-01-26 16:59:17 +13005062 array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
David Forsterdf789fe2017-02-23 16:27:18 +00005063 array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064}
5065
Thomas Grafb382b192010-11-16 04:33:57 +00005066static inline size_t inet6_ifla6_size(void)
5067{
5068 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5069 + nla_total_size(sizeof(struct ifla_cacheinfo))
5070 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5071 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005072 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5073 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
Thomas Grafb382b192010-11-16 04:33:57 +00005074}
5075
Thomas Graf339bf982006-11-10 14:10:15 -08005076static inline size_t inet6_if_nlmsg_size(void)
5077{
5078 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5079 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5080 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5081 + nla_total_size(4) /* IFLA_MTU */
5082 + nla_total_size(4) /* IFLA_LINK */
Andy Gospodarek03443382015-08-13 15:26:35 -04005083 + nla_total_size(1) /* IFLA_OPERSTATE */
Thomas Grafb382b192010-11-16 04:33:57 +00005084 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08005085}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005086
Eric Dumazetbe281e52011-05-19 01:14:23 +00005087static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Jia Heaca05672016-09-30 11:29:03 +08005088 int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005089{
5090 int i;
Jia Heaca05672016-09-30 11:29:03 +08005091 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005092 BUG_ON(pad < 0);
5093
5094 /* Use put_unaligned() because stats may not be aligned for u64. */
Jia Heaca05672016-09-30 11:29:03 +08005095 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5096 for (i = 1; i < ICMP6_MIB_MAX; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00005097 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005098
Jia Heaca05672016-09-30 11:29:03 +08005099 memset(&stats[ICMP6_MIB_MAX], 0, pad);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005100}
5101
WANG Cong698365f2014-05-05 15:55:55 -07005102static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305103 int bytes, size_t syncpoff)
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005104{
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305105 int i, c;
5106 u64 buff[IPSTATS_MIB_MAX];
5107 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5108
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005109 BUG_ON(pad < 0);
5110
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305111 memset(buff, 0, sizeof(buff));
5112 buff[0] = IPSTATS_MIB_MAX;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005113
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305114 for_each_possible_cpu(c) {
5115 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5116 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5117 }
5118
5119 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5120 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005121}
5122
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005123static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5124 int bytes)
5125{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005126 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005127 case IFLA_INET6_STATS:
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305128 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5129 offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005130 break;
5131 case IFLA_INET6_ICMP6STATS:
Jia Heaca05672016-09-30 11:29:03 +08005132 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005133 break;
5134 }
5135}
5136
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005137static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5138 u32 ext_filter_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005140 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08005141 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142
David S. Millerc78679e2012-04-01 20:27:33 -04005143 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5144 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08005146 ci.tstamp = cstamp_delta(idev->tstamp);
5147 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
Jiri Pirko1f9248e2013-12-07 19:26:53 +01005148 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
David S. Millerc78679e2012-04-01 20:27:33 -04005149 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5150 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005151 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
Ian Morris63159f22015-03-29 14:00:04 +01005152 if (!nla)
Thomas Graf04561c12006-11-14 19:53:58 -08005153 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005154 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005156 /* XXX - MC not implemented */
5157
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005158 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5159 return 0;
5160
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005161 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005162 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005163 goto nla_put_failure;
5164 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5165
5166 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005167 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005168 goto nla_put_failure;
5169 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005171 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
Ian Morris63159f22015-03-29 14:00:04 +01005172 if (!nla)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005173 goto nla_put_failure;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005174
Felix Jiad35a00b2017-01-26 16:59:17 +13005175 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005176 goto nla_put_failure;
5177
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005178 read_lock_bh(&idev->lock);
5179 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5180 read_unlock_bh(&idev->lock);
5181
Thomas Grafb382b192010-11-16 04:33:57 +00005182 return 0;
5183
5184nla_put_failure:
5185 return -EMSGSIZE;
5186}
5187
Arad, Ronenb1974ed2015-10-19 09:23:28 -07005188static size_t inet6_get_link_af_size(const struct net_device *dev,
5189 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005190{
5191 if (!__in6_dev_get(dev))
5192 return 0;
5193
5194 return inet6_ifla6_size();
5195}
5196
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005197static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5198 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005199{
5200 struct inet6_dev *idev = __in6_dev_get(dev);
5201
5202 if (!idev)
5203 return -ENODATA;
5204
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005205 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005206 return -EMSGSIZE;
5207
5208 return 0;
5209}
5210
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005211static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5212{
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005213 struct inet6_ifaddr *ifp;
5214 struct net_device *dev = idev->dev;
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005215 bool clear_token, update_rs = false;
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005216 struct in6_addr ll_addr;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005217
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005218 ASSERT_RTNL();
5219
Ian Morris63159f22015-03-29 14:00:04 +01005220 if (!token)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005221 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005222 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5223 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005224 if (!ipv6_accept_ra(idev))
5225 return -EINVAL;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005226 if (idev->cnf.rtr_solicits == 0)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005227 return -EINVAL;
5228
5229 write_lock_bh(&idev->lock);
5230
5231 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5232 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5233
5234 write_unlock_bh(&idev->lock);
5235
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005236 clear_token = ipv6_addr_any(token);
5237 if (clear_token)
5238 goto update_lft;
5239
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005240 if (!idev->dead && (idev->if_flags & IF_READY) &&
5241 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5242 IFA_F_OPTIMISTIC)) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005243 /* If we're not ready, then normal ifup will take care
5244 * of this. Otherwise, we need to request our rs here.
5245 */
5246 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5247 update_rs = true;
5248 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005249
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005250update_lft:
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005251 write_lock_bh(&idev->lock);
Daniel Borkmannfc403832013-04-09 03:47:15 +00005252
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005253 if (update_rs) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005254 idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005255 idev->rs_interval = rfc3315_s14_backoff_init(
5256 idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005257 idev->rs_probes = 1;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005258 addrconf_mod_rs_timer(idev, idev->rs_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005259 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005260
5261 /* Well, that's kinda nasty ... */
5262 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5263 spin_lock(&ifp->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00005264 if (ifp->tokenized) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005265 ifp->valid_lft = 0;
5266 ifp->prefered_lft = 0;
5267 }
5268 spin_unlock(&ifp->lock);
5269 }
5270
5271 write_unlock_bh(&idev->lock);
Lubomir Rintelb2ed64a2014-10-27 17:39:16 +01005272 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005273 addrconf_verify_rtnl();
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005274 return 0;
5275}
5276
Daniel Borkmann11b1f822015-02-05 14:39:11 +01005277static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5278 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5279 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5280};
5281
5282static int inet6_validate_link_af(const struct net_device *dev,
5283 const struct nlattr *nla)
5284{
5285 struct nlattr *tb[IFLA_INET6_MAX + 1];
5286
5287 if (dev && !__in6_dev_get(dev))
5288 return -EAFNOSUPPORT;
5289
Johannes Bergfceb6432017-04-12 14:34:07 +02005290 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy,
5291 NULL);
Daniel Borkmann11b1f822015-02-05 14:39:11 +01005292}
5293
Felix Jiad35a00b2017-01-26 16:59:17 +13005294static int check_addr_gen_mode(int mode)
5295{
5296 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5297 mode != IN6_ADDR_GEN_MODE_NONE &&
5298 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5299 mode != IN6_ADDR_GEN_MODE_RANDOM)
5300 return -EINVAL;
5301 return 1;
5302}
5303
5304static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5305 int mode)
5306{
5307 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5308 !idev->cnf.stable_secret.initialized &&
5309 !net->ipv6.devconf_dflt->stable_secret.initialized)
5310 return -EINVAL;
5311 return 1;
5312}
5313
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005314static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5315{
5316 int err = -EINVAL;
5317 struct inet6_dev *idev = __in6_dev_get(dev);
5318 struct nlattr *tb[IFLA_INET6_MAX + 1];
5319
5320 if (!idev)
5321 return -EAFNOSUPPORT;
5322
Johannes Bergfceb6432017-04-12 14:34:07 +02005323 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL, NULL) < 0)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005324 BUG();
5325
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005326 if (tb[IFLA_INET6_TOKEN]) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005327 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005328 if (err)
5329 return err;
5330 }
5331
5332 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5333 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5334
Felix Jiad35a00b2017-01-26 16:59:17 +13005335 if (check_addr_gen_mode(mode) < 0 ||
5336 check_stable_privacy(idev, dev_net(dev), mode) < 0)
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005337 return -EINVAL;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005338
Felix Jiad35a00b2017-01-26 16:59:17 +13005339 idev->cnf.addr_gen_mode = mode;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005340 err = 0;
5341 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005342
5343 return err;
5344}
5345
Thomas Grafb382b192010-11-16 04:33:57 +00005346static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005347 u32 portid, u32 seq, int event, unsigned int flags)
Thomas Grafb382b192010-11-16 04:33:57 +00005348{
5349 struct net_device *dev = idev->dev;
5350 struct ifinfomsg *hdr;
5351 struct nlmsghdr *nlh;
5352 void *protoinfo;
5353
Eric W. Biederman15e47302012-09-07 20:12:54 +00005354 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005355 if (!nlh)
Thomas Grafb382b192010-11-16 04:33:57 +00005356 return -EMSGSIZE;
5357
5358 hdr = nlmsg_data(nlh);
5359 hdr->ifi_family = AF_INET6;
5360 hdr->__ifi_pad = 0;
5361 hdr->ifi_type = dev->type;
5362 hdr->ifi_index = dev->ifindex;
5363 hdr->ifi_flags = dev_get_flags(dev);
5364 hdr->ifi_change = 0;
5365
David S. Millerc78679e2012-04-01 20:27:33 -04005366 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5367 (dev->addr_len &&
5368 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5369 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02005370 (dev->ifindex != dev_get_iflink(dev) &&
Andy Gospodarek03443382015-08-13 15:26:35 -04005371 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5372 nla_put_u8(skb, IFLA_OPERSTATE,
5373 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
David S. Millerc78679e2012-04-01 20:27:33 -04005374 goto nla_put_failure;
Thomas Grafb382b192010-11-16 04:33:57 +00005375 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
Ian Morris63159f22015-03-29 14:00:04 +01005376 if (!protoinfo)
Thomas Grafb382b192010-11-16 04:33:57 +00005377 goto nla_put_failure;
5378
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005379 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005380 goto nla_put_failure;
5381
Thomas Graf04561c12006-11-14 19:53:58 -08005382 nla_nest_end(skb, protoinfo);
Johannes Berg053c0952015-01-16 22:09:00 +01005383 nlmsg_end(skb, nlh);
5384 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385
Thomas Graf04561c12006-11-14 19:53:58 -08005386nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005387 nlmsg_cancel(skb, nlh);
5388 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389}
5390
5391static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5392{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005393 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00005394 int h, s_h;
David S. Miller434a8a52009-11-11 18:53:00 -08005395 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 struct net_device *dev;
5397 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00005398 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399
Eric Dumazet84d26972009-11-09 12:11:28 +00005400 s_h = cb->args[0];
5401 s_idx = cb->args[1];
5402
5403 rcu_read_lock();
5404 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5405 idx = 0;
5406 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08005407 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet84d26972009-11-09 12:11:28 +00005408 if (idx < s_idx)
5409 goto cont;
5410 idev = __in6_dev_get(dev);
5411 if (!idev)
5412 goto cont;
5413 if (inet6_fill_ifinfo(skb, idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005414 NETLINK_CB(cb->skb).portid,
Eric Dumazet84d26972009-11-09 12:11:28 +00005415 cb->nlh->nlmsg_seq,
Johannes Berg053c0952015-01-16 22:09:00 +01005416 RTM_NEWLINK, NLM_F_MULTI) < 0)
Eric Dumazet84d26972009-11-09 12:11:28 +00005417 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07005418cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00005419 idx++;
5420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 }
Eric Dumazet84d26972009-11-09 12:11:28 +00005422out:
5423 rcu_read_unlock();
5424 cb->args[1] = idx;
5425 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426
5427 return skb->len;
5428}
5429
5430void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5431{
5432 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005433 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005434 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005435
Thomas Graf339bf982006-11-10 14:10:15 -08005436 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005437 if (!skb)
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005438 goto errout;
5439
5440 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005441 if (err < 0) {
5442 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5443 WARN_ON(err == -EMSGSIZE);
5444 kfree_skb(skb);
5445 goto errout;
5446 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005447 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005448 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005449errout:
5450 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005451 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452}
5453
Thomas Graf339bf982006-11-10 14:10:15 -08005454static inline size_t inet6_prefix_nlmsg_size(void)
5455{
5456 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5457 + nla_total_size(sizeof(struct in6_addr))
5458 + nla_total_size(sizeof(struct prefix_cacheinfo));
5459}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005460
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005462 struct prefix_info *pinfo, u32 portid, u32 seq,
Thomas Graf6051e2f2006-11-14 19:54:19 -08005463 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464{
Thomas Graf6051e2f2006-11-14 19:54:19 -08005465 struct prefixmsg *pmsg;
5466 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 struct prefix_cacheinfo ci;
5468
Eric W. Biederman15e47302012-09-07 20:12:54 +00005469 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005470 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005471 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08005472
5473 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07005475 pmsg->prefix_pad1 = 0;
5476 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 pmsg->prefix_ifindex = idev->dev->ifindex;
5478 pmsg->prefix_len = pinfo->prefix_len;
5479 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07005480 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 pmsg->prefix_flags = 0;
5482 if (pinfo->onlink)
5483 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5484 if (pinfo->autoconf)
5485 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5486
David S. Millerc78679e2012-04-01 20:27:33 -04005487 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5488 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 ci.preferred_time = ntohl(pinfo->prefered);
5490 ci.valid_time = ntohl(pinfo->valid);
David S. Millerc78679e2012-04-01 20:27:33 -04005491 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5492 goto nla_put_failure;
Johannes Berg053c0952015-01-16 22:09:00 +01005493 nlmsg_end(skb, nlh);
5494 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495
Thomas Graf6051e2f2006-11-14 19:54:19 -08005496nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005497 nlmsg_cancel(skb, nlh);
5498 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499}
5500
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005501static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 struct prefix_info *pinfo)
5503{
5504 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005505 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005506 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507
Thomas Graf339bf982006-11-10 14:10:15 -08005508 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005509 if (!skb)
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005510 goto errout;
5511
5512 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005513 if (err < 0) {
5514 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5515 WARN_ON(err == -EMSGSIZE);
5516 kfree_skb(skb);
5517 goto errout;
5518 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005519 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5520 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005521errout:
5522 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08005523 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524}
5525
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5527{
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005528 struct net *net = dev_net(ifp->idev->dev);
5529
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005530 if (event)
5531 ASSERT_RTNL();
5532
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5534
5535 switch (event) {
5536 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07005537 /*
5538 * If the address was optimistic
5539 * we inserted the route at the start of
5540 * our DAD process, so we don't need
5541 * to do it again
5542 */
5543 if (!(ifp->rt->rt6i_node))
5544 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 if (ifp->idev->cnf.forwarding)
5546 addrconf_join_anycast(ifp);
Cong Wang7996c792013-05-22 05:41:06 +00005547 if (!ipv6_addr_any(&ifp->peer_addr))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005548 addrconf_prefix_route(&ifp->peer_addr, 128,
5549 ifp->idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 break;
5551 case RTM_DELADDR:
5552 if (ifp->idev->cnf.forwarding)
5553 addrconf_leave_anycast(ifp);
5554 addrconf_leave_solict(ifp->idev, &ifp->addr);
Cong Wang7996c792013-05-22 05:41:06 +00005555 if (!ipv6_addr_any(&ifp->peer_addr)) {
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005556 struct rt6_info *rt;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005557
Nicolas Dichtele7478df2014-09-03 23:59:22 +02005558 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5559 ifp->idev->dev, 0, 0);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07005560 if (rt)
5561 ip6_del_rt(rt);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005562 }
David Ahern38bd10c2016-04-21 20:56:12 -07005563 if (ifp->rt) {
5564 dst_hold(&ifp->rt->dst);
5565 ip6_del_rt(ifp->rt);
5566 }
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02005567 rt_genid_bump_ipv6(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 break;
5569 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005570 atomic_inc(&net->ipv6.dev_addr_genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571}
5572
5573static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5574{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005575 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 if (likely(ifp->idev->dead == 0))
5577 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005578 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579}
5580
5581#ifdef CONFIG_SYSCTL
5582
5583static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005584int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 void __user *buffer, size_t *lenp, loff_t *ppos)
5586{
5587 int *valp = ctl->data;
5588 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005589 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005590 struct ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 int ret;
5592
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005593 /*
5594 * ctl->data points to idev->cnf.forwarding, we should
5595 * not modify it until we get the rtnl lock.
5596 */
5597 lctl = *ctl;
5598 lctl.data = &val;
5599
5600 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08005602 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00005603 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005604 if (ret)
5605 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005606 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607}
5608
Marcelo Leitner77751422015-02-23 11:17:13 -03005609static
5610int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5611 void __user *buffer, size_t *lenp, loff_t *ppos)
5612{
5613 struct inet6_dev *idev = ctl->extra1;
5614 int min_mtu = IPV6_MIN_MTU;
5615 struct ctl_table lctl;
5616
5617 lctl = *ctl;
5618 lctl.extra1 = &min_mtu;
5619 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5620
5621 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5622}
5623
Brian Haley56d417b2009-06-01 03:07:33 -07005624static void dev_disable_change(struct inet6_dev *idev)
5625{
Cong Wang75538c22013-05-29 11:30:50 +08005626 struct netdev_notifier_info info;
5627
Brian Haley56d417b2009-06-01 03:07:33 -07005628 if (!idev || !idev->dev)
5629 return;
5630
Cong Wang75538c22013-05-29 11:30:50 +08005631 netdev_notifier_info_init(&info, idev->dev);
Brian Haley56d417b2009-06-01 03:07:33 -07005632 if (idev->cnf.disable_ipv6)
Cong Wang75538c22013-05-29 11:30:50 +08005633 addrconf_notify(NULL, NETDEV_DOWN, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005634 else
Cong Wang75538c22013-05-29 11:30:50 +08005635 addrconf_notify(NULL, NETDEV_UP, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005636}
5637
5638static void addrconf_disable_change(struct net *net, __s32 newf)
5639{
5640 struct net_device *dev;
5641 struct inet6_dev *idev;
5642
Kefeng Wang03e4def2017-01-19 16:26:21 +08005643 for_each_netdev(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07005644 idev = __in6_dev_get(dev);
5645 if (idev) {
5646 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5647 idev->cnf.disable_ipv6 = newf;
5648 if (changed)
5649 dev_disable_change(idev);
5650 }
Brian Haley56d417b2009-06-01 03:07:33 -07005651 }
Brian Haley56d417b2009-06-01 03:07:33 -07005652}
5653
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005654static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07005655{
5656 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005657 int old;
5658
5659 if (!rtnl_trylock())
5660 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07005661
5662 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005663 old = *p;
5664 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07005665
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005666 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5667 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07005668 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005669 }
Brian Haley56d417b2009-06-01 03:07:33 -07005670
5671 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07005672 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5673 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005674 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07005675 dev_disable_change((struct inet6_dev *)table->extra1);
5676
5677 rtnl_unlock();
5678 return 0;
5679}
5680
5681static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005682int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07005683 void __user *buffer, size_t *lenp, loff_t *ppos)
5684{
5685 int *valp = ctl->data;
5686 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005687 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005688 struct ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07005689 int ret;
5690
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005691 /*
5692 * ctl->data points to idev->cnf.disable_ipv6, we should
5693 * not modify it until we get the rtnl lock.
5694 */
5695 lctl = *ctl;
5696 lctl.data = &val;
5697
5698 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07005699
5700 if (write)
5701 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005702 if (ret)
5703 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07005704 return ret;
5705}
5706
stephen hemmingerc92d5492013-12-17 22:37:14 -08005707static
5708int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5709 void __user *buffer, size_t *lenp, loff_t *ppos)
5710{
5711 int *valp = ctl->data;
5712 int ret;
5713 int old, new;
5714
5715 old = *valp;
5716 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5717 new = *valp;
5718
5719 if (write && old != new) {
5720 struct net *net = ctl->extra2;
5721
5722 if (!rtnl_trylock())
5723 return restart_syscall();
5724
5725 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
David Ahern85b3daa2017-03-28 14:28:04 -07005726 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
5727 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08005728 NETCONFA_IFINDEX_DEFAULT,
5729 net->ipv6.devconf_dflt);
5730 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
David Ahern85b3daa2017-03-28 14:28:04 -07005731 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
5732 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08005733 NETCONFA_IFINDEX_ALL,
5734 net->ipv6.devconf_all);
5735 else {
5736 struct inet6_dev *idev = ctl->extra1;
5737
David Ahern85b3daa2017-03-28 14:28:04 -07005738 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
5739 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08005740 idev->dev->ifindex,
5741 &idev->cnf);
5742 }
5743 rtnl_unlock();
5744 }
5745
5746 return ret;
5747}
5748
Felix Jiad35a00b2017-01-26 16:59:17 +13005749static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
5750 void __user *buffer, size_t *lenp,
5751 loff_t *ppos)
5752{
5753 int ret = 0;
5754 int new_val;
5755 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
5756 struct net *net = (struct net *)ctl->extra2;
5757
Felix Jia8c171d62017-02-27 12:41:23 +13005758 if (!rtnl_trylock())
5759 return restart_syscall();
5760
Felix Jiad35a00b2017-01-26 16:59:17 +13005761 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5762
5763 if (write) {
5764 new_val = *((int *)ctl->data);
5765
Felix Jia8c171d62017-02-27 12:41:23 +13005766 if (check_addr_gen_mode(new_val) < 0) {
5767 ret = -EINVAL;
5768 goto out;
5769 }
Felix Jiad35a00b2017-01-26 16:59:17 +13005770
5771 /* request for default */
5772 if (&net->ipv6.devconf_dflt->addr_gen_mode == ctl->data) {
5773 ipv6_devconf_dflt.addr_gen_mode = new_val;
5774
5775 /* request for individual net device */
5776 } else {
5777 if (!idev)
Felix Jia8c171d62017-02-27 12:41:23 +13005778 goto out;
Felix Jiad35a00b2017-01-26 16:59:17 +13005779
Felix Jia8c171d62017-02-27 12:41:23 +13005780 if (check_stable_privacy(idev, net, new_val) < 0) {
5781 ret = -EINVAL;
5782 goto out;
5783 }
Felix Jiad35a00b2017-01-26 16:59:17 +13005784
5785 if (idev->cnf.addr_gen_mode != new_val) {
5786 idev->cnf.addr_gen_mode = new_val;
Felix Jiad35a00b2017-01-26 16:59:17 +13005787 addrconf_dev_config(idev->dev);
Felix Jiad35a00b2017-01-26 16:59:17 +13005788 }
5789 }
5790 }
5791
Felix Jia8c171d62017-02-27 12:41:23 +13005792out:
5793 rtnl_unlock();
5794
Felix Jiad35a00b2017-01-26 16:59:17 +13005795 return ret;
5796}
5797
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005798static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5799 void __user *buffer, size_t *lenp,
5800 loff_t *ppos)
5801{
5802 int err;
5803 struct in6_addr addr;
5804 char str[IPV6_MAX_STRLEN];
5805 struct ctl_table lctl = *ctl;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005806 struct net *net = ctl->extra2;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005807 struct ipv6_stable_secret *secret = ctl->data;
5808
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005809 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5810 return -EIO;
5811
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005812 lctl.maxlen = IPV6_MAX_STRLEN;
5813 lctl.data = str;
5814
5815 if (!rtnl_trylock())
5816 return restart_syscall();
5817
5818 if (!write && !secret->initialized) {
5819 err = -EIO;
5820 goto out;
5821 }
5822
WANG Cong5449a5c2015-12-21 10:55:45 -08005823 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
5824 if (err >= sizeof(str)) {
5825 err = -EIO;
5826 goto out;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005827 }
5828
5829 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5830 if (err || !write)
5831 goto out;
5832
5833 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5834 err = -EIO;
5835 goto out;
5836 }
5837
5838 secret->initialized = true;
5839 secret->secret = addr;
5840
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005841 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5842 struct net_device *dev;
5843
5844 for_each_netdev(net, dev) {
5845 struct inet6_dev *idev = __in6_dev_get(dev);
5846
5847 if (idev) {
Felix Jiad35a00b2017-01-26 16:59:17 +13005848 idev->cnf.addr_gen_mode =
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005849 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5850 }
5851 }
5852 } else {
5853 struct inet6_dev *idev = ctl->extra1;
5854
Felix Jiad35a00b2017-01-26 16:59:17 +13005855 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005856 }
5857
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005858out:
5859 rtnl_unlock();
5860
5861 return err;
5862}
stephen hemmingerc92d5492013-12-17 22:37:14 -08005863
Andy Gospodarek35103d12015-08-13 10:39:01 -04005864static
5865int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
5866 int write,
5867 void __user *buffer,
5868 size_t *lenp,
5869 loff_t *ppos)
5870{
5871 int *valp = ctl->data;
5872 int val = *valp;
5873 loff_t pos = *ppos;
5874 struct ctl_table lctl;
5875 int ret;
5876
5877 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
5878 * we should not modify it until we get the rtnl lock.
5879 */
5880 lctl = *ctl;
5881 lctl.data = &val;
5882
5883 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5884
5885 if (write)
5886 ret = addrconf_fixup_linkdown(ctl, valp, val);
5887 if (ret)
5888 *ppos = pos;
5889 return ret;
5890}
5891
David Forsterdf789fe2017-02-23 16:27:18 +00005892static
5893void addrconf_set_nopolicy(struct rt6_info *rt, int action)
5894{
5895 if (rt) {
5896 if (action)
5897 rt->dst.flags |= DST_NOPOLICY;
5898 else
5899 rt->dst.flags &= ~DST_NOPOLICY;
5900 }
5901}
5902
5903static
5904void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
5905{
5906 struct inet6_ifaddr *ifa;
5907
5908 read_lock_bh(&idev->lock);
5909 list_for_each_entry(ifa, &idev->addr_list, if_list) {
5910 spin_lock(&ifa->lock);
5911 if (ifa->rt) {
5912 struct rt6_info *rt = ifa->rt;
5913 struct fib6_table *table = rt->rt6i_table;
5914 int cpu;
5915
5916 read_lock(&table->tb6_lock);
5917 addrconf_set_nopolicy(ifa->rt, val);
5918 if (rt->rt6i_pcpu) {
5919 for_each_possible_cpu(cpu) {
5920 struct rt6_info **rtp;
5921
5922 rtp = per_cpu_ptr(rt->rt6i_pcpu, cpu);
5923 addrconf_set_nopolicy(*rtp, val);
5924 }
5925 }
5926 read_unlock(&table->tb6_lock);
5927 }
5928 spin_unlock(&ifa->lock);
5929 }
5930 read_unlock_bh(&idev->lock);
5931}
5932
5933static
5934int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
5935{
5936 struct inet6_dev *idev;
5937 struct net *net;
5938
5939 if (!rtnl_trylock())
5940 return restart_syscall();
5941
5942 *valp = val;
5943
5944 net = (struct net *)ctl->extra2;
5945 if (valp == &net->ipv6.devconf_dflt->disable_policy) {
5946 rtnl_unlock();
5947 return 0;
5948 }
5949
5950 if (valp == &net->ipv6.devconf_all->disable_policy) {
5951 struct net_device *dev;
5952
5953 for_each_netdev(net, dev) {
5954 idev = __in6_dev_get(dev);
5955 if (idev)
5956 addrconf_disable_policy_idev(idev, val);
5957 }
5958 } else {
5959 idev = (struct inet6_dev *)ctl->extra1;
5960 addrconf_disable_policy_idev(idev, val);
5961 }
5962
5963 rtnl_unlock();
5964 return 0;
5965}
5966
5967static
5968int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
5969 void __user *buffer, size_t *lenp,
5970 loff_t *ppos)
5971{
5972 int *valp = ctl->data;
5973 int val = *valp;
5974 loff_t pos = *ppos;
5975 struct ctl_table lctl;
5976 int ret;
5977
5978 lctl = *ctl;
5979 lctl.data = &val;
5980 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5981
5982 if (write && (*valp != val))
5983 ret = addrconf_disable_policy(ctl, valp, val);
5984
5985 if (ret)
5986 *ppos = pos;
5987
5988 return ret;
5989}
5990
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07005991static int minus_one = -1;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07005992static const int one = 1;
5993static const int two_five_five = 255;
5994
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03005995static const struct ctl_table addrconf_sysctl[] = {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005996 {
5997 .procname = "forwarding",
5998 .data = &ipv6_devconf.forwarding,
5999 .maxlen = sizeof(int),
6000 .mode = 0644,
6001 .proc_handler = addrconf_sysctl_forward,
6002 },
6003 {
6004 .procname = "hop_limit",
6005 .data = &ipv6_devconf.hop_limit,
6006 .maxlen = sizeof(int),
6007 .mode = 0644,
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006008 .proc_handler = proc_dointvec_minmax,
6009 .extra1 = (void *)&one,
6010 .extra2 = (void *)&two_five_five,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006011 },
6012 {
6013 .procname = "mtu",
6014 .data = &ipv6_devconf.mtu6,
6015 .maxlen = sizeof(int),
6016 .mode = 0644,
6017 .proc_handler = addrconf_sysctl_mtu,
6018 },
6019 {
6020 .procname = "accept_ra",
6021 .data = &ipv6_devconf.accept_ra,
6022 .maxlen = sizeof(int),
6023 .mode = 0644,
6024 .proc_handler = proc_dointvec,
6025 },
6026 {
6027 .procname = "accept_redirects",
6028 .data = &ipv6_devconf.accept_redirects,
6029 .maxlen = sizeof(int),
6030 .mode = 0644,
6031 .proc_handler = proc_dointvec,
6032 },
6033 {
6034 .procname = "autoconf",
6035 .data = &ipv6_devconf.autoconf,
6036 .maxlen = sizeof(int),
6037 .mode = 0644,
6038 .proc_handler = proc_dointvec,
6039 },
6040 {
6041 .procname = "dad_transmits",
6042 .data = &ipv6_devconf.dad_transmits,
6043 .maxlen = sizeof(int),
6044 .mode = 0644,
6045 .proc_handler = proc_dointvec,
6046 },
6047 {
6048 .procname = "router_solicitations",
6049 .data = &ipv6_devconf.rtr_solicits,
6050 .maxlen = sizeof(int),
6051 .mode = 0644,
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07006052 .proc_handler = proc_dointvec_minmax,
6053 .extra1 = &minus_one,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006054 },
6055 {
6056 .procname = "router_solicitation_interval",
6057 .data = &ipv6_devconf.rtr_solicit_interval,
6058 .maxlen = sizeof(int),
6059 .mode = 0644,
6060 .proc_handler = proc_dointvec_jiffies,
6061 },
6062 {
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07006063 .procname = "router_solicitation_max_interval",
6064 .data = &ipv6_devconf.rtr_solicit_max_interval,
6065 .maxlen = sizeof(int),
6066 .mode = 0644,
6067 .proc_handler = proc_dointvec_jiffies,
6068 },
6069 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006070 .procname = "router_solicitation_delay",
6071 .data = &ipv6_devconf.rtr_solicit_delay,
6072 .maxlen = sizeof(int),
6073 .mode = 0644,
6074 .proc_handler = proc_dointvec_jiffies,
6075 },
6076 {
6077 .procname = "force_mld_version",
6078 .data = &ipv6_devconf.force_mld_version,
6079 .maxlen = sizeof(int),
6080 .mode = 0644,
6081 .proc_handler = proc_dointvec,
6082 },
6083 {
6084 .procname = "mldv1_unsolicited_report_interval",
6085 .data =
6086 &ipv6_devconf.mldv1_unsolicited_report_interval,
6087 .maxlen = sizeof(int),
6088 .mode = 0644,
6089 .proc_handler = proc_dointvec_ms_jiffies,
6090 },
6091 {
6092 .procname = "mldv2_unsolicited_report_interval",
6093 .data =
6094 &ipv6_devconf.mldv2_unsolicited_report_interval,
6095 .maxlen = sizeof(int),
6096 .mode = 0644,
6097 .proc_handler = proc_dointvec_ms_jiffies,
6098 },
6099 {
6100 .procname = "use_tempaddr",
6101 .data = &ipv6_devconf.use_tempaddr,
6102 .maxlen = sizeof(int),
6103 .mode = 0644,
6104 .proc_handler = proc_dointvec,
6105 },
6106 {
6107 .procname = "temp_valid_lft",
6108 .data = &ipv6_devconf.temp_valid_lft,
6109 .maxlen = sizeof(int),
6110 .mode = 0644,
6111 .proc_handler = proc_dointvec,
6112 },
6113 {
6114 .procname = "temp_prefered_lft",
6115 .data = &ipv6_devconf.temp_prefered_lft,
6116 .maxlen = sizeof(int),
6117 .mode = 0644,
6118 .proc_handler = proc_dointvec,
6119 },
6120 {
6121 .procname = "regen_max_retry",
6122 .data = &ipv6_devconf.regen_max_retry,
6123 .maxlen = sizeof(int),
6124 .mode = 0644,
6125 .proc_handler = proc_dointvec,
6126 },
6127 {
6128 .procname = "max_desync_factor",
6129 .data = &ipv6_devconf.max_desync_factor,
6130 .maxlen = sizeof(int),
6131 .mode = 0644,
6132 .proc_handler = proc_dointvec,
6133 },
6134 {
6135 .procname = "max_addresses",
6136 .data = &ipv6_devconf.max_addresses,
6137 .maxlen = sizeof(int),
6138 .mode = 0644,
6139 .proc_handler = proc_dointvec,
6140 },
6141 {
6142 .procname = "accept_ra_defrtr",
6143 .data = &ipv6_devconf.accept_ra_defrtr,
6144 .maxlen = sizeof(int),
6145 .mode = 0644,
6146 .proc_handler = proc_dointvec,
6147 },
6148 {
6149 .procname = "accept_ra_min_hop_limit",
6150 .data = &ipv6_devconf.accept_ra_min_hop_limit,
6151 .maxlen = sizeof(int),
6152 .mode = 0644,
6153 .proc_handler = proc_dointvec,
6154 },
6155 {
6156 .procname = "accept_ra_pinfo",
6157 .data = &ipv6_devconf.accept_ra_pinfo,
6158 .maxlen = sizeof(int),
6159 .mode = 0644,
6160 .proc_handler = proc_dointvec,
6161 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08006162#ifdef CONFIG_IPV6_ROUTER_PREF
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006163 {
6164 .procname = "accept_ra_rtr_pref",
6165 .data = &ipv6_devconf.accept_ra_rtr_pref,
6166 .maxlen = sizeof(int),
6167 .mode = 0644,
6168 .proc_handler = proc_dointvec,
6169 },
6170 {
6171 .procname = "router_probe_interval",
6172 .data = &ipv6_devconf.rtr_probe_interval,
6173 .maxlen = sizeof(int),
6174 .mode = 0644,
6175 .proc_handler = proc_dointvec_jiffies,
6176 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08006177#ifdef CONFIG_IPV6_ROUTE_INFO
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006178 {
Joel Scherpelzbbea1242017-03-22 18:19:04 +09006179 .procname = "accept_ra_rt_info_min_plen",
6180 .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
6181 .maxlen = sizeof(int),
6182 .mode = 0644,
6183 .proc_handler = proc_dointvec,
6184 },
6185 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006186 .procname = "accept_ra_rt_info_max_plen",
6187 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
6188 .maxlen = sizeof(int),
6189 .mode = 0644,
6190 .proc_handler = proc_dointvec,
6191 },
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08006192#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08006193#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006194 {
6195 .procname = "proxy_ndp",
6196 .data = &ipv6_devconf.proxy_ndp,
6197 .maxlen = sizeof(int),
6198 .mode = 0644,
6199 .proc_handler = addrconf_sysctl_proxy_ndp,
6200 },
6201 {
6202 .procname = "accept_source_route",
6203 .data = &ipv6_devconf.accept_source_route,
6204 .maxlen = sizeof(int),
6205 .mode = 0644,
6206 .proc_handler = proc_dointvec,
6207 },
Neil Horman95c385b2007-04-25 17:08:10 -07006208#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006209 {
6210 .procname = "optimistic_dad",
6211 .data = &ipv6_devconf.optimistic_dad,
6212 .maxlen = sizeof(int),
6213 .mode = 0644,
6214 .proc_handler = proc_dointvec,
6215 },
6216 {
6217 .procname = "use_optimistic",
6218 .data = &ipv6_devconf.use_optimistic,
6219 .maxlen = sizeof(int),
6220 .mode = 0644,
6221 .proc_handler = proc_dointvec,
6222 },
Neil Horman95c385b2007-04-25 17:08:10 -07006223#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006224#ifdef CONFIG_IPV6_MROUTE
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006225 {
6226 .procname = "mc_forwarding",
6227 .data = &ipv6_devconf.mc_forwarding,
6228 .maxlen = sizeof(int),
6229 .mode = 0444,
6230 .proc_handler = proc_dointvec,
6231 },
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006232#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006233 {
6234 .procname = "disable_ipv6",
6235 .data = &ipv6_devconf.disable_ipv6,
6236 .maxlen = sizeof(int),
6237 .mode = 0644,
6238 .proc_handler = addrconf_sysctl_disable,
6239 },
6240 {
6241 .procname = "accept_dad",
6242 .data = &ipv6_devconf.accept_dad,
6243 .maxlen = sizeof(int),
6244 .mode = 0644,
6245 .proc_handler = proc_dointvec,
6246 },
6247 {
6248 .procname = "force_tllao",
6249 .data = &ipv6_devconf.force_tllao,
6250 .maxlen = sizeof(int),
6251 .mode = 0644,
6252 .proc_handler = proc_dointvec
6253 },
6254 {
6255 .procname = "ndisc_notify",
6256 .data = &ipv6_devconf.ndisc_notify,
6257 .maxlen = sizeof(int),
6258 .mode = 0644,
6259 .proc_handler = proc_dointvec
6260 },
6261 {
6262 .procname = "suppress_frag_ndisc",
6263 .data = &ipv6_devconf.suppress_frag_ndisc,
6264 .maxlen = sizeof(int),
6265 .mode = 0644,
6266 .proc_handler = proc_dointvec
6267 },
6268 {
6269 .procname = "accept_ra_from_local",
6270 .data = &ipv6_devconf.accept_ra_from_local,
6271 .maxlen = sizeof(int),
6272 .mode = 0644,
6273 .proc_handler = proc_dointvec,
6274 },
6275 {
6276 .procname = "accept_ra_mtu",
6277 .data = &ipv6_devconf.accept_ra_mtu,
6278 .maxlen = sizeof(int),
6279 .mode = 0644,
6280 .proc_handler = proc_dointvec,
6281 },
6282 {
6283 .procname = "stable_secret",
6284 .data = &ipv6_devconf.stable_secret,
6285 .maxlen = IPV6_MAX_STRLEN,
6286 .mode = 0600,
6287 .proc_handler = addrconf_sysctl_stable_secret,
6288 },
6289 {
6290 .procname = "use_oif_addrs_only",
6291 .data = &ipv6_devconf.use_oif_addrs_only,
6292 .maxlen = sizeof(int),
6293 .mode = 0644,
6294 .proc_handler = proc_dointvec,
6295 },
6296 {
6297 .procname = "ignore_routes_with_linkdown",
6298 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6299 .maxlen = sizeof(int),
6300 .mode = 0644,
6301 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6302 },
6303 {
6304 .procname = "drop_unicast_in_l2_multicast",
6305 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6306 .maxlen = sizeof(int),
6307 .mode = 0644,
6308 .proc_handler = proc_dointvec,
6309 },
6310 {
6311 .procname = "drop_unsolicited_na",
6312 .data = &ipv6_devconf.drop_unsolicited_na,
6313 .maxlen = sizeof(int),
6314 .mode = 0644,
6315 .proc_handler = proc_dointvec,
6316 },
6317 {
6318 .procname = "keep_addr_on_down",
6319 .data = &ipv6_devconf.keep_addr_on_down,
6320 .maxlen = sizeof(int),
6321 .mode = 0644,
6322 .proc_handler = proc_dointvec,
David Ahernf1705ec2016-02-24 09:25:37 -08006323
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006324 },
6325 {
David Lebrun1ababeb2016-11-08 14:57:39 +01006326 .procname = "seg6_enabled",
6327 .data = &ipv6_devconf.seg6_enabled,
6328 .maxlen = sizeof(int),
6329 .mode = 0644,
6330 .proc_handler = proc_dointvec,
6331 },
David Lebrunbf355b82016-11-08 14:57:42 +01006332#ifdef CONFIG_IPV6_SEG6_HMAC
6333 {
6334 .procname = "seg6_require_hmac",
6335 .data = &ipv6_devconf.seg6_require_hmac,
6336 .maxlen = sizeof(int),
6337 .mode = 0644,
6338 .proc_handler = proc_dointvec,
6339 },
6340#endif
David Lebrun1ababeb2016-11-08 14:57:39 +01006341 {
Erik Nordmarkadc176c2016-12-02 14:00:08 -08006342 .procname = "enhanced_dad",
6343 .data = &ipv6_devconf.enhanced_dad,
6344 .maxlen = sizeof(int),
6345 .mode = 0644,
6346 .proc_handler = proc_dointvec,
6347 },
6348 {
Felix Jiad35a00b2017-01-26 16:59:17 +13006349 .procname = "addr_gen_mode",
6350 .data = &ipv6_devconf.addr_gen_mode,
6351 .maxlen = sizeof(int),
6352 .mode = 0644,
6353 .proc_handler = addrconf_sysctl_addr_gen_mode,
6354 },
6355 {
David Forsterdf789fe2017-02-23 16:27:18 +00006356 .procname = "disable_policy",
6357 .data = &ipv6_devconf.disable_policy,
6358 .maxlen = sizeof(int),
6359 .mode = 0644,
6360 .proc_handler = addrconf_sysctl_disable_policy,
6361 },
6362 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006363 /* sentinel */
6364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365};
6366
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08006367static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006368 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369{
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006370 int i, ifindex;
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006371 struct ctl_table *table;
Eric W. Biederman6105e292012-04-19 13:41:24 +00006372 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11006373
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006374 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6375 if (!table)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006376 goto out;
6377
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006378 for (i = 0; table[i].data; i++) {
6379 table[i].data += (char *)p - (char *)&ipv6_devconf;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006380 /* If one of these is already set, then it is not safe to
6381 * overwrite either of them: this makes proc_dointvec_minmax
6382 * usable.
6383 */
6384 if (!table[i].extra1 && !table[i].extra2) {
6385 table[i].extra1 = idev; /* embedded; no ref */
6386 table[i].extra2 = net;
6387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389
Eric W. Biederman6105e292012-04-19 13:41:24 +00006390 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006392 p->sysctl_header = register_net_sysctl(net, path, table);
6393 if (!p->sysctl_header)
Eric W. Biederman6105e292012-04-19 13:41:24 +00006394 goto free;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006395
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006396 if (!strcmp(dev_name, "all"))
6397 ifindex = NETCONFA_IFINDEX_ALL;
6398 else if (!strcmp(dev_name, "default"))
6399 ifindex = NETCONFA_IFINDEX_DEFAULT;
6400 else
6401 ifindex = idev->dev->ifindex;
David Ahern85b3daa2017-03-28 14:28:04 -07006402 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
6403 ifindex, p);
Pavel Emelyanov95897312008-01-10 17:41:45 -08006404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006406free:
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006407 kfree(table);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006408out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08006409 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410}
6411
David Ahern23452172017-03-28 14:28:05 -07006412static void __addrconf_sysctl_unregister(struct net *net,
6413 struct ipv6_devconf *p, int ifindex)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006414{
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006415 struct ctl_table *table;
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006416
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006417 if (!p->sysctl_header)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006418 return;
6419
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006420 table = p->sysctl_header->ctl_table_arg;
6421 unregister_net_sysctl_table(p->sysctl_header);
6422 p->sysctl_header = NULL;
6423 kfree(table);
David Ahern23452172017-03-28 14:28:05 -07006424
6425 inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006426}
6427
WANG Conga317a2f2014-07-25 15:25:09 -07006428static int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006429{
WANG Conga317a2f2014-07-25 15:25:09 -07006430 int err;
6431
6432 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6433 return -EINVAL;
6434
6435 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6436 &ndisc_ifinfo_sysctl_change);
6437 if (err)
6438 return err;
6439 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6440 idev, &idev->cnf);
6441 if (err)
6442 neigh_sysctl_unregister(idev->nd_parms);
6443
6444 return err;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006445}
6446
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006447static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448{
David Ahern23452172017-03-28 14:28:05 -07006449 __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
6450 idev->dev->ifindex);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006451 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452}
6453
6454
6455#endif
6456
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006457static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006458{
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006459 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006460 struct ipv6_devconf *all, *dflt;
6461
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006462 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006463 if (!all)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006464 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006465
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006466 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006467 if (!dflt)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006468 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006469
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006470 /* these will be inherited by all namespaces */
6471 dflt->autoconf = ipv6_defaults.autoconf;
6472 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006473
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006474 dflt->stable_secret.initialized = false;
6475 all->stable_secret.initialized = false;
6476
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006477 net->ipv6.devconf_all = all;
6478 net->ipv6.devconf_dflt = dflt;
6479
6480#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006481 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006482 if (err < 0)
6483 goto err_reg_all;
6484
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006485 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006486 if (err < 0)
6487 goto err_reg_dflt;
6488#endif
6489 return 0;
6490
6491#ifdef CONFIG_SYSCTL
6492err_reg_dflt:
David Ahern23452172017-03-28 14:28:05 -07006493 __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006494err_reg_all:
6495 kfree(dflt);
6496#endif
6497err_alloc_dflt:
6498 kfree(all);
6499err_alloc_all:
6500 return err;
6501}
6502
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006503static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006504{
6505#ifdef CONFIG_SYSCTL
David Ahern23452172017-03-28 14:28:05 -07006506 __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
6507 NETCONFA_IFINDEX_DEFAULT);
6508 __addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
6509 NETCONFA_IFINDEX_ALL);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006510#endif
zhuyj73cf0e922014-11-26 10:25:58 +08006511 kfree(net->ipv6.devconf_dflt);
6512 kfree(net->ipv6.devconf_all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006513}
6514
6515static struct pernet_operations addrconf_ops = {
6516 .init = addrconf_init_net,
6517 .exit = addrconf_exit_net,
6518};
6519
Daniel Borkmann207895fd32015-01-29 12:15:03 +01006520static struct rtnl_af_ops inet6_ops __read_mostly = {
Thomas Grafb382b192010-11-16 04:33:57 +00006521 .family = AF_INET6,
6522 .fill_link_af = inet6_fill_link_af,
6523 .get_link_af_size = inet6_get_link_af_size,
Daniel Borkmann11b1f822015-02-05 14:39:11 +01006524 .validate_link_af = inet6_validate_link_af,
Daniel Borkmannf53adae2013-04-08 04:01:30 +00006525 .set_link_af = inet6_set_link_af,
Thomas Grafb382b192010-11-16 04:33:57 +00006526};
6527
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528/*
6529 * Init / cleanup code
6530 */
6531
6532int __init addrconf_init(void)
6533{
WANG Conga317a2f2014-07-25 15:25:09 -07006534 struct inet6_dev *idev;
stephen hemmingerc2e21292010-03-17 20:31:10 +00006535 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006536
Stephen Hemmingere21e8462010-03-20 16:09:01 -07006537 err = ipv6_addr_label_init();
6538 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00006539 pr_crit("%s: cannot initialize default policy table: %d\n",
6540 __func__, err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006541 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006544 err = register_pernet_subsys(&addrconf_ops);
6545 if (err < 0)
6546 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006547
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006548 addrconf_wq = create_workqueue("ipv6_addrconf");
6549 if (!addrconf_wq) {
6550 err = -ENOMEM;
6551 goto out_nowq;
6552 }
6553
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554 /* The addrconf netdev notifier requires that loopback_dev
6555 * has it's ipv6 private information allocated and setup
6556 * before it can bring up and give link-local addresses
6557 * to other devices which are up.
6558 *
6559 * Unfortunately, loopback_dev is not necessarily the first
6560 * entry in the global dev_base list of net devices. In fact,
6561 * it is likely to be the very last entry on that list.
6562 * So this causes the notifier registry below to try and
6563 * give link-local addresses to all devices besides loopback_dev
6564 * first, then loopback_dev, which cases all the non-loopback_dev
6565 * devices to fail to get a link-local address.
6566 *
6567 * So, as a temporary fix, allocate the ipv6 structure for
6568 * loopback_dev first by hand.
6569 * Longer term, all of the dependencies ipv6 has upon the loopback
6570 * device and it being up should be removed.
6571 */
6572 rtnl_lock();
WANG Conga317a2f2014-07-25 15:25:09 -07006573 idev = ipv6_add_dev(init_net.loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574 rtnl_unlock();
WANG Conga317a2f2014-07-25 15:25:09 -07006575 if (IS_ERR(idev)) {
6576 err = PTR_ERR(idev);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006577 goto errlo;
WANG Conga317a2f2014-07-25 15:25:09 -07006578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579
WANG Cong2f460932017-05-03 22:07:31 -07006580 ip6_route_init_special_entries();
6581
stephen hemmingerc2e21292010-03-17 20:31:10 +00006582 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6583 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6584
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585 register_netdevice_notifier(&ipv6_dev_notf);
6586
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006587 addrconf_verify();
Thomas Grafc127ea22007-03-22 11:58:32 -07006588
stephen hemminger3678a9d2013-12-30 10:41:32 -08006589 rtnl_af_register(&inet6_ops);
Thomas Grafb382b192010-11-16 04:33:57 +00006590
Greg Rosec7ac8672011-06-10 01:27:09 +00006591 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6592 NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006593 if (err < 0)
6594 goto errout;
6595
6596 /* Only the first call to __rtnl_register can fail */
Greg Rosec7ac8672011-06-10 01:27:09 +00006597 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
6598 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
6599 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6600 inet6_dump_ifaddr, NULL);
6601 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6602 inet6_dump_ifmcaddr, NULL);
6603 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6604 inet6_dump_ifacaddr, NULL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +00006605 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
Nicolas Dichtel7a674202013-03-05 23:42:06 +00006606 inet6_netconf_dump_devconf, NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006607
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006608 ipv6_addr_label_rtnl_register();
6609
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07006611errout:
Thomas Grafb382b192010-11-16 04:33:57 +00006612 rtnl_af_unregister(&inet6_ops);
Thomas Grafc127ea22007-03-22 11:58:32 -07006613 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006614errlo:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006615 destroy_workqueue(addrconf_wq);
6616out_nowq:
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006617 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006618out_addrlabel:
6619 ipv6_addr_label_cleanup();
6620out:
Thomas Grafc127ea22007-03-22 11:58:32 -07006621 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622}
6623
Daniel Lezcano09f77092007-12-13 05:34:58 -08006624void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006626 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627 int i;
6628
6629 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006630 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006631 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632
6633 rtnl_lock();
6634
Thomas Grafb382b192010-11-16 04:33:57 +00006635 __rtnl_af_unregister(&inet6_ops);
6636
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006637 /* clean dev list */
6638 for_each_netdev(&init_net, dev) {
6639 if (__in6_dev_get(dev) == NULL)
6640 continue;
6641 addrconf_ifdown(dev, 1);
6642 }
6643 addrconf_ifdown(init_net.loopback_dev, 2);
6644
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646 * Check hash table.
6647 */
stephen hemminger5c578aed2010-03-17 20:31:11 +00006648 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00006649 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6650 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578aed2010-03-17 20:31:11 +00006651 spin_unlock_bh(&addrconf_hash_lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006652 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 rtnl_unlock();
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006654
6655 destroy_workqueue(addrconf_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656}