blob: 0ea96c4d334da2821a8d9c0e5e7d0d513dcb4228 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
15/*
16 * Changes:
17 *
18 * Janos Farkas : delete timer on ifdown
19 * <chexum@bankinf.banki.hu>
20 * Andi Kleen : kill double kfree on module
21 * unload.
22 * Maciej W. Rozycki : FDDI support
23 * sekiya@USAGI : Don't send too many RS
24 * packets.
25 * yoshfuji@USAGI : Fixed interval between DAD
26 * packets.
27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
28 * address validation timer.
29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
30 * support.
31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
32 * address on a same interface.
33 * YOSHIFUJI Hideaki @USAGI : ARCnet support
34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
35 * seq_file.
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -080036 * YOSHIFUJI Hideaki @USAGI : improved source address
37 * selection; consider scope,
38 * status etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
40
Joe Perchesf3213832012-05-15 14:11:53 +000041#define pr_fmt(fmt) "IPv6: " fmt
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/errno.h>
44#include <linux/types.h>
Ilpo Järvinena0bffff2009-03-21 13:36:17 -070045#include <linux/kernel.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010046#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/socket.h>
48#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/net.h>
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +010050#include <linux/inet.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/in6.h>
52#include <linux/netdevice.h>
Thomas Graf18237302006-08-04 23:04:54 -070053#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/if_arp.h>
55#include <linux/if_arcnet.h>
56#include <linux/if_infiniband.h>
57#include <linux/route.h>
58#include <linux/inetdevice.h>
59#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090060#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#ifdef CONFIG_SYSCTL
62#include <linux/sysctl.h>
63#endif
Randy Dunlap4fc268d2006-01-11 12:17:47 -080064#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/delay.h>
66#include <linux/notifier.h>
Paulo Marques543537b2005-06-23 00:09:02 -070067#include <linux/string.h>
Eric Dumazetddbe5032012-07-18 08:11:12 +000068#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020070#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <net/sock.h>
72#include <net/snmp.h>
73
Alexander Aring5241c2d2015-12-14 20:55:22 +010074#include <net/6lowpan.h>
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +000075#include <net/firewire.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <net/ipv6.h>
77#include <net/protocol.h>
78#include <net/ndisc.h>
79#include <net/ip6_route.h>
80#include <net/addrconf.h>
81#include <net/tcp.h>
82#include <net/ip.h>
Thomas Graf5d620262006-08-15 00:35:02 -070083#include <net/netlink.h>
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -070084#include <net/pkt_sched.h>
David Ahernca254492015-10-12 11:47:10 -070085#include <net/l3mdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include <linux/if_tunnel.h>
87#include <linux/rtnetlink.h>
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +000088#include <linux/netconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <linux/random.h>
Stephen Hemmingere21e8462010-03-20 16:09:01 -070090#include <linux/uaccess.h>
Herbert Xu7f7d9a62007-04-24 21:54:09 -070091#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93#include <linux/proc_fs.h>
94#include <linux/seq_file.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040095#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/* Set to 3 to get tracing... */
98#define ACONF_DEBUG 2
99
100#if ACONF_DEBUG >= 3
dingtianhongba3542e2013-08-17 10:27:04 +0800101#define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#else
dingtianhongba3542e2013-08-17 10:27:04 +0800103#define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#endif
105
106#define INFINITY_LIFE_TIME 0xFFFFFFFF
Thomas Graf18a31e12010-11-17 04:12:02 +0000107
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100108#define IPV6_MAX_STRLEN \
109 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
110
Thomas Graf18a31e12010-11-17 04:12:02 +0000111static inline u32 cstamp_delta(unsigned long cstamp)
112{
113 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
114}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700116static inline s32 rfc3315_s14_backoff_init(s32 irt)
117{
118 /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
119 u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
120 do_div(tmp, 1000000);
121 return (s32)tmp;
122}
123
124static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
125{
126 /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
127 u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
128 do_div(tmp, 1000000);
129 if ((s32)tmp > mrt) {
130 /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
131 tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
132 do_div(tmp, 1000000);
133 }
134 return (s32)tmp;
135}
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#ifdef CONFIG_SYSCTL
WANG Conga317a2f2014-07-25 15:25:09 -0700138static int addrconf_sysctl_register(struct inet6_dev *idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800139static void addrconf_sysctl_unregister(struct inet6_dev *idev);
140#else
WANG Conga317a2f2014-07-25 15:25:09 -0700141static inline int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800142{
WANG Conga317a2f2014-07-25 15:25:09 -0700143 return 0;
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800144}
145
146static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
147{
148}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#endif
150
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200151static void ipv6_regen_rndid(struct inet6_dev *idev);
152static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900154static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static int ipv6_count_addresses(struct inet6_dev *idev);
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +0100156static int ipv6_generate_stable_address(struct in6_addr *addr,
157 u8 dad_count,
158 const struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160/*
161 * Configured unicast address hash table
162 */
stephen hemmingerc2e21292010-03-17 20:31:10 +0000163static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
stephen hemminger5c578aed2010-03-17 20:31:11 +0000164static DEFINE_SPINLOCK(addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100166static void addrconf_verify(void);
167static void addrconf_verify_rtnl(void);
168static void addrconf_verify_work(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100170static struct workqueue_struct *addrconf_wq;
171static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
174static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
175
Jiri Pirko93d9b7d2010-03-10 10:28:56 +0000176static void addrconf_type_change(struct net_device *dev,
177 unsigned long event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178static int addrconf_ifdown(struct net_device *dev, int how);
179
Romain Kuntz21caa662013-01-09 21:06:03 +0000180static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
181 int plen,
182 const struct net_device *dev,
183 u32 flags, u32 noflags);
184
David S. Millercf22f9a2012-04-14 21:37:40 -0400185static void addrconf_dad_start(struct inet6_ifaddr *ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100186static void addrconf_dad_work(struct work_struct *w);
Paolo Abeni764d3be2016-11-22 16:57:40 +0100187static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900188static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static void addrconf_rs_timer(unsigned long data);
190static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
191static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
192
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900193static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct prefix_info *pinfo);
David S. Miller3e81c6d2010-03-20 16:18:00 -0700195static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
196 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Adrian Bunk888c8482008-07-22 14:21:58 -0700198static struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 .forwarding = 0,
200 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
201 .mtu6 = IPV6_MIN_MTU,
202 .accept_ra = 1,
203 .accept_redirects = 1,
204 .autoconf = 1,
205 .force_mld_version = 0,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200206 .mldv1_unsolicited_report_interval = 10 * HZ,
207 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 .dad_transmits = 1,
209 .rtr_solicits = MAX_RTR_SOLICITATIONS,
210 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700211 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Ian Morris67ba4152014-08-24 21:53:10 +0100213 .use_tempaddr = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 .temp_valid_lft = TEMP_VALID_LIFETIME,
215 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
216 .regen_max_retry = REGEN_MAX_RETRY,
217 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800219 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700220 .accept_ra_from_local = 0,
Hangbin Liu8013d1d2015-07-30 14:28:42 +0800221 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800222 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800223#ifdef CONFIG_IPV6_ROUTER_PREF
224 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800225 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800226#ifdef CONFIG_IPV6_ROUTE_INFO
227 .accept_ra_rt_info_max_plen = 0,
228#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800229#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700230 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700231 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900232 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900233 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200234 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700235 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100236 .stable_secret = {
237 .initialized = false,
Erik Kline3985e8a2015-07-22 16:38:25 +0900238 },
239 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400240 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800241 .keep_addr_on_down = 0,
David Lebrun1ababeb2016-11-08 14:57:39 +0100242 .seg6_enabled = 0,
David Lebrunbf355b82016-11-08 14:57:42 +0100243#ifdef CONFIG_IPV6_SEG6_HMAC
244 .seg6_require_hmac = 0,
245#endif
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800246 .enhanced_dad = 1,
Felix Jiad35a00b2017-01-26 16:59:17 +1300247 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248};
249
Brian Haleyab32ea52006-09-22 14:15:41 -0700250static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 .forwarding = 0,
252 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
253 .mtu6 = IPV6_MIN_MTU,
254 .accept_ra = 1,
255 .accept_redirects = 1,
256 .autoconf = 1,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200257 .force_mld_version = 0,
258 .mldv1_unsolicited_report_interval = 10 * HZ,
259 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 .dad_transmits = 1,
261 .rtr_solicits = MAX_RTR_SOLICITATIONS,
262 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700263 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 .use_tempaddr = 0,
266 .temp_valid_lft = TEMP_VALID_LIFETIME,
267 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
268 .regen_max_retry = REGEN_MAX_RETRY,
269 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800271 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700272 .accept_ra_from_local = 0,
Hangbin Liu8013d1d2015-07-30 14:28:42 +0800273 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800274 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800275#ifdef CONFIG_IPV6_ROUTER_PREF
276 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800277 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800278#ifdef CONFIG_IPV6_ROUTE_INFO
279 .accept_ra_rt_info_max_plen = 0,
280#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800281#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700282 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700283 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900284 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900285 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200286 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700287 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100288 .stable_secret = {
289 .initialized = false,
290 },
Erik Kline3985e8a2015-07-22 16:38:25 +0900291 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400292 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800293 .keep_addr_on_down = 0,
David Lebrun1ababeb2016-11-08 14:57:39 +0100294 .seg6_enabled = 0,
David Lebrunbf355b82016-11-08 14:57:42 +0100295#ifdef CONFIG_IPV6_SEG6_HMAC
296 .seg6_require_hmac = 0,
297#endif
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800298 .enhanced_dad = 1,
Felix Jiad35a00b2017-01-26 16:59:17 +1300299 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300};
301
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700302/* Check if a valid qdisc is available */
David S. Miller05297942008-07-08 23:01:27 -0700303static inline bool addrconf_qdisc_ok(const struct net_device *dev)
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700304{
David S. Miller05297942008-07-08 23:01:27 -0700305 return !qdisc_tx_is_noop(dev);
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700306}
307
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200308static void addrconf_del_rs_timer(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200310 if (del_timer(&idev->rs_timer))
311 __in6_dev_put(idev);
312}
313
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100314static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200315{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100316 if (cancel_delayed_work(&ifp->dad_work))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 __in6_ifa_put(ifp);
318}
319
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200320static void addrconf_mod_rs_timer(struct inet6_dev *idev,
321 unsigned long when)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200323 if (!timer_pending(&idev->rs_timer))
324 in6_dev_hold(idev);
325 mod_timer(&idev->rs_timer, jiffies + when);
326}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100328static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
329 unsigned long delay)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200330{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100331 if (!delayed_work_pending(&ifp->dad_work))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200332 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100333 mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700336static int snmp6_alloc_dev(struct inet6_dev *idev)
337{
John Stultz827da442013-10-07 15:51:58 -0700338 int i;
339
WANG Cong698365f2014-05-05 15:55:55 -0700340 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
341 if (!idev->stats.ipv6)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700342 goto err_ip;
John Stultz827da442013-10-07 15:51:58 -0700343
344 for_each_possible_cpu(i) {
345 struct ipstats_mib *addrconf_stats;
WANG Cong698365f2014-05-05 15:55:55 -0700346 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
John Stultz827da442013-10-07 15:51:58 -0700347 u64_stats_init(&addrconf_stats->syncp);
John Stultz827da442013-10-07 15:51:58 -0700348 }
349
350
Eric Dumazetbe281e52011-05-19 01:14:23 +0000351 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
352 GFP_KERNEL);
353 if (!idev->stats.icmpv6dev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700354 goto err_icmp;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000355 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
356 GFP_KERNEL);
357 if (!idev->stats.icmpv6msgdev)
David L Stevens14878f72007-09-16 16:52:35 -0700358 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700359
360 return 0;
361
David L Stevens14878f72007-09-16 16:52:35 -0700362err_icmpmsg:
Eric Dumazetbe281e52011-05-19 01:14:23 +0000363 kfree(idev->stats.icmpv6dev);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700364err_icmp:
WANG Cong698365f2014-05-05 15:55:55 -0700365 free_percpu(idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700366err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700367 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700368}
369
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000370static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 struct inet6_dev *ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700373 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 ASSERT_RTNL();
376
377 if (dev->mtu < IPV6_MIN_MTU)
WANG Conga317a2f2014-07-25 15:25:09 -0700378 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900380 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100381 if (!ndev)
WANG Conga317a2f2014-07-25 15:25:09 -0700382 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Ingo Oeser322f74a2006-03-20 23:01:47 -0800384 rwlock_init(&ndev->lock);
385 ndev->dev = dev;
stephen hemminger502a2ff2010-03-17 20:31:13 +0000386 INIT_LIST_HEAD(&ndev->addr_list);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200387 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
388 (unsigned long)ndev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900389 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100390
391 if (ndev->cnf.stable_secret.initialized)
Felix Jiad35a00b2017-01-26 16:59:17 +1300392 ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100393 else
Felix Jiad35a00b2017-01-26 16:59:17 +1300394 ndev->cnf.addr_gen_mode = ipv6_devconf_dflt.addr_gen_mode;
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100395
Ingo Oeser322f74a2006-03-20 23:01:47 -0800396 ndev->cnf.mtu6 = dev->mtu;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800397 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
Ian Morris63159f22015-03-29 14:00:04 +0100398 if (!ndev->nd_parms) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800399 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700400 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800401 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700402 if (ndev->cnf.forwarding)
403 dev_disable_lro(dev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800404 /* We refer to the device */
405 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Ingo Oeser322f74a2006-03-20 23:01:47 -0800407 if (snmp6_alloc_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800408 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000409 "%s: cannot allocate memory for statistics; dev=%s.\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800410 __func__, dev->name);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800411 neigh_parms_release(&nd_tbl, ndev->nd_parms);
Roy Li8603e332011-09-20 15:10:16 -0400412 dev_put(dev);
413 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700414 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Ingo Oeser322f74a2006-03-20 23:01:47 -0800417 if (snmp6_register_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800418 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000419 "%s: cannot create /proc/net/dev_snmp6/%s\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800420 __func__, dev->name);
WANG Conga317a2f2014-07-25 15:25:09 -0700421 goto err_release;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200424 /* One reference from device. */
Ingo Oeser322f74a2006-03-20 23:01:47 -0800425 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900427 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
428 ndev->cnf.accept_dad = -1;
429
Amerigo Wang07a93622012-10-29 16:23:10 +0000430#if IS_ENABLED(CONFIG_IPV6_SIT)
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700431 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
Joe Perchesf3213832012-05-15 14:11:53 +0000432 pr_info("%s: Disabled Multicast RS\n", dev->name);
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700433 ndev->cnf.rtr_solicits = 0;
434 }
435#endif
436
stephen hemminger372e6c82010-03-17 20:31:09 +0000437 INIT_LIST_HEAD(&ndev->tempaddr_list);
Jiri Bohac76506a92016-10-13 18:52:15 +0200438 ndev->desync_factor = U32_MAX;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800439 if ((dev->flags&IFF_LOOPBACK) ||
440 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700441 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700442 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700443 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800444 ndev->cnf.use_tempaddr = -1;
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200445 } else
446 ipv6_regen_rndid(ndev);
David S. Miller5d9efa72013-10-28 20:07:50 -0400447
Daniel Borkmann914faa12013-04-09 03:47:14 +0000448 ndev->token = in6addr_any;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800449
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700450 if (netif_running(dev) && addrconf_qdisc_ok(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700451 ndev->if_flags |= IF_READY;
452
Ingo Oeser322f74a2006-03-20 23:01:47 -0800453 ipv6_mc_init_dev(ndev);
454 ndev->tstamp = jiffies;
WANG Conga317a2f2014-07-25 15:25:09 -0700455 err = addrconf_sysctl_register(ndev);
456 if (err) {
457 ipv6_mc_destroy_dev(ndev);
Sabrina Dubroca2a189f9e2015-11-04 14:47:53 +0100458 snmp6_unregister_dev(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700459 goto err_release;
460 }
David L Stevens30c4cf52007-01-04 12:31:14 -0800461 /* protected by rtnl_lock */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000462 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800463
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000464 /* Join interface-local all-node multicast group */
465 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
466
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800467 /* Join all-node multicast group */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900468 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800469
Li Weid6ddef92012-03-05 14:45:17 +0000470 /* Join all-router multicast group if forwarding is set */
Li Wei8b2aaed2012-03-07 14:58:07 +0000471 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
Li Weid6ddef92012-03-05 14:45:17 +0000472 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700475
476err_release:
477 neigh_parms_release(&nd_tbl, ndev->nd_parms);
478 ndev->dead = 1;
479 in6_dev_finish_destroy(ndev);
480 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000483static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 struct inet6_dev *idev;
486
487 ASSERT_RTNL();
488
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700489 idev = __in6_dev_get(dev);
490 if (!idev) {
491 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -0700492 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return NULL;
494 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (dev->flags&IFF_UP)
497 ipv6_mc_up(idev);
498 return idev;
499}
500
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000501static int inet6_netconf_msgsize_devconf(int type)
502{
503 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
504 + nla_total_size(4); /* NETCONFA_IFINDEX */
Zhang Shengju136ba622016-03-10 08:55:50 +0000505 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000506
Zhang Shengju136ba622016-03-10 08:55:50 +0000507 if (type == NETCONFA_ALL)
508 all = true;
509
510 if (all || type == NETCONFA_FORWARDING)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000511 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500512#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000513 if (all || type == NETCONFA_MC_FORWARDING)
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000514 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500515#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000516 if (all || type == NETCONFA_PROXY_NEIGH)
stephen hemmingerc92d5492013-12-17 22:37:14 -0800517 size += nla_total_size(4);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000518
Zhang Shengju136ba622016-03-10 08:55:50 +0000519 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
Andy Gospodarek35103d12015-08-13 10:39:01 -0400520 size += nla_total_size(4);
521
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000522 return size;
523}
524
525static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
526 struct ipv6_devconf *devconf, u32 portid,
527 u32 seq, int event, unsigned int flags,
528 int type)
529{
530 struct nlmsghdr *nlh;
531 struct netconfmsg *ncm;
Zhang Shengju136ba622016-03-10 08:55:50 +0000532 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000533
534 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
535 flags);
Ian Morris63159f22015-03-29 14:00:04 +0100536 if (!nlh)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000537 return -EMSGSIZE;
538
Zhang Shengju136ba622016-03-10 08:55:50 +0000539 if (type == NETCONFA_ALL)
540 all = true;
541
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000542 ncm = nlmsg_data(nlh);
543 ncm->ncm_family = AF_INET6;
544
545 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
546 goto nla_put_failure;
547
Zhang Shengju136ba622016-03-10 08:55:50 +0000548 if ((all || type == NETCONFA_FORWARDING) &&
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000549 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
550 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500551#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000552 if ((all || type == NETCONFA_MC_FORWARDING) &&
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000553 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
554 devconf->mc_forwarding) < 0)
555 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500556#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000557 if ((all || type == NETCONFA_PROXY_NEIGH) &&
stephen hemmingerc92d5492013-12-17 22:37:14 -0800558 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
559 goto nla_put_failure;
560
Zhang Shengju136ba622016-03-10 08:55:50 +0000561 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
Andy Gospodarek35103d12015-08-13 10:39:01 -0400562 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
563 devconf->ignore_routes_with_linkdown) < 0)
564 goto nla_put_failure;
565
Johannes Berg053c0952015-01-16 22:09:00 +0100566 nlmsg_end(skb, nlh);
567 return 0;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000568
569nla_put_failure:
570 nlmsg_cancel(skb, nlh);
571 return -EMSGSIZE;
572}
573
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000574void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
575 struct ipv6_devconf *devconf)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000576{
577 struct sk_buff *skb;
578 int err = -ENOBUFS;
579
Eric Dumazet927265b2016-07-08 05:46:04 +0200580 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100581 if (!skb)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000582 goto errout;
583
584 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
585 RTM_NEWNETCONF, 0, type);
586 if (err < 0) {
587 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
588 WARN_ON(err == -EMSGSIZE);
589 kfree_skb(skb);
590 goto errout;
591 }
Eric Dumazet927265b2016-07-08 05:46:04 +0200592 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000593 return;
594errout:
Cong Dingbd779022012-12-18 12:08:56 +0000595 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000596}
597
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000598static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
599 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
600 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
stephen hemmingerc92d5492013-12-17 22:37:14 -0800601 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
Andy Gospodarek35103d12015-08-13 10:39:01 -0400602 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000603};
604
605static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
Thomas Graf661d2962013-03-21 07:45:29 +0000606 struct nlmsghdr *nlh)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000607{
608 struct net *net = sock_net(in_skb->sk);
609 struct nlattr *tb[NETCONFA_MAX+1];
610 struct netconfmsg *ncm;
611 struct sk_buff *skb;
612 struct ipv6_devconf *devconf;
613 struct inet6_dev *in6_dev;
614 struct net_device *dev;
615 int ifindex;
616 int err;
617
618 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
619 devconf_ipv6_policy);
620 if (err < 0)
621 goto errout;
622
Anton Protopopova97eb332016-02-16 21:43:16 -0500623 err = -EINVAL;
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000624 if (!tb[NETCONFA_IFINDEX])
625 goto errout;
626
627 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
628 switch (ifindex) {
629 case NETCONFA_IFINDEX_ALL:
630 devconf = net->ipv6.devconf_all;
631 break;
632 case NETCONFA_IFINDEX_DEFAULT:
633 devconf = net->ipv6.devconf_dflt;
634 break;
635 default:
636 dev = __dev_get_by_index(net, ifindex);
Ian Morris63159f22015-03-29 14:00:04 +0100637 if (!dev)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000638 goto errout;
639 in6_dev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +0100640 if (!in6_dev)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000641 goto errout;
642 devconf = &in6_dev->cnf;
643 break;
644 }
645
646 err = -ENOBUFS;
Zhang Shengju136ba622016-03-10 08:55:50 +0000647 skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +0100648 if (!skb)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000649 goto errout;
650
651 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
652 NETLINK_CB(in_skb).portid,
653 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
Zhang Shengju136ba622016-03-10 08:55:50 +0000654 NETCONFA_ALL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000655 if (err < 0) {
656 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
657 WARN_ON(err == -EMSGSIZE);
658 kfree_skb(skb);
659 goto errout;
660 }
661 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
662errout:
663 return err;
664}
665
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000666static int inet6_netconf_dump_devconf(struct sk_buff *skb,
667 struct netlink_callback *cb)
668{
669 struct net *net = sock_net(skb->sk);
670 int h, s_h;
671 int idx, s_idx;
672 struct net_device *dev;
673 struct inet6_dev *idev;
674 struct hlist_head *head;
675
676 s_h = cb->args[0];
677 s_idx = idx = cb->args[1];
678
679 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
680 idx = 0;
681 head = &net->dev_index_head[h];
682 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000683 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
684 net->dev_base_seq;
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000685 hlist_for_each_entry_rcu(dev, head, index_hlist) {
686 if (idx < s_idx)
687 goto cont;
688 idev = __in6_dev_get(dev);
689 if (!idev)
690 goto cont;
691
692 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
693 &idev->cnf,
694 NETLINK_CB(cb->skb).portid,
695 cb->nlh->nlmsg_seq,
696 RTM_NEWNETCONF,
697 NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000698 NETCONFA_ALL) < 0) {
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000699 rcu_read_unlock();
700 goto done;
701 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000702 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000703cont:
704 idx++;
705 }
706 rcu_read_unlock();
707 }
708 if (h == NETDEV_HASHENTRIES) {
709 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
710 net->ipv6.devconf_all,
711 NETLINK_CB(cb->skb).portid,
712 cb->nlh->nlmsg_seq,
713 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000714 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000715 goto done;
716 else
717 h++;
718 }
719 if (h == NETDEV_HASHENTRIES + 1) {
720 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
721 net->ipv6.devconf_dflt,
722 NETLINK_CB(cb->skb).portid,
723 cb->nlh->nlmsg_seq,
724 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000725 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000726 goto done;
727 else
728 h++;
729 }
730done:
731 cb->args[0] = h;
732 cb->args[1] = idx;
733
734 return skb->len;
735}
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737#ifdef CONFIG_SYSCTL
738static void dev_forward_change(struct inet6_dev *idev)
739{
740 struct net_device *dev;
741 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 if (!idev)
744 return;
745 dev = idev->dev;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700746 if (idev->cnf.forwarding)
747 dev_disable_lro(dev);
Amerigo Wang1a940832012-10-28 17:43:53 +0000748 if (dev->flags & IFF_MULTICAST) {
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000749 if (idev->cnf.forwarding) {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900750 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000751 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
752 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
753 } else {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900754 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000755 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
756 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
stephen hemminger502a2ff2010-03-17 20:31:13 +0000759
760 list_for_each_entry(ifa, &idev->addr_list, if_list) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800761 if (ifa->flags&IFA_F_TENTATIVE)
762 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (idev->cnf.forwarding)
764 addrconf_join_anycast(ifa);
765 else
766 addrconf_leave_anycast(ifa);
767 }
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000768 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
769 dev->ifindex, &idev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
772
Pavel Emelyanove1869322008-01-10 17:43:50 -0800773static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 struct net_device *dev;
776 struct inet6_dev *idev;
777
Ben Hutchings4acd4942012-08-14 08:54:51 +0000778 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 idev = __in6_dev_get(dev);
780 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800781 int changed = (!idev->cnf.forwarding) ^ (!newf);
782 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (changed)
784 dev_forward_change(idev);
785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800788
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000789static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800790{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800791 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000792 int old;
793
794 if (!rtnl_trylock())
795 return restart_syscall();
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800796
797 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000798 old = *p;
799 *p = newf;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800800
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000801 if (p == &net->ipv6.devconf_dflt->forwarding) {
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000802 if ((!newf) ^ (!old))
803 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
804 NETCONFA_IFINDEX_DEFAULT,
805 net->ipv6.devconf_dflt);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000806 rtnl_unlock();
807 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +0000808 }
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000809
Pavel Emelyanove1869322008-01-10 17:43:50 -0800810 if (p == &net->ipv6.devconf_all->forwarding) {
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200811 int old_dflt = net->ipv6.devconf_dflt->forwarding;
812
Pavel Emelyanove1869322008-01-10 17:43:50 -0800813 net->ipv6.devconf_dflt->forwarding = newf;
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200814 if ((!newf) ^ (!old_dflt))
815 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
816 NETCONFA_IFINDEX_DEFAULT,
817 net->ipv6.devconf_dflt);
818
Pavel Emelyanove1869322008-01-10 17:43:50 -0800819 addrconf_forward_change(net, newf);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000820 if ((!newf) ^ (!old))
821 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
822 NETCONFA_IFINDEX_ALL,
823 net->ipv6.devconf_all);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000824 } else if ((!newf) ^ (!old))
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800825 dev_forward_change((struct inet6_dev *)table->extra1);
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700826 rtnl_unlock();
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800827
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000828 if (newf)
Daniel Lezcano7b4da532008-03-04 13:47:14 -0800829 rt6_purge_dflt_routers(net);
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000830 return 1;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800831}
Andy Gospodarek35103d12015-08-13 10:39:01 -0400832
833static void addrconf_linkdown_change(struct net *net, __s32 newf)
834{
835 struct net_device *dev;
836 struct inet6_dev *idev;
837
838 for_each_netdev(net, dev) {
839 idev = __in6_dev_get(dev);
840 if (idev) {
841 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
842
843 idev->cnf.ignore_routes_with_linkdown = newf;
844 if (changed)
845 inet6_netconf_notify_devconf(dev_net(dev),
846 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
847 dev->ifindex,
848 &idev->cnf);
849 }
850 }
851}
852
853static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
854{
855 struct net *net;
856 int old;
857
858 if (!rtnl_trylock())
859 return restart_syscall();
860
861 net = (struct net *)table->extra2;
862 old = *p;
863 *p = newf;
864
865 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
866 if ((!newf) ^ (!old))
867 inet6_netconf_notify_devconf(net,
868 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
869 NETCONFA_IFINDEX_DEFAULT,
870 net->ipv6.devconf_dflt);
871 rtnl_unlock();
872 return 0;
873 }
874
875 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
876 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
877 addrconf_linkdown_change(net, newf);
878 if ((!newf) ^ (!old))
879 inet6_netconf_notify_devconf(net,
880 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
881 NETCONFA_IFINDEX_ALL,
882 net->ipv6.devconf_all);
883 }
884 rtnl_unlock();
885
886 return 1;
887}
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889#endif
890
stephen hemminger5c578aed2010-03-17 20:31:11 +0000891/* Nobody refers to this ifaddr, destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
893{
stephen hemmingerc2e21292010-03-17 20:31:10 +0000894 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896#ifdef NET_REFCNT_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +0000897 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898#endif
899
900 in6_dev_put(ifp->idev);
901
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100902 if (cancel_delayed_work(&ifp->dad_work))
903 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
904 ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Herbert Xue9d3e082010-05-18 15:36:06 -0700906 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
Joe Perchesf3213832012-05-15 14:11:53 +0000907 pr_warn("Freeing alive inet6 address %p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return;
909 }
Amerigo Wang94e187c2012-10-29 00:13:19 +0000910 ip6_rt_put(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Lai Jiangshane5785982011-03-15 18:00:14 +0800912 kfree_rcu(ifp, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
Brian Haleye55ffac2006-07-10 15:25:51 -0700915static void
916ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
917{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000918 struct list_head *p;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700919 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700920
921 /*
922 * Each device address list is sorted in order of scope -
923 * global before linklocal.
924 */
stephen hemminger502a2ff2010-03-17 20:31:13 +0000925 list_for_each(p, &idev->addr_list) {
926 struct inet6_ifaddr *ifa
927 = list_entry(p, struct inet6_ifaddr, if_list);
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700928 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700929 break;
930 }
931
David S. Millerb54c9b92010-03-25 21:25:30 -0700932 list_add_tail(&ifp->if_list, p);
Brian Haleye55ffac2006-07-10 15:25:51 -0700933}
934
Eric Dumazetddbe5032012-07-18 08:11:12 +0000935static u32 inet6_addr_hash(const struct in6_addr *addr)
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900936{
Eric Dumazetddbe5032012-07-18 08:11:12 +0000937 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900938}
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940/* On success it returns ifp with increased reference count */
941
942static struct inet6_ifaddr *
Jiri Pirko3f8f5292013-08-01 10:41:27 +0200943ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
944 const struct in6_addr *peer_addr, int pfxlen,
Jiri Benc8a226b22013-08-01 10:41:28 +0200945 int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 struct inet6_ifaddr *ifa = NULL;
948 struct rt6_info *rt;
stephen hemminger3a88a812010-03-17 20:31:12 +0000949 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 int err = 0;
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +0900951 int addr_type = ipv6_addr_type(addr);
952
953 if (addr_type == IPV6_ADDR_ANY ||
954 addr_type & IPV6_ADDR_MULTICAST ||
955 (!(idev->dev->flags & IFF_LOOPBACK) &&
956 addr_type & IPV6_ADDR_LOOPBACK))
957 return ERR_PTR(-EADDRNOTAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700959 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (idev->dead) {
961 err = -ENODEV; /*XXX*/
962 goto out2;
963 }
964
Brian Haley56d417b2009-06-01 03:07:33 -0700965 if (idev->cnf.disable_ipv6) {
Brian Haley9bdd8d42009-03-18 18:22:48 -0700966 err = -EACCES;
967 goto out2;
968 }
969
stephen hemminger5c578aed2010-03-17 20:31:11 +0000970 spin_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 /* Ignore adding duplicate addresses on an interface */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900973 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
dingtianhongba3542e2013-08-17 10:27:04 +0800974 ADBG("ipv6_add_addr: already assigned\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 err = -EEXIST;
976 goto out;
977 }
978
Ingo Oeser322f74a2006-03-20 23:01:47 -0800979 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Ian Morris63159f22015-03-29 14:00:04 +0100981 if (!ifa) {
dingtianhongba3542e2013-08-17 10:27:04 +0800982 ADBG("ipv6_add_addr: malloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 err = -ENOBUFS;
984 goto out;
985 }
986
David S. Miller8f031512011-12-06 16:48:14 -0500987 rt = addrconf_dst_alloc(idev, addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (IS_ERR(rt)) {
989 err = PTR_ERR(rt);
990 goto out;
991 }
992
Jiri Pirkobba24892013-12-07 19:26:57 +0100993 neigh_parms_data_state_setall(idev->nd_parms);
994
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000995 ifa->addr = *addr;
Jiri Pirko3f8f5292013-08-01 10:41:27 +0200996 if (peer_addr)
997 ifa->peer_addr = *peer_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 spin_lock_init(&ifa->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001000 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001001 INIT_HLIST_NODE(&ifa->addr_lst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 ifa->scope = scope;
1003 ifa->prefix_len = pfxlen;
1004 ifa->flags = flags | IFA_F_TENTATIVE;
Jiri Benc8a226b22013-08-01 10:41:28 +02001005 ifa->valid_lft = valid_lft;
1006 ifa->prefered_lft = prefered_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 ifa->cstamp = ifa->tstamp = jiffies;
Daniel Borkmann617fe292013-04-09 03:47:16 +00001008 ifa->tokenized = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Keir Fraser57f5f542006-08-29 02:43:49 -07001010 ifa->rt = rt;
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 ifa->idev = idev;
1013 in6_dev_hold(idev);
1014 /* For caller */
1015 in6_ifa_hold(ifa);
1016
1017 /* Add to big hash table */
Eric Dumazetddbe5032012-07-18 08:11:12 +00001018 hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
stephen hemminger5c578aed2010-03-17 20:31:11 +00001020 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
stephen hemminger5c578aed2010-03-17 20:31:11 +00001021 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 write_lock(&idev->lock);
1024 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -07001025 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (ifa->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001028 list_add(&ifa->tmp_list, &idev->tempaddr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 in6_ifa_hold(ifa);
1030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 in6_ifa_hold(ifa);
1033 write_unlock(&idev->lock);
1034out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001035 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -07001037 if (likely(err == 0))
Cong Wangf88c91d2013-04-14 23:18:43 +08001038 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 else {
1040 kfree(ifa);
1041 ifa = ERR_PTR(err);
1042 }
1043
1044 return ifa;
1045out:
stephen hemminger5c578aed2010-03-17 20:31:11 +00001046 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 goto out2;
1048}
1049
Thomas Haller5b84efe2014-01-15 15:36:59 +01001050enum cleanup_prefix_rt_t {
1051 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1052 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1053 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1054};
1055
1056/*
1057 * Check, whether the prefix for ifp would still need a prefix route
1058 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1059 * constants.
1060 *
1061 * 1) we don't purge prefix if address was not permanent.
1062 * prefix is managed by its own lifetime.
1063 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1064 * 3) if there are no addresses, delete prefix.
1065 * 4) if there are still other permanent address(es),
1066 * corresponding prefix is still permanent.
1067 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1068 * don't purge the prefix, assume user space is managing it.
1069 * 6) otherwise, update prefix lifetime to the
1070 * longest valid lifetime among the corresponding
1071 * addresses on the device.
1072 * Note: subsequent RA will update lifetime.
1073 **/
1074static enum cleanup_prefix_rt_t
1075check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1076{
1077 struct inet6_ifaddr *ifa;
1078 struct inet6_dev *idev = ifp->idev;
1079 unsigned long lifetime;
1080 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1081
1082 *expires = jiffies;
1083
1084 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1085 if (ifa == ifp)
1086 continue;
1087 if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
1088 ifp->prefix_len))
1089 continue;
1090 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1091 return CLEANUP_PREFIX_RT_NOP;
1092
1093 action = CLEANUP_PREFIX_RT_EXPIRE;
1094
1095 spin_lock(&ifa->lock);
1096
1097 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1098 /*
1099 * Note: Because this address is
1100 * not permanent, lifetime <
1101 * LONG_MAX / HZ here.
1102 */
1103 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1104 *expires = ifa->tstamp + lifetime * HZ;
1105 spin_unlock(&ifa->lock);
1106 }
1107
1108 return action;
1109}
1110
1111static void
1112cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
1113{
1114 struct rt6_info *rt;
1115
1116 rt = addrconf_get_prefix_route(&ifp->addr,
1117 ifp->prefix_len,
1118 ifp->idev->dev,
1119 0, RTF_GATEWAY | RTF_DEFAULT);
1120 if (rt) {
1121 if (del_rt)
1122 ip6_del_rt(rt);
1123 else {
1124 if (!(rt->rt6i_flags & RTF_EXPIRES))
1125 rt6_set_expires(rt, expires);
1126 ip6_rt_put(rt);
1127 }
1128 }
1129}
1130
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132/* This function wants to get referenced ifp and releases it before return */
1133
1134static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1135{
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001136 int state;
Thomas Haller5b84efe2014-01-15 15:36:59 +01001137 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1138 unsigned long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001140 ASSERT_RTNL();
1141
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001142 spin_lock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001143 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -07001144 ifp->state = INET6_IFADDR_STATE_DEAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001145 spin_unlock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001146
1147 if (state == INET6_IFADDR_STATE_DEAD)
1148 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
stephen hemminger5c578aed2010-03-17 20:31:11 +00001150 spin_lock_bh(&addrconf_hash_lock);
1151 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578aed2010-03-17 20:31:11 +00001152 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Thomas Haller5b84efe2014-01-15 15:36:59 +01001154 write_lock_bh(&ifp->idev->lock);
David S. Miller5d9efa72013-10-28 20:07:50 -04001155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001157 list_del(&ifp->tmp_list);
1158 if (ifp->ifpub) {
1159 in6_ifa_put(ifp->ifpub);
1160 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
stephen hemminger372e6c82010-03-17 20:31:09 +00001162 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Thomas Haller5b84efe2014-01-15 15:36:59 +01001165 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1166 action = check_cleanup_prefix_route(ifp, &expires);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001167
Thomas Haller5b84efe2014-01-15 15:36:59 +01001168 list_del_init(&ifp->if_list);
1169 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Thomas Haller5b84efe2014-01-15 15:36:59 +01001171 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001173 addrconf_del_dad_work(ifp);
Andrey Vaginb2238562008-07-08 15:13:31 -07001174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 ipv6_ifa_notify(RTM_DELADDR, ifp);
1176
Cong Wangf88c91d2013-04-14 23:18:43 +08001177 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Thomas Haller5b84efe2014-01-15 15:36:59 +01001179 if (action != CLEANUP_PREFIX_RT_NOP) {
1180 cleanup_prefix_route(ifp, expires,
1181 action == CLEANUP_PREFIX_RT_DEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183
Daniel Walterc3968a82011-04-13 21:10:57 +00001184 /* clean up prefsrc entries */
1185 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001186out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 in6_ifa_put(ifp);
1188}
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1191{
1192 struct inet6_dev *idev = ifp->idev;
1193 struct in6_addr addr, *tmpaddr;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001194 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001195 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 int tmp_plen;
1197 int ret = 0;
Neil Horman95c385b2007-04-25 17:08:10 -07001198 u32 addr_flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001199 unsigned long now = jiffies;
Jiri Bohac76506a92016-10-13 18:52:15 +02001200 long max_desync_factor;
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001201 s32 cnf_temp_preferred_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Jiri Pirko53bd6742013-12-06 09:45:22 +01001203 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (ift) {
1205 spin_lock_bh(&ift->lock);
1206 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1207 spin_unlock_bh(&ift->lock);
1208 tmpaddr = &addr;
1209 } else {
1210 tmpaddr = NULL;
1211 }
1212retry:
1213 in6_dev_hold(idev);
1214 if (idev->cnf.use_tempaddr <= 0) {
Jiri Pirko53bd6742013-12-06 09:45:22 +01001215 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001216 pr_info("%s: use_tempaddr is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 in6_dev_put(idev);
1218 ret = -1;
1219 goto out;
1220 }
1221 spin_lock_bh(&ifp->lock);
1222 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1223 idev->cnf.use_tempaddr = -1; /*XXX*/
1224 spin_unlock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01001225 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001226 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1227 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 in6_dev_put(idev);
1229 ret = -1;
1230 goto out;
1231 }
1232 in6_ifa_hold(ifp);
1233 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
Jiri Bohac9d6280d2016-10-13 18:50:02 +02001234 ipv6_try_regen_rndid(idev, tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001236 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001238 regen_advance = idev->cnf.regen_max_retry *
Ian Morris67ba4152014-08-24 21:53:10 +01001239 idev->cnf.dad_transmits *
1240 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
Jiri Bohac76506a92016-10-13 18:52:15 +02001241
1242 /* recalculate max_desync_factor each time and update
1243 * idev->desync_factor if it's larger
1244 */
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001245 cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
Jiri Bohac76506a92016-10-13 18:52:15 +02001246 max_desync_factor = min_t(__u32,
1247 idev->cnf.max_desync_factor,
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001248 cnf_temp_preferred_lft - regen_advance);
Jiri Bohac76506a92016-10-13 18:52:15 +02001249
1250 if (unlikely(idev->desync_factor > max_desync_factor)) {
1251 if (max_desync_factor > 0) {
1252 get_random_bytes(&idev->desync_factor,
1253 sizeof(idev->desync_factor));
1254 idev->desync_factor %= max_desync_factor;
1255 } else {
1256 idev->desync_factor = 0;
1257 }
1258 }
1259
1260 tmp_valid_lft = min_t(__u32,
1261 ifp->valid_lft,
1262 idev->cnf.temp_valid_lft + age);
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001263 tmp_prefered_lft = cnf_temp_preferred_lft + age -
Jiri Bohac76506a92016-10-13 18:52:15 +02001264 idev->desync_factor;
Jiri Bohac76506a92016-10-13 18:52:15 +02001265 tmp_prefered_lft = min_t(__u32, ifp->prefered_lft, tmp_prefered_lft);
1266 tmp_plen = ifp->prefix_len;
1267 tmp_tstamp = ifp->tstamp;
1268 spin_unlock_bh(&ifp->lock);
1269
Jiri Pirko53bd6742013-12-06 09:45:22 +01001270 write_unlock_bh(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -07001271
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001272 /* A temporary address is created only if this calculated Preferred
1273 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1274 * an implementation must not create a temporary address with a zero
1275 * Preferred Lifetime.
Heiner Kallweitecab6702014-03-12 22:13:19 +01001276 * Use age calculation as in addrconf_verify to avoid unnecessary
1277 * temporary addresses being generated.
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001278 */
Heiner Kallweitecab6702014-03-12 22:13:19 +01001279 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1280 if (tmp_prefered_lft <= regen_advance + age) {
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001281 in6_ifa_put(ifp);
1282 in6_dev_put(idev);
1283 ret = -1;
1284 goto out;
1285 }
1286
Neil Horman95c385b2007-04-25 17:08:10 -07001287 addr_flags = IFA_F_TEMPORARY;
1288 /* set in addrconf_prefix_rcv() */
1289 if (ifp->flags & IFA_F_OPTIMISTIC)
1290 addr_flags |= IFA_F_OPTIMISTIC;
1291
Hannes Frederic Sowa4b08a8f2013-08-16 13:02:27 +02001292 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1293 ipv6_addr_scope(&addr), addr_flags,
1294 tmp_valid_lft, tmp_prefered_lft);
1295 if (IS_ERR(ift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 in6_ifa_put(ifp);
1297 in6_dev_put(idev);
Joe Perchesf3213832012-05-15 14:11:53 +00001298 pr_info("%s: retry temporary address regeneration\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 tmpaddr = &addr;
Jiri Pirko53bd6742013-12-06 09:45:22 +01001300 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 goto retry;
1302 }
1303
1304 spin_lock_bh(&ift->lock);
1305 ift->ifpub = ifp;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001306 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 ift->tstamp = tmp_tstamp;
1308 spin_unlock_bh(&ift->lock);
1309
David S. Millercf22f9a2012-04-14 21:37:40 -04001310 addrconf_dad_start(ift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 in6_ifa_put(ift);
1312 in6_dev_put(idev);
1313out:
1314 return ret;
1315}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317/*
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001318 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001320enum {
1321 IPV6_SADDR_RULE_INIT = 0,
1322 IPV6_SADDR_RULE_LOCAL,
1323 IPV6_SADDR_RULE_SCOPE,
1324 IPV6_SADDR_RULE_PREFERRED,
1325#ifdef CONFIG_IPV6_MIP6
1326 IPV6_SADDR_RULE_HOA,
1327#endif
1328 IPV6_SADDR_RULE_OIF,
1329 IPV6_SADDR_RULE_LABEL,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001330 IPV6_SADDR_RULE_PRIVACY,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001331 IPV6_SADDR_RULE_ORCHID,
1332 IPV6_SADDR_RULE_PREFIX,
Erik Kline7fd25612014-10-28 18:11:14 +09001333#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1334 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1335#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001336 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001337};
1338
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001339struct ipv6_saddr_score {
1340 int rule;
1341 int addr_type;
1342 struct inet6_ifaddr *ifa;
1343 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1344 int scopedist;
1345 int matchlen;
1346};
1347
1348struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001349 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001350 int ifindex;
1351 int scope;
1352 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001353 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001354};
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001355
Dave Jonesb6f99a22007-03-22 12:27:49 -07001356static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Ulrich Weber45bb0062010-02-25 23:28:58 +00001358 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001359 return 1;
1360 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
Erik Kline7fd25612014-10-28 18:11:14 +09001363static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
1364{
1365#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1366 return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
1367#else
1368 return false;
1369#endif
1370}
1371
Benjamin Thery3de23252008-05-28 14:51:24 +02001372static int ipv6_get_saddr_eval(struct net *net,
1373 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001374 struct ipv6_saddr_dst *dst,
1375 int i)
1376{
1377 int ret;
1378
1379 if (i <= score->rule) {
1380 switch (i) {
1381 case IPV6_SADDR_RULE_SCOPE:
1382 ret = score->scopedist;
1383 break;
1384 case IPV6_SADDR_RULE_PREFIX:
1385 ret = score->matchlen;
1386 break;
1387 default:
1388 ret = !!test_bit(i, score->scorebits);
1389 }
1390 goto out;
1391 }
1392
1393 switch (i) {
1394 case IPV6_SADDR_RULE_INIT:
1395 /* Rule 0: remember if hiscore is not ready yet */
1396 ret = !!score->ifa;
1397 break;
1398 case IPV6_SADDR_RULE_LOCAL:
1399 /* Rule 1: Prefer same address */
1400 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1401 break;
1402 case IPV6_SADDR_RULE_SCOPE:
1403 /* Rule 2: Prefer appropriate scope
1404 *
1405 * ret
1406 * ^
1407 * -1 | d 15
1408 * ---+--+-+---> scope
1409 * |
1410 * | d is scope of the destination.
1411 * B-d | \
1412 * | \ <- smaller scope is better if
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08001413 * B-15 | \ if scope is enough for destination.
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001414 * | ret = B - scope (-1 <= scope >= d <= 15).
1415 * d-C-1 | /
1416 * |/ <- greater is better
1417 * -C / if scope is not enough for destination.
1418 * /| ret = scope - C (-1 <= d < scope <= 15).
1419 *
1420 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1421 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1422 * Assume B = 0 and we get C > 29.
1423 */
1424 ret = __ipv6_addr_src_scope(score->addr_type);
1425 if (ret >= dst->scope)
1426 ret = -ret;
1427 else
1428 ret -= 128; /* 30 is enough */
1429 score->scopedist = ret;
1430 break;
1431 case IPV6_SADDR_RULE_PREFERRED:
Erik Kline7fd25612014-10-28 18:11:14 +09001432 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001433 /* Rule 3: Avoid deprecated and optimistic addresses */
Erik Kline7fd25612014-10-28 18:11:14 +09001434 u8 avoid = IFA_F_DEPRECATED;
1435
1436 if (!ipv6_use_optimistic_addr(score->ifa->idev))
1437 avoid |= IFA_F_OPTIMISTIC;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001438 ret = ipv6_saddr_preferred(score->addr_type) ||
Erik Kline7fd25612014-10-28 18:11:14 +09001439 !(score->ifa->flags & avoid);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001440 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001441 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001442#ifdef CONFIG_IPV6_MIP6
1443 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001444 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001445 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001446 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1447 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001448 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001449 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001450#endif
1451 case IPV6_SADDR_RULE_OIF:
1452 /* Rule 5: Prefer outgoing interface */
1453 ret = (!dst->ifindex ||
1454 dst->ifindex == score->ifa->idev->dev->ifindex);
1455 break;
1456 case IPV6_SADDR_RULE_LABEL:
1457 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001458 ret = ipv6_addr_label(net,
1459 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001460 score->ifa->idev->dev->ifindex) == dst->label;
1461 break;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001462 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001463 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001464 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001465 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001466 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001467 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1468 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1469 score->ifa->idev->cnf.use_tempaddr >= 2;
1470 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001471 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001472 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001473 case IPV6_SADDR_RULE_ORCHID:
1474 /* Rule 8-: Prefer ORCHID vs ORCHID or
1475 * non-ORCHID vs non-ORCHID
1476 */
1477 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1478 ipv6_addr_orchid(dst->addr));
1479 break;
1480 case IPV6_SADDR_RULE_PREFIX:
1481 /* Rule 8: Use longest matching prefix */
YOSHIFUJI Hideaki / 吉藤英明91b4b042012-09-10 18:41:07 +00001482 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1483 if (ret > score->ifa->prefix_len)
1484 ret = score->ifa->prefix_len;
1485 score->matchlen = ret;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001486 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001487#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1488 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1489 /* Optimistic addresses still have lower precedence than other
1490 * preferred addresses.
1491 */
1492 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1493 break;
1494#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001495 default:
1496 ret = 0;
1497 }
1498
1499 if (ret)
1500 __set_bit(i, score->scorebits);
1501 score->rule = i;
1502out:
1503 return ret;
1504}
1505
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001506static int __ipv6_dev_get_saddr(struct net *net,
1507 struct ipv6_saddr_dst *dst,
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001508 struct inet6_dev *idev,
1509 struct ipv6_saddr_score *scores,
1510 int hiscore_idx)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001511{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001512 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001513
1514 read_lock_bh(&idev->lock);
1515 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1516 int i;
1517
1518 /*
1519 * - Tentative Address (RFC2462 section 5.4)
1520 * - A tentative address is not considered
1521 * "assigned to an interface" in the traditional
1522 * sense, unless it is also flagged as optimistic.
1523 * - Candidate Source Address (section 4)
1524 * - In any case, anycast addresses, multicast
1525 * addresses, and the unspecified address MUST
1526 * NOT be included in a candidate set.
1527 */
1528 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1529 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1530 continue;
1531
1532 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1533
1534 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1535 score->addr_type & IPV6_ADDR_MULTICAST)) {
1536 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1537 idev->dev->name);
1538 continue;
1539 }
1540
1541 score->rule = -1;
1542 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1543
1544 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1545 int minihiscore, miniscore;
1546
1547 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1548 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1549
1550 if (minihiscore > miniscore) {
1551 if (i == IPV6_SADDR_RULE_SCOPE &&
1552 score->scopedist > 0) {
1553 /*
1554 * special case:
1555 * each remaining entry
1556 * has too small (not enough)
1557 * scope, because ifa entries
1558 * are sorted by their scope
1559 * values.
1560 */
1561 goto out;
1562 }
1563 break;
1564 } else if (minihiscore < miniscore) {
1565 if (hiscore->ifa)
1566 in6_ifa_put(hiscore->ifa);
1567
1568 in6_ifa_hold(score->ifa);
1569
1570 swap(hiscore, score);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001571 hiscore_idx = 1 - hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001572
1573 /* restore our iterator */
1574 score->ifa = hiscore->ifa;
1575
1576 break;
1577 }
1578 }
1579 }
1580out:
1581 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001582 return hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001583}
1584
David Ahernafbac6012016-06-16 16:24:26 -07001585static int ipv6_get_saddr_master(struct net *net,
1586 const struct net_device *dst_dev,
1587 const struct net_device *master,
1588 struct ipv6_saddr_dst *dst,
1589 struct ipv6_saddr_score *scores,
1590 int hiscore_idx)
1591{
1592 struct inet6_dev *idev;
1593
1594 idev = __in6_dev_get(dst_dev);
1595 if (idev)
1596 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1597 scores, hiscore_idx);
1598
1599 idev = __in6_dev_get(master);
1600 if (idev)
1601 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1602 scores, hiscore_idx);
1603
1604 return hiscore_idx;
1605}
1606
Patrick McHardyb3f644f2012-08-26 19:14:14 +02001607int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001608 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001609 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001611 struct ipv6_saddr_score scores[2], *hiscore;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001612 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001613 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001614 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001615 int dst_type;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001616 bool use_oif_addr = false;
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001617 int hiscore_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001619 dst_type = __ipv6_addr_type(daddr);
1620 dst.addr = daddr;
1621 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1622 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001623 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001624 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001625
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001626 scores[hiscore_idx].rule = -1;
1627 scores[hiscore_idx].ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001629 rcu_read_lock();
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001630
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001631 /* Candidate Source Address (section 4)
1632 * - multicast and link-local destination address,
1633 * the set of candidate source address MUST only
1634 * include addresses assigned to interfaces
1635 * belonging to the same link as the outgoing
1636 * interface.
1637 * (- For site-local destination addresses, the
1638 * set of candidate source addresses MUST only
1639 * include addresses assigned to interfaces
1640 * belonging to the same site as the outgoing
1641 * interface.)
Erik Kline3985e8a2015-07-22 16:38:25 +09001642 * - "It is RECOMMENDED that the candidate source addresses
1643 * be the set of unicast addresses assigned to the
1644 * interface that will be used to send to the destination
1645 * (the 'outgoing' interface)." (RFC 6724)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001646 */
1647 if (dst_dev) {
Erik Kline3985e8a2015-07-22 16:38:25 +09001648 idev = __in6_dev_get(dst_dev);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001649 if ((dst_type & IPV6_ADDR_MULTICAST) ||
Erik Kline3985e8a2015-07-22 16:38:25 +09001650 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1651 (idev && idev->cnf.use_oif_addrs_only)) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001652 use_oif_addr = true;
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001653 }
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001654 }
1655
1656 if (use_oif_addr) {
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001657 if (idev)
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001658 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001659 } else {
David Ahernafbac6012016-06-16 16:24:26 -07001660 const struct net_device *master;
1661 int master_idx = 0;
1662
1663 /* if dst_dev exists and is enslaved to an L3 device, then
1664 * prefer addresses from dst_dev and then the master over
1665 * any other enslaved devices in the L3 domain.
1666 */
1667 master = l3mdev_master_dev_rcu(dst_dev);
1668 if (master) {
1669 master_idx = master->ifindex;
1670
1671 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1672 master, &dst,
1673 scores, hiscore_idx);
1674
1675 if (scores[hiscore_idx].ifa)
1676 goto out;
1677 }
1678
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001679 for_each_netdev_rcu(net, dev) {
David Ahernafbac6012016-06-16 16:24:26 -07001680 /* only consider addresses on devices in the
1681 * same L3 domain
1682 */
1683 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1684 continue;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001685 idev = __in6_dev_get(dev);
1686 if (!idev)
1687 continue;
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001688 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001689 }
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001690 }
David Ahernafbac6012016-06-16 16:24:26 -07001691
1692out:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001693 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001695 hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001696 if (!hiscore->ifa)
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001697 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001698
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001699 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001700 in6_ifa_put(hiscore->ifa);
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001703EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Amerigo Wang89657792013-06-29 21:30:49 +08001705int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001706 u32 banned_flags)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001707{
1708 struct inet6_ifaddr *ifp;
1709 int err = -EADDRNOTAVAIL;
1710
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01001711 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1712 if (ifp->scope > IFA_LINK)
1713 break;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001714 if (ifp->scope == IFA_LINK &&
1715 !(ifp->flags & banned_flags)) {
1716 *addr = ifp->addr;
1717 err = 0;
1718 break;
1719 }
1720 }
1721 return err;
1722}
1723
Neil Horman95c385b2007-04-25 17:08:10 -07001724int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001725 u32 banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
1727 struct inet6_dev *idev;
1728 int err = -EADDRNOTAVAIL;
1729
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001730 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001731 idev = __in6_dev_get(dev);
1732 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 read_lock_bh(&idev->lock);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001734 err = __ipv6_get_lladdr(idev, addr, banned_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 read_unlock_bh(&idev->lock);
1736 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001737 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 return err;
1739}
1740
1741static int ipv6_count_addresses(struct inet6_dev *idev)
1742{
1743 int cnt = 0;
1744 struct inet6_ifaddr *ifp;
1745
1746 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001747 list_for_each_entry(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 cnt++;
1749 read_unlock_bh(&idev->lock);
1750 return cnt;
1751}
1752
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001753int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Florian Westphal2a7851b2013-05-17 03:56:10 +00001754 const struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Erik Klinec58da4c2015-02-04 20:01:23 +09001756 return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1757}
1758EXPORT_SYMBOL(ipv6_chk_addr);
1759
1760int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1761 const struct net_device *dev, int strict,
1762 u32 banned_flags)
1763{
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001764 struct inet6_ifaddr *ifp;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001765 unsigned int hash = inet6_addr_hash(addr);
Erik Klinec58da4c2015-02-04 20:01:23 +09001766 u32 ifp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
stephen hemminger5c578aed2010-03-17 20:31:11 +00001768 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001769 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001770 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001771 continue;
Erik Klinec58da4c2015-02-04 20:01:23 +09001772 /* Decouple optimistic from tentative for evaluation here.
1773 * Ban optimistic addresses explicitly, when required.
1774 */
1775 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1776 ? (ifp->flags&~IFA_F_TENTATIVE)
1777 : ifp->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (ipv6_addr_equal(&ifp->addr, addr) &&
Erik Klinec58da4c2015-02-04 20:01:23 +09001779 !(ifp_flags&banned_flags) &&
Ian Morris63159f22015-03-29 14:00:04 +01001780 (!dev || ifp->idev->dev == dev ||
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001781 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1782 rcu_read_unlock_bh();
1783 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00001786
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001787 rcu_read_unlock_bh();
1788 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
Erik Klinec58da4c2015-02-04 20:01:23 +09001790EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001791
David S. Miller3e81c6d2010-03-20 16:18:00 -07001792static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1793 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
Eric Dumazetddbe5032012-07-18 08:11:12 +00001795 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001796 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Sasha Levinb67bfe02013-02-27 17:06:00 -08001798 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001799 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001800 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001802 if (!dev || ifp->idev->dev == dev)
David S. Miller3e81c6d2010-03-20 16:18:00 -07001803 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
1805 }
David S. Miller3e81c6d2010-03-20 16:18:00 -07001806 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001809/* Compares an address/prefix_len with addresses on device @dev.
1810 * If one is found it returns true.
1811 */
1812bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1813 const unsigned int prefix_len, struct net_device *dev)
1814{
1815 struct inet6_dev *idev;
1816 struct inet6_ifaddr *ifa;
1817 bool ret = false;
1818
1819 rcu_read_lock();
1820 idev = __in6_dev_get(dev);
1821 if (idev) {
1822 read_lock_bh(&idev->lock);
1823 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1824 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1825 if (ret)
1826 break;
1827 }
1828 read_unlock_bh(&idev->lock);
1829 }
1830 rcu_read_unlock();
1831
1832 return ret;
1833}
1834EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1835
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001836int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001837{
1838 struct inet6_dev *idev;
1839 struct inet6_ifaddr *ifa;
1840 int onlink;
1841
1842 onlink = 0;
1843 rcu_read_lock();
1844 idev = __in6_dev_get(dev);
1845 if (idev) {
1846 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001847 list_for_each_entry(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001848 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1849 ifa->prefix_len);
1850 if (onlink)
1851 break;
1852 }
1853 read_unlock_bh(&idev->lock);
1854 }
1855 rcu_read_unlock();
1856 return onlink;
1857}
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001858EXPORT_SYMBOL(ipv6_chk_prefix);
1859
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001860struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001861 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
David S. Millerb79d1d52010-03-25 21:39:21 -07001863 struct inet6_ifaddr *ifp, *result = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001864 unsigned int hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
stephen hemminger5c578aed2010-03-17 20:31:11 +00001866 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001867 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001868 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001869 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001871 if (!dev || ifp->idev->dev == dev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07001873 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 in6_ifa_hold(ifp);
1875 break;
1876 }
1877 }
1878 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00001879 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
David S. Millerb79d1d52010-03-25 21:39:21 -07001881 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884/* Gets referenced address, destroys ifaddr */
1885
Brian Haleycc411d02009-09-09 14:41:32 +00001886static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Lubomir Rintel3d171f32016-01-08 13:47:23 +01001888 if (dad_failed)
1889 ifp->flags |= IFA_F_DADFAILED;
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (ifp->flags&IFA_F_PERMANENT) {
1892 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001893 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 ifp->flags |= IFA_F_TENTATIVE;
1895 spin_unlock_bh(&ifp->lock);
Herbert Xue2577a02010-03-13 12:23:29 -08001896 if (dad_failed)
1897 ipv6_ifa_notify(0, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 } else if (ifp->flags&IFA_F_TEMPORARY) {
1900 struct inet6_ifaddr *ifpub;
1901 spin_lock_bh(&ifp->lock);
1902 ifpub = ifp->ifpub;
1903 if (ifpub) {
1904 in6_ifa_hold(ifpub);
1905 spin_unlock_bh(&ifp->lock);
1906 ipv6_create_tempaddr(ifpub, ifp);
1907 in6_ifa_put(ifpub);
1908 } else {
1909 spin_unlock_bh(&ifp->lock);
1910 }
1911 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001912 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
Herbert Xuf2344a12010-05-18 15:55:27 -07001917static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1918{
1919 int err = -ENOENT;
1920
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001921 spin_lock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001922 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1923 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1924 err = 0;
1925 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001926 spin_unlock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001927
1928 return err;
1929}
1930
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001931void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1932{
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001933 struct inet6_dev *idev = ifp->idev;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001934 struct net *net = dev_net(ifp->idev->dev);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001935
Ursula Braun853dc2e2010-10-24 23:06:43 +00001936 if (addrconf_dad_end(ifp)) {
1937 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07001938 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00001939 }
Herbert Xuf2344a12010-05-18 15:55:27 -07001940
Joe Perchese87cc472012-05-13 21:56:26 +00001941 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1942 ifp->idev->dev->name, &ifp->addr);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001943
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001944 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001945
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001946 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
1947 int scope = ifp->scope;
1948 u32 flags = ifp->flags;
1949 struct in6_addr new_addr;
1950 struct inet6_ifaddr *ifp2;
1951 u32 valid_lft, preferred_lft;
1952 int pfxlen = ifp->prefix_len;
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001953 int retries = ifp->stable_privacy_retry + 1;
1954
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001955 if (retries > net->ipv6.sysctl.idgen_retries) {
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001956 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
1957 ifp->idev->dev->name);
1958 goto errdad;
1959 }
1960
1961 new_addr = ifp->addr;
1962 if (ipv6_generate_stable_address(&new_addr, retries,
1963 idev))
1964 goto errdad;
1965
1966 valid_lft = ifp->valid_lft;
1967 preferred_lft = ifp->prefered_lft;
1968
1969 spin_unlock_bh(&ifp->lock);
1970
1971 if (idev->cnf.max_addresses &&
1972 ipv6_count_addresses(idev) >=
1973 idev->cnf.max_addresses)
1974 goto lock_errdad;
1975
1976 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
1977 ifp->idev->dev->name);
1978
1979 ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
1980 scope, flags, valid_lft,
1981 preferred_lft);
1982 if (IS_ERR(ifp2))
1983 goto lock_errdad;
1984
1985 spin_lock_bh(&ifp2->lock);
1986 ifp2->stable_privacy_retry = retries;
1987 ifp2->state = INET6_IFADDR_STATE_PREDAD;
1988 spin_unlock_bh(&ifp2->lock);
1989
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001990 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001991 in6_ifa_put(ifp2);
1992lock_errdad:
1993 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001994 }
1995
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001996errdad:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001997 /* transition from _POSTDAD to _ERRDAD */
1998 ifp->state = INET6_IFADDR_STATE_ERRDAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001999 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002000
2001 addrconf_mod_dad_work(ifp, 0);
Wei Yongjun751eb6b2016-09-05 16:06:31 +08002002 in6_ifa_put(ifp);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002003}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002005/* Join to solicited addr multicast group.
2006 * caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002007void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
2009 struct in6_addr maddr;
2010
2011 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2012 return;
2013
2014 addrconf_addr_solict_mult(addr, &maddr);
2015 ipv6_dev_mc_inc(dev, &maddr);
2016}
2017
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002018/* caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002019void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
2021 struct in6_addr maddr;
2022
2023 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2024 return;
2025
2026 addrconf_addr_solict_mult(addr, &maddr);
2027 __ipv6_dev_mc_dec(idev, &maddr);
2028}
2029
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002030/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002031static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002034
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002035 if (ifp->prefix_len >= 127) /* RFC 6164 */
Bjørn Mork2bda8a02011-07-05 23:04:13 +00002036 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2038 if (ipv6_addr_any(&addr))
2039 return;
WANG Cong013b4d92014-09-11 15:35:11 -07002040 __ipv6_dev_ac_inc(ifp->idev, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
2042
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002043/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002044static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
2046 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002047
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002048 if (ifp->prefix_len >= 127) /* RFC 6164 */
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00002049 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2051 if (ipv6_addr_any(&addr))
2052 return;
2053 __ipv6_dev_ac_dec(ifp->idev, &addr);
2054}
2055
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002056static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
2057{
Alexander Aring5241c2d2015-12-14 20:55:22 +01002058 if (dev->addr_len != EUI64_ADDR_LEN)
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002059 return -1;
Alexander Aring5241c2d2015-12-14 20:55:22 +01002060 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
YOSHIFUJI Hideaki / 吉藤英明5e98a362013-01-28 10:44:29 +00002061 eui[0] ^= 2;
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002062 return 0;
2063}
2064
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002065static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2066{
2067 union fwnet_hwaddr *ha;
2068
2069 if (dev->addr_len != FWNET_ALEN)
2070 return -1;
2071
2072 ha = (union fwnet_hwaddr *)dev->dev_addr;
2073
2074 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2075 eui[0] ^= 2;
2076 return 0;
2077}
2078
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002079static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2080{
2081 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2082 if (dev->addr_len != ARCNET_ALEN)
2083 return -1;
2084 memset(eui, 0, 7);
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002085 eui[7] = *(u8 *)dev->dev_addr;
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002086 return 0;
2087}
2088
2089static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2090{
2091 if (dev->addr_len != INFINIBAND_ALEN)
2092 return -1;
2093 memcpy(eui, dev->dev_addr + 12, 8);
2094 eui[0] |= 2;
2095 return 0;
2096}
2097
stephen hemmingerc61393e2010-10-04 20:17:53 +00002098static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002099{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00002100 if (addr == 0)
2101 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002102 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2103 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2104 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2105 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2106 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2107 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2108 eui[1] = 0;
2109 eui[2] = 0x5E;
2110 eui[3] = 0xFE;
2111 memcpy(eui + 4, &addr, 4);
2112 return 0;
2113}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002114
2115static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2116{
2117 if (dev->priv_flags & IFF_ISATAP)
2118 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2119 return -1;
2120}
2121
stephen hemmingeraee80b52011-06-08 10:44:30 +00002122static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2123{
2124 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2125}
2126
Nicolas Dichtele8377352013-08-20 12:16:06 +02002127static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2128{
2129 memcpy(eui, dev->perm_addr, 3);
2130 memcpy(eui + 5, dev->perm_addr + 3, 3);
2131 eui[3] = 0xFF;
2132 eui[4] = 0xFE;
2133 eui[0] ^= 2;
2134 return 0;
2135}
2136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2138{
2139 switch (dev->type) {
2140 case ARPHRD_ETHER:
2141 case ARPHRD_FDDI:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002142 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002144 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002146 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002147 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002148 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002149 case ARPHRD_IPGRE:
Felix Jia45ce0fd2017-01-26 16:59:18 +13002150 case ARPHRD_TUNNEL:
stephen hemmingeraee80b52011-06-08 10:44:30 +00002151 return addrconf_ifid_gre(eui, dev);
Jukka Rissanene74bccb82013-12-11 17:05:36 +02002152 case ARPHRD_6LOWPAN:
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002153 return addrconf_ifid_eui64(eui, dev);
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002154 case ARPHRD_IEEE1394:
2155 return addrconf_ifid_ieee1394(eui, dev);
Nicolas Dichtele8377352013-08-20 12:16:06 +02002156 case ARPHRD_TUNNEL6:
Felix Jia45ce0fd2017-01-26 16:59:18 +13002157 case ARPHRD_IP6GRE:
Nicolas Dichtele8377352013-08-20 12:16:06 +02002158 return addrconf_ifid_ip6tnl(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 }
2160 return -1;
2161}
2162
2163static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2164{
2165 int err = -1;
2166 struct inet6_ifaddr *ifp;
2167
2168 read_lock_bh(&idev->lock);
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01002169 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2170 if (ifp->scope > IFA_LINK)
2171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2173 memcpy(eui, ifp->addr.s6_addr+8, 8);
2174 err = 0;
2175 break;
2176 }
2177 }
2178 read_unlock_bh(&idev->lock);
2179 return err;
2180}
2181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002183static void ipv6_regen_rndid(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08002186 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 /*
2190 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2191 * check if generated address is not inappropriate
2192 *
2193 * - Reserved subnet anycast (RFC 2526)
2194 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002195 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 * 00-00-5E-FE-xx-xx-xx-xx
2197 * - value 0
2198 * - XXX: already assigned to an address on the device
2199 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002200 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2202 (idev->rndid[7]&0x80))
2203 goto regen;
2204 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2205 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2206 goto regen;
2207 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2208 goto regen;
2209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
2211
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002212static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002213{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002215 ipv6_regen_rndid(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218/*
2219 * Add prefix route.
2220 */
2221
2222static void
2223addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002224 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
Thomas Graf86872cb2006-08-22 00:01:08 -07002226 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002227 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
Thomas Graf86872cb2006-08-22 00:01:08 -07002228 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2229 .fc_ifindex = dev->ifindex,
2230 .fc_expires = expires,
2231 .fc_dst_len = plen,
2232 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002233 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07002234 .fc_protocol = RTPROT_KERNEL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002235 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002237 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
2239 /* Prevent useless cloning on PtP SIT.
2240 This thing is done here expecting that the whole
2241 class of non-broadcast devices need not cloning.
2242 */
Amerigo Wang07a93622012-10-29 16:23:10 +00002243#if IS_ENABLED(CONFIG_IPV6_SIT)
Thomas Graf86872cb2006-08-22 00:01:08 -07002244 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2245 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002246#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Thomas Graf86872cb2006-08-22 00:01:08 -07002248 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
2250
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002251
2252static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2253 int plen,
2254 const struct net_device *dev,
2255 u32 flags, u32 noflags)
2256{
2257 struct fib6_node *fn;
2258 struct rt6_info *rt = NULL;
2259 struct fib6_table *table;
David Ahernca254492015-10-12 11:47:10 -07002260 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002261
David Ahernca254492015-10-12 11:47:10 -07002262 table = fib6_get_table(dev_net(dev), tb_id);
Ian Morris63159f22015-03-29 14:00:04 +01002263 if (!table)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002264 return NULL;
2265
Li RongQing5744dd92012-09-11 21:59:01 +00002266 read_lock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002267 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2268 if (!fn)
2269 goto out;
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07002270
2271 noflags |= RTF_CACHE;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002272 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002273 if (rt->dst.dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002274 continue;
2275 if ((rt->rt6i_flags & flags) != flags)
2276 continue;
Romain Kuntz85da53b2013-01-09 15:02:26 +01002277 if ((rt->rt6i_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002278 continue;
2279 dst_hold(&rt->dst);
2280 break;
2281 }
2282out:
Li RongQing5744dd92012-09-11 21:59:01 +00002283 read_unlock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002284 return rt;
2285}
2286
2287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288/* Create "default" multicast route to the interface */
2289
2290static void addrconf_add_mroute(struct net_device *dev)
2291{
Thomas Graf86872cb2006-08-22 00:01:08 -07002292 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002293 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002294 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2295 .fc_ifindex = dev->ifindex,
2296 .fc_dst_len = 8,
2297 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002298 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002299 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Thomas Graf86872cb2006-08-22 00:01:08 -07002301 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2302
2303 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2307{
2308 struct inet6_dev *idev;
2309
2310 ASSERT_RTNL();
2311
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002312 idev = ipv6_find_idev(dev);
2313 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00002314 return ERR_PTR(-ENOBUFS);
2315
2316 if (idev->cnf.disable_ipv6)
2317 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319 /* Add default multicast route */
David Ahernba46ee42016-06-13 13:44:18 -07002320 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
Li Wei4af04ab2011-12-06 21:23:45 +00002321 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 return idev;
2324}
2325
Jiri Pirko53bd6742013-12-06 09:45:22 +01002326static void manage_tempaddrs(struct inet6_dev *idev,
2327 struct inet6_ifaddr *ifp,
2328 __u32 valid_lft, __u32 prefered_lft,
2329 bool create, unsigned long now)
2330{
2331 u32 flags;
2332 struct inet6_ifaddr *ift;
2333
2334 read_lock_bh(&idev->lock);
2335 /* update all temporary addresses in the list */
2336 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2337 int age, max_valid, max_prefered;
2338
2339 if (ifp != ift->ifpub)
2340 continue;
2341
2342 /* RFC 4941 section 3.3:
2343 * If a received option will extend the lifetime of a public
2344 * address, the lifetimes of temporary addresses should
2345 * be extended, subject to the overall constraint that no
2346 * temporary addresses should ever remain "valid" or "preferred"
2347 * for a time longer than (TEMP_VALID_LIFETIME) or
2348 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2349 */
2350 age = (now - ift->cstamp) / HZ;
2351 max_valid = idev->cnf.temp_valid_lft - age;
2352 if (max_valid < 0)
2353 max_valid = 0;
2354
2355 max_prefered = idev->cnf.temp_prefered_lft -
Jiri Bohac76506a92016-10-13 18:52:15 +02002356 idev->desync_factor - age;
Jiri Pirko53bd6742013-12-06 09:45:22 +01002357 if (max_prefered < 0)
2358 max_prefered = 0;
2359
2360 if (valid_lft > max_valid)
2361 valid_lft = max_valid;
2362
2363 if (prefered_lft > max_prefered)
2364 prefered_lft = max_prefered;
2365
2366 spin_lock(&ift->lock);
2367 flags = ift->flags;
2368 ift->valid_lft = valid_lft;
2369 ift->prefered_lft = prefered_lft;
2370 ift->tstamp = now;
2371 if (prefered_lft > 0)
2372 ift->flags &= ~IFA_F_DEPRECATED;
2373
2374 spin_unlock(&ift->lock);
2375 if (!(flags&IFA_F_TENTATIVE))
2376 ipv6_ifa_notify(0, ift);
2377 }
2378
2379 if ((create || list_empty(&idev->tempaddr_list)) &&
2380 idev->cnf.use_tempaddr > 0) {
2381 /* When a new public address is created as described
2382 * in [ADDRCONF], also create a new temporary address.
2383 * Also create a temporary address if it's enabled but
2384 * no temporary address currently exists.
2385 */
2386 read_unlock_bh(&idev->lock);
2387 ipv6_create_tempaddr(ifp, NULL);
2388 } else {
2389 read_unlock_bh(&idev->lock);
2390 }
2391}
2392
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002393static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2394{
Felix Jiad35a00b2017-01-26 16:59:17 +13002395 return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2396 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002397}
2398
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002399int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2400 const struct prefix_info *pinfo,
2401 struct inet6_dev *in6_dev,
2402 const struct in6_addr *addr, int addr_type,
2403 u32 addr_flags, bool sllao, bool tokenized,
2404 __u32 valid_lft, u32 prefered_lft)
Alexander Aring4f672232016-06-15 21:20:22 +02002405{
2406 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2407 int create = 0, update_lft = 0;
2408
2409 if (!ifp && valid_lft) {
2410 int max_addresses = in6_dev->cnf.max_addresses;
2411
2412#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2413 if (in6_dev->cnf.optimistic_dad &&
2414 !net->ipv6.devconf_all->forwarding && sllao)
2415 addr_flags |= IFA_F_OPTIMISTIC;
2416#endif
2417
2418 /* Do not allow to create too much of autoconfigured
2419 * addresses; this would be too easy way to crash kernel.
2420 */
2421 if (!max_addresses ||
2422 ipv6_count_addresses(in6_dev) < max_addresses)
2423 ifp = ipv6_add_addr(in6_dev, addr, NULL,
2424 pinfo->prefix_len,
2425 addr_type&IPV6_ADDR_SCOPE_MASK,
2426 addr_flags, valid_lft,
2427 prefered_lft);
2428
2429 if (IS_ERR_OR_NULL(ifp))
2430 return -1;
2431
2432 update_lft = 0;
2433 create = 1;
2434 spin_lock_bh(&ifp->lock);
2435 ifp->flags |= IFA_F_MANAGETEMPADDR;
2436 ifp->cstamp = jiffies;
2437 ifp->tokenized = tokenized;
2438 spin_unlock_bh(&ifp->lock);
2439 addrconf_dad_start(ifp);
2440 }
2441
2442 if (ifp) {
2443 u32 flags;
2444 unsigned long now;
2445 u32 stored_lft;
2446
2447 /* update lifetime (RFC2462 5.5.3 e) */
2448 spin_lock_bh(&ifp->lock);
2449 now = jiffies;
2450 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2451 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2452 else
2453 stored_lft = 0;
2454 if (!update_lft && !create && stored_lft) {
2455 const u32 minimum_lft = min_t(u32,
2456 stored_lft, MIN_VALID_LIFETIME);
2457 valid_lft = max(valid_lft, minimum_lft);
2458
2459 /* RFC4862 Section 5.5.3e:
2460 * "Note that the preferred lifetime of the
2461 * corresponding address is always reset to
2462 * the Preferred Lifetime in the received
2463 * Prefix Information option, regardless of
2464 * whether the valid lifetime is also reset or
2465 * ignored."
2466 *
2467 * So we should always update prefered_lft here.
2468 */
2469 update_lft = 1;
2470 }
2471
2472 if (update_lft) {
2473 ifp->valid_lft = valid_lft;
2474 ifp->prefered_lft = prefered_lft;
2475 ifp->tstamp = now;
2476 flags = ifp->flags;
2477 ifp->flags &= ~IFA_F_DEPRECATED;
2478 spin_unlock_bh(&ifp->lock);
2479
2480 if (!(flags&IFA_F_TENTATIVE))
2481 ipv6_ifa_notify(0, ifp);
2482 } else
2483 spin_unlock_bh(&ifp->lock);
2484
2485 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2486 create, now);
2487
2488 in6_ifa_put(ifp);
2489 addrconf_verify();
2490 }
2491
2492 return 0;
2493}
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002494EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
Alexander Aring4f672232016-06-15 21:20:22 +02002495
Neil Hormane6bff992012-01-04 10:49:15 +00002496void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
2498 struct prefix_info *pinfo;
2499 __u32 valid_lft;
2500 __u32 prefered_lft;
Alexander Aring4f672232016-06-15 21:20:22 +02002501 int addr_type, err;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002502 u32 addr_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07002504 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
2506 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 if (len < sizeof(struct prefix_info)) {
dingtianhongba3542e2013-08-17 10:27:04 +08002509 ADBG("addrconf: prefix option too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 return;
2511 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002512
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 /*
2514 * Validation checks ([ADDRCONF], page 19)
2515 */
2516
2517 addr_type = ipv6_addr_type(&pinfo->prefix);
2518
2519 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2520 return;
2521
2522 valid_lft = ntohl(pinfo->valid);
2523 prefered_lft = ntohl(pinfo->prefered);
2524
2525 if (prefered_lft > valid_lft) {
Joe Perchese87cc472012-05-13 21:56:26 +00002526 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 return;
2528 }
2529
2530 in6_dev = in6_dev_get(dev);
2531
Ian Morris63159f22015-03-29 14:00:04 +01002532 if (!in6_dev) {
Joe Perchese87cc472012-05-13 21:56:26 +00002533 net_dbg_ratelimited("addrconf: device %s not configured\n",
2534 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 return;
2536 }
2537
2538 /*
2539 * Two things going on here:
2540 * 1) Add routes for on-link prefixes
2541 * 2) Configure prefixes with the auto flag set
2542 */
2543
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002544 if (pinfo->onlink) {
2545 struct rt6_info *rt;
2546 unsigned long rt_expires;
2547
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002548 /* Avoid arithmetic overflow. Really, we could
2549 * save rt_expires in seconds, likely valid_lft,
2550 * but it would require division in fib gc, that it
2551 * not good.
2552 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002553 if (HZ > USER_HZ)
2554 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2555 else
2556 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08002557
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002558 if (addrconf_finite_timeout(rt_expires))
2559 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002561 rt = addrconf_get_prefix_route(&pinfo->prefix,
2562 pinfo->prefix_len,
2563 dev,
2564 RTF_ADDRCONF | RTF_PREFIX_RT,
2565 RTF_GATEWAY | RTF_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002567 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002568 /* Autoconf prefix route */
2569 if (valid_lft == 0) {
2570 ip6_del_rt(rt);
2571 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002572 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002573 /* not infinity */
Gao feng1716a962012-04-06 00:13:10 +00002574 rt6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002575 } else {
Gao feng1716a962012-04-06 00:13:10 +00002576 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 }
2578 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002579 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002580 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2581 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002582 /* not infinity */
2583 flags |= RTF_EXPIRES;
2584 expires = jiffies_to_clock_t(rt_expires);
2585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002587 dev, expires, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00002589 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 }
2591
2592 /* Try to figure out our local address for this prefix */
2593
2594 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 struct in6_addr addr;
Alexander Aringf997c552016-06-15 21:20:23 +02002596 bool tokenized = false, dev_addr_generated = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598 if (pinfo->prefix_len == 64) {
2599 memcpy(&addr, &pinfo->prefix, 8);
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002600
2601 if (!ipv6_addr_any(&in6_dev->token)) {
2602 read_lock_bh(&in6_dev->lock);
2603 memcpy(addr.s6_addr + 8,
2604 in6_dev->token.s6_addr + 8, 8);
2605 read_unlock_bh(&in6_dev->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00002606 tokenized = true;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002607 } else if (is_addr_mode_generate_stable(in6_dev) &&
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01002608 !ipv6_generate_stable_address(&addr, 0,
2609 in6_dev)) {
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002610 addr_flags |= IFA_F_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01002611 goto ok;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002612 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2613 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
Alexander Aring4f672232016-06-15 21:20:22 +02002614 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002615 } else {
2616 dev_addr_generated = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
2618 goto ok;
2619 }
Joe Perchese87cc472012-05-13 21:56:26 +00002620 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2621 pinfo->prefix_len);
Alexander Aring4f672232016-06-15 21:20:22 +02002622 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
2624ok:
Alexander Aring4f672232016-06-15 21:20:22 +02002625 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2626 &addr, addr_type,
2627 addr_flags, sllao,
2628 tokenized, valid_lft,
2629 prefered_lft);
2630 if (err)
2631 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002632
2633 /* Ignore error case here because previous prefix add addr was
2634 * successful which will be notified.
2635 */
2636 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2637 addr_type, addr_flags, sllao,
2638 tokenized, valid_lft,
2639 prefered_lft,
2640 dev_addr_generated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 }
2642 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
Alexander Aring4f672232016-06-15 21:20:22 +02002643put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 in6_dev_put(in6_dev);
2645}
2646
2647/*
2648 * Set destination address.
2649 * Special case for SIT interfaces where we create a new "virtual"
2650 * device.
2651 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002652int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
2654 struct in6_ifreq ireq;
2655 struct net_device *dev;
2656 int err = -EINVAL;
2657
2658 rtnl_lock();
2659
2660 err = -EFAULT;
2661 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2662 goto err_exit;
2663
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002664 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
2666 err = -ENODEV;
Ian Morris63159f22015-03-29 14:00:04 +01002667 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 goto err_exit;
2669
Amerigo Wang07a93622012-10-29 16:23:10 +00002670#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002672 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 struct ip_tunnel_parm p;
2675
2676 err = -EADDRNOTAVAIL;
2677 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2678 goto err_exit;
2679
2680 memset(&p, 0, sizeof(p));
2681 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2682 p.iph.saddr = 0;
2683 p.iph.version = 4;
2684 p.iph.ihl = 5;
2685 p.iph.protocol = IPPROTO_IPV6;
2686 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002687 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002689 if (ops->ndo_do_ioctl) {
2690 mm_segment_t oldfs = get_fs();
2691
2692 set_fs(KERNEL_DS);
2693 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2694 set_fs(oldfs);
2695 } else
2696 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
2698 if (err == 0) {
2699 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002700 dev = __dev_get_by_name(net, p.name);
2701 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 goto err_exit;
2703 err = dev_open(dev);
2704 }
2705 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002706#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
2708err_exit:
2709 rtnl_unlock();
2710 return err;
2711}
2712
Madhu Challa93a714d2015-02-25 09:58:35 -08002713static int ipv6_mc_config(struct sock *sk, bool join,
2714 const struct in6_addr *addr, int ifindex)
2715{
2716 int ret;
2717
2718 ASSERT_RTNL();
2719
2720 lock_sock(sk);
2721 if (join)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002722 ret = ipv6_sock_mc_join(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002723 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002724 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002725 release_sock(sk);
2726
2727 return ret;
2728}
2729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730/*
2731 * Manual configuration of address on an interface
2732 */
Jiri Pirko479840f2013-12-06 09:45:21 +01002733static int inet6_addr_add(struct net *net, int ifindex,
2734 const struct in6_addr *pfx,
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002735 const struct in6_addr *peer_pfx,
Jiri Pirko479840f2013-12-06 09:45:21 +01002736 unsigned int plen, __u32 ifa_flags,
2737 __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
2739 struct inet6_ifaddr *ifp;
2740 struct inet6_dev *idev;
2741 struct net_device *dev;
Madhu Challa93a714d2015-02-25 09:58:35 -08002742 unsigned long timeout;
2743 clock_t expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002745 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
2747 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002748
Thomas Graf24ef0da2008-05-28 16:54:22 +02002749 if (plen > 128)
2750 return -EINVAL;
2751
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002752 /* check the lifetime */
2753 if (!valid_lft || prefered_lft > valid_lft)
2754 return -EINVAL;
2755
Jiri Pirko53bd6742013-12-06 09:45:22 +01002756 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2757 return -EINVAL;
2758
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002759 dev = __dev_get_by_index(net, ifindex);
2760 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002762
Brian Haley64e724f2010-07-20 10:34:30 +00002763 idev = addrconf_add_dev(dev);
2764 if (IS_ERR(idev))
2765 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Madhu Challa93a714d2015-02-25 09:58:35 -08002767 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2768 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2769 true, pfx, ifindex);
2770
2771 if (ret < 0)
2772 return ret;
2773 }
2774
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 scope = ipv6_addr_scope(pfx);
2776
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002777 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2778 if (addrconf_finite_timeout(timeout)) {
2779 expires = jiffies_to_clock_t(timeout * HZ);
2780 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002781 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002782 } else {
2783 expires = 0;
2784 flags = 0;
2785 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002786 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002787
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002788 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2789 if (addrconf_finite_timeout(timeout)) {
2790 if (timeout == 0)
2791 ifa_flags |= IFA_F_DEPRECATED;
2792 prefered_lft = timeout;
2793 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002794
Jiri Benc8a226b22013-08-01 10:41:28 +02002795 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2796 valid_lft, prefered_lft);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 if (!IS_ERR(ifp)) {
Thomas Haller761aac72014-01-15 15:36:58 +01002799 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2800 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2801 expires, flags);
2802 }
2803
Neil Horman95c385b2007-04-25 17:08:10 -07002804 /*
2805 * Note that section 3.1 of RFC 4429 indicates
2806 * that the Optimistic flag should not be set for
2807 * manually configured addresses
2808 */
David S. Millercf22f9a2012-04-14 21:37:40 -04002809 addrconf_dad_start(ifp);
Jiri Pirko53bd6742013-12-06 09:45:22 +01002810 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2811 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2812 true, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002814 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 return 0;
Madhu Challa93a714d2015-02-25 09:58:35 -08002816 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2817 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2818 false, pfx, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 }
2820
2821 return PTR_ERR(ifp);
2822}
2823
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002824static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2825 const struct in6_addr *pfx, unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826{
2827 struct inet6_ifaddr *ifp;
2828 struct inet6_dev *idev;
2829 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002830
Thomas Graf24ef0da2008-05-28 16:54:22 +02002831 if (plen > 128)
2832 return -EINVAL;
2833
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002834 dev = __dev_get_by_index(net, ifindex);
2835 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return -ENODEV;
2837
Ian Morrise5d08d72014-11-23 21:28:43 +00002838 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01002839 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 return -ENXIO;
2841
2842 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002843 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 if (ifp->prefix_len == plen &&
2845 ipv6_addr_equal(pfx, &ifp->addr)) {
2846 in6_ifa_hold(ifp);
2847 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002848
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002849 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2850 (ifa_flags & IFA_F_MANAGETEMPADDR))
2851 manage_tempaddrs(idev, ifp, 0, 0, false,
2852 jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 ipv6_del_addr(ifp);
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002854 addrconf_verify_rtnl();
Madhu Challa93a714d2015-02-25 09:58:35 -08002855 if (ipv6_addr_is_multicast(pfx)) {
2856 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2857 false, pfx, dev->ifindex);
2858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 return 0;
2860 }
2861 }
2862 read_unlock_bh(&idev->lock);
2863 return -EADDRNOTAVAIL;
2864}
2865
2866
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002867int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
2869 struct in6_ifreq ireq;
2870 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002871
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002872 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002874
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2876 return -EFAULT;
2877
2878 rtnl_lock();
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002879 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002880 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2881 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 rtnl_unlock();
2883 return err;
2884}
2885
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002886int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
2888 struct in6_ifreq ireq;
2889 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002890
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002891 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 return -EPERM;
2893
2894 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2895 return -EFAULT;
2896
2897 rtnl_lock();
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002898 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002899 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 rtnl_unlock();
2901 return err;
2902}
2903
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002904static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2905 int plen, int scope)
2906{
2907 struct inet6_ifaddr *ifp;
2908
Jiri Benc8a226b22013-08-01 10:41:28 +02002909 ifp = ipv6_add_addr(idev, addr, NULL, plen,
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01002910 scope, IFA_F_PERMANENT,
2911 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002912 if (!IS_ERR(ifp)) {
2913 spin_lock_bh(&ifp->lock);
2914 ifp->flags &= ~IFA_F_TENTATIVE;
2915 spin_unlock_bh(&ifp->lock);
Paolo Abeni764d3be2016-11-22 16:57:40 +01002916 rt_genid_bump_ipv6(dev_net(idev->dev));
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002917 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2918 in6_ifa_put(ifp);
2919 }
2920}
2921
Amerigo Wang07a93622012-10-29 16:23:10 +00002922#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923static void sit_add_v4_addrs(struct inet6_dev *idev)
2924{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 struct in6_addr addr;
2926 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002927 struct net *net = dev_net(idev->dev);
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002928 int scope, plen;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002929 u32 pflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
2931 ASSERT_RTNL();
2932
2933 memset(&addr, 0, sizeof(struct in6_addr));
2934 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2935
2936 if (idev->dev->flags&IFF_POINTOPOINT) {
2937 addr.s6_addr32[0] = htonl(0xfe800000);
2938 scope = IFA_LINK;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002939 plen = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 } else {
2941 scope = IPV6_ADDR_COMPATv4;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002942 plen = 96;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002943 pflags |= RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 }
2945
2946 if (addr.s6_addr32[3]) {
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002947 add_addr(idev, &addr, plen, scope);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002948 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 return;
2950 }
2951
Benjamin Thery6fda7352008-03-05 10:47:47 -08002952 for_each_netdev(net, dev) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002953 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 if (in_dev && (dev->flags & IFF_UP)) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002955 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
2957 int flag = scope;
2958
2959 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
2961 addr.s6_addr32[3] = ifa->ifa_local;
2962
2963 if (ifa->ifa_scope == RT_SCOPE_LINK)
2964 continue;
2965 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2966 if (idev->dev->flags&IFF_POINTOPOINT)
2967 continue;
2968 flag |= IFA_HOST;
2969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002971 add_addr(idev, &addr, plen, flag);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002972 addrconf_prefix_route(&addr, plen, idev->dev, 0,
2973 pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 }
2975 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002978#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
2980static void init_loopback(struct net_device *dev)
2981{
2982 struct inet6_dev *idev;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05302983 struct net_device *sp_dev;
2984 struct inet6_ifaddr *sp_ifa;
2985 struct rt6_info *sp_rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
2987 /* ::1 */
2988
2989 ASSERT_RTNL();
2990
Ian Morrise5d08d72014-11-23 21:28:43 +00002991 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01002992 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00002993 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 return;
2995 }
2996
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002997 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05302998
2999 /* Add routes to other interface's IPv6 addresses */
3000 for_each_netdev(dev_net(dev), sp_dev) {
3001 if (!strcmp(sp_dev->name, dev->name))
3002 continue;
3003
3004 idev = __in6_dev_get(sp_dev);
3005 if (!idev)
3006 continue;
3007
3008 read_lock_bh(&idev->lock);
3009 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
3010
3011 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
3012 continue;
3013
Gao feng33d99112014-01-24 16:29:11 +08003014 if (sp_ifa->rt) {
3015 /* This dst has been added to garbage list when
3016 * lo device down, release this obsolete dst and
3017 * reallocate a new router for ifa.
3018 */
Nicolas Dichtela2204452016-10-12 10:10:40 +02003019 if (!atomic_read(&sp_ifa->rt->rt6i_ref)) {
Gao feng33d99112014-01-24 16:29:11 +08003020 ip6_rt_put(sp_ifa->rt);
3021 sp_ifa->rt = NULL;
3022 } else {
3023 continue;
3024 }
3025 }
Gao fenga881ae12013-06-16 11:14:30 +08003026
François-Xavier Le Bail57ec0af2013-12-02 11:28:49 +01003027 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303028
3029 /* Failure cases are ignored */
Gao feng534c8772013-06-02 22:16:21 +00003030 if (!IS_ERR(sp_rt)) {
3031 sp_ifa->rt = sp_rt;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303032 ip6_ins_rt(sp_rt);
Gao feng534c8772013-06-02 22:16:21 +00003033 }
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303034 }
3035 read_unlock_bh(&idev->lock);
3036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037}
3038
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003039void addrconf_add_linklocal(struct inet6_dev *idev,
3040 const struct in6_addr *addr, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003042 struct inet6_ifaddr *ifp;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003043 u32 addr_flags = flags | IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Neil Horman95c385b2007-04-25 17:08:10 -07003045#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3046 if (idev->cnf.optimistic_dad &&
David Miller702beb82008-07-20 18:17:02 -07003047 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
Neil Horman95c385b2007-04-25 17:08:10 -07003048 addr_flags |= IFA_F_OPTIMISTIC;
3049#endif
3050
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01003051 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
3052 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003054 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
David S. Millercf22f9a2012-04-14 21:37:40 -04003055 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 in6_ifa_put(ifp);
3057 }
3058}
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003059EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003061static bool ipv6_reserved_interfaceid(struct in6_addr address)
3062{
3063 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3064 return true;
3065
3066 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3067 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3068 return true;
3069
3070 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3071 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3072 return true;
3073
3074 return false;
3075}
3076
3077static int ipv6_generate_stable_address(struct in6_addr *address,
3078 u8 dad_count,
3079 const struct inet6_dev *idev)
3080{
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003081 static DEFINE_SPINLOCK(lock);
3082 static __u32 digest[SHA_DIGEST_WORDS];
3083 static __u32 workspace[SHA_WORKSPACE_WORDS];
3084
3085 static union {
3086 char __data[SHA_MESSAGE_BYTES];
3087 struct {
3088 struct in6_addr secret;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003089 __be32 prefix[2];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003090 unsigned char hwaddr[MAX_ADDR_LEN];
3091 u8 dad_count;
3092 } __packed;
3093 } data;
3094
3095 struct in6_addr secret;
3096 struct in6_addr temp;
3097 struct net *net = dev_net(idev->dev);
3098
3099 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3100
3101 if (idev->cnf.stable_secret.initialized)
3102 secret = idev->cnf.stable_secret.secret;
3103 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3104 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3105 else
3106 return -1;
3107
3108retry:
3109 spin_lock_bh(&lock);
3110
3111 sha_init(digest);
3112 memset(&data, 0, sizeof(data));
3113 memset(workspace, 0, sizeof(workspace));
3114 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003115 data.prefix[0] = address->s6_addr32[0];
3116 data.prefix[1] = address->s6_addr32[1];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003117 data.secret = secret;
3118 data.dad_count = dad_count;
3119
3120 sha_transform(digest, data.__data, workspace);
3121
3122 temp = *address;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003123 temp.s6_addr32[2] = (__force __be32)digest[0];
3124 temp.s6_addr32[3] = (__force __be32)digest[1];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003125
3126 spin_unlock_bh(&lock);
3127
3128 if (ipv6_reserved_interfaceid(temp)) {
3129 dad_count++;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01003130 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003131 return -1;
3132 goto retry;
3133 }
3134
3135 *address = temp;
3136 return 0;
3137}
3138
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003139static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3140{
3141 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3142
3143 if (s->initialized)
3144 return;
3145 s = &idev->cnf.stable_secret;
3146 get_random_bytes(&s->secret, sizeof(s->secret));
3147 s->initialized = true;
3148}
3149
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003150static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3151{
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003152 struct in6_addr addr;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003153
David Ahernca254492015-10-12 11:47:10 -07003154 /* no link local addresses on L3 master devices */
3155 if (netif_is_l3_master(idev->dev))
3156 return;
3157
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003158 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3159
Felix Jiad35a00b2017-01-26 16:59:17 +13003160 switch (idev->cnf.addr_gen_mode) {
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003161 case IN6_ADDR_GEN_MODE_RANDOM:
3162 ipv6_gen_mode_random_init(idev);
3163 /* fallthrough */
3164 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003165 if (!ipv6_generate_stable_address(&addr, 0, idev))
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003166 addrconf_add_linklocal(idev, &addr,
3167 IFA_F_STABLE_PRIVACY);
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003168 else if (prefix_route)
3169 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003170 break;
3171 case IN6_ADDR_GEN_MODE_EUI64:
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003172 /* addrconf_add_linklocal also adds a prefix_route and we
3173 * only need to care about prefix routes if ipv6_generate_eui64
3174 * couldn't generate one.
3175 */
3176 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003177 addrconf_add_linklocal(idev, &addr, 0);
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003178 else if (prefix_route)
3179 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003180 break;
3181 case IN6_ADDR_GEN_MODE_NONE:
3182 default:
3183 /* will not add any link local address */
3184 break;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003185 }
3186}
3187
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188static void addrconf_dev_config(struct net_device *dev)
3189{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003190 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
3192 ASSERT_RTNL();
3193
Herbert Xu74235a22007-06-14 13:02:55 -07003194 if ((dev->type != ARPHRD_ETHER) &&
3195 (dev->type != ARPHRD_FDDI) &&
Herbert Xu74235a22007-06-14 13:02:55 -07003196 (dev->type != ARPHRD_ARCNET) &&
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00003197 (dev->type != ARPHRD_INFINIBAND) &&
Nicolas Dichtele8377352013-08-20 12:16:06 +02003198 (dev->type != ARPHRD_IEEE1394) &&
Jukka Rissanene74bccb82013-12-11 17:05:36 +02003199 (dev->type != ARPHRD_TUNNEL6) &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003200 (dev->type != ARPHRD_6LOWPAN) &&
Felix Jia45ce0fd2017-01-26 16:59:18 +13003201 (dev->type != ARPHRD_IP6GRE) &&
3202 (dev->type != ARPHRD_IPGRE) &&
3203 (dev->type != ARPHRD_TUNNEL) &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003204 (dev->type != ARPHRD_NONE)) {
Herbert Xu74235a22007-06-14 13:02:55 -07003205 /* Alas, we support only Ethernet autoconfiguration. */
3206 return;
3207 }
3208
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00003210 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 return;
3212
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003213 /* this device type has no EUI support */
3214 if (dev->type == ARPHRD_NONE &&
Felix Jiad35a00b2017-01-26 16:59:17 +13003215 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3216 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003217
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003218 addrconf_addr_gen(idev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219}
3220
Amerigo Wang07a93622012-10-29 16:23:10 +00003221#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222static void addrconf_sit_config(struct net_device *dev)
3223{
3224 struct inet6_dev *idev;
3225
3226 ASSERT_RTNL();
3227
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003228 /*
3229 * Configure the tunnel with one of our IPv4
3230 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 * our v4 addrs in the tunnel
3232 */
3233
Ian Morrise5d08d72014-11-23 21:28:43 +00003234 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003235 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003236 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 return;
3238 }
3239
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003240 if (dev->priv_flags & IFF_ISATAP) {
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003241 addrconf_addr_gen(idev, false);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003242 return;
3243 }
3244
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 sit_add_v4_addrs(idev);
3246
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00003247 if (dev->flags&IFF_POINTOPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003250#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
Amerigo Wang07a93622012-10-29 16:23:10 +00003252#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003253static void addrconf_gre_config(struct net_device *dev)
3254{
3255 struct inet6_dev *idev;
stephen hemmingeraee80b52011-06-08 10:44:30 +00003256
stephen hemmingeraee80b52011-06-08 10:44:30 +00003257 ASSERT_RTNL();
3258
Ian Morrise5d08d72014-11-23 21:28:43 +00003259 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003260 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003261 pr_debug("%s: add_dev failed\n", __func__);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003262 return;
3263 }
3264
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003265 addrconf_addr_gen(idev, true);
Hannes Frederic Sowad9e4ce62015-10-08 18:19:39 +02003266 if (dev->flags & IFF_POINTOPOINT)
3267 addrconf_add_mroute(dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003268}
3269#endif
3270
David Ahernf1705ec2016-02-24 09:25:37 -08003271static int fixup_permanent_addr(struct inet6_dev *idev,
3272 struct inet6_ifaddr *ifp)
3273{
David Ahern8048ced2017-04-25 09:17:29 -07003274 /* rt6i_ref == 0 means the host route was removed from the
3275 * FIB, for example, if 'lo' device is taken down. In that
3276 * case regenerate the host route.
3277 */
3278 if (!ifp->rt || !atomic_read(&ifp->rt->rt6i_ref)) {
3279 struct rt6_info *rt, *prev;
David Ahernf1705ec2016-02-24 09:25:37 -08003280
3281 rt = addrconf_dst_alloc(idev, &ifp->addr, false);
3282 if (unlikely(IS_ERR(rt)))
3283 return PTR_ERR(rt);
3284
David Ahern8048ced2017-04-25 09:17:29 -07003285 /* ifp->rt can be accessed outside of rtnl */
3286 spin_lock(&ifp->lock);
3287 prev = ifp->rt;
David Ahernf1705ec2016-02-24 09:25:37 -08003288 ifp->rt = rt;
David Ahern8048ced2017-04-25 09:17:29 -07003289 spin_unlock(&ifp->lock);
3290
3291 ip6_rt_put(prev);
David Ahernf1705ec2016-02-24 09:25:37 -08003292 }
3293
3294 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3295 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3296 idev->dev, 0, 0);
3297 }
3298
3299 addrconf_dad_start(ifp);
3300
3301 return 0;
3302}
3303
3304static void addrconf_permanent_addr(struct net_device *dev)
3305{
3306 struct inet6_ifaddr *ifp, *tmp;
3307 struct inet6_dev *idev;
3308
3309 idev = __in6_dev_get(dev);
3310 if (!idev)
3311 return;
3312
3313 write_lock_bh(&idev->lock);
3314
3315 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3316 if ((ifp->flags & IFA_F_PERMANENT) &&
3317 fixup_permanent_addr(idev, ifp) < 0) {
3318 write_unlock_bh(&idev->lock);
3319 ipv6_del_addr(ifp);
3320 write_lock_bh(&idev->lock);
3321
3322 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3323 idev->dev->name, &ifp->addr);
3324 }
3325 }
3326
3327 write_unlock_bh(&idev->lock);
3328}
3329
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003330static int addrconf_notify(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00003331 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332{
Jiri Pirko351638e2013-05-28 01:30:21 +00003333 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
David Ahern4f7f34e2016-04-07 11:10:41 -07003334 struct netdev_notifier_changeupper_info *info;
Eric Dumazet748e2d92012-08-22 21:50:59 +00003335 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003336 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07003337 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003339 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003340 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07003341 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003342 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -07003343 if (IS_ERR(idev))
3344 return notifier_from_errno(PTR_ERR(idev));
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003345 }
3346 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003347
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003348 case NETDEV_CHANGEMTU:
3349 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3350 if (dev->mtu < IPV6_MIN_MTU) {
3351 addrconf_ifdown(dev, 1);
3352 break;
3353 }
3354
3355 if (idev) {
3356 rt6_mtu_change(dev, dev->mtu);
3357 idev->cnf.mtu6 = dev->mtu;
3358 break;
3359 }
3360
3361 /* allocate new idev */
3362 idev = ipv6_add_dev(dev);
3363 if (IS_ERR(idev))
3364 break;
3365
3366 /* device is still not ready */
3367 if (!(idev->if_flags & IF_READY))
3368 break;
3369
3370 run_pending = 1;
3371
3372 /* fall through */
3373
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003375 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07003376 if (dev->flags & IFF_SLAVE)
3377 break;
3378
WANG Cong3ce62a82014-09-11 15:07:16 -07003379 if (idev && idev->cnf.disable_ipv6)
3380 break;
3381
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003382 if (event == NETDEV_UP) {
David Ahern38bd10c2016-04-21 20:56:12 -07003383 /* restore routes for permanent addresses */
3384 addrconf_permanent_addr(dev);
3385
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07003386 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003387 /* device is not ready yet. */
Joe Perchesf3213832012-05-15 14:11:53 +00003388 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003389 dev->name);
3390 break;
3391 }
Kristian Slavov99081042006-02-08 16:10:53 -08003392
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11003393 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3394 idev = ipv6_add_dev(dev);
3395
WANG Conga317a2f2014-07-25 15:25:09 -07003396 if (!IS_ERR_OR_NULL(idev)) {
Kristian Slavov99081042006-02-08 16:10:53 -08003397 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08003398 run_pending = 1;
3399 }
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003400 } else if (event == NETDEV_CHANGE) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07003401 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003402 /* device is still not ready. */
3403 break;
3404 }
3405
3406 if (idev) {
Linus Lüssinga088d1d2017-02-03 08:11:03 +01003407 if (idev->if_flags & IF_READY) {
3408 /* device is already configured -
3409 * but resend MLD reports, we might
3410 * have roamed and need to update
3411 * multicast snooping switches
3412 */
3413 ipv6_mc_up(idev);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003414 break;
Linus Lüssinga088d1d2017-02-03 08:11:03 +01003415 }
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003416 idev->if_flags |= IF_READY;
3417 }
3418
Joe Perchesf3213832012-05-15 14:11:53 +00003419 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3420 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003421
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003422 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003423 }
3424
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003425 switch (dev->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +00003426#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 case ARPHRD_SIT:
3428 addrconf_sit_config(dev);
3429 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07003430#endif
Amerigo Wang07a93622012-10-29 16:23:10 +00003431#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003432 case ARPHRD_IPGRE:
3433 addrconf_gre_config(dev);
3434 break;
3435#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 case ARPHRD_LOOPBACK:
3437 init_loopback(dev);
3438 break;
3439
3440 default:
3441 addrconf_dev_config(dev);
3442 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003443 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003444
WANG Conga317a2f2014-07-25 15:25:09 -07003445 if (!IS_ERR_OR_NULL(idev)) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003446 if (run_pending)
3447 addrconf_dad_run(idev);
3448
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003449 /*
3450 * If the MTU changed during the interface down,
3451 * when the interface up, the changed MTU must be
3452 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003454 if (idev->cnf.mtu6 != dev->mtu &&
3455 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 rt6_mtu_change(dev, dev->mtu);
3457 idev->cnf.mtu6 = dev->mtu;
3458 }
3459 idev->tstamp = jiffies;
3460 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003461
3462 /*
3463 * If the changed mtu during down is lower than
3464 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 */
3466 if (dev->mtu < IPV6_MIN_MTU)
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003467 addrconf_ifdown(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 }
3469 break;
3470
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 case NETDEV_DOWN:
3472 case NETDEV_UNREGISTER:
3473 /*
3474 * Remove all addresses from this interface.
3475 */
3476 addrconf_ifdown(dev, event != NETDEV_DOWN);
3477 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003478
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07003481 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003482 addrconf_sysctl_unregister(idev);
WANG Conga317a2f2014-07-25 15:25:09 -07003483 err = addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07003484 if (err)
3485 return notifier_from_errno(err);
WANG Conga317a2f2014-07-25 15:25:09 -07003486 err = snmp6_register_dev(idev);
3487 if (err) {
3488 addrconf_sysctl_unregister(idev);
3489 return notifier_from_errno(err);
3490 }
Stephen Hemminger5632c512007-04-28 21:16:39 -07003491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003493
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003494 case NETDEV_PRE_TYPE_CHANGE:
3495 case NETDEV_POST_TYPE_CHANGE:
Andrew Lunn3ef09522015-12-03 21:12:32 +01003496 if (idev)
3497 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07003498 break;
David Ahern4f7f34e2016-04-07 11:10:41 -07003499
3500 case NETDEV_CHANGEUPPER:
3501 info = ptr;
3502
3503 /* flush all routes if dev is linked to or unlinked from
3504 * an L3 master device (e.g., VRF)
3505 */
3506 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3507 addrconf_ifdown(dev, 0);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
3510 return NOTIFY_OK;
3511}
3512
3513/*
3514 * addrconf module should be notified of a device going up
3515 */
3516static struct notifier_block ipv6_dev_notf = {
3517 .notifier_call = addrconf_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518};
3519
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003520static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07003521{
3522 struct inet6_dev *idev;
3523 ASSERT_RTNL();
3524
3525 idev = __in6_dev_get(dev);
3526
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003527 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003528 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003529 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003530 ipv6_mc_unmap(idev);
3531}
3532
David Ahern70af9212016-04-08 12:01:21 -07003533static bool addr_is_local(const struct in6_addr *addr)
3534{
3535 return ipv6_addr_type(addr) &
3536 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3537}
3538
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539static int addrconf_ifdown(struct net_device *dev, int how)
3540{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003541 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003542 struct inet6_dev *idev;
David Ahernf1705ec2016-02-24 09:25:37 -08003543 struct inet6_ifaddr *ifa, *tmp;
3544 struct list_head del_list;
3545 int _keep_addr;
3546 bool keep_addr;
David S. Miller73a8bd72011-01-23 23:27:15 -08003547 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
3549 ASSERT_RTNL();
3550
David S. Miller73a8bd72011-01-23 23:27:15 -08003551 rt6_ifdown(net, dev);
3552 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
3554 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003555 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 return -ENODEV;
3557
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003558 /*
3559 * Step 1: remove reference to ipv6 device from parent device.
3560 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003562 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003564
3565 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003566 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567
3568 /* Step 1.5: remove snmp6 entry */
3569 snmp6_unregister_dev(idev);
3570
3571 }
3572
David Ahernf1705ec2016-02-24 09:25:37 -08003573 /* aggregate the system setting and interface setting */
3574 _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3575 if (!_keep_addr)
3576 _keep_addr = idev->cnf.keep_addr_on_down;
3577
3578 /* combine the user config with event to determine if permanent
3579 * addresses are to be removed from address hash table
3580 */
Mike Manningbc561632016-08-12 12:02:38 +01003581 keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003582
David S. Miller73a8bd72011-01-23 23:27:15 -08003583 /* Step 2: clear hash table */
3584 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3585 struct hlist_head *h = &inet6_addr_lst[i];
David S. Miller73a8bd72011-01-23 23:27:15 -08003586
3587 spin_lock_bh(&addrconf_hash_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01003588restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08003589 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
David S. Miller73a8bd72011-01-23 23:27:15 -08003590 if (ifa->idev == idev) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003591 addrconf_del_dad_work(ifa);
David Ahernf1705ec2016-02-24 09:25:37 -08003592 /* combined flag + permanent flag decide if
3593 * address is retained on a down event
3594 */
3595 if (!keep_addr ||
David Ahern70af9212016-04-08 12:01:21 -07003596 !(ifa->flags & IFA_F_PERMANENT) ||
3597 addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003598 hlist_del_init_rcu(&ifa->addr_lst);
3599 goto restart;
3600 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003601 }
3602 }
3603 spin_unlock_bh(&addrconf_hash_lock);
3604 }
3605
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 write_lock_bh(&idev->lock);
3607
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003608 addrconf_del_rs_timer(idev);
3609
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003610 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003611 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003612 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003614 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00003615 while (!list_empty(&idev->tempaddr_list)) {
3616 ifa = list_first_entry(&idev->tempaddr_list,
3617 struct inet6_ifaddr, tmp_list);
3618 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 write_unlock_bh(&idev->lock);
3620 spin_lock_bh(&ifa->lock);
3621
3622 if (ifa->ifpub) {
3623 in6_ifa_put(ifa->ifpub);
3624 ifa->ifpub = NULL;
3625 }
3626 spin_unlock_bh(&ifa->lock);
3627 in6_ifa_put(ifa);
3628 write_lock_bh(&idev->lock);
3629 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003630
David Ahernf1705ec2016-02-24 09:25:37 -08003631 /* re-combine the user config with event to determine if permanent
3632 * addresses are to be removed from the interface list
3633 */
Mike Manningbc561632016-08-12 12:02:38 +01003634 keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003635
3636 INIT_LIST_HEAD(&del_list);
3637 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
David Ahern38bd10c2016-04-21 20:56:12 -07003638 struct rt6_info *rt = NULL;
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003639 bool keep;
David Ahern38bd10c2016-04-21 20:56:12 -07003640
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003641 addrconf_del_dad_work(ifa);
stephen hemminger84e8b802010-03-02 13:32:46 +00003642
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003643 keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3644 !addr_is_local(&ifa->addr);
3645 if (!keep)
3646 list_move(&ifa->if_list, &del_list);
3647
David S. Miller73a8bd72011-01-23 23:27:15 -08003648 write_unlock_bh(&idev->lock);
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003649 spin_lock_bh(&ifa->lock);
David Ahernf1705ec2016-02-24 09:25:37 -08003650
Rabin Vincenta2d6cbb2017-04-10 08:36:39 +02003651 if (keep) {
David Ahernf1705ec2016-02-24 09:25:37 -08003652 /* set state to skip the notifier below */
3653 state = INET6_IFADDR_STATE_DEAD;
3654 ifa->state = 0;
3655 if (!(ifa->flags & IFA_F_NODAD))
3656 ifa->flags |= IFA_F_TENTATIVE;
David Ahern38bd10c2016-04-21 20:56:12 -07003657
3658 rt = ifa->rt;
3659 ifa->rt = NULL;
David Ahernf1705ec2016-02-24 09:25:37 -08003660 } else {
3661 state = ifa->state;
3662 ifa->state = INET6_IFADDR_STATE_DEAD;
David Ahernf1705ec2016-02-24 09:25:37 -08003663 }
3664
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003665 spin_unlock_bh(&ifa->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003666
David Ahern38bd10c2016-04-21 20:56:12 -07003667 if (rt)
3668 ip6_del_rt(rt);
3669
David S. Miller73a8bd72011-01-23 23:27:15 -08003670 if (state != INET6_IFADDR_STATE_DEAD) {
3671 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Cong Wangf88c91d2013-04-14 23:18:43 +08003672 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
Mike Manningea06f712016-07-22 18:32:11 +01003673 } else {
3674 if (idev->cnf.forwarding)
3675 addrconf_leave_anycast(ifa);
3676 addrconf_leave_solict(ifa->idev, &ifa->addr);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00003677 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003678
David S. Miller73a8bd72011-01-23 23:27:15 -08003679 write_lock_bh(&idev->lock);
3680 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003681
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 write_unlock_bh(&idev->lock);
3683
David Ahernf1705ec2016-02-24 09:25:37 -08003684 /* now clean up addresses to be removed */
3685 while (!list_empty(&del_list)) {
3686 ifa = list_first_entry(&del_list,
3687 struct inet6_ifaddr, if_list);
3688 list_del(&ifa->if_list);
3689
3690 in6_ifa_put(ifa);
3691 }
3692
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003693 /* Step 5: Discard anycast and multicast list */
3694 if (how) {
3695 ipv6_ac_destroy_dev(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 ipv6_mc_destroy_dev(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003697 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 ipv6_mc_down(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003702
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003703 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003704 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003705 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 neigh_parms_release(&nd_tbl, idev->nd_parms);
3707 neigh_ifdown(&nd_tbl, dev);
3708 in6_dev_put(idev);
3709 }
3710 return 0;
3711}
3712
3713static void addrconf_rs_timer(unsigned long data)
3714{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003715 struct inet6_dev *idev = (struct inet6_dev *)data;
Cong Wangcaf92bc2013-08-31 13:44:32 +08003716 struct net_device *dev = idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003717 struct in6_addr lladdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003719 write_lock(&idev->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00003720 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 goto out;
3722
Shmulik Ladkani9ba2add2012-12-02 01:44:53 +00003723 if (!ipv6_accept_ra(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003725
3726 /* Announcement received after solicitation was sent */
3727 if (idev->if_flags & IF_RA_RCVD)
3728 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003730 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
Cong Wangcaf92bc2013-08-31 13:44:32 +08003731 write_unlock(&idev->lock);
3732 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3733 ndisc_send_rs(dev, &lladdr,
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003734 &in6addr_linklocal_allrouters);
3735 else
Cong Wangcaf92bc2013-08-31 13:44:32 +08003736 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
Cong Wangcaf92bc2013-08-31 13:44:32 +08003738 write_lock(&idev->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003739 idev->rs_interval = rfc3315_s14_backoff_update(
3740 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003741 /* The wait after the last probe can be shorter */
3742 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3743 idev->cnf.rtr_solicits) ?
3744 idev->cnf.rtr_solicit_delay :
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003745 idev->rs_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 /*
3748 * Note: we do not support deprecated "all on-link"
3749 * assumption any longer.
3750 */
Joe Perches91df42b2012-05-15 14:11:54 +00003751 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 }
3753
3754out:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003755 write_unlock(&idev->lock);
Cong Wangcaf92bc2013-08-31 13:44:32 +08003756put:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003757 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758}
3759
3760/*
3761 * Duplicate Address Detection
3762 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003763static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3764{
3765 unsigned long rand_num;
3766 struct inet6_dev *idev = ifp->idev;
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003767 u64 nonce;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003768
Neil Horman95c385b2007-04-25 17:08:10 -07003769 if (ifp->flags & IFA_F_OPTIMISTIC)
3770 rand_num = 0;
3771 else
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003772 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
Neil Horman95c385b2007-04-25 17:08:10 -07003773
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003774 nonce = 0;
3775 if (idev->cnf.enhanced_dad ||
3776 dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
3777 do
3778 get_random_bytes(&nonce, 6);
3779 while (nonce == 0);
3780 }
3781 ifp->dad_nonce = nonce;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003782 ifp->dad_probes = idev->cnf.dad_transmits;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003783 addrconf_mod_dad_work(ifp, rand_num);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003784}
3785
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003786static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787{
3788 struct inet6_dev *idev = ifp->idev;
3789 struct net_device *dev = idev->dev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003790 bool bump_id, notify = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
3792 addrconf_join_solict(dev, &ifp->addr);
3793
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003794 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
3796 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003797 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003798 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800
3801 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09003802 idev->cnf.accept_dad < 1 ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003803 !(ifp->flags&IFA_F_TENTATIVE) ||
3804 ifp->flags & IFA_F_NODAD) {
Paolo Abeni764d3be2016-11-22 16:57:40 +01003805 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003806 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003807 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 read_unlock_bh(&idev->lock);
3809
Paolo Abeni764d3be2016-11-22 16:57:40 +01003810 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 return;
3812 }
3813
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003814 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003815 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003816 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003817 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003818 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003819 * - keep it tentative if it is a permanent address.
3820 * - otherwise, kill it.
3821 */
3822 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003823 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003824 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003825 }
Neil Horman95c385b2007-04-25 17:08:10 -07003826
3827 /*
3828 * Optimistic nodes can start receiving
3829 * Frames right away
3830 */
Erik Kline7fd25612014-10-28 18:11:14 +09003831 if (ifp->flags & IFA_F_OPTIMISTIC) {
Neil Horman95c385b2007-04-25 17:08:10 -07003832 ip6_ins_rt(ifp->rt);
Erik Kline7fd25612014-10-28 18:11:14 +09003833 if (ipv6_use_optimistic_addr(idev)) {
3834 /* Because optimistic nodes can use this address,
3835 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3836 */
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003837 notify = true;
Erik Kline7fd25612014-10-28 18:11:14 +09003838 }
3839 }
Neil Horman95c385b2007-04-25 17:08:10 -07003840
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003841 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003843 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 read_unlock_bh(&idev->lock);
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003845 if (notify)
3846 ipv6_ifa_notify(RTM_NEWADDR, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847}
3848
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003849static void addrconf_dad_start(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003851 bool begin_dad = false;
3852
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003853 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003854 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3855 ifp->state = INET6_IFADDR_STATE_PREDAD;
3856 begin_dad = true;
3857 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003858 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003859
3860 if (begin_dad)
3861 addrconf_mod_dad_work(ifp, 0);
3862}
3863
3864static void addrconf_dad_work(struct work_struct *w)
3865{
3866 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3867 struct inet6_ifaddr,
3868 dad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 struct inet6_dev *idev = ifp->idev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003870 bool bump_id, disable_ipv6 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 struct in6_addr mcaddr;
3872
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003873 enum {
3874 DAD_PROCESS,
3875 DAD_BEGIN,
3876 DAD_ABORT,
3877 } action = DAD_PROCESS;
3878
3879 rtnl_lock();
3880
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003881 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003882 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3883 action = DAD_BEGIN;
3884 ifp->state = INET6_IFADDR_STATE_DAD;
3885 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3886 action = DAD_ABORT;
3887 ifp->state = INET6_IFADDR_STATE_POSTDAD;
Mike Manning85b51b12016-08-18 14:39:40 +01003888
3889 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6 &&
3890 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
3891 struct in6_addr addr;
3892
3893 addr.s6_addr32[0] = htonl(0xfe800000);
3894 addr.s6_addr32[1] = 0;
3895
3896 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
3897 ipv6_addr_equal(&ifp->addr, &addr)) {
3898 /* DAD failed for link-local based on MAC */
3899 idev->cnf.disable_ipv6 = 1;
3900
3901 pr_info("%s: IPv6 being disabled!\n",
3902 ifp->idev->dev->name);
3903 disable_ipv6 = true;
3904 }
3905 }
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003906 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003907 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003908
3909 if (action == DAD_BEGIN) {
3910 addrconf_dad_begin(ifp);
3911 goto out;
3912 } else if (action == DAD_ABORT) {
Wei Yongjun751eb6b2016-09-05 16:06:31 +08003913 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003914 addrconf_dad_stop(ifp, 1);
Mike Manning85b51b12016-08-18 14:39:40 +01003915 if (disable_ipv6)
3916 addrconf_ifdown(idev->dev, 0);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003917 goto out;
3918 }
3919
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003920 if (!ifp->dad_probes && addrconf_dad_end(ifp))
Herbert Xuf2344a12010-05-18 15:55:27 -07003921 goto out;
3922
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003923 write_lock_bh(&idev->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003924 if (idev->dead || !(idev->if_flags & IF_READY)) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003925 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 goto out;
3927 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003928
3929 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003930 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3931 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003932 write_unlock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003933 goto out;
3934 }
3935
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003936 if (ifp->dad_probes == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 /*
3938 * DAD was successful
3939 */
3940
Paolo Abeni764d3be2016-11-22 16:57:40 +01003941 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003942 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003943 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003944 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
Paolo Abeni764d3be2016-11-22 16:57:40 +01003946 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947
3948 goto out;
3949 }
3950
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003951 ifp->dad_probes--;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003952 addrconf_mod_dad_work(ifp,
3953 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
stephen hemminger21809fa2010-02-08 19:48:52 +00003954 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003955 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956
3957 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003959 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
3960 ifp->dad_nonce);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961out:
3962 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003963 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964}
3965
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003966/* ifp->idev must be at least read locked */
3967static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3968{
3969 struct inet6_ifaddr *ifpiter;
3970 struct inet6_dev *idev = ifp->idev;
3971
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01003972 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3973 if (ifpiter->scope > IFA_LINK)
3974 break;
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003975 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3976 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3977 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
3978 IFA_F_PERMANENT)
3979 return false;
3980 }
3981 return true;
3982}
3983
Paolo Abeni764d3be2016-11-22 16:57:40 +01003984static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003986 struct net_device *dev = ifp->idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003987 struct in6_addr lladdr;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003988 bool send_rs, send_mld;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003989
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003990 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
3992 /*
3993 * Configure the address for reception. Now it is valid.
3994 */
3995
3996 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3997
Tore Anderson026359b2011-08-28 23:47:33 +00003998 /* If added prefix is link local and we are prepared to process
3999 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 */
4001
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004002 read_lock_bh(&ifp->idev->lock);
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004003 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004004 send_rs = send_mld &&
4005 ipv6_accept_ra(ifp->idev) &&
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004006 ifp->idev->cnf.rtr_solicits != 0 &&
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004007 (dev->flags&IFF_LOOPBACK) == 0;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004008 read_unlock_bh(&ifp->idev->lock);
4009
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004010 /* While dad is in progress mld report's source address is in6_addrany.
4011 * Resend with proper ll now.
4012 */
4013 if (send_mld)
4014 ipv6_mc_dad_complete(ifp->idev);
4015
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004016 if (send_rs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 /*
4018 * If a host as already performed a random delay
4019 * [...] as part of DAD [...] there is no need
4020 * to delay again before sending the first RS
4021 */
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004022 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004023 return;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004024 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004026 write_lock_bh(&ifp->idev->lock);
4027 spin_lock(&ifp->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004028 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4029 ifp->idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004030 ifp->idev->rs_probes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 ifp->idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004032 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004033 spin_unlock(&ifp->lock);
4034 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 }
Paolo Abeni764d3be2016-11-22 16:57:40 +01004036
4037 if (bump_id)
4038 rt_genid_bump_ipv6(dev_net(dev));
Marcus Huewea11a7f72017-02-06 18:34:56 +01004039
4040 /* Make sure that a new temporary address will be created
4041 * before this temporary address becomes deprecated.
4042 */
4043 if (ifp->flags & IFA_F_TEMPORARY)
4044 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045}
4046
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004047static void addrconf_dad_run(struct inet6_dev *idev)
4048{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004049 struct inet6_ifaddr *ifp;
4050
4051 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00004052 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00004053 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07004054 if (ifp->flags & IFA_F_TENTATIVE &&
4055 ifp->state == INET6_IFADDR_STATE_DAD)
4056 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00004057 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004058 }
4059 read_unlock_bh(&idev->lock);
4060}
4061
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062#ifdef CONFIG_PROC_FS
4063struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08004064 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004066 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067};
4068
Mihai Maruseac1d578302012-01-03 23:31:35 +00004069static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070{
4071 struct inet6_ifaddr *ifa = NULL;
4072 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004073 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00004074 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
Mihai Maruseac1d578302012-01-03 23:31:35 +00004076 /* initial bucket if pos is 0 */
4077 if (pos == 0) {
4078 state->bucket = 0;
4079 state->offset = 0;
4080 }
4081
4082 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08004083 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00004084 addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004085 if (!net_eq(dev_net(ifa->idev->dev), net))
4086 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004087 /* sync with offset */
4088 if (p < state->offset) {
4089 p++;
4090 continue;
4091 }
4092 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004093 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004094 }
4095
4096 /* prepare for next bucket */
4097 state->offset = 0;
4098 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004100 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101}
4102
stephen hemmingerc2e21292010-03-17 20:31:10 +00004103static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4104 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105{
4106 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004107 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
Sasha Levinb67bfe02013-02-27 17:06:00 -08004109 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004110 if (!net_eq(dev_net(ifa->idev->dev), net))
4111 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004112 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004113 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004114 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004115
4116 while (++state->bucket < IN6_ADDR_HSIZE) {
Mihai Maruseac1d578302012-01-03 23:31:35 +00004117 state->offset = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004118 hlist_for_each_entry_rcu_bh(ifa,
stephen hemmingerc2e21292010-03-17 20:31:10 +00004119 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004120 if (!net_eq(dev_net(ifa->idev->dev), net))
4121 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004122 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004123 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08004124 }
4125 }
4126
stephen hemmingerc2e21292010-03-17 20:31:10 +00004127 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128}
4129
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004131 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132{
stephen hemminger5c578aed2010-03-17 20:31:11 +00004133 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00004134 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135}
4136
4137static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4138{
4139 struct inet6_ifaddr *ifa;
4140
4141 ifa = if6_get_next(seq, v);
4142 ++*pos;
4143 return ifa;
4144}
4145
4146static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004147 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148{
stephen hemminger5c578aed2010-03-17 20:31:11 +00004149 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150}
4151
4152static int if6_seq_show(struct seq_file *seq, void *v)
4153{
4154 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Jiri Pirko971a3512013-12-10 13:56:29 +01004155 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07004156 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 ifp->idev->dev->ifindex,
4158 ifp->prefix_len,
4159 ifp->scope,
Jiri Pirko971a3512013-12-10 13:56:29 +01004160 (u8) ifp->flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 ifp->idev->dev->name);
4162 return 0;
4163}
4164
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004165static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 .start = if6_seq_start,
4167 .next = if6_seq_next,
4168 .show = if6_seq_show,
4169 .stop = if6_seq_stop,
4170};
4171
4172static int if6_seq_open(struct inode *inode, struct file *file)
4173{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004174 return seq_open_net(inode, file, &if6_seq_ops,
4175 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176}
4177
Arjan van de Ven9a321442007-02-12 00:55:35 -08004178static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 .owner = THIS_MODULE,
4180 .open = if6_seq_open,
4181 .read = seq_read,
4182 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004183 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184};
4185
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004186static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187{
Gao fengd4beaa62013-02-18 01:34:54 +00004188 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 return -ENOMEM;
4190 return 0;
4191}
4192
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004193static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08004194{
Gao fengece31ff2013-02-18 01:34:56 +00004195 remove_proc_entry("if_inet6", net->proc_net);
Daniel Lezcano3c400902008-01-10 22:42:49 -08004196}
4197
4198static struct pernet_operations if6_proc_net_ops = {
Ian Morris67ba4152014-08-24 21:53:10 +01004199 .init = if6_proc_net_init,
4200 .exit = if6_proc_net_exit,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004201};
4202
4203int __init if6_proc_init(void)
4204{
4205 return register_pernet_subsys(&if6_proc_net_ops);
4206}
4207
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208void if6_proc_exit(void)
4209{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004210 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211}
4212#endif /* CONFIG_PROC_FS */
4213
Amerigo Wang07a93622012-10-29 16:23:10 +00004214#if IS_ENABLED(CONFIG_IPV6_MIP6)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004215/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004216int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004217{
4218 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004219 struct inet6_ifaddr *ifp = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00004220 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004221
stephen hemminger5c578aed2010-03-17 20:31:11 +00004222 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08004223 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09004224 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08004225 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09004226 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004227 (ifp->flags & IFA_F_HOMEADDRESS)) {
4228 ret = 1;
4229 break;
4230 }
4231 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00004232 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004233 return ret;
4234}
4235#endif
4236
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237/*
4238 * Periodic address status verification
4239 */
4240
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004241static void addrconf_verify_rtnl(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004243 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 int i;
4246
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004247 ASSERT_RTNL();
4248
stephen hemminger5c578aed2010-03-17 20:31:11 +00004249 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004251 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004253 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004255 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256restart:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004257 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
Yasushi Asanofad8da32013-12-31 12:04:19 +09004260 /* When setting preferred_lft to a value not zero or
4261 * infinity, while valid_lft is infinity
4262 * IFA_F_PERMANENT has a non-infinity life time.
4263 */
4264 if ((ifp->flags & IFA_F_PERMANENT) &&
4265 (ifp->prefered_lft == INFINITY_LIFE_TIME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 continue;
4267
4268 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004269 /* We try to batch several events at once. */
4270 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004272 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4273 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 spin_unlock(&ifp->lock);
4275 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 ipv6_del_addr(ifp);
4277 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004278 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4279 spin_unlock(&ifp->lock);
4280 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00004282 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 int deprecate = 0;
4284
4285 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4286 deprecate = 1;
4287 ifp->flags |= IFA_F_DEPRECATED;
4288 }
4289
Yasushi Asanofad8da32013-12-31 12:04:19 +09004290 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4291 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 next = ifp->tstamp + ifp->valid_lft * HZ;
4293
4294 spin_unlock(&ifp->lock);
4295
4296 if (deprecate) {
4297 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
4299 ipv6_ifa_notify(0, ifp);
4300 in6_ifa_put(ifp);
4301 goto restart;
4302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4304 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00004305 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4306 ifp->idev->cnf.dad_transmits *
Jiri Pirko1f9248e2013-12-07 19:26:53 +01004307 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
stephen hemminger88949cf2010-03-17 20:31:17 +00004308
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 if (age >= ifp->prefered_lft - regen_advance) {
4310 struct inet6_ifaddr *ifpub = ifp->ifpub;
4311 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4312 next = ifp->tstamp + ifp->prefered_lft * HZ;
4313 if (!ifp->regen_count && ifpub) {
4314 ifp->regen_count++;
4315 in6_ifa_hold(ifp);
4316 in6_ifa_hold(ifpub);
4317 spin_unlock(&ifp->lock);
stephen hemminger5c578aed2010-03-17 20:31:11 +00004318
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08004319 spin_lock(&ifpub->lock);
4320 ifpub->regen_count = 0;
4321 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 ipv6_create_tempaddr(ifpub, ifp);
4323 in6_ifa_put(ifpub);
4324 in6_ifa_put(ifp);
4325 goto restart;
4326 }
4327 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4328 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4329 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 } else {
4331 /* ifp->prefered_lft <= ifp->valid_lft */
4332 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4333 next = ifp->tstamp + ifp->prefered_lft * HZ;
4334 spin_unlock(&ifp->lock);
4335 }
4336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 }
4338
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004339 next_sec = round_jiffies_up(next);
4340 next_sched = next;
4341
4342 /* If rounded timeout is accurate enough, accept it. */
4343 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4344 next_sched = next_sec;
4345
4346 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4347 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4348 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4349
dingtianhongba3542e2013-08-17 10:27:04 +08004350 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4351 now, next, next_sec, next_sched);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004352 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
stephen hemminger5c578aed2010-03-17 20:31:11 +00004353 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354}
4355
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004356static void addrconf_verify_work(struct work_struct *w)
4357{
4358 rtnl_lock();
4359 addrconf_verify_rtnl();
4360 rtnl_unlock();
4361}
4362
4363static void addrconf_verify(void)
4364{
4365 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4366}
4367
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004368static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4369 struct in6_addr **peer_pfx)
Thomas Graf461d8832006-09-18 00:09:49 -07004370{
4371 struct in6_addr *pfx = NULL;
4372
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004373 *peer_pfx = NULL;
4374
Thomas Graf461d8832006-09-18 00:09:49 -07004375 if (addr)
4376 pfx = nla_data(addr);
4377
4378 if (local) {
4379 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004380 *peer_pfx = pfx;
4381 pfx = nla_data(local);
Thomas Graf461d8832006-09-18 00:09:49 -07004382 }
4383
4384 return pfx;
4385}
4386
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004387static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07004388 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4389 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4390 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
Jiri Pirko479840f2013-12-06 09:45:21 +01004391 [IFA_FLAGS] = { .len = sizeof(u32) },
Thomas Graf461d8832006-09-18 00:09:49 -07004392};
4393
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394static int
Thomas Graf661d2962013-03-21 07:45:29 +00004395inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004397 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07004398 struct ifaddrmsg *ifm;
4399 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004400 struct in6_addr *pfx, *peer_pfx;
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004401 u32 ifa_flags;
Thomas Grafb933f712006-09-18 00:10:19 -07004402 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
Thomas Grafb933f712006-09-18 00:10:19 -07004404 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4405 if (err < 0)
4406 return err;
4407
4408 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004409 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004410 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 return -EINVAL;
4412
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004413 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4414
4415 /* We ignore other flags so far. */
4416 ifa_flags &= IFA_F_MANAGETEMPADDR;
4417
4418 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4419 ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420}
4421
Jiri Pirko479840f2013-12-06 09:45:21 +01004422static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004423 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004424{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004425 u32 flags;
4426 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004427 unsigned long timeout;
Jiri Pirko53bd6742013-12-06 09:45:22 +01004428 bool was_managetempaddr;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004429 bool had_prefixroute;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09004430
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004431 ASSERT_RTNL();
4432
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004433 if (!valid_lft || (prefered_lft > valid_lft))
4434 return -EINVAL;
4435
Jiri Pirko53bd6742013-12-06 09:45:22 +01004436 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4437 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4438 return -EINVAL;
4439
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004440 timeout = addrconf_timeout_fixup(valid_lft, HZ);
4441 if (addrconf_finite_timeout(timeout)) {
4442 expires = jiffies_to_clock_t(timeout * HZ);
4443 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004444 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004445 } else {
4446 expires = 0;
4447 flags = 0;
4448 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004449 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004450
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004451 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4452 if (addrconf_finite_timeout(timeout)) {
4453 if (timeout == 0)
4454 ifa_flags |= IFA_F_DEPRECATED;
4455 prefered_lft = timeout;
4456 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004457
4458 spin_lock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004459 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004460 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4461 !(ifp->flags & IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004462 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
Thomas Haller761aac72014-01-15 15:36:58 +01004463 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4464 IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004465 ifp->flags |= ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004466 ifp->tstamp = jiffies;
4467 ifp->valid_lft = valid_lft;
4468 ifp->prefered_lft = prefered_lft;
4469
4470 spin_unlock_bh(&ifp->lock);
4471 if (!(ifp->flags&IFA_F_TENTATIVE))
4472 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004473
Thomas Haller761aac72014-01-15 15:36:58 +01004474 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4475 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4476 expires, flags);
Thomas Haller5b84efe2014-01-15 15:36:59 +01004477 } else if (had_prefixroute) {
4478 enum cleanup_prefix_rt_t action;
4479 unsigned long rt_expires;
4480
4481 write_lock_bh(&ifp->idev->lock);
4482 action = check_cleanup_prefix_route(ifp, &rt_expires);
4483 write_unlock_bh(&ifp->idev->lock);
4484
4485 if (action != CLEANUP_PREFIX_RT_NOP) {
4486 cleanup_prefix_route(ifp, rt_expires,
4487 action == CLEANUP_PREFIX_RT_DEL);
4488 }
Thomas Haller761aac72014-01-15 15:36:58 +01004489 }
Jiri Pirko53bd6742013-12-06 09:45:22 +01004490
4491 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4492 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4493 valid_lft = prefered_lft = 0;
4494 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4495 !was_managetempaddr, jiffies);
4496 }
4497
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004498 addrconf_verify_rtnl();
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004499
4500 return 0;
4501}
4502
4503static int
Thomas Graf661d2962013-03-21 07:45:29 +00004504inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004506 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07004507 struct ifaddrmsg *ifm;
4508 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004509 struct in6_addr *pfx, *peer_pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07004510 struct inet6_ifaddr *ifa;
4511 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004512 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
Jiri Pirko479840f2013-12-06 09:45:21 +01004513 u32 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07004514 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515
Thomas Graf461d8832006-09-18 00:09:49 -07004516 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4517 if (err < 0)
4518 return err;
4519
4520 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004521 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004522 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 return -EINVAL;
4524
Thomas Graf461d8832006-09-18 00:09:49 -07004525 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004526 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07004527
4528 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004529 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07004530 preferred_lft = ci->ifa_prefered;
4531 } else {
4532 preferred_lft = INFINITY_LIFE_TIME;
4533 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004534 }
4535
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004536 dev = __dev_get_by_index(net, ifm->ifa_index);
Ian Morris63159f22015-03-29 14:00:04 +01004537 if (!dev)
Thomas Graf7198f8c2006-09-18 00:13:46 -07004538 return -ENODEV;
4539
Jiri Pirko479840f2013-12-06 09:45:21 +01004540 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4541
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004542 /* We ignore other flags so far. */
Thomas Haller761aac72014-01-15 15:36:58 +01004543 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
Madhu Challa93a714d2015-02-25 09:58:35 -08004544 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004545
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08004546 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Ian Morris63159f22015-03-29 14:00:04 +01004547 if (!ifa) {
Thomas Graf7198f8c2006-09-18 00:13:46 -07004548 /*
4549 * It would be best to check for !NLM_F_CREATE here but
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08004550 * userspace already relies on not having to provide this.
Thomas Graf7198f8c2006-09-18 00:13:46 -07004551 */
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004552 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004553 ifm->ifa_prefixlen, ifa_flags,
4554 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004555 }
4556
Thomas Graf7198f8c2006-09-18 00:13:46 -07004557 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4558 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4559 err = -EEXIST;
4560 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004561 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07004562
4563 in6_ifa_put(ifa);
4564
4565 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566}
4567
Jiri Pirko479840f2013-12-06 09:45:21 +01004568static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
Thomas Graf101bb222006-09-18 00:11:52 -07004569 u8 scope, int ifindex)
4570{
4571 struct ifaddrmsg *ifm;
4572
4573 ifm = nlmsg_data(nlh);
4574 ifm->ifa_family = AF_INET6;
4575 ifm->ifa_prefixlen = prefixlen;
4576 ifm->ifa_flags = flags;
4577 ifm->ifa_scope = scope;
4578 ifm->ifa_index = ifindex;
4579}
4580
Thomas Graf85486af2006-09-18 00:11:24 -07004581static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4582 unsigned long tstamp, u32 preferred, u32 valid)
4583{
4584 struct ifa_cacheinfo ci;
4585
Thomas Graf18a31e12010-11-17 04:12:02 +00004586 ci.cstamp = cstamp_delta(cstamp);
4587 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07004588 ci.ifa_prefered = preferred;
4589 ci.ifa_valid = valid;
4590
4591 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4592}
4593
Thomas Graf101bb222006-09-18 00:11:52 -07004594static inline int rt_scope(int ifa_scope)
4595{
4596 if (ifa_scope & IFA_HOST)
4597 return RT_SCOPE_HOST;
4598 else if (ifa_scope & IFA_LINK)
4599 return RT_SCOPE_LINK;
4600 else if (ifa_scope & IFA_SITE)
4601 return RT_SCOPE_SITE;
4602 else
4603 return RT_SCOPE_UNIVERSE;
4604}
4605
Thomas Graf0ab68032006-09-18 00:12:35 -07004606static inline int inet6_ifaddr_msgsize(void)
4607{
Thomas Graf339bf982006-11-10 14:10:15 -08004608 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004609 + nla_total_size(16) /* IFA_LOCAL */
Thomas Graf339bf982006-11-10 14:10:15 -08004610 + nla_total_size(16) /* IFA_ADDRESS */
Jiri Pirko479840f2013-12-06 09:45:21 +01004611 + nla_total_size(sizeof(struct ifa_cacheinfo))
4612 + nla_total_size(4) /* IFA_FLAGS */;
Thomas Graf0ab68032006-09-18 00:12:35 -07004613}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004614
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004616 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07004619 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
Eric W. Biederman15e47302012-09-07 20:12:54 +00004621 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004622 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004623 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004624
Thomas Graf101bb222006-09-18 00:11:52 -07004625 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4626 ifa->idev->dev->ifindex);
4627
Yasushi Asanofad8da32013-12-31 12:04:19 +09004628 if (!((ifa->flags&IFA_F_PERMANENT) &&
4629 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
Thomas Graf85486af2006-09-18 00:11:24 -07004630 preferred = ifa->prefered_lft;
4631 valid = ifa->valid_lft;
4632 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00004634 if (preferred > tval)
4635 preferred -= tval;
4636 else
4637 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00004638 if (valid != INFINITY_LIFE_TIME) {
4639 if (valid > tval)
4640 valid -= tval;
4641 else
4642 valid = 0;
4643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 }
4645 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07004646 preferred = INFINITY_LIFE_TIME;
4647 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 }
Thomas Graf85486af2006-09-18 00:11:24 -07004649
Cong Wang7996c792013-05-22 05:41:06 +00004650 if (!ipv6_addr_any(&ifa->peer_addr)) {
Jiri Benc930345e2015-03-29 16:59:25 +02004651 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4652 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004653 goto error;
4654 } else
Jiri Benc930345e2015-03-29 16:59:25 +02004655 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004656 goto error;
4657
4658 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4659 goto error;
Thomas Graf85486af2006-09-18 00:11:24 -07004660
Jiri Pirko479840f2013-12-06 09:45:21 +01004661 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4662 goto error;
4663
Johannes Berg053c0952015-01-16 22:09:00 +01004664 nlmsg_end(skb, nlh);
4665 return 0;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004666
4667error:
4668 nlmsg_cancel(skb, nlh);
4669 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670}
4671
4672static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004673 u32 portid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004676 u8 scope = RT_SCOPE_UNIVERSE;
4677 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
Thomas Graf101bb222006-09-18 00:11:52 -07004679 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4680 scope = RT_SCOPE_SITE;
4681
Eric W. Biederman15e47302012-09-07 20:12:54 +00004682 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004683 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004684 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004685
Thomas Graf101bb222006-09-18 00:11:52 -07004686 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004687 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004688 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004689 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4690 nlmsg_cancel(skb, nlh);
4691 return -EMSGSIZE;
4692 }
Thomas Graf85486af2006-09-18 00:11:24 -07004693
Johannes Berg053c0952015-01-16 22:09:00 +01004694 nlmsg_end(skb, nlh);
4695 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696}
4697
4698static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004699 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004702 u8 scope = RT_SCOPE_UNIVERSE;
4703 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
Thomas Graf101bb222006-09-18 00:11:52 -07004705 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4706 scope = RT_SCOPE_SITE;
4707
Eric W. Biederman15e47302012-09-07 20:12:54 +00004708 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004709 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004710 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004711
Thomas Graf101bb222006-09-18 00:11:52 -07004712 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004713 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004714 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004715 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4716 nlmsg_cancel(skb, nlh);
4717 return -EMSGSIZE;
4718 }
Thomas Graf85486af2006-09-18 00:11:24 -07004719
Johannes Berg053c0952015-01-16 22:09:00 +01004720 nlmsg_end(skb, nlh);
4721 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722}
4723
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004724enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 UNICAST_ADDR,
4726 MULTICAST_ADDR,
4727 ANYCAST_ADDR,
4728};
4729
Eric Dumazet234b27c2009-11-12 04:11:50 +00004730/* called with rcu_read_lock() */
4731static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4732 struct netlink_callback *cb, enum addr_type_t type,
4733 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 struct ifmcaddr6 *ifmca;
4736 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004737 int err = 1;
4738 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739
Eric Dumazet234b27c2009-11-12 04:11:50 +00004740 read_lock_bh(&idev->lock);
4741 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00004742 case UNICAST_ADDR: {
4743 struct inet6_ifaddr *ifa;
4744
Eric Dumazet234b27c2009-11-12 04:11:50 +00004745 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00004746 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4747 if (++ip_idx < s_ip_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004748 continue;
4749 err = inet6_fill_ifaddr(skb, ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004750 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004751 cb->nlh->nlmsg_seq,
4752 RTM_NEWADDR,
4753 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004754 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004755 break;
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004756 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004758 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00004759 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004760 case MULTICAST_ADDR:
4761 /* multicast address */
4762 for (ifmca = idev->mc_list; ifmca;
4763 ifmca = ifmca->next, ip_idx++) {
4764 if (ip_idx < s_ip_idx)
4765 continue;
4766 err = inet6_fill_ifmcaddr(skb, ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004767 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004768 cb->nlh->nlmsg_seq,
4769 RTM_GETMULTICAST,
4770 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004771 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004772 break;
4773 }
4774 break;
4775 case ANYCAST_ADDR:
4776 /* anycast address */
4777 for (ifaca = idev->ac_list; ifaca;
4778 ifaca = ifaca->aca_next, ip_idx++) {
4779 if (ip_idx < s_ip_idx)
4780 continue;
4781 err = inet6_fill_ifacaddr(skb, ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004782 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004783 cb->nlh->nlmsg_seq,
4784 RTM_GETANYCAST,
4785 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004786 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004787 break;
4788 }
4789 break;
4790 default:
4791 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004793 read_unlock_bh(&idev->lock);
4794 *p_ip_idx = ip_idx;
4795 return err;
4796}
4797
4798static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4799 enum addr_type_t type)
4800{
4801 struct net *net = sock_net(skb->sk);
4802 int h, s_h;
4803 int idx, ip_idx;
4804 int s_idx, s_ip_idx;
4805 struct net_device *dev;
4806 struct inet6_dev *idev;
4807 struct hlist_head *head;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004808
4809 s_h = cb->args[0];
4810 s_idx = idx = cb->args[1];
4811 s_ip_idx = ip_idx = cb->args[2];
4812
4813 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004814 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004815 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4816 idx = 0;
4817 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08004818 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet234b27c2009-11-12 04:11:50 +00004819 if (idx < s_idx)
4820 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07004821 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004822 s_ip_idx = 0;
4823 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004824 idev = __in6_dev_get(dev);
4825 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004826 goto cont;
4827
4828 if (in6_dump_addrs(idev, skb, cb, type,
David S. Miller7b46a642015-01-18 23:36:08 -05004829 s_ip_idx, &ip_idx) < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004830 goto done;
4831cont:
4832 idx++;
4833 }
4834 }
4835done:
4836 rcu_read_unlock();
4837 cb->args[0] = h;
4838 cb->args[1] = idx;
4839 cb->args[2] = ip_idx;
4840
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 return skb->len;
4842}
4843
4844static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4845{
4846 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004847
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 return inet6_dump_addr(skb, cb, type);
4849}
4850
4851static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4852{
4853 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004854
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 return inet6_dump_addr(skb, cb, type);
4856}
4857
4858
4859static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4860{
4861 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004862
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 return inet6_dump_addr(skb, cb, type);
4864}
4865
Thomas Graf661d2962013-03-21 07:45:29 +00004866static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004867{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004868 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004869 struct ifaddrmsg *ifm;
4870 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004871 struct in6_addr *addr = NULL, *peer;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004872 struct net_device *dev = NULL;
4873 struct inet6_ifaddr *ifa;
4874 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004875 int err;
4876
Thomas Graf1b29fc22006-09-18 00:10:50 -07004877 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4878 if (err < 0)
4879 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004880
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004881 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
Ian Morris63159f22015-03-29 14:00:04 +01004882 if (!addr) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004883 err = -EINVAL;
4884 goto errout;
4885 }
4886
4887 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004888 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004889 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004890
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004891 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4892 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004893 err = -EADDRNOTAVAIL;
4894 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004895 }
4896
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004897 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4898 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004899 err = -ENOBUFS;
4900 goto errout_ifa;
4901 }
4902
Eric W. Biederman15e47302012-09-07 20:12:54 +00004903 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004904 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004905 if (err < 0) {
4906 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4907 WARN_ON(err == -EMSGSIZE);
4908 kfree_skb(skb);
4909 goto errout_ifa;
4910 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00004911 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004912errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004913 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004914errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004915 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004916}
4917
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4919{
4920 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004921 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07004922 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923
Mahesh Bandewarf784ad32017-01-04 15:01:01 -08004924 /* Don't send DELADDR notification for TENTATIVE address,
4925 * since NEWADDR notification is sent only after removing
4926 * TENTATIVE flag.
4927 */
4928 if (ifa->flags & IFA_F_TENTATIVE && event == RTM_DELADDR)
4929 return;
4930
Thomas Graf0ab68032006-09-18 00:12:35 -07004931 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01004932 if (!skb)
Thomas Graf5d620262006-08-15 00:35:02 -07004933 goto errout;
4934
4935 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004936 if (err < 0) {
4937 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4938 WARN_ON(err == -EMSGSIZE);
4939 kfree_skb(skb);
4940 goto errout;
4941 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004942 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4943 return;
Thomas Graf5d620262006-08-15 00:35:02 -07004944errout:
4945 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004946 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947}
4948
Dave Jonesb6f99a22007-03-22 12:27:49 -07004949static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 __s32 *array, int bytes)
4951{
Thomas Graf04561c12006-11-14 19:53:58 -08004952 BUG_ON(bytes < (DEVCONF_MAX * 4));
4953
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 memset(array, 0, bytes);
4955 array[DEVCONF_FORWARDING] = cnf->forwarding;
4956 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4957 array[DEVCONF_MTU6] = cnf->mtu6;
4958 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4959 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4960 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4961 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4962 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00004963 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4964 jiffies_to_msecs(cnf->rtr_solicit_interval);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004965 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
4966 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
Thomas Graf93908d12010-11-17 01:44:24 +00004967 array[DEVCONF_RTR_SOLICIT_DELAY] =
4968 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02004970 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4971 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4972 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4973 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4975 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4976 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4977 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4978 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004980 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
Hangbin Liu8013d1d2015-07-30 14:28:42 +08004981 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004982 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004983#ifdef CONFIG_IPV6_ROUTER_PREF
4984 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00004985 array[DEVCONF_RTR_PROBE_INTERVAL] =
4986 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08004987#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004988 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4989#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004990#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004991 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004992 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07004993#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4994 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
Erik Kline7fd25612014-10-28 18:11:14 +09004995 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
Neil Horman95c385b2007-04-25 17:08:10 -07004996#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004997#ifdef CONFIG_IPV6_MROUTE
4998 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4999#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09005000 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09005001 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00005002 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00005003 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02005004 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
Ben Greeard9333192014-06-25 14:44:53 -07005005 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
Harout Hedeshianc2943f12015-01-20 10:06:05 -07005006 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
Andy Gospodarek35103d12015-08-13 10:39:01 -04005007 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005008 /* we omit DEVCONF_STABLE_SECRET for now */
Erik Kline3985e8a2015-07-22 16:38:25 +09005009 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
Johannes Bergabbc3042016-02-04 13:31:19 +01005010 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
Johannes Berg7a02bf82016-02-04 13:31:20 +01005011 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
David Ahernf1705ec2016-02-24 09:25:37 -08005012 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
David Lebrun1ababeb2016-11-08 14:57:39 +01005013 array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
David Lebrunbf355b82016-11-08 14:57:42 +01005014#ifdef CONFIG_IPV6_SEG6_HMAC
5015 array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5016#endif
Erik Nordmarkadc176c2016-12-02 14:00:08 -08005017 array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
Felix Jiad35a00b2017-01-26 16:59:17 +13005018 array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019}
5020
Thomas Grafb382b192010-11-16 04:33:57 +00005021static inline size_t inet6_ifla6_size(void)
5022{
5023 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5024 + nla_total_size(sizeof(struct ifla_cacheinfo))
5025 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5026 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005027 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5028 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
Thomas Grafb382b192010-11-16 04:33:57 +00005029}
5030
Thomas Graf339bf982006-11-10 14:10:15 -08005031static inline size_t inet6_if_nlmsg_size(void)
5032{
5033 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5034 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5035 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5036 + nla_total_size(4) /* IFLA_MTU */
5037 + nla_total_size(4) /* IFLA_LINK */
Andy Gospodarek03443382015-08-13 15:26:35 -04005038 + nla_total_size(1) /* IFLA_OPERSTATE */
Thomas Grafb382b192010-11-16 04:33:57 +00005039 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08005040}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005041
Eric Dumazetbe281e52011-05-19 01:14:23 +00005042static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Jia Heaca05672016-09-30 11:29:03 +08005043 int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005044{
5045 int i;
Jia Heaca05672016-09-30 11:29:03 +08005046 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005047 BUG_ON(pad < 0);
5048
5049 /* Use put_unaligned() because stats may not be aligned for u64. */
Jia Heaca05672016-09-30 11:29:03 +08005050 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5051 for (i = 1; i < ICMP6_MIB_MAX; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00005052 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005053
Jia Heaca05672016-09-30 11:29:03 +08005054 memset(&stats[ICMP6_MIB_MAX], 0, pad);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005055}
5056
WANG Cong698365f2014-05-05 15:55:55 -07005057static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305058 int bytes, size_t syncpoff)
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005059{
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305060 int i, c;
5061 u64 buff[IPSTATS_MIB_MAX];
5062 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5063
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005064 BUG_ON(pad < 0);
5065
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305066 memset(buff, 0, sizeof(buff));
5067 buff[0] = IPSTATS_MIB_MAX;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005068
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305069 for_each_possible_cpu(c) {
5070 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5071 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5072 }
5073
5074 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5075 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005076}
5077
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005078static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5079 int bytes)
5080{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005081 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005082 case IFLA_INET6_STATS:
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305083 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5084 offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005085 break;
5086 case IFLA_INET6_ICMP6STATS:
Jia Heaca05672016-09-30 11:29:03 +08005087 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005088 break;
5089 }
5090}
5091
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005092static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5093 u32 ext_filter_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005095 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08005096 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097
David S. Millerc78679e2012-04-01 20:27:33 -04005098 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5099 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08005101 ci.tstamp = cstamp_delta(idev->tstamp);
5102 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
Jiri Pirko1f9248e2013-12-07 19:26:53 +01005103 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
David S. Millerc78679e2012-04-01 20:27:33 -04005104 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5105 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005106 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
Ian Morris63159f22015-03-29 14:00:04 +01005107 if (!nla)
Thomas Graf04561c12006-11-14 19:53:58 -08005108 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005109 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005111 /* XXX - MC not implemented */
5112
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005113 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5114 return 0;
5115
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005116 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005117 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005118 goto nla_put_failure;
5119 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5120
5121 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005122 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005123 goto nla_put_failure;
5124 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005126 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
Ian Morris63159f22015-03-29 14:00:04 +01005127 if (!nla)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005128 goto nla_put_failure;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005129
Felix Jiad35a00b2017-01-26 16:59:17 +13005130 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005131 goto nla_put_failure;
5132
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005133 read_lock_bh(&idev->lock);
5134 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5135 read_unlock_bh(&idev->lock);
5136
Thomas Grafb382b192010-11-16 04:33:57 +00005137 return 0;
5138
5139nla_put_failure:
5140 return -EMSGSIZE;
5141}
5142
Arad, Ronenb1974ed2015-10-19 09:23:28 -07005143static size_t inet6_get_link_af_size(const struct net_device *dev,
5144 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005145{
5146 if (!__in6_dev_get(dev))
5147 return 0;
5148
5149 return inet6_ifla6_size();
5150}
5151
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005152static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5153 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005154{
5155 struct inet6_dev *idev = __in6_dev_get(dev);
5156
5157 if (!idev)
5158 return -ENODATA;
5159
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005160 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005161 return -EMSGSIZE;
5162
5163 return 0;
5164}
5165
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005166static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5167{
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005168 struct inet6_ifaddr *ifp;
5169 struct net_device *dev = idev->dev;
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005170 bool clear_token, update_rs = false;
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005171 struct in6_addr ll_addr;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005172
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005173 ASSERT_RTNL();
5174
Ian Morris63159f22015-03-29 14:00:04 +01005175 if (!token)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005176 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005177 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5178 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005179 if (!ipv6_accept_ra(idev))
5180 return -EINVAL;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005181 if (idev->cnf.rtr_solicits == 0)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005182 return -EINVAL;
5183
5184 write_lock_bh(&idev->lock);
5185
5186 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5187 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5188
5189 write_unlock_bh(&idev->lock);
5190
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005191 clear_token = ipv6_addr_any(token);
5192 if (clear_token)
5193 goto update_lft;
5194
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005195 if (!idev->dead && (idev->if_flags & IF_READY) &&
5196 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5197 IFA_F_OPTIMISTIC)) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005198 /* If we're not ready, then normal ifup will take care
5199 * of this. Otherwise, we need to request our rs here.
5200 */
5201 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5202 update_rs = true;
5203 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005204
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005205update_lft:
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005206 write_lock_bh(&idev->lock);
Daniel Borkmannfc403832013-04-09 03:47:15 +00005207
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005208 if (update_rs) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005209 idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005210 idev->rs_interval = rfc3315_s14_backoff_init(
5211 idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005212 idev->rs_probes = 1;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005213 addrconf_mod_rs_timer(idev, idev->rs_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005214 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005215
5216 /* Well, that's kinda nasty ... */
5217 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5218 spin_lock(&ifp->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00005219 if (ifp->tokenized) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005220 ifp->valid_lft = 0;
5221 ifp->prefered_lft = 0;
5222 }
5223 spin_unlock(&ifp->lock);
5224 }
5225
5226 write_unlock_bh(&idev->lock);
Lubomir Rintelb2ed64a2014-10-27 17:39:16 +01005227 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005228 addrconf_verify_rtnl();
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005229 return 0;
5230}
5231
Daniel Borkmann11b1f822015-02-05 14:39:11 +01005232static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5233 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5234 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5235};
5236
5237static int inet6_validate_link_af(const struct net_device *dev,
5238 const struct nlattr *nla)
5239{
5240 struct nlattr *tb[IFLA_INET6_MAX + 1];
5241
5242 if (dev && !__in6_dev_get(dev))
5243 return -EAFNOSUPPORT;
5244
5245 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
5246}
5247
Felix Jiad35a00b2017-01-26 16:59:17 +13005248static int check_addr_gen_mode(int mode)
5249{
5250 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5251 mode != IN6_ADDR_GEN_MODE_NONE &&
5252 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5253 mode != IN6_ADDR_GEN_MODE_RANDOM)
5254 return -EINVAL;
5255 return 1;
5256}
5257
5258static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5259 int mode)
5260{
5261 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5262 !idev->cnf.stable_secret.initialized &&
5263 !net->ipv6.devconf_dflt->stable_secret.initialized)
5264 return -EINVAL;
5265 return 1;
5266}
5267
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005268static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5269{
5270 int err = -EINVAL;
5271 struct inet6_dev *idev = __in6_dev_get(dev);
5272 struct nlattr *tb[IFLA_INET6_MAX + 1];
5273
5274 if (!idev)
5275 return -EAFNOSUPPORT;
5276
5277 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
5278 BUG();
5279
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005280 if (tb[IFLA_INET6_TOKEN]) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005281 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005282 if (err)
5283 return err;
5284 }
5285
5286 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5287 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5288
Felix Jiad35a00b2017-01-26 16:59:17 +13005289 if (check_addr_gen_mode(mode) < 0 ||
5290 check_stable_privacy(idev, dev_net(dev), mode) < 0)
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005291 return -EINVAL;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005292
Felix Jiad35a00b2017-01-26 16:59:17 +13005293 idev->cnf.addr_gen_mode = mode;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005294 err = 0;
5295 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005296
5297 return err;
5298}
5299
Thomas Grafb382b192010-11-16 04:33:57 +00005300static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005301 u32 portid, u32 seq, int event, unsigned int flags)
Thomas Grafb382b192010-11-16 04:33:57 +00005302{
5303 struct net_device *dev = idev->dev;
5304 struct ifinfomsg *hdr;
5305 struct nlmsghdr *nlh;
5306 void *protoinfo;
5307
Eric W. Biederman15e47302012-09-07 20:12:54 +00005308 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005309 if (!nlh)
Thomas Grafb382b192010-11-16 04:33:57 +00005310 return -EMSGSIZE;
5311
5312 hdr = nlmsg_data(nlh);
5313 hdr->ifi_family = AF_INET6;
5314 hdr->__ifi_pad = 0;
5315 hdr->ifi_type = dev->type;
5316 hdr->ifi_index = dev->ifindex;
5317 hdr->ifi_flags = dev_get_flags(dev);
5318 hdr->ifi_change = 0;
5319
David S. Millerc78679e2012-04-01 20:27:33 -04005320 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5321 (dev->addr_len &&
5322 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5323 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02005324 (dev->ifindex != dev_get_iflink(dev) &&
Andy Gospodarek03443382015-08-13 15:26:35 -04005325 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5326 nla_put_u8(skb, IFLA_OPERSTATE,
5327 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
David S. Millerc78679e2012-04-01 20:27:33 -04005328 goto nla_put_failure;
Thomas Grafb382b192010-11-16 04:33:57 +00005329 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
Ian Morris63159f22015-03-29 14:00:04 +01005330 if (!protoinfo)
Thomas Grafb382b192010-11-16 04:33:57 +00005331 goto nla_put_failure;
5332
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005333 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005334 goto nla_put_failure;
5335
Thomas Graf04561c12006-11-14 19:53:58 -08005336 nla_nest_end(skb, protoinfo);
Johannes Berg053c0952015-01-16 22:09:00 +01005337 nlmsg_end(skb, nlh);
5338 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339
Thomas Graf04561c12006-11-14 19:53:58 -08005340nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005341 nlmsg_cancel(skb, nlh);
5342 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343}
5344
5345static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5346{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005347 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00005348 int h, s_h;
David S. Miller434a8a52009-11-11 18:53:00 -08005349 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 struct net_device *dev;
5351 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00005352 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353
Eric Dumazet84d26972009-11-09 12:11:28 +00005354 s_h = cb->args[0];
5355 s_idx = cb->args[1];
5356
5357 rcu_read_lock();
5358 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5359 idx = 0;
5360 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08005361 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet84d26972009-11-09 12:11:28 +00005362 if (idx < s_idx)
5363 goto cont;
5364 idev = __in6_dev_get(dev);
5365 if (!idev)
5366 goto cont;
5367 if (inet6_fill_ifinfo(skb, idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005368 NETLINK_CB(cb->skb).portid,
Eric Dumazet84d26972009-11-09 12:11:28 +00005369 cb->nlh->nlmsg_seq,
Johannes Berg053c0952015-01-16 22:09:00 +01005370 RTM_NEWLINK, NLM_F_MULTI) < 0)
Eric Dumazet84d26972009-11-09 12:11:28 +00005371 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07005372cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00005373 idx++;
5374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 }
Eric Dumazet84d26972009-11-09 12:11:28 +00005376out:
5377 rcu_read_unlock();
5378 cb->args[1] = idx;
5379 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
5381 return skb->len;
5382}
5383
5384void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5385{
5386 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005387 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005388 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005389
Thomas Graf339bf982006-11-10 14:10:15 -08005390 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005391 if (!skb)
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005392 goto errout;
5393
5394 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005395 if (err < 0) {
5396 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5397 WARN_ON(err == -EMSGSIZE);
5398 kfree_skb(skb);
5399 goto errout;
5400 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005401 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005402 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005403errout:
5404 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005405 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406}
5407
Thomas Graf339bf982006-11-10 14:10:15 -08005408static inline size_t inet6_prefix_nlmsg_size(void)
5409{
5410 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5411 + nla_total_size(sizeof(struct in6_addr))
5412 + nla_total_size(sizeof(struct prefix_cacheinfo));
5413}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005414
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005416 struct prefix_info *pinfo, u32 portid, u32 seq,
Thomas Graf6051e2f2006-11-14 19:54:19 -08005417 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418{
Thomas Graf6051e2f2006-11-14 19:54:19 -08005419 struct prefixmsg *pmsg;
5420 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 struct prefix_cacheinfo ci;
5422
Eric W. Biederman15e47302012-09-07 20:12:54 +00005423 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005424 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005425 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08005426
5427 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07005429 pmsg->prefix_pad1 = 0;
5430 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 pmsg->prefix_ifindex = idev->dev->ifindex;
5432 pmsg->prefix_len = pinfo->prefix_len;
5433 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07005434 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 pmsg->prefix_flags = 0;
5436 if (pinfo->onlink)
5437 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5438 if (pinfo->autoconf)
5439 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5440
David S. Millerc78679e2012-04-01 20:27:33 -04005441 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5442 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 ci.preferred_time = ntohl(pinfo->prefered);
5444 ci.valid_time = ntohl(pinfo->valid);
David S. Millerc78679e2012-04-01 20:27:33 -04005445 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5446 goto nla_put_failure;
Johannes Berg053c0952015-01-16 22:09:00 +01005447 nlmsg_end(skb, nlh);
5448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449
Thomas Graf6051e2f2006-11-14 19:54:19 -08005450nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005451 nlmsg_cancel(skb, nlh);
5452 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453}
5454
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005455static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 struct prefix_info *pinfo)
5457{
5458 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005459 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005460 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461
Thomas Graf339bf982006-11-10 14:10:15 -08005462 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005463 if (!skb)
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005464 goto errout;
5465
5466 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005467 if (err < 0) {
5468 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5469 WARN_ON(err == -EMSGSIZE);
5470 kfree_skb(skb);
5471 goto errout;
5472 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005473 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5474 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005475errout:
5476 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08005477 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478}
5479
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5481{
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005482 struct net *net = dev_net(ifp->idev->dev);
5483
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005484 if (event)
5485 ASSERT_RTNL();
5486
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5488
5489 switch (event) {
5490 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07005491 /*
5492 * If the address was optimistic
5493 * we inserted the route at the start of
5494 * our DAD process, so we don't need
5495 * to do it again
5496 */
5497 if (!(ifp->rt->rt6i_node))
5498 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 if (ifp->idev->cnf.forwarding)
5500 addrconf_join_anycast(ifp);
Cong Wang7996c792013-05-22 05:41:06 +00005501 if (!ipv6_addr_any(&ifp->peer_addr))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005502 addrconf_prefix_route(&ifp->peer_addr, 128,
5503 ifp->idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 break;
5505 case RTM_DELADDR:
5506 if (ifp->idev->cnf.forwarding)
5507 addrconf_leave_anycast(ifp);
5508 addrconf_leave_solict(ifp->idev, &ifp->addr);
Cong Wang7996c792013-05-22 05:41:06 +00005509 if (!ipv6_addr_any(&ifp->peer_addr)) {
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005510 struct rt6_info *rt;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005511
Nicolas Dichtele7478df2014-09-03 23:59:22 +02005512 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5513 ifp->idev->dev, 0, 0);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07005514 if (rt)
5515 ip6_del_rt(rt);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005516 }
David Ahern38bd10c2016-04-21 20:56:12 -07005517 if (ifp->rt) {
5518 dst_hold(&ifp->rt->dst);
5519 ip6_del_rt(ifp->rt);
5520 }
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02005521 rt_genid_bump_ipv6(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 break;
5523 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005524 atomic_inc(&net->ipv6.dev_addr_genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525}
5526
5527static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5528{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005529 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 if (likely(ifp->idev->dead == 0))
5531 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005532 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533}
5534
5535#ifdef CONFIG_SYSCTL
5536
5537static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005538int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 void __user *buffer, size_t *lenp, loff_t *ppos)
5540{
5541 int *valp = ctl->data;
5542 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005543 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005544 struct ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 int ret;
5546
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005547 /*
5548 * ctl->data points to idev->cnf.forwarding, we should
5549 * not modify it until we get the rtnl lock.
5550 */
5551 lctl = *ctl;
5552 lctl.data = &val;
5553
5554 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08005556 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00005557 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005558 if (ret)
5559 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005560 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561}
5562
Marcelo Leitner77751422015-02-23 11:17:13 -03005563static
5564int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5565 void __user *buffer, size_t *lenp, loff_t *ppos)
5566{
5567 struct inet6_dev *idev = ctl->extra1;
5568 int min_mtu = IPV6_MIN_MTU;
5569 struct ctl_table lctl;
5570
5571 lctl = *ctl;
5572 lctl.extra1 = &min_mtu;
5573 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5574
5575 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5576}
5577
Brian Haley56d417b2009-06-01 03:07:33 -07005578static void dev_disable_change(struct inet6_dev *idev)
5579{
Cong Wang75538c22013-05-29 11:30:50 +08005580 struct netdev_notifier_info info;
5581
Brian Haley56d417b2009-06-01 03:07:33 -07005582 if (!idev || !idev->dev)
5583 return;
5584
Cong Wang75538c22013-05-29 11:30:50 +08005585 netdev_notifier_info_init(&info, idev->dev);
Brian Haley56d417b2009-06-01 03:07:33 -07005586 if (idev->cnf.disable_ipv6)
Cong Wang75538c22013-05-29 11:30:50 +08005587 addrconf_notify(NULL, NETDEV_DOWN, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005588 else
Cong Wang75538c22013-05-29 11:30:50 +08005589 addrconf_notify(NULL, NETDEV_UP, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005590}
5591
5592static void addrconf_disable_change(struct net *net, __s32 newf)
5593{
5594 struct net_device *dev;
5595 struct inet6_dev *idev;
5596
Kefeng Wang03e4def2017-01-19 16:26:21 +08005597 for_each_netdev(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07005598 idev = __in6_dev_get(dev);
5599 if (idev) {
5600 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5601 idev->cnf.disable_ipv6 = newf;
5602 if (changed)
5603 dev_disable_change(idev);
5604 }
Brian Haley56d417b2009-06-01 03:07:33 -07005605 }
Brian Haley56d417b2009-06-01 03:07:33 -07005606}
5607
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005608static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07005609{
5610 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005611 int old;
5612
5613 if (!rtnl_trylock())
5614 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07005615
5616 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005617 old = *p;
5618 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07005619
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005620 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5621 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07005622 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005623 }
Brian Haley56d417b2009-06-01 03:07:33 -07005624
5625 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07005626 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5627 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005628 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07005629 dev_disable_change((struct inet6_dev *)table->extra1);
5630
5631 rtnl_unlock();
5632 return 0;
5633}
5634
5635static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005636int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07005637 void __user *buffer, size_t *lenp, loff_t *ppos)
5638{
5639 int *valp = ctl->data;
5640 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005641 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005642 struct ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07005643 int ret;
5644
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005645 /*
5646 * ctl->data points to idev->cnf.disable_ipv6, we should
5647 * not modify it until we get the rtnl lock.
5648 */
5649 lctl = *ctl;
5650 lctl.data = &val;
5651
5652 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07005653
5654 if (write)
5655 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005656 if (ret)
5657 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07005658 return ret;
5659}
5660
stephen hemmingerc92d5492013-12-17 22:37:14 -08005661static
5662int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5663 void __user *buffer, size_t *lenp, loff_t *ppos)
5664{
5665 int *valp = ctl->data;
5666 int ret;
5667 int old, new;
5668
5669 old = *valp;
5670 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5671 new = *valp;
5672
5673 if (write && old != new) {
5674 struct net *net = ctl->extra2;
5675
5676 if (!rtnl_trylock())
5677 return restart_syscall();
5678
5679 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5680 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5681 NETCONFA_IFINDEX_DEFAULT,
5682 net->ipv6.devconf_dflt);
5683 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5684 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5685 NETCONFA_IFINDEX_ALL,
5686 net->ipv6.devconf_all);
5687 else {
5688 struct inet6_dev *idev = ctl->extra1;
5689
5690 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5691 idev->dev->ifindex,
5692 &idev->cnf);
5693 }
5694 rtnl_unlock();
5695 }
5696
5697 return ret;
5698}
5699
Felix Jiad35a00b2017-01-26 16:59:17 +13005700static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
5701 void __user *buffer, size_t *lenp,
5702 loff_t *ppos)
5703{
5704 int ret = 0;
5705 int new_val;
5706 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
5707 struct net *net = (struct net *)ctl->extra2;
5708
Felix Jia8c171d62017-02-27 12:41:23 +13005709 if (!rtnl_trylock())
5710 return restart_syscall();
5711
Felix Jiad35a00b2017-01-26 16:59:17 +13005712 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5713
5714 if (write) {
5715 new_val = *((int *)ctl->data);
5716
Felix Jia8c171d62017-02-27 12:41:23 +13005717 if (check_addr_gen_mode(new_val) < 0) {
5718 ret = -EINVAL;
5719 goto out;
5720 }
Felix Jiad35a00b2017-01-26 16:59:17 +13005721
5722 /* request for default */
5723 if (&net->ipv6.devconf_dflt->addr_gen_mode == ctl->data) {
5724 ipv6_devconf_dflt.addr_gen_mode = new_val;
5725
5726 /* request for individual net device */
5727 } else {
5728 if (!idev)
Felix Jia8c171d62017-02-27 12:41:23 +13005729 goto out;
Felix Jiad35a00b2017-01-26 16:59:17 +13005730
Felix Jia8c171d62017-02-27 12:41:23 +13005731 if (check_stable_privacy(idev, net, new_val) < 0) {
5732 ret = -EINVAL;
5733 goto out;
5734 }
Felix Jiad35a00b2017-01-26 16:59:17 +13005735
5736 if (idev->cnf.addr_gen_mode != new_val) {
5737 idev->cnf.addr_gen_mode = new_val;
Felix Jiad35a00b2017-01-26 16:59:17 +13005738 addrconf_dev_config(idev->dev);
Felix Jiad35a00b2017-01-26 16:59:17 +13005739 }
5740 }
5741 }
5742
Felix Jia8c171d62017-02-27 12:41:23 +13005743out:
5744 rtnl_unlock();
5745
Felix Jiad35a00b2017-01-26 16:59:17 +13005746 return ret;
5747}
5748
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005749static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5750 void __user *buffer, size_t *lenp,
5751 loff_t *ppos)
5752{
5753 int err;
5754 struct in6_addr addr;
5755 char str[IPV6_MAX_STRLEN];
5756 struct ctl_table lctl = *ctl;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005757 struct net *net = ctl->extra2;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005758 struct ipv6_stable_secret *secret = ctl->data;
5759
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005760 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5761 return -EIO;
5762
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005763 lctl.maxlen = IPV6_MAX_STRLEN;
5764 lctl.data = str;
5765
5766 if (!rtnl_trylock())
5767 return restart_syscall();
5768
5769 if (!write && !secret->initialized) {
5770 err = -EIO;
5771 goto out;
5772 }
5773
WANG Cong5449a5c2015-12-21 10:55:45 -08005774 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
5775 if (err >= sizeof(str)) {
5776 err = -EIO;
5777 goto out;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005778 }
5779
5780 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5781 if (err || !write)
5782 goto out;
5783
5784 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5785 err = -EIO;
5786 goto out;
5787 }
5788
5789 secret->initialized = true;
5790 secret->secret = addr;
5791
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005792 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5793 struct net_device *dev;
5794
5795 for_each_netdev(net, dev) {
5796 struct inet6_dev *idev = __in6_dev_get(dev);
5797
5798 if (idev) {
Felix Jiad35a00b2017-01-26 16:59:17 +13005799 idev->cnf.addr_gen_mode =
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005800 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5801 }
5802 }
5803 } else {
5804 struct inet6_dev *idev = ctl->extra1;
5805
Felix Jiad35a00b2017-01-26 16:59:17 +13005806 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005807 }
5808
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005809out:
5810 rtnl_unlock();
5811
5812 return err;
5813}
stephen hemmingerc92d5492013-12-17 22:37:14 -08005814
Andy Gospodarek35103d12015-08-13 10:39:01 -04005815static
5816int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
5817 int write,
5818 void __user *buffer,
5819 size_t *lenp,
5820 loff_t *ppos)
5821{
5822 int *valp = ctl->data;
5823 int val = *valp;
5824 loff_t pos = *ppos;
5825 struct ctl_table lctl;
5826 int ret;
5827
5828 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
5829 * we should not modify it until we get the rtnl lock.
5830 */
5831 lctl = *ctl;
5832 lctl.data = &val;
5833
5834 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5835
5836 if (write)
5837 ret = addrconf_fixup_linkdown(ctl, valp, val);
5838 if (ret)
5839 *ppos = pos;
5840 return ret;
5841}
5842
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07005843static int minus_one = -1;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07005844static const int one = 1;
5845static const int two_five_five = 255;
5846
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03005847static const struct ctl_table addrconf_sysctl[] = {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005848 {
5849 .procname = "forwarding",
5850 .data = &ipv6_devconf.forwarding,
5851 .maxlen = sizeof(int),
5852 .mode = 0644,
5853 .proc_handler = addrconf_sysctl_forward,
5854 },
5855 {
5856 .procname = "hop_limit",
5857 .data = &ipv6_devconf.hop_limit,
5858 .maxlen = sizeof(int),
5859 .mode = 0644,
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07005860 .proc_handler = proc_dointvec_minmax,
5861 .extra1 = (void *)&one,
5862 .extra2 = (void *)&two_five_five,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005863 },
5864 {
5865 .procname = "mtu",
5866 .data = &ipv6_devconf.mtu6,
5867 .maxlen = sizeof(int),
5868 .mode = 0644,
5869 .proc_handler = addrconf_sysctl_mtu,
5870 },
5871 {
5872 .procname = "accept_ra",
5873 .data = &ipv6_devconf.accept_ra,
5874 .maxlen = sizeof(int),
5875 .mode = 0644,
5876 .proc_handler = proc_dointvec,
5877 },
5878 {
5879 .procname = "accept_redirects",
5880 .data = &ipv6_devconf.accept_redirects,
5881 .maxlen = sizeof(int),
5882 .mode = 0644,
5883 .proc_handler = proc_dointvec,
5884 },
5885 {
5886 .procname = "autoconf",
5887 .data = &ipv6_devconf.autoconf,
5888 .maxlen = sizeof(int),
5889 .mode = 0644,
5890 .proc_handler = proc_dointvec,
5891 },
5892 {
5893 .procname = "dad_transmits",
5894 .data = &ipv6_devconf.dad_transmits,
5895 .maxlen = sizeof(int),
5896 .mode = 0644,
5897 .proc_handler = proc_dointvec,
5898 },
5899 {
5900 .procname = "router_solicitations",
5901 .data = &ipv6_devconf.rtr_solicits,
5902 .maxlen = sizeof(int),
5903 .mode = 0644,
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07005904 .proc_handler = proc_dointvec_minmax,
5905 .extra1 = &minus_one,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005906 },
5907 {
5908 .procname = "router_solicitation_interval",
5909 .data = &ipv6_devconf.rtr_solicit_interval,
5910 .maxlen = sizeof(int),
5911 .mode = 0644,
5912 .proc_handler = proc_dointvec_jiffies,
5913 },
5914 {
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005915 .procname = "router_solicitation_max_interval",
5916 .data = &ipv6_devconf.rtr_solicit_max_interval,
5917 .maxlen = sizeof(int),
5918 .mode = 0644,
5919 .proc_handler = proc_dointvec_jiffies,
5920 },
5921 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005922 .procname = "router_solicitation_delay",
5923 .data = &ipv6_devconf.rtr_solicit_delay,
5924 .maxlen = sizeof(int),
5925 .mode = 0644,
5926 .proc_handler = proc_dointvec_jiffies,
5927 },
5928 {
5929 .procname = "force_mld_version",
5930 .data = &ipv6_devconf.force_mld_version,
5931 .maxlen = sizeof(int),
5932 .mode = 0644,
5933 .proc_handler = proc_dointvec,
5934 },
5935 {
5936 .procname = "mldv1_unsolicited_report_interval",
5937 .data =
5938 &ipv6_devconf.mldv1_unsolicited_report_interval,
5939 .maxlen = sizeof(int),
5940 .mode = 0644,
5941 .proc_handler = proc_dointvec_ms_jiffies,
5942 },
5943 {
5944 .procname = "mldv2_unsolicited_report_interval",
5945 .data =
5946 &ipv6_devconf.mldv2_unsolicited_report_interval,
5947 .maxlen = sizeof(int),
5948 .mode = 0644,
5949 .proc_handler = proc_dointvec_ms_jiffies,
5950 },
5951 {
5952 .procname = "use_tempaddr",
5953 .data = &ipv6_devconf.use_tempaddr,
5954 .maxlen = sizeof(int),
5955 .mode = 0644,
5956 .proc_handler = proc_dointvec,
5957 },
5958 {
5959 .procname = "temp_valid_lft",
5960 .data = &ipv6_devconf.temp_valid_lft,
5961 .maxlen = sizeof(int),
5962 .mode = 0644,
5963 .proc_handler = proc_dointvec,
5964 },
5965 {
5966 .procname = "temp_prefered_lft",
5967 .data = &ipv6_devconf.temp_prefered_lft,
5968 .maxlen = sizeof(int),
5969 .mode = 0644,
5970 .proc_handler = proc_dointvec,
5971 },
5972 {
5973 .procname = "regen_max_retry",
5974 .data = &ipv6_devconf.regen_max_retry,
5975 .maxlen = sizeof(int),
5976 .mode = 0644,
5977 .proc_handler = proc_dointvec,
5978 },
5979 {
5980 .procname = "max_desync_factor",
5981 .data = &ipv6_devconf.max_desync_factor,
5982 .maxlen = sizeof(int),
5983 .mode = 0644,
5984 .proc_handler = proc_dointvec,
5985 },
5986 {
5987 .procname = "max_addresses",
5988 .data = &ipv6_devconf.max_addresses,
5989 .maxlen = sizeof(int),
5990 .mode = 0644,
5991 .proc_handler = proc_dointvec,
5992 },
5993 {
5994 .procname = "accept_ra_defrtr",
5995 .data = &ipv6_devconf.accept_ra_defrtr,
5996 .maxlen = sizeof(int),
5997 .mode = 0644,
5998 .proc_handler = proc_dointvec,
5999 },
6000 {
6001 .procname = "accept_ra_min_hop_limit",
6002 .data = &ipv6_devconf.accept_ra_min_hop_limit,
6003 .maxlen = sizeof(int),
6004 .mode = 0644,
6005 .proc_handler = proc_dointvec,
6006 },
6007 {
6008 .procname = "accept_ra_pinfo",
6009 .data = &ipv6_devconf.accept_ra_pinfo,
6010 .maxlen = sizeof(int),
6011 .mode = 0644,
6012 .proc_handler = proc_dointvec,
6013 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08006014#ifdef CONFIG_IPV6_ROUTER_PREF
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006015 {
6016 .procname = "accept_ra_rtr_pref",
6017 .data = &ipv6_devconf.accept_ra_rtr_pref,
6018 .maxlen = sizeof(int),
6019 .mode = 0644,
6020 .proc_handler = proc_dointvec,
6021 },
6022 {
6023 .procname = "router_probe_interval",
6024 .data = &ipv6_devconf.rtr_probe_interval,
6025 .maxlen = sizeof(int),
6026 .mode = 0644,
6027 .proc_handler = proc_dointvec_jiffies,
6028 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08006029#ifdef CONFIG_IPV6_ROUTE_INFO
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006030 {
6031 .procname = "accept_ra_rt_info_max_plen",
6032 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
6033 .maxlen = sizeof(int),
6034 .mode = 0644,
6035 .proc_handler = proc_dointvec,
6036 },
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08006037#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08006038#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006039 {
6040 .procname = "proxy_ndp",
6041 .data = &ipv6_devconf.proxy_ndp,
6042 .maxlen = sizeof(int),
6043 .mode = 0644,
6044 .proc_handler = addrconf_sysctl_proxy_ndp,
6045 },
6046 {
6047 .procname = "accept_source_route",
6048 .data = &ipv6_devconf.accept_source_route,
6049 .maxlen = sizeof(int),
6050 .mode = 0644,
6051 .proc_handler = proc_dointvec,
6052 },
Neil Horman95c385b2007-04-25 17:08:10 -07006053#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006054 {
6055 .procname = "optimistic_dad",
6056 .data = &ipv6_devconf.optimistic_dad,
6057 .maxlen = sizeof(int),
6058 .mode = 0644,
6059 .proc_handler = proc_dointvec,
6060 },
6061 {
6062 .procname = "use_optimistic",
6063 .data = &ipv6_devconf.use_optimistic,
6064 .maxlen = sizeof(int),
6065 .mode = 0644,
6066 .proc_handler = proc_dointvec,
6067 },
Neil Horman95c385b2007-04-25 17:08:10 -07006068#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006069#ifdef CONFIG_IPV6_MROUTE
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006070 {
6071 .procname = "mc_forwarding",
6072 .data = &ipv6_devconf.mc_forwarding,
6073 .maxlen = sizeof(int),
6074 .mode = 0444,
6075 .proc_handler = proc_dointvec,
6076 },
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006077#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006078 {
6079 .procname = "disable_ipv6",
6080 .data = &ipv6_devconf.disable_ipv6,
6081 .maxlen = sizeof(int),
6082 .mode = 0644,
6083 .proc_handler = addrconf_sysctl_disable,
6084 },
6085 {
6086 .procname = "accept_dad",
6087 .data = &ipv6_devconf.accept_dad,
6088 .maxlen = sizeof(int),
6089 .mode = 0644,
6090 .proc_handler = proc_dointvec,
6091 },
6092 {
6093 .procname = "force_tllao",
6094 .data = &ipv6_devconf.force_tllao,
6095 .maxlen = sizeof(int),
6096 .mode = 0644,
6097 .proc_handler = proc_dointvec
6098 },
6099 {
6100 .procname = "ndisc_notify",
6101 .data = &ipv6_devconf.ndisc_notify,
6102 .maxlen = sizeof(int),
6103 .mode = 0644,
6104 .proc_handler = proc_dointvec
6105 },
6106 {
6107 .procname = "suppress_frag_ndisc",
6108 .data = &ipv6_devconf.suppress_frag_ndisc,
6109 .maxlen = sizeof(int),
6110 .mode = 0644,
6111 .proc_handler = proc_dointvec
6112 },
6113 {
6114 .procname = "accept_ra_from_local",
6115 .data = &ipv6_devconf.accept_ra_from_local,
6116 .maxlen = sizeof(int),
6117 .mode = 0644,
6118 .proc_handler = proc_dointvec,
6119 },
6120 {
6121 .procname = "accept_ra_mtu",
6122 .data = &ipv6_devconf.accept_ra_mtu,
6123 .maxlen = sizeof(int),
6124 .mode = 0644,
6125 .proc_handler = proc_dointvec,
6126 },
6127 {
6128 .procname = "stable_secret",
6129 .data = &ipv6_devconf.stable_secret,
6130 .maxlen = IPV6_MAX_STRLEN,
6131 .mode = 0600,
6132 .proc_handler = addrconf_sysctl_stable_secret,
6133 },
6134 {
6135 .procname = "use_oif_addrs_only",
6136 .data = &ipv6_devconf.use_oif_addrs_only,
6137 .maxlen = sizeof(int),
6138 .mode = 0644,
6139 .proc_handler = proc_dointvec,
6140 },
6141 {
6142 .procname = "ignore_routes_with_linkdown",
6143 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6144 .maxlen = sizeof(int),
6145 .mode = 0644,
6146 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6147 },
6148 {
6149 .procname = "drop_unicast_in_l2_multicast",
6150 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6151 .maxlen = sizeof(int),
6152 .mode = 0644,
6153 .proc_handler = proc_dointvec,
6154 },
6155 {
6156 .procname = "drop_unsolicited_na",
6157 .data = &ipv6_devconf.drop_unsolicited_na,
6158 .maxlen = sizeof(int),
6159 .mode = 0644,
6160 .proc_handler = proc_dointvec,
6161 },
6162 {
6163 .procname = "keep_addr_on_down",
6164 .data = &ipv6_devconf.keep_addr_on_down,
6165 .maxlen = sizeof(int),
6166 .mode = 0644,
6167 .proc_handler = proc_dointvec,
David Ahernf1705ec2016-02-24 09:25:37 -08006168
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006169 },
6170 {
David Lebrun1ababeb2016-11-08 14:57:39 +01006171 .procname = "seg6_enabled",
6172 .data = &ipv6_devconf.seg6_enabled,
6173 .maxlen = sizeof(int),
6174 .mode = 0644,
6175 .proc_handler = proc_dointvec,
6176 },
David Lebrunbf355b82016-11-08 14:57:42 +01006177#ifdef CONFIG_IPV6_SEG6_HMAC
6178 {
6179 .procname = "seg6_require_hmac",
6180 .data = &ipv6_devconf.seg6_require_hmac,
6181 .maxlen = sizeof(int),
6182 .mode = 0644,
6183 .proc_handler = proc_dointvec,
6184 },
6185#endif
David Lebrun1ababeb2016-11-08 14:57:39 +01006186 {
Erik Nordmarkadc176c2016-12-02 14:00:08 -08006187 .procname = "enhanced_dad",
6188 .data = &ipv6_devconf.enhanced_dad,
6189 .maxlen = sizeof(int),
6190 .mode = 0644,
6191 .proc_handler = proc_dointvec,
6192 },
6193 {
Felix Jiad35a00b2017-01-26 16:59:17 +13006194 .procname = "addr_gen_mode",
6195 .data = &ipv6_devconf.addr_gen_mode,
6196 .maxlen = sizeof(int),
6197 .mode = 0644,
6198 .proc_handler = addrconf_sysctl_addr_gen_mode,
6199 },
6200 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006201 /* sentinel */
6202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203};
6204
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08006205static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006206 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207{
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006208 int i, ifindex;
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006209 struct ctl_table *table;
Eric W. Biederman6105e292012-04-19 13:41:24 +00006210 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11006211
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006212 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6213 if (!table)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006214 goto out;
6215
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006216 for (i = 0; table[i].data; i++) {
6217 table[i].data += (char *)p - (char *)&ipv6_devconf;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006218 /* If one of these is already set, then it is not safe to
6219 * overwrite either of them: this makes proc_dointvec_minmax
6220 * usable.
6221 */
6222 if (!table[i].extra1 && !table[i].extra2) {
6223 table[i].extra1 = idev; /* embedded; no ref */
6224 table[i].extra2 = net;
6225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227
Eric W. Biederman6105e292012-04-19 13:41:24 +00006228 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006230 p->sysctl_header = register_net_sysctl(net, path, table);
6231 if (!p->sysctl_header)
Eric W. Biederman6105e292012-04-19 13:41:24 +00006232 goto free;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006233
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006234 if (!strcmp(dev_name, "all"))
6235 ifindex = NETCONFA_IFINDEX_ALL;
6236 else if (!strcmp(dev_name, "default"))
6237 ifindex = NETCONFA_IFINDEX_DEFAULT;
6238 else
6239 ifindex = idev->dev->ifindex;
6240 inet6_netconf_notify_devconf(net, NETCONFA_ALL, ifindex, p);
Pavel Emelyanov95897312008-01-10 17:41:45 -08006241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006243free:
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006244 kfree(table);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006245out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08006246 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247}
6248
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006249static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
6250{
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006251 struct ctl_table *table;
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006252
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006253 if (!p->sysctl_header)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006254 return;
6255
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006256 table = p->sysctl_header->ctl_table_arg;
6257 unregister_net_sysctl_table(p->sysctl_header);
6258 p->sysctl_header = NULL;
6259 kfree(table);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006260}
6261
WANG Conga317a2f2014-07-25 15:25:09 -07006262static int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006263{
WANG Conga317a2f2014-07-25 15:25:09 -07006264 int err;
6265
6266 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6267 return -EINVAL;
6268
6269 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6270 &ndisc_ifinfo_sysctl_change);
6271 if (err)
6272 return err;
6273 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6274 idev, &idev->cnf);
6275 if (err)
6276 neigh_sysctl_unregister(idev->nd_parms);
6277
6278 return err;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006279}
6280
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006281static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006283 __addrconf_sysctl_unregister(&idev->cnf);
6284 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285}
6286
6287
6288#endif
6289
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006290static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006291{
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006292 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006293 struct ipv6_devconf *all, *dflt;
6294
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006295 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006296 if (!all)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006297 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006298
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006299 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006300 if (!dflt)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006301 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006302
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006303 /* these will be inherited by all namespaces */
6304 dflt->autoconf = ipv6_defaults.autoconf;
6305 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006306
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006307 dflt->stable_secret.initialized = false;
6308 all->stable_secret.initialized = false;
6309
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006310 net->ipv6.devconf_all = all;
6311 net->ipv6.devconf_dflt = dflt;
6312
6313#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006314 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006315 if (err < 0)
6316 goto err_reg_all;
6317
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006318 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006319 if (err < 0)
6320 goto err_reg_dflt;
6321#endif
6322 return 0;
6323
6324#ifdef CONFIG_SYSCTL
6325err_reg_dflt:
6326 __addrconf_sysctl_unregister(all);
6327err_reg_all:
6328 kfree(dflt);
6329#endif
6330err_alloc_dflt:
6331 kfree(all);
6332err_alloc_all:
6333 return err;
6334}
6335
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006336static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006337{
6338#ifdef CONFIG_SYSCTL
6339 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
6340 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
6341#endif
zhuyj73cf0e922014-11-26 10:25:58 +08006342 kfree(net->ipv6.devconf_dflt);
6343 kfree(net->ipv6.devconf_all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006344}
6345
6346static struct pernet_operations addrconf_ops = {
6347 .init = addrconf_init_net,
6348 .exit = addrconf_exit_net,
6349};
6350
Daniel Borkmann207895fd32015-01-29 12:15:03 +01006351static struct rtnl_af_ops inet6_ops __read_mostly = {
Thomas Grafb382b192010-11-16 04:33:57 +00006352 .family = AF_INET6,
6353 .fill_link_af = inet6_fill_link_af,
6354 .get_link_af_size = inet6_get_link_af_size,
Daniel Borkmann11b1f822015-02-05 14:39:11 +01006355 .validate_link_af = inet6_validate_link_af,
Daniel Borkmannf53adae2013-04-08 04:01:30 +00006356 .set_link_af = inet6_set_link_af,
Thomas Grafb382b192010-11-16 04:33:57 +00006357};
6358
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359/*
6360 * Init / cleanup code
6361 */
6362
6363int __init addrconf_init(void)
6364{
WANG Conga317a2f2014-07-25 15:25:09 -07006365 struct inet6_dev *idev;
stephen hemmingerc2e21292010-03-17 20:31:10 +00006366 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006367
Stephen Hemmingere21e8462010-03-20 16:09:01 -07006368 err = ipv6_addr_label_init();
6369 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00006370 pr_crit("%s: cannot initialize default policy table: %d\n",
6371 __func__, err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006372 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006375 err = register_pernet_subsys(&addrconf_ops);
6376 if (err < 0)
6377 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006378
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006379 addrconf_wq = create_workqueue("ipv6_addrconf");
6380 if (!addrconf_wq) {
6381 err = -ENOMEM;
6382 goto out_nowq;
6383 }
6384
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 /* The addrconf netdev notifier requires that loopback_dev
6386 * has it's ipv6 private information allocated and setup
6387 * before it can bring up and give link-local addresses
6388 * to other devices which are up.
6389 *
6390 * Unfortunately, loopback_dev is not necessarily the first
6391 * entry in the global dev_base list of net devices. In fact,
6392 * it is likely to be the very last entry on that list.
6393 * So this causes the notifier registry below to try and
6394 * give link-local addresses to all devices besides loopback_dev
6395 * first, then loopback_dev, which cases all the non-loopback_dev
6396 * devices to fail to get a link-local address.
6397 *
6398 * So, as a temporary fix, allocate the ipv6 structure for
6399 * loopback_dev first by hand.
6400 * Longer term, all of the dependencies ipv6 has upon the loopback
6401 * device and it being up should be removed.
6402 */
6403 rtnl_lock();
WANG Conga317a2f2014-07-25 15:25:09 -07006404 idev = ipv6_add_dev(init_net.loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405 rtnl_unlock();
WANG Conga317a2f2014-07-25 15:25:09 -07006406 if (IS_ERR(idev)) {
6407 err = PTR_ERR(idev);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006408 goto errlo;
WANG Conga317a2f2014-07-25 15:25:09 -07006409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410
stephen hemmingerc2e21292010-03-17 20:31:10 +00006411 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6412 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6413
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 register_netdevice_notifier(&ipv6_dev_notf);
6415
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006416 addrconf_verify();
Thomas Grafc127ea22007-03-22 11:58:32 -07006417
stephen hemminger3678a9d2013-12-30 10:41:32 -08006418 rtnl_af_register(&inet6_ops);
Thomas Grafb382b192010-11-16 04:33:57 +00006419
Greg Rosec7ac8672011-06-10 01:27:09 +00006420 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6421 NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006422 if (err < 0)
6423 goto errout;
6424
6425 /* Only the first call to __rtnl_register can fail */
Greg Rosec7ac8672011-06-10 01:27:09 +00006426 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
6427 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
6428 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6429 inet6_dump_ifaddr, NULL);
6430 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6431 inet6_dump_ifmcaddr, NULL);
6432 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6433 inet6_dump_ifacaddr, NULL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +00006434 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
Nicolas Dichtel7a674202013-03-05 23:42:06 +00006435 inet6_netconf_dump_devconf, NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006436
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006437 ipv6_addr_label_rtnl_register();
6438
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07006440errout:
Thomas Grafb382b192010-11-16 04:33:57 +00006441 rtnl_af_unregister(&inet6_ops);
Thomas Grafc127ea22007-03-22 11:58:32 -07006442 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006443errlo:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006444 destroy_workqueue(addrconf_wq);
6445out_nowq:
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006446 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006447out_addrlabel:
6448 ipv6_addr_label_cleanup();
6449out:
Thomas Grafc127ea22007-03-22 11:58:32 -07006450 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451}
6452
Daniel Lezcano09f77092007-12-13 05:34:58 -08006453void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006455 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456 int i;
6457
6458 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006459 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006460 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461
6462 rtnl_lock();
6463
Thomas Grafb382b192010-11-16 04:33:57 +00006464 __rtnl_af_unregister(&inet6_ops);
6465
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006466 /* clean dev list */
6467 for_each_netdev(&init_net, dev) {
6468 if (__in6_dev_get(dev) == NULL)
6469 continue;
6470 addrconf_ifdown(dev, 1);
6471 }
6472 addrconf_ifdown(init_net.loopback_dev, 2);
6473
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 * Check hash table.
6476 */
stephen hemminger5c578aed2010-03-17 20:31:11 +00006477 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00006478 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6479 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578aed2010-03-17 20:31:11 +00006480 spin_unlock_bh(&addrconf_hash_lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006481 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 rtnl_unlock();
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006483
6484 destroy_workqueue(addrconf_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485}