blob: e4570b5b180fca0546642c64f5e76bd47ee2cf99 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/socket.h>
47#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/net.h>
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +010049#include <linux/inet.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/in6.h>
51#include <linux/netdevice.h>
Thomas Graf18237302006-08-04 23:04:54 -070052#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/if_arp.h>
54#include <linux/if_arcnet.h>
55#include <linux/if_infiniband.h>
56#include <linux/route.h>
57#include <linux/inetdevice.h>
58#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090059#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifdef CONFIG_SYSCTL
61#include <linux/sysctl.h>
62#endif
Randy Dunlap4fc268d2006-01-11 12:17:47 -080063#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/delay.h>
65#include <linux/notifier.h>
Paulo Marques543537b2005-06-23 00:09:02 -070066#include <linux/string.h>
Eric Dumazetddbe5032012-07-18 08:11:12 +000067#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020069#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <net/sock.h>
71#include <net/snmp.h>
72
Alexander Aring5241c2d2015-12-14 20:55:22 +010073#include <net/6lowpan.h>
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +000074#include <net/firewire.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <net/ipv6.h>
76#include <net/protocol.h>
77#include <net/ndisc.h>
78#include <net/ip6_route.h>
79#include <net/addrconf.h>
80#include <net/tcp.h>
81#include <net/ip.h>
Thomas Graf5d620262006-08-15 00:35:02 -070082#include <net/netlink.h>
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -070083#include <net/pkt_sched.h>
David Ahernca254492015-10-12 11:47:10 -070084#include <net/l3mdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#include <linux/if_tunnel.h>
86#include <linux/rtnetlink.h>
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +000087#include <linux/netconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include <linux/random.h>
Stephen Hemmingere21e8462010-03-20 16:09:01 -070089#include <linux/uaccess.h>
Herbert Xu7f7d9a62007-04-24 21:54:09 -070090#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92#include <linux/proc_fs.h>
93#include <linux/seq_file.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040094#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/* Set to 3 to get tracing... */
97#define ACONF_DEBUG 2
98
99#if ACONF_DEBUG >= 3
dingtianhongba3542e2013-08-17 10:27:04 +0800100#define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#else
dingtianhongba3542e2013-08-17 10:27:04 +0800102#define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#endif
104
105#define INFINITY_LIFE_TIME 0xFFFFFFFF
Thomas Graf18a31e12010-11-17 04:12:02 +0000106
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100107#define IPV6_MAX_STRLEN \
108 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
109
Thomas Graf18a31e12010-11-17 04:12:02 +0000110static inline u32 cstamp_delta(unsigned long cstamp)
111{
112 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
113}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700115static inline s32 rfc3315_s14_backoff_init(s32 irt)
116{
117 /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
118 u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
119 do_div(tmp, 1000000);
120 return (s32)tmp;
121}
122
123static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
124{
125 /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
126 u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
127 do_div(tmp, 1000000);
128 if ((s32)tmp > mrt) {
129 /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
130 tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
131 do_div(tmp, 1000000);
132 }
133 return (s32)tmp;
134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#ifdef CONFIG_SYSCTL
WANG Conga317a2f2014-07-25 15:25:09 -0700137static int addrconf_sysctl_register(struct inet6_dev *idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800138static void addrconf_sysctl_unregister(struct inet6_dev *idev);
139#else
WANG Conga317a2f2014-07-25 15:25:09 -0700140static inline int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800141{
WANG Conga317a2f2014-07-25 15:25:09 -0700142 return 0;
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800143}
144
145static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
146{
147}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#endif
149
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200150static void ipv6_regen_rndid(struct inet6_dev *idev);
151static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900153static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static int ipv6_count_addresses(struct inet6_dev *idev);
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +0100155static int ipv6_generate_stable_address(struct in6_addr *addr,
156 u8 dad_count,
157 const struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159/*
160 * Configured unicast address hash table
161 */
stephen hemmingerc2e21292010-03-17 20:31:10 +0000162static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
stephen hemminger5c578ae2010-03-17 20:31:11 +0000163static DEFINE_SPINLOCK(addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100165static void addrconf_verify(void);
166static void addrconf_verify_rtnl(void);
167static void addrconf_verify_work(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100169static struct workqueue_struct *addrconf_wq;
170static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
173static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
174
Jiri Pirko93d9b7d2010-03-10 10:28:56 +0000175static void addrconf_type_change(struct net_device *dev,
176 unsigned long event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static int addrconf_ifdown(struct net_device *dev, int how);
178
Romain Kuntz21caa662013-01-09 21:06:03 +0000179static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
180 int plen,
181 const struct net_device *dev,
182 u32 flags, u32 noflags);
183
David S. Millercf22f9a2012-04-14 21:37:40 -0400184static void addrconf_dad_start(struct inet6_ifaddr *ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100185static void addrconf_dad_work(struct work_struct *w);
Paolo Abeni764d3be2016-11-22 16:57:40 +0100186static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900187static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static void addrconf_rs_timer(unsigned long data);
189static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
190static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
191
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900192static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct prefix_info *pinfo);
David S. Miller3e81c6d2010-03-20 16:18:00 -0700194static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
195 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Adrian Bunk888c8482008-07-22 14:21:58 -0700197static struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 .forwarding = 0,
199 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
200 .mtu6 = IPV6_MIN_MTU,
201 .accept_ra = 1,
202 .accept_redirects = 1,
203 .autoconf = 1,
204 .force_mld_version = 0,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200205 .mldv1_unsolicited_report_interval = 10 * HZ,
206 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 .dad_transmits = 1,
208 .rtr_solicits = MAX_RTR_SOLICITATIONS,
209 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700210 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Ian Morris67ba4152014-08-24 21:53:10 +0100212 .use_tempaddr = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 .temp_valid_lft = TEMP_VALID_LIFETIME,
214 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
215 .regen_max_retry = REGEN_MAX_RETRY,
216 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800218 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700219 .accept_ra_from_local = 0,
Hangbin Liu8013d1d2015-07-30 14:28:42 +0800220 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800221 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800222#ifdef CONFIG_IPV6_ROUTER_PREF
223 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800224 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800225#ifdef CONFIG_IPV6_ROUTE_INFO
226 .accept_ra_rt_info_max_plen = 0,
227#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800228#endif
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +0900229 .accept_ra_rt_table = 0,
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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242};
243
Brian Haleyab32ea52006-09-22 14:15:41 -0700244static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 .forwarding = 0,
246 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
247 .mtu6 = IPV6_MIN_MTU,
248 .accept_ra = 1,
249 .accept_redirects = 1,
250 .autoconf = 1,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200251 .force_mld_version = 0,
252 .mldv1_unsolicited_report_interval = 10 * HZ,
253 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 .dad_transmits = 1,
255 .rtr_solicits = MAX_RTR_SOLICITATIONS,
256 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700257 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 .use_tempaddr = 0,
260 .temp_valid_lft = TEMP_VALID_LIFETIME,
261 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
262 .regen_max_retry = REGEN_MAX_RETRY,
263 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800265 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700266 .accept_ra_from_local = 0,
Hangbin Liu8013d1d2015-07-30 14:28:42 +0800267 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800268 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800269#ifdef CONFIG_IPV6_ROUTER_PREF
270 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800271 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800272#ifdef CONFIG_IPV6_ROUTE_INFO
273 .accept_ra_rt_info_max_plen = 0,
274#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800275#endif
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +0900276 .accept_ra_rt_table = 0,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700277 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700278 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900279 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900280 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200281 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700282 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100283 .stable_secret = {
284 .initialized = false,
285 },
Erik Kline3985e8a2015-07-22 16:38:25 +0900286 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400287 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800288 .keep_addr_on_down = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289};
290
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700291/* Check if a valid qdisc is available */
David S. Miller05297942008-07-08 23:01:27 -0700292static inline bool addrconf_qdisc_ok(const struct net_device *dev)
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700293{
David S. Miller05297942008-07-08 23:01:27 -0700294 return !qdisc_tx_is_noop(dev);
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700295}
296
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200297static void addrconf_del_rs_timer(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200299 if (del_timer(&idev->rs_timer))
300 __in6_dev_put(idev);
301}
302
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100303static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200304{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100305 if (cancel_delayed_work(&ifp->dad_work))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 __in6_ifa_put(ifp);
307}
308
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200309static void addrconf_mod_rs_timer(struct inet6_dev *idev,
310 unsigned long when)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200312 if (!timer_pending(&idev->rs_timer))
313 in6_dev_hold(idev);
314 mod_timer(&idev->rs_timer, jiffies + when);
315}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100317static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
318 unsigned long delay)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200319{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100320 if (!delayed_work_pending(&ifp->dad_work))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200321 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100322 mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700325static int snmp6_alloc_dev(struct inet6_dev *idev)
326{
John Stultz827da442013-10-07 15:51:58 -0700327 int i;
328
WANG Cong698365f2014-05-05 15:55:55 -0700329 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
330 if (!idev->stats.ipv6)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700331 goto err_ip;
John Stultz827da442013-10-07 15:51:58 -0700332
333 for_each_possible_cpu(i) {
334 struct ipstats_mib *addrconf_stats;
WANG Cong698365f2014-05-05 15:55:55 -0700335 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
John Stultz827da442013-10-07 15:51:58 -0700336 u64_stats_init(&addrconf_stats->syncp);
John Stultz827da442013-10-07 15:51:58 -0700337 }
338
339
Eric Dumazetbe281e52011-05-19 01:14:23 +0000340 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
341 GFP_KERNEL);
342 if (!idev->stats.icmpv6dev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700343 goto err_icmp;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000344 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
345 GFP_KERNEL);
346 if (!idev->stats.icmpv6msgdev)
David L Stevens14878f72007-09-16 16:52:35 -0700347 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700348
349 return 0;
350
David L Stevens14878f72007-09-16 16:52:35 -0700351err_icmpmsg:
Eric Dumazetbe281e52011-05-19 01:14:23 +0000352 kfree(idev->stats.icmpv6dev);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700353err_icmp:
WANG Cong698365f2014-05-05 15:55:55 -0700354 free_percpu(idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700355err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700356 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700357}
358
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000359static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 struct inet6_dev *ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700362 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 ASSERT_RTNL();
365
366 if (dev->mtu < IPV6_MIN_MTU)
WANG Conga317a2f2014-07-25 15:25:09 -0700367 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900369 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100370 if (!ndev)
WANG Conga317a2f2014-07-25 15:25:09 -0700371 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Ingo Oeser322f74a2006-03-20 23:01:47 -0800373 rwlock_init(&ndev->lock);
374 ndev->dev = dev;
stephen hemminger502a2ff2010-03-17 20:31:13 +0000375 INIT_LIST_HEAD(&ndev->addr_list);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200376 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
377 (unsigned long)ndev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900378 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100379
380 if (ndev->cnf.stable_secret.initialized)
381 ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
382 else
383 ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64;
384
Ingo Oeser322f74a2006-03-20 23:01:47 -0800385 ndev->cnf.mtu6 = dev->mtu;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800386 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
Ian Morris63159f22015-03-29 14:00:04 +0100387 if (!ndev->nd_parms) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800388 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700389 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800390 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700391 if (ndev->cnf.forwarding)
392 dev_disable_lro(dev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800393 /* We refer to the device */
394 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Ingo Oeser322f74a2006-03-20 23:01:47 -0800396 if (snmp6_alloc_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800397 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000398 "%s: cannot allocate memory for statistics; dev=%s.\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800399 __func__, dev->name);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800400 neigh_parms_release(&nd_tbl, ndev->nd_parms);
Roy Li8603e332011-09-20 15:10:16 -0400401 dev_put(dev);
402 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700403 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Ingo Oeser322f74a2006-03-20 23:01:47 -0800406 if (snmp6_register_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800407 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000408 "%s: cannot create /proc/net/dev_snmp6/%s\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800409 __func__, dev->name);
WANG Conga317a2f2014-07-25 15:25:09 -0700410 goto err_release;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200413 /* One reference from device. */
Ingo Oeser322f74a2006-03-20 23:01:47 -0800414 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900416 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
417 ndev->cnf.accept_dad = -1;
418
Amerigo Wang07a93622012-10-29 16:23:10 +0000419#if IS_ENABLED(CONFIG_IPV6_SIT)
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700420 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
Joe Perchesf3213832012-05-15 14:11:53 +0000421 pr_info("%s: Disabled Multicast RS\n", dev->name);
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700422 ndev->cnf.rtr_solicits = 0;
423 }
424#endif
425
stephen hemminger372e6c82010-03-17 20:31:09 +0000426 INIT_LIST_HEAD(&ndev->tempaddr_list);
Jiri Bohac76506a92016-10-13 18:52:15 +0200427 ndev->desync_factor = U32_MAX;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800428 if ((dev->flags&IFF_LOOPBACK) ||
429 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700430 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700431 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700432 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800433 ndev->cnf.use_tempaddr = -1;
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200434 } else
435 ipv6_regen_rndid(ndev);
David S. Miller5d9efa72013-10-28 20:07:50 -0400436
Daniel Borkmann914faa12013-04-09 03:47:14 +0000437 ndev->token = in6addr_any;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800438
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700439 if (netif_running(dev) && addrconf_qdisc_ok(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700440 ndev->if_flags |= IF_READY;
441
Ingo Oeser322f74a2006-03-20 23:01:47 -0800442 ipv6_mc_init_dev(ndev);
443 ndev->tstamp = jiffies;
WANG Conga317a2f2014-07-25 15:25:09 -0700444 err = addrconf_sysctl_register(ndev);
445 if (err) {
446 ipv6_mc_destroy_dev(ndev);
Sabrina Dubroca2a189f92015-11-04 14:47:53 +0100447 snmp6_unregister_dev(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700448 goto err_release;
449 }
David L Stevens30c4cf52007-01-04 12:31:14 -0800450 /* protected by rtnl_lock */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000451 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800452
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000453 /* Join interface-local all-node multicast group */
454 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
455
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800456 /* Join all-node multicast group */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900457 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800458
Li Weid6ddef92012-03-05 14:45:17 +0000459 /* Join all-router multicast group if forwarding is set */
Li Wei8b2aaed2012-03-07 14:58:07 +0000460 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
Li Weid6ddef92012-03-05 14:45:17 +0000461 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700464
465err_release:
466 neigh_parms_release(&nd_tbl, ndev->nd_parms);
467 ndev->dead = 1;
468 in6_dev_finish_destroy(ndev);
469 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000472static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 struct inet6_dev *idev;
475
476 ASSERT_RTNL();
477
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700478 idev = __in6_dev_get(dev);
479 if (!idev) {
480 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -0700481 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return NULL;
483 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (dev->flags&IFF_UP)
486 ipv6_mc_up(idev);
487 return idev;
488}
489
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000490static int inet6_netconf_msgsize_devconf(int type)
491{
492 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
493 + nla_total_size(4); /* NETCONFA_IFINDEX */
Zhang Shengju136ba622016-03-10 08:55:50 +0000494 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000495
Zhang Shengju136ba622016-03-10 08:55:50 +0000496 if (type == NETCONFA_ALL)
497 all = true;
498
499 if (all || type == NETCONFA_FORWARDING)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000500 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500501#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000502 if (all || type == NETCONFA_MC_FORWARDING)
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000503 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500504#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000505 if (all || type == NETCONFA_PROXY_NEIGH)
stephen hemmingerc92d5492013-12-17 22:37:14 -0800506 size += nla_total_size(4);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000507
Zhang Shengju136ba622016-03-10 08:55:50 +0000508 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
Andy Gospodarek35103d12015-08-13 10:39:01 -0400509 size += nla_total_size(4);
510
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000511 return size;
512}
513
514static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
515 struct ipv6_devconf *devconf, u32 portid,
516 u32 seq, int event, unsigned int flags,
517 int type)
518{
519 struct nlmsghdr *nlh;
520 struct netconfmsg *ncm;
Zhang Shengju136ba622016-03-10 08:55:50 +0000521 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000522
523 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
524 flags);
Ian Morris63159f22015-03-29 14:00:04 +0100525 if (!nlh)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000526 return -EMSGSIZE;
527
Zhang Shengju136ba622016-03-10 08:55:50 +0000528 if (type == NETCONFA_ALL)
529 all = true;
530
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000531 ncm = nlmsg_data(nlh);
532 ncm->ncm_family = AF_INET6;
533
534 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
535 goto nla_put_failure;
536
Zhang Shengju136ba622016-03-10 08:55:50 +0000537 if ((all || type == NETCONFA_FORWARDING) &&
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000538 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
539 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500540#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000541 if ((all || type == NETCONFA_MC_FORWARDING) &&
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000542 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
543 devconf->mc_forwarding) < 0)
544 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500545#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000546 if ((all || type == NETCONFA_PROXY_NEIGH) &&
stephen hemmingerc92d5492013-12-17 22:37:14 -0800547 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
548 goto nla_put_failure;
549
Zhang Shengju136ba622016-03-10 08:55:50 +0000550 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
Andy Gospodarek35103d12015-08-13 10:39:01 -0400551 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
552 devconf->ignore_routes_with_linkdown) < 0)
553 goto nla_put_failure;
554
Johannes Berg053c0952015-01-16 22:09:00 +0100555 nlmsg_end(skb, nlh);
556 return 0;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000557
558nla_put_failure:
559 nlmsg_cancel(skb, nlh);
560 return -EMSGSIZE;
561}
562
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000563void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
564 struct ipv6_devconf *devconf)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000565{
566 struct sk_buff *skb;
567 int err = -ENOBUFS;
568
Eric Dumazet927265b2016-07-08 05:46:04 +0200569 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100570 if (!skb)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000571 goto errout;
572
573 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
574 RTM_NEWNETCONF, 0, type);
575 if (err < 0) {
576 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
577 WARN_ON(err == -EMSGSIZE);
578 kfree_skb(skb);
579 goto errout;
580 }
Eric Dumazet927265b2016-07-08 05:46:04 +0200581 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000582 return;
583errout:
Cong Dingbd779022012-12-18 12:08:56 +0000584 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000585}
586
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000587static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
588 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
589 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
stephen hemmingerc92d5492013-12-17 22:37:14 -0800590 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
Andy Gospodarek35103d12015-08-13 10:39:01 -0400591 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000592};
593
594static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
Thomas Graf661d2962013-03-21 07:45:29 +0000595 struct nlmsghdr *nlh)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000596{
597 struct net *net = sock_net(in_skb->sk);
598 struct nlattr *tb[NETCONFA_MAX+1];
599 struct netconfmsg *ncm;
600 struct sk_buff *skb;
601 struct ipv6_devconf *devconf;
602 struct inet6_dev *in6_dev;
603 struct net_device *dev;
604 int ifindex;
605 int err;
606
607 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
608 devconf_ipv6_policy);
609 if (err < 0)
610 goto errout;
611
Anton Protopopova97eb332016-02-16 21:43:16 -0500612 err = -EINVAL;
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000613 if (!tb[NETCONFA_IFINDEX])
614 goto errout;
615
616 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
617 switch (ifindex) {
618 case NETCONFA_IFINDEX_ALL:
619 devconf = net->ipv6.devconf_all;
620 break;
621 case NETCONFA_IFINDEX_DEFAULT:
622 devconf = net->ipv6.devconf_dflt;
623 break;
624 default:
625 dev = __dev_get_by_index(net, ifindex);
Ian Morris63159f22015-03-29 14:00:04 +0100626 if (!dev)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000627 goto errout;
628 in6_dev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +0100629 if (!in6_dev)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000630 goto errout;
631 devconf = &in6_dev->cnf;
632 break;
633 }
634
635 err = -ENOBUFS;
Zhang Shengju136ba622016-03-10 08:55:50 +0000636 skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +0100637 if (!skb)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000638 goto errout;
639
640 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
641 NETLINK_CB(in_skb).portid,
642 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
Zhang Shengju136ba622016-03-10 08:55:50 +0000643 NETCONFA_ALL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000644 if (err < 0) {
645 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
646 WARN_ON(err == -EMSGSIZE);
647 kfree_skb(skb);
648 goto errout;
649 }
650 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
651errout:
652 return err;
653}
654
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000655static int inet6_netconf_dump_devconf(struct sk_buff *skb,
656 struct netlink_callback *cb)
657{
658 struct net *net = sock_net(skb->sk);
659 int h, s_h;
660 int idx, s_idx;
661 struct net_device *dev;
662 struct inet6_dev *idev;
663 struct hlist_head *head;
664
665 s_h = cb->args[0];
666 s_idx = idx = cb->args[1];
667
668 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
669 idx = 0;
670 head = &net->dev_index_head[h];
671 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000672 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
673 net->dev_base_seq;
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000674 hlist_for_each_entry_rcu(dev, head, index_hlist) {
675 if (idx < s_idx)
676 goto cont;
677 idev = __in6_dev_get(dev);
678 if (!idev)
679 goto cont;
680
681 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
682 &idev->cnf,
683 NETLINK_CB(cb->skb).portid,
684 cb->nlh->nlmsg_seq,
685 RTM_NEWNETCONF,
686 NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000687 NETCONFA_ALL) < 0) {
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000688 rcu_read_unlock();
689 goto done;
690 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000691 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000692cont:
693 idx++;
694 }
695 rcu_read_unlock();
696 }
697 if (h == NETDEV_HASHENTRIES) {
698 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
699 net->ipv6.devconf_all,
700 NETLINK_CB(cb->skb).portid,
701 cb->nlh->nlmsg_seq,
702 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000703 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000704 goto done;
705 else
706 h++;
707 }
708 if (h == NETDEV_HASHENTRIES + 1) {
709 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
710 net->ipv6.devconf_dflt,
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 }
719done:
720 cb->args[0] = h;
721 cb->args[1] = idx;
722
723 return skb->len;
724}
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726#ifdef CONFIG_SYSCTL
727static void dev_forward_change(struct inet6_dev *idev)
728{
729 struct net_device *dev;
730 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 if (!idev)
733 return;
734 dev = idev->dev;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700735 if (idev->cnf.forwarding)
736 dev_disable_lro(dev);
Amerigo Wang1a940832012-10-28 17:43:53 +0000737 if (dev->flags & IFF_MULTICAST) {
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000738 if (idev->cnf.forwarding) {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900739 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000740 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
741 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
742 } else {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900743 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000744 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
745 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
stephen hemminger502a2ff2010-03-17 20:31:13 +0000748
749 list_for_each_entry(ifa, &idev->addr_list, if_list) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800750 if (ifa->flags&IFA_F_TENTATIVE)
751 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (idev->cnf.forwarding)
753 addrconf_join_anycast(ifa);
754 else
755 addrconf_leave_anycast(ifa);
756 }
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000757 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
758 dev->ifindex, &idev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
761
Pavel Emelyanove1869322008-01-10 17:43:50 -0800762static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 struct net_device *dev;
765 struct inet6_dev *idev;
766
Ben Hutchings4acd4942012-08-14 08:54:51 +0000767 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 idev = __in6_dev_get(dev);
769 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800770 int changed = (!idev->cnf.forwarding) ^ (!newf);
771 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (changed)
773 dev_forward_change(idev);
774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800777
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000778static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800779{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800780 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000781 int old;
782
783 if (!rtnl_trylock())
784 return restart_syscall();
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800785
786 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000787 old = *p;
788 *p = newf;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800789
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000790 if (p == &net->ipv6.devconf_dflt->forwarding) {
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000791 if ((!newf) ^ (!old))
792 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
793 NETCONFA_IFINDEX_DEFAULT,
794 net->ipv6.devconf_dflt);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000795 rtnl_unlock();
796 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +0000797 }
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000798
Pavel Emelyanove1869322008-01-10 17:43:50 -0800799 if (p == &net->ipv6.devconf_all->forwarding) {
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200800 int old_dflt = net->ipv6.devconf_dflt->forwarding;
801
Pavel Emelyanove1869322008-01-10 17:43:50 -0800802 net->ipv6.devconf_dflt->forwarding = newf;
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200803 if ((!newf) ^ (!old_dflt))
804 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
805 NETCONFA_IFINDEX_DEFAULT,
806 net->ipv6.devconf_dflt);
807
Pavel Emelyanove1869322008-01-10 17:43:50 -0800808 addrconf_forward_change(net, newf);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000809 if ((!newf) ^ (!old))
810 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
811 NETCONFA_IFINDEX_ALL,
812 net->ipv6.devconf_all);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000813 } else if ((!newf) ^ (!old))
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800814 dev_forward_change((struct inet6_dev *)table->extra1);
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700815 rtnl_unlock();
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800816
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000817 if (newf)
Daniel Lezcano7b4da532008-03-04 13:47:14 -0800818 rt6_purge_dflt_routers(net);
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000819 return 1;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800820}
Andy Gospodarek35103d12015-08-13 10:39:01 -0400821
822static void addrconf_linkdown_change(struct net *net, __s32 newf)
823{
824 struct net_device *dev;
825 struct inet6_dev *idev;
826
827 for_each_netdev(net, dev) {
828 idev = __in6_dev_get(dev);
829 if (idev) {
830 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
831
832 idev->cnf.ignore_routes_with_linkdown = newf;
833 if (changed)
834 inet6_netconf_notify_devconf(dev_net(dev),
835 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
836 dev->ifindex,
837 &idev->cnf);
838 }
839 }
840}
841
842static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
843{
844 struct net *net;
845 int old;
846
847 if (!rtnl_trylock())
848 return restart_syscall();
849
850 net = (struct net *)table->extra2;
851 old = *p;
852 *p = newf;
853
854 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
855 if ((!newf) ^ (!old))
856 inet6_netconf_notify_devconf(net,
857 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
858 NETCONFA_IFINDEX_DEFAULT,
859 net->ipv6.devconf_dflt);
860 rtnl_unlock();
861 return 0;
862 }
863
864 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
865 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
866 addrconf_linkdown_change(net, newf);
867 if ((!newf) ^ (!old))
868 inet6_netconf_notify_devconf(net,
869 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
870 NETCONFA_IFINDEX_ALL,
871 net->ipv6.devconf_all);
872 }
873 rtnl_unlock();
874
875 return 1;
876}
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878#endif
879
stephen hemminger5c578ae2010-03-17 20:31:11 +0000880/* Nobody refers to this ifaddr, destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
882{
stephen hemmingerc2e21292010-03-17 20:31:10 +0000883 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885#ifdef NET_REFCNT_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +0000886 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887#endif
888
889 in6_dev_put(ifp->idev);
890
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100891 if (cancel_delayed_work(&ifp->dad_work))
892 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
893 ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Herbert Xue9d3e082010-05-18 15:36:06 -0700895 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
Joe Perchesf3213832012-05-15 14:11:53 +0000896 pr_warn("Freeing alive inet6 address %p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return;
898 }
Amerigo Wang94e187c2012-10-29 00:13:19 +0000899 ip6_rt_put(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Lai Jiangshane5785982011-03-15 18:00:14 +0800901 kfree_rcu(ifp, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Brian Haleye55ffac2006-07-10 15:25:51 -0700904static void
905ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
906{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000907 struct list_head *p;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700908 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700909
910 /*
911 * Each device address list is sorted in order of scope -
912 * global before linklocal.
913 */
stephen hemminger502a2ff2010-03-17 20:31:13 +0000914 list_for_each(p, &idev->addr_list) {
915 struct inet6_ifaddr *ifa
916 = list_entry(p, struct inet6_ifaddr, if_list);
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700917 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700918 break;
919 }
920
David S. Millerb54c9b92010-03-25 21:25:30 -0700921 list_add_tail(&ifp->if_list, p);
Brian Haleye55ffac2006-07-10 15:25:51 -0700922}
923
Eric Dumazetddbe5032012-07-18 08:11:12 +0000924static u32 inet6_addr_hash(const struct in6_addr *addr)
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900925{
Eric Dumazetddbe5032012-07-18 08:11:12 +0000926 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900927}
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929/* On success it returns ifp with increased reference count */
930
931static struct inet6_ifaddr *
Jiri Pirko3f8f5292013-08-01 10:41:27 +0200932ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
933 const struct in6_addr *peer_addr, int pfxlen,
Jiri Benc8a226b22013-08-01 10:41:28 +0200934 int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 struct inet6_ifaddr *ifa = NULL;
937 struct rt6_info *rt;
stephen hemminger3a88a812010-03-17 20:31:12 +0000938 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 int err = 0;
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +0900940 int addr_type = ipv6_addr_type(addr);
941
942 if (addr_type == IPV6_ADDR_ANY ||
943 addr_type & IPV6_ADDR_MULTICAST ||
944 (!(idev->dev->flags & IFF_LOOPBACK) &&
945 addr_type & IPV6_ADDR_LOOPBACK))
946 return ERR_PTR(-EADDRNOTAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700948 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (idev->dead) {
950 err = -ENODEV; /*XXX*/
951 goto out2;
952 }
953
Brian Haley56d417b2009-06-01 03:07:33 -0700954 if (idev->cnf.disable_ipv6) {
Brian Haley9bdd8d42009-03-18 18:22:48 -0700955 err = -EACCES;
956 goto out2;
957 }
958
stephen hemminger5c578ae2010-03-17 20:31:11 +0000959 spin_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 /* Ignore adding duplicate addresses on an interface */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900962 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
dingtianhongba3542e2013-08-17 10:27:04 +0800963 ADBG("ipv6_add_addr: already assigned\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 err = -EEXIST;
965 goto out;
966 }
967
Ingo Oeser322f74a2006-03-20 23:01:47 -0800968 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Ian Morris63159f22015-03-29 14:00:04 +0100970 if (!ifa) {
dingtianhongba3542e2013-08-17 10:27:04 +0800971 ADBG("ipv6_add_addr: malloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 err = -ENOBUFS;
973 goto out;
974 }
975
David S. Miller8f031512011-12-06 16:48:14 -0500976 rt = addrconf_dst_alloc(idev, addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (IS_ERR(rt)) {
978 err = PTR_ERR(rt);
979 goto out;
980 }
981
Jiri Pirkobba24892013-12-07 19:26:57 +0100982 neigh_parms_data_state_setall(idev->nd_parms);
983
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000984 ifa->addr = *addr;
Jiri Pirko3f8f5292013-08-01 10:41:27 +0200985 if (peer_addr)
986 ifa->peer_addr = *peer_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 spin_lock_init(&ifa->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100989 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
stephen hemmingerc2e21292010-03-17 20:31:10 +0000990 INIT_HLIST_NODE(&ifa->addr_lst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 ifa->scope = scope;
992 ifa->prefix_len = pfxlen;
993 ifa->flags = flags | IFA_F_TENTATIVE;
Jiri Benc8a226b22013-08-01 10:41:28 +0200994 ifa->valid_lft = valid_lft;
995 ifa->prefered_lft = prefered_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 ifa->cstamp = ifa->tstamp = jiffies;
Daniel Borkmann617fe292013-04-09 03:47:16 +0000997 ifa->tokenized = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Keir Fraser57f5f542006-08-29 02:43:49 -0700999 ifa->rt = rt;
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 ifa->idev = idev;
1002 in6_dev_hold(idev);
1003 /* For caller */
1004 in6_ifa_hold(ifa);
1005
1006 /* Add to big hash table */
Eric Dumazetddbe5032012-07-18 08:11:12 +00001007 hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
stephen hemminger5c578ae2010-03-17 20:31:11 +00001009 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
stephen hemminger5c578ae2010-03-17 20:31:11 +00001010 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 write_lock(&idev->lock);
1013 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -07001014 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (ifa->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001017 list_add(&ifa->tmp_list, &idev->tempaddr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 in6_ifa_hold(ifa);
1019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 in6_ifa_hold(ifa);
1022 write_unlock(&idev->lock);
1023out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001024 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -07001026 if (likely(err == 0))
Cong Wangf88c91d2013-04-14 23:18:43 +08001027 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 else {
1029 kfree(ifa);
1030 ifa = ERR_PTR(err);
1031 }
1032
1033 return ifa;
1034out:
stephen hemminger5c578ae2010-03-17 20:31:11 +00001035 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 goto out2;
1037}
1038
Thomas Haller5b84efe2014-01-15 15:36:59 +01001039enum cleanup_prefix_rt_t {
1040 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1041 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1042 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1043};
1044
1045/*
1046 * Check, whether the prefix for ifp would still need a prefix route
1047 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1048 * constants.
1049 *
1050 * 1) we don't purge prefix if address was not permanent.
1051 * prefix is managed by its own lifetime.
1052 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1053 * 3) if there are no addresses, delete prefix.
1054 * 4) if there are still other permanent address(es),
1055 * corresponding prefix is still permanent.
1056 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1057 * don't purge the prefix, assume user space is managing it.
1058 * 6) otherwise, update prefix lifetime to the
1059 * longest valid lifetime among the corresponding
1060 * addresses on the device.
1061 * Note: subsequent RA will update lifetime.
1062 **/
1063static enum cleanup_prefix_rt_t
1064check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1065{
1066 struct inet6_ifaddr *ifa;
1067 struct inet6_dev *idev = ifp->idev;
1068 unsigned long lifetime;
1069 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1070
1071 *expires = jiffies;
1072
1073 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1074 if (ifa == ifp)
1075 continue;
1076 if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
1077 ifp->prefix_len))
1078 continue;
1079 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1080 return CLEANUP_PREFIX_RT_NOP;
1081
1082 action = CLEANUP_PREFIX_RT_EXPIRE;
1083
1084 spin_lock(&ifa->lock);
1085
1086 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1087 /*
1088 * Note: Because this address is
1089 * not permanent, lifetime <
1090 * LONG_MAX / HZ here.
1091 */
1092 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1093 *expires = ifa->tstamp + lifetime * HZ;
1094 spin_unlock(&ifa->lock);
1095 }
1096
1097 return action;
1098}
1099
1100static void
1101cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
1102{
1103 struct rt6_info *rt;
1104
1105 rt = addrconf_get_prefix_route(&ifp->addr,
1106 ifp->prefix_len,
1107 ifp->idev->dev,
1108 0, RTF_GATEWAY | RTF_DEFAULT);
1109 if (rt) {
1110 if (del_rt)
1111 ip6_del_rt(rt);
1112 else {
1113 if (!(rt->rt6i_flags & RTF_EXPIRES))
1114 rt6_set_expires(rt, expires);
1115 ip6_rt_put(rt);
1116 }
1117 }
1118}
1119
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121/* This function wants to get referenced ifp and releases it before return */
1122
1123static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1124{
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001125 int state;
Thomas Haller5b84efe2014-01-15 15:36:59 +01001126 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1127 unsigned long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001129 ASSERT_RTNL();
1130
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001131 spin_lock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001132 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -07001133 ifp->state = INET6_IFADDR_STATE_DEAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001134 spin_unlock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001135
1136 if (state == INET6_IFADDR_STATE_DEAD)
1137 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
stephen hemminger5c578ae2010-03-17 20:31:11 +00001139 spin_lock_bh(&addrconf_hash_lock);
1140 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578ae2010-03-17 20:31:11 +00001141 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Thomas Haller5b84efe2014-01-15 15:36:59 +01001143 write_lock_bh(&ifp->idev->lock);
David S. Miller5d9efa72013-10-28 20:07:50 -04001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001146 list_del(&ifp->tmp_list);
1147 if (ifp->ifpub) {
1148 in6_ifa_put(ifp->ifpub);
1149 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
stephen hemminger372e6c82010-03-17 20:31:09 +00001151 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Thomas Haller5b84efe2014-01-15 15:36:59 +01001154 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1155 action = check_cleanup_prefix_route(ifp, &expires);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001156
Thomas Haller5b84efe2014-01-15 15:36:59 +01001157 list_del_init(&ifp->if_list);
1158 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Thomas Haller5b84efe2014-01-15 15:36:59 +01001160 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001162 addrconf_del_dad_work(ifp);
Andrey Vaginb2238562008-07-08 15:13:31 -07001163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 ipv6_ifa_notify(RTM_DELADDR, ifp);
1165
Cong Wangf88c91d2013-04-14 23:18:43 +08001166 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Thomas Haller5b84efe2014-01-15 15:36:59 +01001168 if (action != CLEANUP_PREFIX_RT_NOP) {
1169 cleanup_prefix_route(ifp, expires,
1170 action == CLEANUP_PREFIX_RT_DEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172
Daniel Walterc3968a82011-04-13 21:10:57 +00001173 /* clean up prefsrc entries */
1174 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001175out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 in6_ifa_put(ifp);
1177}
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1180{
1181 struct inet6_dev *idev = ifp->idev;
1182 struct in6_addr addr, *tmpaddr;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001183 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001184 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 int tmp_plen;
1186 int ret = 0;
Neil Horman95c385b2007-04-25 17:08:10 -07001187 u32 addr_flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001188 unsigned long now = jiffies;
Jiri Bohac76506a92016-10-13 18:52:15 +02001189 long max_desync_factor;
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001190 s32 cnf_temp_preferred_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Jiri Pirko53bd6742013-12-06 09:45:22 +01001192 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (ift) {
1194 spin_lock_bh(&ift->lock);
1195 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1196 spin_unlock_bh(&ift->lock);
1197 tmpaddr = &addr;
1198 } else {
1199 tmpaddr = NULL;
1200 }
1201retry:
1202 in6_dev_hold(idev);
1203 if (idev->cnf.use_tempaddr <= 0) {
Jiri Pirko53bd6742013-12-06 09:45:22 +01001204 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001205 pr_info("%s: use_tempaddr is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 in6_dev_put(idev);
1207 ret = -1;
1208 goto out;
1209 }
1210 spin_lock_bh(&ifp->lock);
1211 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1212 idev->cnf.use_tempaddr = -1; /*XXX*/
1213 spin_unlock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01001214 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001215 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1216 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 in6_dev_put(idev);
1218 ret = -1;
1219 goto out;
1220 }
1221 in6_ifa_hold(ifp);
1222 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
Jiri Bohac9d6280d2016-10-13 18:50:02 +02001223 ipv6_try_regen_rndid(idev, tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001225 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001227 regen_advance = idev->cnf.regen_max_retry *
Ian Morris67ba4152014-08-24 21:53:10 +01001228 idev->cnf.dad_transmits *
1229 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
Jiri Bohac76506a92016-10-13 18:52:15 +02001230
1231 /* recalculate max_desync_factor each time and update
1232 * idev->desync_factor if it's larger
1233 */
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001234 cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
Jiri Bohac76506a92016-10-13 18:52:15 +02001235 max_desync_factor = min_t(__u32,
1236 idev->cnf.max_desync_factor,
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001237 cnf_temp_preferred_lft - regen_advance);
Jiri Bohac76506a92016-10-13 18:52:15 +02001238
1239 if (unlikely(idev->desync_factor > max_desync_factor)) {
1240 if (max_desync_factor > 0) {
1241 get_random_bytes(&idev->desync_factor,
1242 sizeof(idev->desync_factor));
1243 idev->desync_factor %= max_desync_factor;
1244 } else {
1245 idev->desync_factor = 0;
1246 }
1247 }
1248
1249 tmp_valid_lft = min_t(__u32,
1250 ifp->valid_lft,
1251 idev->cnf.temp_valid_lft + age);
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001252 tmp_prefered_lft = cnf_temp_preferred_lft + age -
Jiri Bohac76506a92016-10-13 18:52:15 +02001253 idev->desync_factor;
Jiri Bohac76506a92016-10-13 18:52:15 +02001254 tmp_prefered_lft = min_t(__u32, ifp->prefered_lft, tmp_prefered_lft);
1255 tmp_plen = ifp->prefix_len;
1256 tmp_tstamp = ifp->tstamp;
1257 spin_unlock_bh(&ifp->lock);
1258
Jiri Pirko53bd6742013-12-06 09:45:22 +01001259 write_unlock_bh(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -07001260
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001261 /* A temporary address is created only if this calculated Preferred
1262 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1263 * an implementation must not create a temporary address with a zero
1264 * Preferred Lifetime.
Heiner Kallweitecab6702014-03-12 22:13:19 +01001265 * Use age calculation as in addrconf_verify to avoid unnecessary
1266 * temporary addresses being generated.
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001267 */
Heiner Kallweitecab6702014-03-12 22:13:19 +01001268 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1269 if (tmp_prefered_lft <= regen_advance + age) {
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001270 in6_ifa_put(ifp);
1271 in6_dev_put(idev);
1272 ret = -1;
1273 goto out;
1274 }
1275
Neil Horman95c385b2007-04-25 17:08:10 -07001276 addr_flags = IFA_F_TEMPORARY;
1277 /* set in addrconf_prefix_rcv() */
1278 if (ifp->flags & IFA_F_OPTIMISTIC)
1279 addr_flags |= IFA_F_OPTIMISTIC;
1280
Hannes Frederic Sowa4b08a8f2013-08-16 13:02:27 +02001281 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1282 ipv6_addr_scope(&addr), addr_flags,
1283 tmp_valid_lft, tmp_prefered_lft);
1284 if (IS_ERR(ift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 in6_ifa_put(ifp);
1286 in6_dev_put(idev);
Joe Perchesf3213832012-05-15 14:11:53 +00001287 pr_info("%s: retry temporary address regeneration\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 tmpaddr = &addr;
Jiri Pirko53bd6742013-12-06 09:45:22 +01001289 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 goto retry;
1291 }
1292
1293 spin_lock_bh(&ift->lock);
1294 ift->ifpub = ifp;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001295 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 ift->tstamp = tmp_tstamp;
1297 spin_unlock_bh(&ift->lock);
1298
David S. Millercf22f9a2012-04-14 21:37:40 -04001299 addrconf_dad_start(ift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 in6_ifa_put(ift);
1301 in6_dev_put(idev);
1302out:
1303 return ret;
1304}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001307 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001309enum {
1310 IPV6_SADDR_RULE_INIT = 0,
1311 IPV6_SADDR_RULE_LOCAL,
1312 IPV6_SADDR_RULE_SCOPE,
1313 IPV6_SADDR_RULE_PREFERRED,
1314#ifdef CONFIG_IPV6_MIP6
1315 IPV6_SADDR_RULE_HOA,
1316#endif
1317 IPV6_SADDR_RULE_OIF,
1318 IPV6_SADDR_RULE_LABEL,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001319 IPV6_SADDR_RULE_PRIVACY,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001320 IPV6_SADDR_RULE_ORCHID,
1321 IPV6_SADDR_RULE_PREFIX,
Erik Kline7fd25612014-10-28 18:11:14 +09001322#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1323 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1324#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001325 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001326};
1327
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001328struct ipv6_saddr_score {
1329 int rule;
1330 int addr_type;
1331 struct inet6_ifaddr *ifa;
1332 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1333 int scopedist;
1334 int matchlen;
1335};
1336
1337struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001338 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001339 int ifindex;
1340 int scope;
1341 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001342 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001343};
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001344
Dave Jonesb6f99a22007-03-22 12:27:49 -07001345static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Ulrich Weber45bb0062010-02-25 23:28:58 +00001347 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001348 return 1;
1349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
Erik Kline7fd25612014-10-28 18:11:14 +09001352static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
1353{
1354#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1355 return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
1356#else
1357 return false;
1358#endif
1359}
1360
Benjamin Thery3de23252008-05-28 14:51:24 +02001361static int ipv6_get_saddr_eval(struct net *net,
1362 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001363 struct ipv6_saddr_dst *dst,
1364 int i)
1365{
1366 int ret;
1367
1368 if (i <= score->rule) {
1369 switch (i) {
1370 case IPV6_SADDR_RULE_SCOPE:
1371 ret = score->scopedist;
1372 break;
1373 case IPV6_SADDR_RULE_PREFIX:
1374 ret = score->matchlen;
1375 break;
1376 default:
1377 ret = !!test_bit(i, score->scorebits);
1378 }
1379 goto out;
1380 }
1381
1382 switch (i) {
1383 case IPV6_SADDR_RULE_INIT:
1384 /* Rule 0: remember if hiscore is not ready yet */
1385 ret = !!score->ifa;
1386 break;
1387 case IPV6_SADDR_RULE_LOCAL:
1388 /* Rule 1: Prefer same address */
1389 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1390 break;
1391 case IPV6_SADDR_RULE_SCOPE:
1392 /* Rule 2: Prefer appropriate scope
1393 *
1394 * ret
1395 * ^
1396 * -1 | d 15
1397 * ---+--+-+---> scope
1398 * |
1399 * | d is scope of the destination.
1400 * B-d | \
1401 * | \ <- smaller scope is better if
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08001402 * B-15 | \ if scope is enough for destination.
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001403 * | ret = B - scope (-1 <= scope >= d <= 15).
1404 * d-C-1 | /
1405 * |/ <- greater is better
1406 * -C / if scope is not enough for destination.
1407 * /| ret = scope - C (-1 <= d < scope <= 15).
1408 *
1409 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1410 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1411 * Assume B = 0 and we get C > 29.
1412 */
1413 ret = __ipv6_addr_src_scope(score->addr_type);
1414 if (ret >= dst->scope)
1415 ret = -ret;
1416 else
1417 ret -= 128; /* 30 is enough */
1418 score->scopedist = ret;
1419 break;
1420 case IPV6_SADDR_RULE_PREFERRED:
Erik Kline7fd25612014-10-28 18:11:14 +09001421 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001422 /* Rule 3: Avoid deprecated and optimistic addresses */
Erik Kline7fd25612014-10-28 18:11:14 +09001423 u8 avoid = IFA_F_DEPRECATED;
1424
1425 if (!ipv6_use_optimistic_addr(score->ifa->idev))
1426 avoid |= IFA_F_OPTIMISTIC;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001427 ret = ipv6_saddr_preferred(score->addr_type) ||
Erik Kline7fd25612014-10-28 18:11:14 +09001428 !(score->ifa->flags & avoid);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001429 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001430 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001431#ifdef CONFIG_IPV6_MIP6
1432 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001433 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001434 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001435 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1436 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001437 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001438 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001439#endif
1440 case IPV6_SADDR_RULE_OIF:
1441 /* Rule 5: Prefer outgoing interface */
1442 ret = (!dst->ifindex ||
1443 dst->ifindex == score->ifa->idev->dev->ifindex);
1444 break;
1445 case IPV6_SADDR_RULE_LABEL:
1446 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001447 ret = ipv6_addr_label(net,
1448 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001449 score->ifa->idev->dev->ifindex) == dst->label;
1450 break;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001451 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001452 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001453 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001454 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001455 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001456 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1457 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1458 score->ifa->idev->cnf.use_tempaddr >= 2;
1459 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001460 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001461 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001462 case IPV6_SADDR_RULE_ORCHID:
1463 /* Rule 8-: Prefer ORCHID vs ORCHID or
1464 * non-ORCHID vs non-ORCHID
1465 */
1466 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1467 ipv6_addr_orchid(dst->addr));
1468 break;
1469 case IPV6_SADDR_RULE_PREFIX:
1470 /* Rule 8: Use longest matching prefix */
YOSHIFUJI Hideaki / 吉藤英明91b4b042012-09-10 18:41:07 +00001471 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1472 if (ret > score->ifa->prefix_len)
1473 ret = score->ifa->prefix_len;
1474 score->matchlen = ret;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001475 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001476#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1477 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1478 /* Optimistic addresses still have lower precedence than other
1479 * preferred addresses.
1480 */
1481 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1482 break;
1483#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001484 default:
1485 ret = 0;
1486 }
1487
1488 if (ret)
1489 __set_bit(i, score->scorebits);
1490 score->rule = i;
1491out:
1492 return ret;
1493}
1494
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001495static int __ipv6_dev_get_saddr(struct net *net,
1496 struct ipv6_saddr_dst *dst,
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001497 struct inet6_dev *idev,
1498 struct ipv6_saddr_score *scores,
1499 int hiscore_idx)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001500{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001501 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001502
1503 read_lock_bh(&idev->lock);
1504 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1505 int i;
1506
1507 /*
1508 * - Tentative Address (RFC2462 section 5.4)
1509 * - A tentative address is not considered
1510 * "assigned to an interface" in the traditional
1511 * sense, unless it is also flagged as optimistic.
1512 * - Candidate Source Address (section 4)
1513 * - In any case, anycast addresses, multicast
1514 * addresses, and the unspecified address MUST
1515 * NOT be included in a candidate set.
1516 */
1517 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1518 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1519 continue;
1520
1521 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1522
1523 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1524 score->addr_type & IPV6_ADDR_MULTICAST)) {
1525 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1526 idev->dev->name);
1527 continue;
1528 }
1529
1530 score->rule = -1;
1531 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1532
1533 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1534 int minihiscore, miniscore;
1535
1536 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1537 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1538
1539 if (minihiscore > miniscore) {
1540 if (i == IPV6_SADDR_RULE_SCOPE &&
1541 score->scopedist > 0) {
1542 /*
1543 * special case:
1544 * each remaining entry
1545 * has too small (not enough)
1546 * scope, because ifa entries
1547 * are sorted by their scope
1548 * values.
1549 */
1550 goto out;
1551 }
1552 break;
1553 } else if (minihiscore < miniscore) {
1554 if (hiscore->ifa)
1555 in6_ifa_put(hiscore->ifa);
1556
1557 in6_ifa_hold(score->ifa);
1558
1559 swap(hiscore, score);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001560 hiscore_idx = 1 - hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001561
1562 /* restore our iterator */
1563 score->ifa = hiscore->ifa;
1564
1565 break;
1566 }
1567 }
1568 }
1569out:
1570 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001571 return hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001572}
1573
David Ahernafbac6012016-06-16 16:24:26 -07001574static int ipv6_get_saddr_master(struct net *net,
1575 const struct net_device *dst_dev,
1576 const struct net_device *master,
1577 struct ipv6_saddr_dst *dst,
1578 struct ipv6_saddr_score *scores,
1579 int hiscore_idx)
1580{
1581 struct inet6_dev *idev;
1582
1583 idev = __in6_dev_get(dst_dev);
1584 if (idev)
1585 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1586 scores, hiscore_idx);
1587
1588 idev = __in6_dev_get(master);
1589 if (idev)
1590 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1591 scores, hiscore_idx);
1592
1593 return hiscore_idx;
1594}
1595
Patrick McHardyb3f644f2012-08-26 19:14:14 +02001596int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001597 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001598 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001600 struct ipv6_saddr_score scores[2], *hiscore;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001601 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001602 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001603 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001604 int dst_type;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001605 bool use_oif_addr = false;
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001606 int hiscore_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001608 dst_type = __ipv6_addr_type(daddr);
1609 dst.addr = daddr;
1610 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1611 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001612 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001613 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001614
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001615 scores[hiscore_idx].rule = -1;
1616 scores[hiscore_idx].ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001618 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001619
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001620 /* Candidate Source Address (section 4)
1621 * - multicast and link-local destination address,
1622 * the set of candidate source address MUST only
1623 * include addresses assigned to interfaces
1624 * belonging to the same link as the outgoing
1625 * interface.
1626 * (- For site-local destination addresses, the
1627 * set of candidate source addresses MUST only
1628 * include addresses assigned to interfaces
1629 * belonging to the same site as the outgoing
1630 * interface.)
Erik Kline3985e8a2015-07-22 16:38:25 +09001631 * - "It is RECOMMENDED that the candidate source addresses
1632 * be the set of unicast addresses assigned to the
1633 * interface that will be used to send to the destination
1634 * (the 'outgoing' interface)." (RFC 6724)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001635 */
1636 if (dst_dev) {
Erik Kline3985e8a2015-07-22 16:38:25 +09001637 idev = __in6_dev_get(dst_dev);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001638 if ((dst_type & IPV6_ADDR_MULTICAST) ||
Erik Kline3985e8a2015-07-22 16:38:25 +09001639 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1640 (idev && idev->cnf.use_oif_addrs_only)) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001641 use_oif_addr = true;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001642 }
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001643 }
1644
1645 if (use_oif_addr) {
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001646 if (idev)
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001647 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001648 } else {
David Ahernafbac6012016-06-16 16:24:26 -07001649 const struct net_device *master;
1650 int master_idx = 0;
1651
1652 /* if dst_dev exists and is enslaved to an L3 device, then
1653 * prefer addresses from dst_dev and then the master over
1654 * any other enslaved devices in the L3 domain.
1655 */
1656 master = l3mdev_master_dev_rcu(dst_dev);
1657 if (master) {
1658 master_idx = master->ifindex;
1659
1660 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1661 master, &dst,
1662 scores, hiscore_idx);
1663
1664 if (scores[hiscore_idx].ifa)
1665 goto out;
1666 }
1667
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001668 for_each_netdev_rcu(net, dev) {
David Ahernafbac6012016-06-16 16:24:26 -07001669 /* only consider addresses on devices in the
1670 * same L3 domain
1671 */
1672 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1673 continue;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001674 idev = __in6_dev_get(dev);
1675 if (!idev)
1676 continue;
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001677 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001678 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001679 }
David Ahernafbac6012016-06-16 16:24:26 -07001680
1681out:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001682 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001684 hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001685 if (!hiscore->ifa)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001686 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001687
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001688 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001689 in6_ifa_put(hiscore->ifa);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001690 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001692EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Amerigo Wang89657792013-06-29 21:30:49 +08001694int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001695 u32 banned_flags)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001696{
1697 struct inet6_ifaddr *ifp;
1698 int err = -EADDRNOTAVAIL;
1699
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01001700 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1701 if (ifp->scope > IFA_LINK)
1702 break;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001703 if (ifp->scope == IFA_LINK &&
1704 !(ifp->flags & banned_flags)) {
1705 *addr = ifp->addr;
1706 err = 0;
1707 break;
1708 }
1709 }
1710 return err;
1711}
1712
Neil Horman95c385b2007-04-25 17:08:10 -07001713int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001714 u32 banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
1716 struct inet6_dev *idev;
1717 int err = -EADDRNOTAVAIL;
1718
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001719 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001720 idev = __in6_dev_get(dev);
1721 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 read_lock_bh(&idev->lock);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001723 err = __ipv6_get_lladdr(idev, addr, banned_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 read_unlock_bh(&idev->lock);
1725 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001726 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return err;
1728}
1729
1730static int ipv6_count_addresses(struct inet6_dev *idev)
1731{
1732 int cnt = 0;
1733 struct inet6_ifaddr *ifp;
1734
1735 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001736 list_for_each_entry(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 cnt++;
1738 read_unlock_bh(&idev->lock);
1739 return cnt;
1740}
1741
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001742int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Florian Westphal2a7851b2013-05-17 03:56:10 +00001743 const struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Erik Klinec58da4c2015-02-04 20:01:23 +09001745 return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1746}
1747EXPORT_SYMBOL(ipv6_chk_addr);
1748
1749int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1750 const struct net_device *dev, int strict,
1751 u32 banned_flags)
1752{
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001753 struct inet6_ifaddr *ifp;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001754 unsigned int hash = inet6_addr_hash(addr);
Erik Klinec58da4c2015-02-04 20:01:23 +09001755 u32 ifp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
stephen hemminger5c578ae2010-03-17 20:31:11 +00001757 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001758 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001759 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001760 continue;
Erik Klinec58da4c2015-02-04 20:01:23 +09001761 /* Decouple optimistic from tentative for evaluation here.
1762 * Ban optimistic addresses explicitly, when required.
1763 */
1764 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1765 ? (ifp->flags&~IFA_F_TENTATIVE)
1766 : ifp->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (ipv6_addr_equal(&ifp->addr, addr) &&
Erik Klinec58da4c2015-02-04 20:01:23 +09001768 !(ifp_flags&banned_flags) &&
Ian Morris63159f22015-03-29 14:00:04 +01001769 (!dev || ifp->idev->dev == dev ||
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001770 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1771 rcu_read_unlock_bh();
1772 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001775
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001776 rcu_read_unlock_bh();
1777 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
Erik Klinec58da4c2015-02-04 20:01:23 +09001779EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001780
David S. Miller3e81c6d2010-03-20 16:18:00 -07001781static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1782 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Eric Dumazetddbe5032012-07-18 08:11:12 +00001784 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001785 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Sasha Levinb67bfe02013-02-27 17:06:00 -08001787 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001788 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001789 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001791 if (!dev || ifp->idev->dev == dev)
David S. Miller3e81c6d2010-03-20 16:18:00 -07001792 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794 }
David S. Miller3e81c6d2010-03-20 16:18:00 -07001795 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
1797
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001798/* Compares an address/prefix_len with addresses on device @dev.
1799 * If one is found it returns true.
1800 */
1801bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1802 const unsigned int prefix_len, struct net_device *dev)
1803{
1804 struct inet6_dev *idev;
1805 struct inet6_ifaddr *ifa;
1806 bool ret = false;
1807
1808 rcu_read_lock();
1809 idev = __in6_dev_get(dev);
1810 if (idev) {
1811 read_lock_bh(&idev->lock);
1812 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1813 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1814 if (ret)
1815 break;
1816 }
1817 read_unlock_bh(&idev->lock);
1818 }
1819 rcu_read_unlock();
1820
1821 return ret;
1822}
1823EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1824
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001825int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001826{
1827 struct inet6_dev *idev;
1828 struct inet6_ifaddr *ifa;
1829 int onlink;
1830
1831 onlink = 0;
1832 rcu_read_lock();
1833 idev = __in6_dev_get(dev);
1834 if (idev) {
1835 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001836 list_for_each_entry(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001837 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1838 ifa->prefix_len);
1839 if (onlink)
1840 break;
1841 }
1842 read_unlock_bh(&idev->lock);
1843 }
1844 rcu_read_unlock();
1845 return onlink;
1846}
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001847EXPORT_SYMBOL(ipv6_chk_prefix);
1848
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001849struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001850 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
David S. Millerb79d1d52010-03-25 21:39:21 -07001852 struct inet6_ifaddr *ifp, *result = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001853 unsigned int hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
stephen hemminger5c578ae2010-03-17 20:31:11 +00001855 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001856 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001857 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001858 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001860 if (!dev || ifp->idev->dev == dev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07001862 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 in6_ifa_hold(ifp);
1864 break;
1865 }
1866 }
1867 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001868 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
David S. Millerb79d1d52010-03-25 21:39:21 -07001870 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871}
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873/* Gets referenced address, destroys ifaddr */
1874
Brian Haleycc411d02009-09-09 14:41:32 +00001875static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Lubomir Rintel3d171f32016-01-08 13:47:23 +01001877 if (dad_failed)
1878 ifp->flags |= IFA_F_DADFAILED;
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 if (ifp->flags&IFA_F_PERMANENT) {
1881 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001882 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 ifp->flags |= IFA_F_TENTATIVE;
1884 spin_unlock_bh(&ifp->lock);
Herbert Xue2577a02010-03-13 12:23:29 -08001885 if (dad_failed)
1886 ipv6_ifa_notify(0, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 } else if (ifp->flags&IFA_F_TEMPORARY) {
1889 struct inet6_ifaddr *ifpub;
1890 spin_lock_bh(&ifp->lock);
1891 ifpub = ifp->ifpub;
1892 if (ifpub) {
1893 in6_ifa_hold(ifpub);
1894 spin_unlock_bh(&ifp->lock);
1895 ipv6_create_tempaddr(ifpub, ifp);
1896 in6_ifa_put(ifpub);
1897 } else {
1898 spin_unlock_bh(&ifp->lock);
1899 }
1900 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001901 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904}
1905
Herbert Xuf2344a12010-05-18 15:55:27 -07001906static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1907{
1908 int err = -ENOENT;
1909
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001910 spin_lock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001911 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1912 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1913 err = 0;
1914 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001915 spin_unlock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001916
1917 return err;
1918}
1919
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001920void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1921{
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001922 struct inet6_dev *idev = ifp->idev;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001923 struct net *net = dev_net(ifp->idev->dev);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001924
Ursula Braun853dc2e2010-10-24 23:06:43 +00001925 if (addrconf_dad_end(ifp)) {
1926 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07001927 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00001928 }
Herbert Xuf2344a12010-05-18 15:55:27 -07001929
Joe Perchese87cc472012-05-13 21:56:26 +00001930 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1931 ifp->idev->dev->name, &ifp->addr);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001932
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001933 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001934
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001935 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
1936 int scope = ifp->scope;
1937 u32 flags = ifp->flags;
1938 struct in6_addr new_addr;
1939 struct inet6_ifaddr *ifp2;
1940 u32 valid_lft, preferred_lft;
1941 int pfxlen = ifp->prefix_len;
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001942 int retries = ifp->stable_privacy_retry + 1;
1943
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001944 if (retries > net->ipv6.sysctl.idgen_retries) {
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001945 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
1946 ifp->idev->dev->name);
1947 goto errdad;
1948 }
1949
1950 new_addr = ifp->addr;
1951 if (ipv6_generate_stable_address(&new_addr, retries,
1952 idev))
1953 goto errdad;
1954
1955 valid_lft = ifp->valid_lft;
1956 preferred_lft = ifp->prefered_lft;
1957
1958 spin_unlock_bh(&ifp->lock);
1959
1960 if (idev->cnf.max_addresses &&
1961 ipv6_count_addresses(idev) >=
1962 idev->cnf.max_addresses)
1963 goto lock_errdad;
1964
1965 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
1966 ifp->idev->dev->name);
1967
1968 ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
1969 scope, flags, valid_lft,
1970 preferred_lft);
1971 if (IS_ERR(ifp2))
1972 goto lock_errdad;
1973
1974 spin_lock_bh(&ifp2->lock);
1975 ifp2->stable_privacy_retry = retries;
1976 ifp2->state = INET6_IFADDR_STATE_PREDAD;
1977 spin_unlock_bh(&ifp2->lock);
1978
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001979 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001980 in6_ifa_put(ifp2);
1981lock_errdad:
1982 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001983 }
1984
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001985errdad:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001986 /* transition from _POSTDAD to _ERRDAD */
1987 ifp->state = INET6_IFADDR_STATE_ERRDAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001988 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001989
1990 addrconf_mod_dad_work(ifp, 0);
Wei Yongjun751eb6b2016-09-05 16:06:31 +08001991 in6_ifa_put(ifp);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001992}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02001994/* Join to solicited addr multicast group.
1995 * caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001996void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 struct in6_addr maddr;
1999
2000 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2001 return;
2002
2003 addrconf_addr_solict_mult(addr, &maddr);
2004 ipv6_dev_mc_inc(dev, &maddr);
2005}
2006
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002007/* caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002008void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
2010 struct in6_addr maddr;
2011
2012 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2013 return;
2014
2015 addrconf_addr_solict_mult(addr, &maddr);
2016 __ipv6_dev_mc_dec(idev, &maddr);
2017}
2018
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002019/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002020static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
2022 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002023
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002024 if (ifp->prefix_len >= 127) /* RFC 6164 */
Bjørn Mork2bda8a02011-07-05 23:04:13 +00002025 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2027 if (ipv6_addr_any(&addr))
2028 return;
WANG Cong013b4d92014-09-11 15:35:11 -07002029 __ipv6_dev_ac_inc(ifp->idev, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002032/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002033static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
2035 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002036
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002037 if (ifp->prefix_len >= 127) /* RFC 6164 */
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00002038 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2040 if (ipv6_addr_any(&addr))
2041 return;
2042 __ipv6_dev_ac_dec(ifp->idev, &addr);
2043}
2044
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002045static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
2046{
Alexander Aring5241c2d2015-12-14 20:55:22 +01002047 if (dev->addr_len != EUI64_ADDR_LEN)
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002048 return -1;
Alexander Aring5241c2d2015-12-14 20:55:22 +01002049 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
YOSHIFUJI Hideaki / 吉藤英明5e98a362013-01-28 10:44:29 +00002050 eui[0] ^= 2;
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002051 return 0;
2052}
2053
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002054static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2055{
2056 union fwnet_hwaddr *ha;
2057
2058 if (dev->addr_len != FWNET_ALEN)
2059 return -1;
2060
2061 ha = (union fwnet_hwaddr *)dev->dev_addr;
2062
2063 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2064 eui[0] ^= 2;
2065 return 0;
2066}
2067
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002068static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2069{
2070 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2071 if (dev->addr_len != ARCNET_ALEN)
2072 return -1;
2073 memset(eui, 0, 7);
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002074 eui[7] = *(u8 *)dev->dev_addr;
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002075 return 0;
2076}
2077
2078static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2079{
2080 if (dev->addr_len != INFINIBAND_ALEN)
2081 return -1;
2082 memcpy(eui, dev->dev_addr + 12, 8);
2083 eui[0] |= 2;
2084 return 0;
2085}
2086
stephen hemmingerc61393e2010-10-04 20:17:53 +00002087static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002088{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00002089 if (addr == 0)
2090 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002091 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2092 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2093 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2094 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2095 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2096 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2097 eui[1] = 0;
2098 eui[2] = 0x5E;
2099 eui[3] = 0xFE;
2100 memcpy(eui + 4, &addr, 4);
2101 return 0;
2102}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002103
2104static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2105{
2106 if (dev->priv_flags & IFF_ISATAP)
2107 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2108 return -1;
2109}
2110
stephen hemmingeraee80b52011-06-08 10:44:30 +00002111static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2112{
2113 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2114}
2115
Nicolas Dichtele8377352013-08-20 12:16:06 +02002116static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2117{
2118 memcpy(eui, dev->perm_addr, 3);
2119 memcpy(eui + 5, dev->perm_addr + 3, 3);
2120 eui[3] = 0xFF;
2121 eui[4] = 0xFE;
2122 eui[0] ^= 2;
2123 return 0;
2124}
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2127{
2128 switch (dev->type) {
2129 case ARPHRD_ETHER:
2130 case ARPHRD_FDDI:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002131 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002133 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002135 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002136 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002137 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002138 case ARPHRD_IPGRE:
2139 return addrconf_ifid_gre(eui, dev);
Jukka Rissanene74bccb2013-12-11 17:05:36 +02002140 case ARPHRD_6LOWPAN:
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002141 return addrconf_ifid_eui64(eui, dev);
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002142 case ARPHRD_IEEE1394:
2143 return addrconf_ifid_ieee1394(eui, dev);
Nicolas Dichtele8377352013-08-20 12:16:06 +02002144 case ARPHRD_TUNNEL6:
2145 return addrconf_ifid_ip6tnl(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 }
2147 return -1;
2148}
2149
2150static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2151{
2152 int err = -1;
2153 struct inet6_ifaddr *ifp;
2154
2155 read_lock_bh(&idev->lock);
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01002156 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2157 if (ifp->scope > IFA_LINK)
2158 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2160 memcpy(eui, ifp->addr.s6_addr+8, 8);
2161 err = 0;
2162 break;
2163 }
2164 }
2165 read_unlock_bh(&idev->lock);
2166 return err;
2167}
2168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002170static void ipv6_regen_rndid(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08002173 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
2176 /*
2177 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2178 * check if generated address is not inappropriate
2179 *
2180 * - Reserved subnet anycast (RFC 2526)
2181 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002182 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 * 00-00-5E-FE-xx-xx-xx-xx
2184 * - value 0
2185 * - XXX: already assigned to an address on the device
2186 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002187 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2189 (idev->rndid[7]&0x80))
2190 goto regen;
2191 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2192 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2193 goto regen;
2194 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2195 goto regen;
2196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002199static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002200{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002202 ipv6_regen_rndid(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +09002205u32 addrconf_rt_table(const struct net_device *dev, u32 default_table) {
2206 /* Determines into what table to put autoconf PIO/RIO/default routes
2207 * learned on this device.
2208 *
2209 * - If 0, use the same table for every device. This puts routes into
2210 * one of RT_TABLE_{PREFIX,INFO,DFLT} depending on the type of route
2211 * (but note that these three are currently all equal to
2212 * RT6_TABLE_MAIN).
2213 * - If > 0, use the specified table.
2214 * - If < 0, put routes into table dev->ifindex + (-rt_table).
2215 */
2216 struct inet6_dev *idev = in6_dev_get(dev);
2217 u32 table;
2218 int sysctl = idev->cnf.accept_ra_rt_table;
2219 if (sysctl == 0) {
2220 table = default_table;
2221 } else if (sysctl > 0) {
2222 table = (u32) sysctl;
2223 } else {
2224 table = (unsigned) dev->ifindex + (-sysctl);
2225 }
2226 in6_dev_put(idev);
2227 return table;
2228}
2229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230/*
2231 * Add prefix route.
2232 */
2233
2234static void
2235addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002236 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
Thomas Graf86872cb2006-08-22 00:01:08 -07002238 struct fib6_config cfg = {
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +09002239 .fc_table = l3mdev_fib_table(dev) ? : addrconf_rt_table(dev, RT6_TABLE_PREFIX),
Thomas Graf86872cb2006-08-22 00:01:08 -07002240 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2241 .fc_ifindex = dev->ifindex,
2242 .fc_expires = expires,
2243 .fc_dst_len = plen,
2244 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002245 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07002246 .fc_protocol = RTPROT_KERNEL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002247 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002249 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
2251 /* Prevent useless cloning on PtP SIT.
2252 This thing is done here expecting that the whole
2253 class of non-broadcast devices need not cloning.
2254 */
Amerigo Wang07a93622012-10-29 16:23:10 +00002255#if IS_ENABLED(CONFIG_IPV6_SIT)
Thomas Graf86872cb2006-08-22 00:01:08 -07002256 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2257 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002258#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Thomas Graf86872cb2006-08-22 00:01:08 -07002260 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261}
2262
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002263
2264static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2265 int plen,
2266 const struct net_device *dev,
2267 u32 flags, u32 noflags)
2268{
2269 struct fib6_node *fn;
2270 struct rt6_info *rt = NULL;
2271 struct fib6_table *table;
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +09002272 u32 tb_id = l3mdev_fib_table(dev) ? : addrconf_rt_table(dev, RT6_TABLE_PREFIX);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002273
David Ahernca254492015-10-12 11:47:10 -07002274 table = fib6_get_table(dev_net(dev), tb_id);
Ian Morris63159f22015-03-29 14:00:04 +01002275 if (!table)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002276 return NULL;
2277
Li RongQing5744dd92012-09-11 21:59:01 +00002278 read_lock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002279 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2280 if (!fn)
2281 goto out;
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07002282
2283 noflags |= RTF_CACHE;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002284 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002285 if (rt->dst.dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002286 continue;
2287 if ((rt->rt6i_flags & flags) != flags)
2288 continue;
Romain Kuntz85da53b2013-01-09 15:02:26 +01002289 if ((rt->rt6i_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002290 continue;
2291 dst_hold(&rt->dst);
2292 break;
2293 }
2294out:
Li RongQing5744dd92012-09-11 21:59:01 +00002295 read_unlock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002296 return rt;
2297}
2298
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300/* Create "default" multicast route to the interface */
2301
2302static void addrconf_add_mroute(struct net_device *dev)
2303{
Thomas Graf86872cb2006-08-22 00:01:08 -07002304 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002305 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002306 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2307 .fc_ifindex = dev->ifindex,
2308 .fc_dst_len = 8,
2309 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002310 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002311 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Thomas Graf86872cb2006-08-22 00:01:08 -07002313 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2314
2315 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316}
2317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2319{
2320 struct inet6_dev *idev;
2321
2322 ASSERT_RTNL();
2323
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002324 idev = ipv6_find_idev(dev);
2325 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00002326 return ERR_PTR(-ENOBUFS);
2327
2328 if (idev->cnf.disable_ipv6)
2329 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
2331 /* Add default multicast route */
David Ahernba46ee42016-06-13 13:44:18 -07002332 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
Li Wei4af04ab2011-12-06 21:23:45 +00002333 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 return idev;
2336}
2337
Jiri Pirko53bd6742013-12-06 09:45:22 +01002338static void manage_tempaddrs(struct inet6_dev *idev,
2339 struct inet6_ifaddr *ifp,
2340 __u32 valid_lft, __u32 prefered_lft,
2341 bool create, unsigned long now)
2342{
2343 u32 flags;
2344 struct inet6_ifaddr *ift;
2345
2346 read_lock_bh(&idev->lock);
2347 /* update all temporary addresses in the list */
2348 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2349 int age, max_valid, max_prefered;
2350
2351 if (ifp != ift->ifpub)
2352 continue;
2353
2354 /* RFC 4941 section 3.3:
2355 * If a received option will extend the lifetime of a public
2356 * address, the lifetimes of temporary addresses should
2357 * be extended, subject to the overall constraint that no
2358 * temporary addresses should ever remain "valid" or "preferred"
2359 * for a time longer than (TEMP_VALID_LIFETIME) or
2360 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2361 */
2362 age = (now - ift->cstamp) / HZ;
2363 max_valid = idev->cnf.temp_valid_lft - age;
2364 if (max_valid < 0)
2365 max_valid = 0;
2366
2367 max_prefered = idev->cnf.temp_prefered_lft -
Jiri Bohac76506a92016-10-13 18:52:15 +02002368 idev->desync_factor - age;
Jiri Pirko53bd6742013-12-06 09:45:22 +01002369 if (max_prefered < 0)
2370 max_prefered = 0;
2371
2372 if (valid_lft > max_valid)
2373 valid_lft = max_valid;
2374
2375 if (prefered_lft > max_prefered)
2376 prefered_lft = max_prefered;
2377
2378 spin_lock(&ift->lock);
2379 flags = ift->flags;
2380 ift->valid_lft = valid_lft;
2381 ift->prefered_lft = prefered_lft;
2382 ift->tstamp = now;
2383 if (prefered_lft > 0)
2384 ift->flags &= ~IFA_F_DEPRECATED;
2385
2386 spin_unlock(&ift->lock);
2387 if (!(flags&IFA_F_TENTATIVE))
2388 ipv6_ifa_notify(0, ift);
2389 }
2390
2391 if ((create || list_empty(&idev->tempaddr_list)) &&
2392 idev->cnf.use_tempaddr > 0) {
2393 /* When a new public address is created as described
2394 * in [ADDRCONF], also create a new temporary address.
2395 * Also create a temporary address if it's enabled but
2396 * no temporary address currently exists.
2397 */
2398 read_unlock_bh(&idev->lock);
2399 ipv6_create_tempaddr(ifp, NULL);
2400 } else {
2401 read_unlock_bh(&idev->lock);
2402 }
2403}
2404
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002405static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2406{
2407 return idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2408 idev->addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2409}
2410
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002411int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2412 const struct prefix_info *pinfo,
2413 struct inet6_dev *in6_dev,
2414 const struct in6_addr *addr, int addr_type,
2415 u32 addr_flags, bool sllao, bool tokenized,
2416 __u32 valid_lft, u32 prefered_lft)
Alexander Aring4f672232016-06-15 21:20:22 +02002417{
2418 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2419 int create = 0, update_lft = 0;
2420
2421 if (!ifp && valid_lft) {
2422 int max_addresses = in6_dev->cnf.max_addresses;
2423
2424#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2425 if (in6_dev->cnf.optimistic_dad &&
2426 !net->ipv6.devconf_all->forwarding && sllao)
2427 addr_flags |= IFA_F_OPTIMISTIC;
2428#endif
2429
2430 /* Do not allow to create too much of autoconfigured
2431 * addresses; this would be too easy way to crash kernel.
2432 */
2433 if (!max_addresses ||
2434 ipv6_count_addresses(in6_dev) < max_addresses)
2435 ifp = ipv6_add_addr(in6_dev, addr, NULL,
2436 pinfo->prefix_len,
2437 addr_type&IPV6_ADDR_SCOPE_MASK,
2438 addr_flags, valid_lft,
2439 prefered_lft);
2440
2441 if (IS_ERR_OR_NULL(ifp))
2442 return -1;
2443
2444 update_lft = 0;
2445 create = 1;
2446 spin_lock_bh(&ifp->lock);
2447 ifp->flags |= IFA_F_MANAGETEMPADDR;
2448 ifp->cstamp = jiffies;
2449 ifp->tokenized = tokenized;
2450 spin_unlock_bh(&ifp->lock);
2451 addrconf_dad_start(ifp);
2452 }
2453
2454 if (ifp) {
2455 u32 flags;
2456 unsigned long now;
2457 u32 stored_lft;
2458
2459 /* update lifetime (RFC2462 5.5.3 e) */
2460 spin_lock_bh(&ifp->lock);
2461 now = jiffies;
2462 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2463 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2464 else
2465 stored_lft = 0;
2466 if (!update_lft && !create && stored_lft) {
2467 const u32 minimum_lft = min_t(u32,
2468 stored_lft, MIN_VALID_LIFETIME);
2469 valid_lft = max(valid_lft, minimum_lft);
2470
2471 /* RFC4862 Section 5.5.3e:
2472 * "Note that the preferred lifetime of the
2473 * corresponding address is always reset to
2474 * the Preferred Lifetime in the received
2475 * Prefix Information option, regardless of
2476 * whether the valid lifetime is also reset or
2477 * ignored."
2478 *
2479 * So we should always update prefered_lft here.
2480 */
2481 update_lft = 1;
2482 }
2483
2484 if (update_lft) {
2485 ifp->valid_lft = valid_lft;
2486 ifp->prefered_lft = prefered_lft;
2487 ifp->tstamp = now;
2488 flags = ifp->flags;
2489 ifp->flags &= ~IFA_F_DEPRECATED;
2490 spin_unlock_bh(&ifp->lock);
2491
2492 if (!(flags&IFA_F_TENTATIVE))
2493 ipv6_ifa_notify(0, ifp);
2494 } else
2495 spin_unlock_bh(&ifp->lock);
2496
2497 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2498 create, now);
2499
2500 in6_ifa_put(ifp);
2501 addrconf_verify();
2502 }
2503
2504 return 0;
2505}
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002506EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
Alexander Aring4f672232016-06-15 21:20:22 +02002507
Neil Hormane6bff992012-01-04 10:49:15 +00002508void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
2510 struct prefix_info *pinfo;
2511 __u32 valid_lft;
2512 __u32 prefered_lft;
Alexander Aring4f672232016-06-15 21:20:22 +02002513 int addr_type, err;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002514 u32 addr_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07002516 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 if (len < sizeof(struct prefix_info)) {
dingtianhongba3542e2013-08-17 10:27:04 +08002521 ADBG("addrconf: prefix option too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 return;
2523 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 /*
2526 * Validation checks ([ADDRCONF], page 19)
2527 */
2528
2529 addr_type = ipv6_addr_type(&pinfo->prefix);
2530
2531 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2532 return;
2533
2534 valid_lft = ntohl(pinfo->valid);
2535 prefered_lft = ntohl(pinfo->prefered);
2536
2537 if (prefered_lft > valid_lft) {
Joe Perchese87cc472012-05-13 21:56:26 +00002538 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 return;
2540 }
2541
2542 in6_dev = in6_dev_get(dev);
2543
Ian Morris63159f22015-03-29 14:00:04 +01002544 if (!in6_dev) {
Joe Perchese87cc472012-05-13 21:56:26 +00002545 net_dbg_ratelimited("addrconf: device %s not configured\n",
2546 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 return;
2548 }
2549
2550 /*
2551 * Two things going on here:
2552 * 1) Add routes for on-link prefixes
2553 * 2) Configure prefixes with the auto flag set
2554 */
2555
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002556 if (pinfo->onlink) {
2557 struct rt6_info *rt;
2558 unsigned long rt_expires;
2559
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002560 /* Avoid arithmetic overflow. Really, we could
2561 * save rt_expires in seconds, likely valid_lft,
2562 * but it would require division in fib gc, that it
2563 * not good.
2564 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002565 if (HZ > USER_HZ)
2566 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2567 else
2568 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08002569
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002570 if (addrconf_finite_timeout(rt_expires))
2571 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002573 rt = addrconf_get_prefix_route(&pinfo->prefix,
2574 pinfo->prefix_len,
2575 dev,
2576 RTF_ADDRCONF | RTF_PREFIX_RT,
2577 RTF_GATEWAY | RTF_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002579 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002580 /* Autoconf prefix route */
2581 if (valid_lft == 0) {
2582 ip6_del_rt(rt);
2583 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002584 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002585 /* not infinity */
Gao feng1716a962012-04-06 00:13:10 +00002586 rt6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002587 } else {
Gao feng1716a962012-04-06 00:13:10 +00002588 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 }
2590 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002591 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002592 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2593 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002594 /* not infinity */
2595 flags |= RTF_EXPIRES;
2596 expires = jiffies_to_clock_t(rt_expires);
2597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002599 dev, expires, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00002601 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 }
2603
2604 /* Try to figure out our local address for this prefix */
2605
2606 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 struct in6_addr addr;
Alexander Aringf997c552016-06-15 21:20:23 +02002608 bool tokenized = false, dev_addr_generated = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610 if (pinfo->prefix_len == 64) {
2611 memcpy(&addr, &pinfo->prefix, 8);
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002612
2613 if (!ipv6_addr_any(&in6_dev->token)) {
2614 read_lock_bh(&in6_dev->lock);
2615 memcpy(addr.s6_addr + 8,
2616 in6_dev->token.s6_addr + 8, 8);
2617 read_unlock_bh(&in6_dev->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00002618 tokenized = true;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002619 } else if (is_addr_mode_generate_stable(in6_dev) &&
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002620 !ipv6_generate_stable_address(&addr, 0,
2621 in6_dev)) {
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002622 addr_flags |= IFA_F_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002623 goto ok;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002624 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2625 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
Alexander Aring4f672232016-06-15 21:20:22 +02002626 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002627 } else {
2628 dev_addr_generated = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 }
2630 goto ok;
2631 }
Joe Perchese87cc472012-05-13 21:56:26 +00002632 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2633 pinfo->prefix_len);
Alexander Aring4f672232016-06-15 21:20:22 +02002634 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
2636ok:
Alexander Aring4f672232016-06-15 21:20:22 +02002637 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2638 &addr, addr_type,
2639 addr_flags, sllao,
2640 tokenized, valid_lft,
2641 prefered_lft);
2642 if (err)
2643 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002644
2645 /* Ignore error case here because previous prefix add addr was
2646 * successful which will be notified.
2647 */
2648 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2649 addr_type, addr_flags, sllao,
2650 tokenized, valid_lft,
2651 prefered_lft,
2652 dev_addr_generated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 }
2654 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
Alexander Aring4f672232016-06-15 21:20:22 +02002655put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 in6_dev_put(in6_dev);
2657}
2658
2659/*
2660 * Set destination address.
2661 * Special case for SIT interfaces where we create a new "virtual"
2662 * device.
2663 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002664int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665{
2666 struct in6_ifreq ireq;
2667 struct net_device *dev;
2668 int err = -EINVAL;
2669
2670 rtnl_lock();
2671
2672 err = -EFAULT;
2673 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2674 goto err_exit;
2675
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002676 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
2678 err = -ENODEV;
Ian Morris63159f22015-03-29 14:00:04 +01002679 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 goto err_exit;
2681
Amerigo Wang07a93622012-10-29 16:23:10 +00002682#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002684 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 struct ip_tunnel_parm p;
2687
2688 err = -EADDRNOTAVAIL;
2689 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2690 goto err_exit;
2691
2692 memset(&p, 0, sizeof(p));
2693 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2694 p.iph.saddr = 0;
2695 p.iph.version = 4;
2696 p.iph.ihl = 5;
2697 p.iph.protocol = IPPROTO_IPV6;
2698 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002699 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002701 if (ops->ndo_do_ioctl) {
2702 mm_segment_t oldfs = get_fs();
2703
2704 set_fs(KERNEL_DS);
2705 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2706 set_fs(oldfs);
2707 } else
2708 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710 if (err == 0) {
2711 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002712 dev = __dev_get_by_name(net, p.name);
2713 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 goto err_exit;
2715 err = dev_open(dev);
2716 }
2717 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002718#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
2720err_exit:
2721 rtnl_unlock();
2722 return err;
2723}
2724
Madhu Challa93a714d2015-02-25 09:58:35 -08002725static int ipv6_mc_config(struct sock *sk, bool join,
2726 const struct in6_addr *addr, int ifindex)
2727{
2728 int ret;
2729
2730 ASSERT_RTNL();
2731
2732 lock_sock(sk);
2733 if (join)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002734 ret = ipv6_sock_mc_join(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002735 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002736 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002737 release_sock(sk);
2738
2739 return ret;
2740}
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742/*
2743 * Manual configuration of address on an interface
2744 */
Jiri Pirko479840f2013-12-06 09:45:21 +01002745static int inet6_addr_add(struct net *net, int ifindex,
2746 const struct in6_addr *pfx,
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002747 const struct in6_addr *peer_pfx,
Jiri Pirko479840f2013-12-06 09:45:21 +01002748 unsigned int plen, __u32 ifa_flags,
2749 __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750{
2751 struct inet6_ifaddr *ifp;
2752 struct inet6_dev *idev;
2753 struct net_device *dev;
Madhu Challa93a714d2015-02-25 09:58:35 -08002754 unsigned long timeout;
2755 clock_t expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002757 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
2759 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002760
Thomas Graf24ef0da2008-05-28 16:54:22 +02002761 if (plen > 128)
2762 return -EINVAL;
2763
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002764 /* check the lifetime */
2765 if (!valid_lft || prefered_lft > valid_lft)
2766 return -EINVAL;
2767
Jiri Pirko53bd6742013-12-06 09:45:22 +01002768 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2769 return -EINVAL;
2770
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002771 dev = __dev_get_by_index(net, ifindex);
2772 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002774
Brian Haley64e724f2010-07-20 10:34:30 +00002775 idev = addrconf_add_dev(dev);
2776 if (IS_ERR(idev))
2777 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
Madhu Challa93a714d2015-02-25 09:58:35 -08002779 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2780 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2781 true, pfx, ifindex);
2782
2783 if (ret < 0)
2784 return ret;
2785 }
2786
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 scope = ipv6_addr_scope(pfx);
2788
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002789 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2790 if (addrconf_finite_timeout(timeout)) {
2791 expires = jiffies_to_clock_t(timeout * HZ);
2792 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002793 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002794 } else {
2795 expires = 0;
2796 flags = 0;
2797 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002798 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002799
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002800 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2801 if (addrconf_finite_timeout(timeout)) {
2802 if (timeout == 0)
2803 ifa_flags |= IFA_F_DEPRECATED;
2804 prefered_lft = timeout;
2805 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002806
Jiri Benc8a226b22013-08-01 10:41:28 +02002807 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2808 valid_lft, prefered_lft);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 if (!IS_ERR(ifp)) {
Thomas Haller761aac72014-01-15 15:36:58 +01002811 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2812 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2813 expires, flags);
2814 }
2815
Neil Horman95c385b2007-04-25 17:08:10 -07002816 /*
2817 * Note that section 3.1 of RFC 4429 indicates
2818 * that the Optimistic flag should not be set for
2819 * manually configured addresses
2820 */
David S. Millercf22f9a2012-04-14 21:37:40 -04002821 addrconf_dad_start(ifp);
Jiri Pirko53bd6742013-12-06 09:45:22 +01002822 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2823 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2824 true, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002826 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 return 0;
Madhu Challa93a714d2015-02-25 09:58:35 -08002828 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2829 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2830 false, pfx, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 }
2832
2833 return PTR_ERR(ifp);
2834}
2835
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002836static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2837 const struct in6_addr *pfx, unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838{
2839 struct inet6_ifaddr *ifp;
2840 struct inet6_dev *idev;
2841 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002842
Thomas Graf24ef0da2008-05-28 16:54:22 +02002843 if (plen > 128)
2844 return -EINVAL;
2845
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002846 dev = __dev_get_by_index(net, ifindex);
2847 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return -ENODEV;
2849
Ian Morrise5d08d72014-11-23 21:28:43 +00002850 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01002851 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 return -ENXIO;
2853
2854 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002855 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 if (ifp->prefix_len == plen &&
2857 ipv6_addr_equal(pfx, &ifp->addr)) {
2858 in6_ifa_hold(ifp);
2859 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002860
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002861 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2862 (ifa_flags & IFA_F_MANAGETEMPADDR))
2863 manage_tempaddrs(idev, ifp, 0, 0, false,
2864 jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 ipv6_del_addr(ifp);
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002866 addrconf_verify_rtnl();
Madhu Challa93a714d2015-02-25 09:58:35 -08002867 if (ipv6_addr_is_multicast(pfx)) {
2868 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2869 false, pfx, dev->ifindex);
2870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 return 0;
2872 }
2873 }
2874 read_unlock_bh(&idev->lock);
2875 return -EADDRNOTAVAIL;
2876}
2877
2878
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002879int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880{
2881 struct in6_ifreq ireq;
2882 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002883
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002884 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2888 return -EFAULT;
2889
2890 rtnl_lock();
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002891 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002892 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2893 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 rtnl_unlock();
2895 return err;
2896}
2897
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002898int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899{
2900 struct in6_ifreq ireq;
2901 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002902
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002903 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 return -EPERM;
2905
2906 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2907 return -EFAULT;
2908
2909 rtnl_lock();
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002910 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002911 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 rtnl_unlock();
2913 return err;
2914}
2915
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002916static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2917 int plen, int scope)
2918{
2919 struct inet6_ifaddr *ifp;
2920
Jiri Benc8a226b22013-08-01 10:41:28 +02002921 ifp = ipv6_add_addr(idev, addr, NULL, plen,
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01002922 scope, IFA_F_PERMANENT,
2923 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002924 if (!IS_ERR(ifp)) {
2925 spin_lock_bh(&ifp->lock);
2926 ifp->flags &= ~IFA_F_TENTATIVE;
2927 spin_unlock_bh(&ifp->lock);
Paolo Abeni764d3be2016-11-22 16:57:40 +01002928 rt_genid_bump_ipv6(dev_net(idev->dev));
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002929 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2930 in6_ifa_put(ifp);
2931 }
2932}
2933
Amerigo Wang07a93622012-10-29 16:23:10 +00002934#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935static void sit_add_v4_addrs(struct inet6_dev *idev)
2936{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 struct in6_addr addr;
2938 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002939 struct net *net = dev_net(idev->dev);
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002940 int scope, plen;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002941 u32 pflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
2943 ASSERT_RTNL();
2944
2945 memset(&addr, 0, sizeof(struct in6_addr));
2946 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2947
2948 if (idev->dev->flags&IFF_POINTOPOINT) {
2949 addr.s6_addr32[0] = htonl(0xfe800000);
2950 scope = IFA_LINK;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002951 plen = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 } else {
2953 scope = IPV6_ADDR_COMPATv4;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002954 plen = 96;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002955 pflags |= RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 }
2957
2958 if (addr.s6_addr32[3]) {
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002959 add_addr(idev, &addr, plen, scope);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002960 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 return;
2962 }
2963
Benjamin Thery6fda7352008-03-05 10:47:47 -08002964 for_each_netdev(net, dev) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002965 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 if (in_dev && (dev->flags & IFF_UP)) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002967 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968
2969 int flag = scope;
2970
2971 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
2973 addr.s6_addr32[3] = ifa->ifa_local;
2974
2975 if (ifa->ifa_scope == RT_SCOPE_LINK)
2976 continue;
2977 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2978 if (idev->dev->flags&IFF_POINTOPOINT)
2979 continue;
2980 flag |= IFA_HOST;
2981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002983 add_addr(idev, &addr, plen, flag);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002984 addrconf_prefix_route(&addr, plen, idev->dev, 0,
2985 pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 }
2987 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002990#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
2992static void init_loopback(struct net_device *dev)
2993{
2994 struct inet6_dev *idev;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05302995 struct net_device *sp_dev;
2996 struct inet6_ifaddr *sp_ifa;
2997 struct rt6_info *sp_rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
2999 /* ::1 */
3000
3001 ASSERT_RTNL();
3002
Ian Morrise5d08d72014-11-23 21:28:43 +00003003 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003004 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003005 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 return;
3007 }
3008
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003009 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303010
3011 /* Add routes to other interface's IPv6 addresses */
3012 for_each_netdev(dev_net(dev), sp_dev) {
3013 if (!strcmp(sp_dev->name, dev->name))
3014 continue;
3015
3016 idev = __in6_dev_get(sp_dev);
3017 if (!idev)
3018 continue;
3019
3020 read_lock_bh(&idev->lock);
3021 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
3022
3023 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
3024 continue;
3025
Gao feng33d99112014-01-24 16:29:11 +08003026 if (sp_ifa->rt) {
3027 /* This dst has been added to garbage list when
3028 * lo device down, release this obsolete dst and
3029 * reallocate a new router for ifa.
3030 */
Nicolas Dichtela2204452016-10-12 10:10:40 +02003031 if (!atomic_read(&sp_ifa->rt->rt6i_ref)) {
Gao feng33d99112014-01-24 16:29:11 +08003032 ip6_rt_put(sp_ifa->rt);
3033 sp_ifa->rt = NULL;
3034 } else {
3035 continue;
3036 }
3037 }
Gao fenga881ae12013-06-16 11:14:30 +08003038
François-Xavier Le Bail57ec0af2013-12-02 11:28:49 +01003039 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303040
3041 /* Failure cases are ignored */
Gao feng534c8772013-06-02 22:16:21 +00003042 if (!IS_ERR(sp_rt)) {
3043 sp_ifa->rt = sp_rt;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303044 ip6_ins_rt(sp_rt);
Gao feng534c8772013-06-02 22:16:21 +00003045 }
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303046 }
3047 read_unlock_bh(&idev->lock);
3048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049}
3050
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003051void addrconf_add_linklocal(struct inet6_dev *idev,
3052 const struct in6_addr *addr, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003054 struct inet6_ifaddr *ifp;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003055 u32 addr_flags = flags | IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Neil Horman95c385b2007-04-25 17:08:10 -07003057#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3058 if (idev->cnf.optimistic_dad &&
David Miller702beb82008-07-20 18:17:02 -07003059 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
Neil Horman95c385b2007-04-25 17:08:10 -07003060 addr_flags |= IFA_F_OPTIMISTIC;
3061#endif
3062
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01003063 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
3064 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003066 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
David S. Millercf22f9a2012-04-14 21:37:40 -04003067 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 in6_ifa_put(ifp);
3069 }
3070}
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003071EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003073static bool ipv6_reserved_interfaceid(struct in6_addr address)
3074{
3075 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3076 return true;
3077
3078 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3079 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3080 return true;
3081
3082 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3083 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3084 return true;
3085
3086 return false;
3087}
3088
3089static int ipv6_generate_stable_address(struct in6_addr *address,
3090 u8 dad_count,
3091 const struct inet6_dev *idev)
3092{
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003093 static DEFINE_SPINLOCK(lock);
3094 static __u32 digest[SHA_DIGEST_WORDS];
3095 static __u32 workspace[SHA_WORKSPACE_WORDS];
3096
3097 static union {
3098 char __data[SHA_MESSAGE_BYTES];
3099 struct {
3100 struct in6_addr secret;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003101 __be32 prefix[2];
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003102 unsigned char hwaddr[MAX_ADDR_LEN];
3103 u8 dad_count;
3104 } __packed;
3105 } data;
3106
3107 struct in6_addr secret;
3108 struct in6_addr temp;
3109 struct net *net = dev_net(idev->dev);
3110
3111 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3112
3113 if (idev->cnf.stable_secret.initialized)
3114 secret = idev->cnf.stable_secret.secret;
3115 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3116 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3117 else
3118 return -1;
3119
3120retry:
3121 spin_lock_bh(&lock);
3122
3123 sha_init(digest);
3124 memset(&data, 0, sizeof(data));
3125 memset(workspace, 0, sizeof(workspace));
3126 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003127 data.prefix[0] = address->s6_addr32[0];
3128 data.prefix[1] = address->s6_addr32[1];
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003129 data.secret = secret;
3130 data.dad_count = dad_count;
3131
3132 sha_transform(digest, data.__data, workspace);
3133
3134 temp = *address;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003135 temp.s6_addr32[2] = (__force __be32)digest[0];
3136 temp.s6_addr32[3] = (__force __be32)digest[1];
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003137
3138 spin_unlock_bh(&lock);
3139
3140 if (ipv6_reserved_interfaceid(temp)) {
3141 dad_count++;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01003142 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003143 return -1;
3144 goto retry;
3145 }
3146
3147 *address = temp;
3148 return 0;
3149}
3150
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003151static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3152{
3153 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3154
3155 if (s->initialized)
3156 return;
3157 s = &idev->cnf.stable_secret;
3158 get_random_bytes(&s->secret, sizeof(s->secret));
3159 s->initialized = true;
3160}
3161
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003162static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3163{
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003164 struct in6_addr addr;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003165
David Ahernca254492015-10-12 11:47:10 -07003166 /* no link local addresses on L3 master devices */
3167 if (netif_is_l3_master(idev->dev))
3168 return;
3169
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003170 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3171
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003172 switch (idev->addr_gen_mode) {
3173 case IN6_ADDR_GEN_MODE_RANDOM:
3174 ipv6_gen_mode_random_init(idev);
3175 /* fallthrough */
3176 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003177 if (!ipv6_generate_stable_address(&addr, 0, idev))
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003178 addrconf_add_linklocal(idev, &addr,
3179 IFA_F_STABLE_PRIVACY);
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003180 else if (prefix_route)
3181 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003182 break;
3183 case IN6_ADDR_GEN_MODE_EUI64:
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003184 /* addrconf_add_linklocal also adds a prefix_route and we
3185 * only need to care about prefix routes if ipv6_generate_eui64
3186 * couldn't generate one.
3187 */
3188 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003189 addrconf_add_linklocal(idev, &addr, 0);
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003190 else if (prefix_route)
3191 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003192 break;
3193 case IN6_ADDR_GEN_MODE_NONE:
3194 default:
3195 /* will not add any link local address */
3196 break;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003197 }
3198}
3199
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200static void addrconf_dev_config(struct net_device *dev)
3201{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003202 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
3204 ASSERT_RTNL();
3205
Herbert Xu74235a22007-06-14 13:02:55 -07003206 if ((dev->type != ARPHRD_ETHER) &&
3207 (dev->type != ARPHRD_FDDI) &&
Herbert Xu74235a22007-06-14 13:02:55 -07003208 (dev->type != ARPHRD_ARCNET) &&
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00003209 (dev->type != ARPHRD_INFINIBAND) &&
Nicolas Dichtele8377352013-08-20 12:16:06 +02003210 (dev->type != ARPHRD_IEEE1394) &&
Jukka Rissanene74bccb2013-12-11 17:05:36 +02003211 (dev->type != ARPHRD_TUNNEL6) &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003212 (dev->type != ARPHRD_6LOWPAN) &&
3213 (dev->type != ARPHRD_NONE)) {
Herbert Xu74235a22007-06-14 13:02:55 -07003214 /* Alas, we support only Ethernet autoconfiguration. */
3215 return;
3216 }
3217
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00003219 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 return;
3221
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003222 /* this device type has no EUI support */
3223 if (dev->type == ARPHRD_NONE &&
3224 idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3225 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3226
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003227 addrconf_addr_gen(idev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
3229
Amerigo Wang07a93622012-10-29 16:23:10 +00003230#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231static void addrconf_sit_config(struct net_device *dev)
3232{
3233 struct inet6_dev *idev;
3234
3235 ASSERT_RTNL();
3236
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003237 /*
3238 * Configure the tunnel with one of our IPv4
3239 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 * our v4 addrs in the tunnel
3241 */
3242
Ian Morrise5d08d72014-11-23 21:28:43 +00003243 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003244 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003245 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 return;
3247 }
3248
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003249 if (dev->priv_flags & IFF_ISATAP) {
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003250 addrconf_addr_gen(idev, false);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003251 return;
3252 }
3253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 sit_add_v4_addrs(idev);
3255
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00003256 if (dev->flags&IFF_POINTOPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003259#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Amerigo Wang07a93622012-10-29 16:23:10 +00003261#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003262static void addrconf_gre_config(struct net_device *dev)
3263{
3264 struct inet6_dev *idev;
stephen hemmingeraee80b52011-06-08 10:44:30 +00003265
stephen hemmingeraee80b52011-06-08 10:44:30 +00003266 ASSERT_RTNL();
3267
Ian Morrise5d08d72014-11-23 21:28:43 +00003268 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003269 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003270 pr_debug("%s: add_dev failed\n", __func__);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003271 return;
3272 }
3273
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003274 addrconf_addr_gen(idev, true);
Hannes Frederic Sowad9e4ce62015-10-08 18:19:39 +02003275 if (dev->flags & IFF_POINTOPOINT)
3276 addrconf_add_mroute(dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003277}
3278#endif
3279
David Ahernf1705ec2016-02-24 09:25:37 -08003280static int fixup_permanent_addr(struct inet6_dev *idev,
3281 struct inet6_ifaddr *ifp)
3282{
3283 if (!ifp->rt) {
3284 struct rt6_info *rt;
3285
3286 rt = addrconf_dst_alloc(idev, &ifp->addr, false);
3287 if (unlikely(IS_ERR(rt)))
3288 return PTR_ERR(rt);
3289
3290 ifp->rt = rt;
3291 }
3292
3293 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3294 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3295 idev->dev, 0, 0);
3296 }
3297
3298 addrconf_dad_start(ifp);
3299
3300 return 0;
3301}
3302
3303static void addrconf_permanent_addr(struct net_device *dev)
3304{
3305 struct inet6_ifaddr *ifp, *tmp;
3306 struct inet6_dev *idev;
3307
3308 idev = __in6_dev_get(dev);
3309 if (!idev)
3310 return;
3311
3312 write_lock_bh(&idev->lock);
3313
3314 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3315 if ((ifp->flags & IFA_F_PERMANENT) &&
3316 fixup_permanent_addr(idev, ifp) < 0) {
3317 write_unlock_bh(&idev->lock);
3318 ipv6_del_addr(ifp);
3319 write_lock_bh(&idev->lock);
3320
3321 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3322 idev->dev->name, &ifp->addr);
3323 }
3324 }
3325
3326 write_unlock_bh(&idev->lock);
3327}
3328
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003329static int addrconf_notify(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00003330 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
Jiri Pirko351638e2013-05-28 01:30:21 +00003332 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
David Ahern4f7f34e2016-04-07 11:10:41 -07003333 struct netdev_notifier_changeupper_info *info;
Eric Dumazet748e2d92012-08-22 21:50:59 +00003334 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003335 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07003336 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003338 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003339 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07003340 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003341 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -07003342 if (IS_ERR(idev))
3343 return notifier_from_errno(PTR_ERR(idev));
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003344 }
3345 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003346
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003347 case NETDEV_CHANGEMTU:
3348 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3349 if (dev->mtu < IPV6_MIN_MTU) {
3350 addrconf_ifdown(dev, 1);
3351 break;
3352 }
3353
3354 if (idev) {
3355 rt6_mtu_change(dev, dev->mtu);
3356 idev->cnf.mtu6 = dev->mtu;
3357 break;
3358 }
3359
3360 /* allocate new idev */
3361 idev = ipv6_add_dev(dev);
3362 if (IS_ERR(idev))
3363 break;
3364
3365 /* device is still not ready */
3366 if (!(idev->if_flags & IF_READY))
3367 break;
3368
3369 run_pending = 1;
3370
3371 /* fall through */
3372
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003374 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07003375 if (dev->flags & IFF_SLAVE)
3376 break;
3377
WANG Cong3ce62a82014-09-11 15:07:16 -07003378 if (idev && idev->cnf.disable_ipv6)
3379 break;
3380
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003381 if (event == NETDEV_UP) {
David Ahern38bd10c2016-04-21 20:56:12 -07003382 /* restore routes for permanent addresses */
3383 addrconf_permanent_addr(dev);
3384
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07003385 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003386 /* device is not ready yet. */
Joe Perchesf3213832012-05-15 14:11:53 +00003387 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003388 dev->name);
3389 break;
3390 }
Kristian Slavov99081042006-02-08 16:10:53 -08003391
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11003392 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3393 idev = ipv6_add_dev(dev);
3394
WANG Conga317a2f2014-07-25 15:25:09 -07003395 if (!IS_ERR_OR_NULL(idev)) {
Kristian Slavov99081042006-02-08 16:10:53 -08003396 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08003397 run_pending = 1;
3398 }
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003399 } else if (event == NETDEV_CHANGE) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07003400 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003401 /* device is still not ready. */
3402 break;
3403 }
3404
3405 if (idev) {
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003406 if (idev->if_flags & IF_READY)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003407 /* device is already configured. */
3408 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003409 idev->if_flags |= IF_READY;
3410 }
3411
Joe Perchesf3213832012-05-15 14:11:53 +00003412 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3413 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003414
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003415 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003416 }
3417
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003418 switch (dev->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +00003419#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 case ARPHRD_SIT:
3421 addrconf_sit_config(dev);
3422 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07003423#endif
Amerigo Wang07a93622012-10-29 16:23:10 +00003424#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003425 case ARPHRD_IPGRE:
3426 addrconf_gre_config(dev);
3427 break;
3428#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 case ARPHRD_LOOPBACK:
3430 init_loopback(dev);
3431 break;
3432
3433 default:
3434 addrconf_dev_config(dev);
3435 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003436 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003437
WANG Conga317a2f2014-07-25 15:25:09 -07003438 if (!IS_ERR_OR_NULL(idev)) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003439 if (run_pending)
3440 addrconf_dad_run(idev);
3441
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003442 /*
3443 * If the MTU changed during the interface down,
3444 * when the interface up, the changed MTU must be
3445 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003447 if (idev->cnf.mtu6 != dev->mtu &&
3448 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 rt6_mtu_change(dev, dev->mtu);
3450 idev->cnf.mtu6 = dev->mtu;
3451 }
3452 idev->tstamp = jiffies;
3453 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003454
3455 /*
3456 * If the changed mtu during down is lower than
3457 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 */
3459 if (dev->mtu < IPV6_MIN_MTU)
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003460 addrconf_ifdown(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 }
3462 break;
3463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 case NETDEV_DOWN:
3465 case NETDEV_UNREGISTER:
3466 /*
3467 * Remove all addresses from this interface.
3468 */
3469 addrconf_ifdown(dev, event != NETDEV_DOWN);
3470 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003471
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07003474 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003475 addrconf_sysctl_unregister(idev);
WANG Conga317a2f2014-07-25 15:25:09 -07003476 err = addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07003477 if (err)
3478 return notifier_from_errno(err);
WANG Conga317a2f2014-07-25 15:25:09 -07003479 err = snmp6_register_dev(idev);
3480 if (err) {
3481 addrconf_sysctl_unregister(idev);
3482 return notifier_from_errno(err);
3483 }
Stephen Hemminger5632c512007-04-28 21:16:39 -07003484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003486
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003487 case NETDEV_PRE_TYPE_CHANGE:
3488 case NETDEV_POST_TYPE_CHANGE:
Andrew Lunn3ef09522015-12-03 21:12:32 +01003489 if (idev)
3490 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07003491 break;
David Ahern4f7f34e2016-04-07 11:10:41 -07003492
3493 case NETDEV_CHANGEUPPER:
3494 info = ptr;
3495
3496 /* flush all routes if dev is linked to or unlinked from
3497 * an L3 master device (e.g., VRF)
3498 */
3499 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3500 addrconf_ifdown(dev, 0);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502
3503 return NOTIFY_OK;
3504}
3505
3506/*
3507 * addrconf module should be notified of a device going up
3508 */
3509static struct notifier_block ipv6_dev_notf = {
3510 .notifier_call = addrconf_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511};
3512
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003513static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07003514{
3515 struct inet6_dev *idev;
3516 ASSERT_RTNL();
3517
3518 idev = __in6_dev_get(dev);
3519
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003520 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003521 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003522 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003523 ipv6_mc_unmap(idev);
3524}
3525
David Ahern70af9212016-04-08 12:01:21 -07003526static bool addr_is_local(const struct in6_addr *addr)
3527{
3528 return ipv6_addr_type(addr) &
3529 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3530}
3531
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532static int addrconf_ifdown(struct net_device *dev, int how)
3533{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003534 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003535 struct inet6_dev *idev;
David Ahernf1705ec2016-02-24 09:25:37 -08003536 struct inet6_ifaddr *ifa, *tmp;
3537 struct list_head del_list;
3538 int _keep_addr;
3539 bool keep_addr;
David S. Miller73a8bd72011-01-23 23:27:15 -08003540 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
3542 ASSERT_RTNL();
3543
David S. Miller73a8bd72011-01-23 23:27:15 -08003544 rt6_ifdown(net, dev);
3545 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
3547 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003548 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 return -ENODEV;
3550
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003551 /*
3552 * Step 1: remove reference to ipv6 device from parent device.
3553 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003555 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003557
3558 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003559 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
3561 /* Step 1.5: remove snmp6 entry */
3562 snmp6_unregister_dev(idev);
3563
3564 }
3565
David Ahernf1705ec2016-02-24 09:25:37 -08003566 /* aggregate the system setting and interface setting */
3567 _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3568 if (!_keep_addr)
3569 _keep_addr = idev->cnf.keep_addr_on_down;
3570
3571 /* combine the user config with event to determine if permanent
3572 * addresses are to be removed from address hash table
3573 */
Mike Manningbc561632016-08-12 12:02:38 +01003574 keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003575
David S. Miller73a8bd72011-01-23 23:27:15 -08003576 /* Step 2: clear hash table */
3577 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3578 struct hlist_head *h = &inet6_addr_lst[i];
David S. Miller73a8bd72011-01-23 23:27:15 -08003579
3580 spin_lock_bh(&addrconf_hash_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01003581restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08003582 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
David S. Miller73a8bd72011-01-23 23:27:15 -08003583 if (ifa->idev == idev) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003584 addrconf_del_dad_work(ifa);
David Ahernf1705ec2016-02-24 09:25:37 -08003585 /* combined flag + permanent flag decide if
3586 * address is retained on a down event
3587 */
3588 if (!keep_addr ||
David Ahern70af9212016-04-08 12:01:21 -07003589 !(ifa->flags & IFA_F_PERMANENT) ||
3590 addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003591 hlist_del_init_rcu(&ifa->addr_lst);
3592 goto restart;
3593 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003594 }
3595 }
3596 spin_unlock_bh(&addrconf_hash_lock);
3597 }
3598
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 write_lock_bh(&idev->lock);
3600
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003601 addrconf_del_rs_timer(idev);
3602
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003603 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003604 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003605 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003607 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00003608 while (!list_empty(&idev->tempaddr_list)) {
3609 ifa = list_first_entry(&idev->tempaddr_list,
3610 struct inet6_ifaddr, tmp_list);
3611 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 write_unlock_bh(&idev->lock);
3613 spin_lock_bh(&ifa->lock);
3614
3615 if (ifa->ifpub) {
3616 in6_ifa_put(ifa->ifpub);
3617 ifa->ifpub = NULL;
3618 }
3619 spin_unlock_bh(&ifa->lock);
3620 in6_ifa_put(ifa);
3621 write_lock_bh(&idev->lock);
3622 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003623
David Ahernf1705ec2016-02-24 09:25:37 -08003624 /* re-combine the user config with event to determine if permanent
3625 * addresses are to be removed from the interface list
3626 */
Mike Manningbc561632016-08-12 12:02:38 +01003627 keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003628
3629 INIT_LIST_HEAD(&del_list);
3630 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
David Ahern38bd10c2016-04-21 20:56:12 -07003631 struct rt6_info *rt = NULL;
3632
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003633 addrconf_del_dad_work(ifa);
stephen hemminger84e8b802010-03-02 13:32:46 +00003634
David S. Miller73a8bd72011-01-23 23:27:15 -08003635 write_unlock_bh(&idev->lock);
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003636 spin_lock_bh(&ifa->lock);
David Ahernf1705ec2016-02-24 09:25:37 -08003637
David Ahern70af9212016-04-08 12:01:21 -07003638 if (keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3639 !addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003640 /* set state to skip the notifier below */
3641 state = INET6_IFADDR_STATE_DEAD;
3642 ifa->state = 0;
3643 if (!(ifa->flags & IFA_F_NODAD))
3644 ifa->flags |= IFA_F_TENTATIVE;
David Ahern38bd10c2016-04-21 20:56:12 -07003645
3646 rt = ifa->rt;
3647 ifa->rt = NULL;
David Ahernf1705ec2016-02-24 09:25:37 -08003648 } else {
3649 state = ifa->state;
3650 ifa->state = INET6_IFADDR_STATE_DEAD;
3651
Wei Yongjunc8822192016-07-28 16:19:03 +00003652 list_move(&ifa->if_list, &del_list);
David Ahernf1705ec2016-02-24 09:25:37 -08003653 }
3654
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003655 spin_unlock_bh(&ifa->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003656
David Ahern38bd10c2016-04-21 20:56:12 -07003657 if (rt)
3658 ip6_del_rt(rt);
3659
David S. Miller73a8bd72011-01-23 23:27:15 -08003660 if (state != INET6_IFADDR_STATE_DEAD) {
3661 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Cong Wangf88c91d2013-04-14 23:18:43 +08003662 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
Mike Manningea06f712016-07-22 18:32:11 +01003663 } else {
3664 if (idev->cnf.forwarding)
3665 addrconf_leave_anycast(ifa);
3666 addrconf_leave_solict(ifa->idev, &ifa->addr);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00003667 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003668
David S. Miller73a8bd72011-01-23 23:27:15 -08003669 write_lock_bh(&idev->lock);
3670 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003671
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 write_unlock_bh(&idev->lock);
3673
David Ahernf1705ec2016-02-24 09:25:37 -08003674 /* now clean up addresses to be removed */
3675 while (!list_empty(&del_list)) {
3676 ifa = list_first_entry(&del_list,
3677 struct inet6_ifaddr, if_list);
3678 list_del(&ifa->if_list);
3679
3680 in6_ifa_put(ifa);
3681 }
3682
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003683 /* Step 5: Discard anycast and multicast list */
3684 if (how) {
3685 ipv6_ac_destroy_dev(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 ipv6_mc_destroy_dev(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003687 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 ipv6_mc_down(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003692
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003693 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003694 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003695 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 neigh_parms_release(&nd_tbl, idev->nd_parms);
3697 neigh_ifdown(&nd_tbl, dev);
3698 in6_dev_put(idev);
3699 }
3700 return 0;
3701}
3702
3703static void addrconf_rs_timer(unsigned long data)
3704{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003705 struct inet6_dev *idev = (struct inet6_dev *)data;
Cong Wangcaf92bc2013-08-31 13:44:32 +08003706 struct net_device *dev = idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003707 struct in6_addr lladdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003709 write_lock(&idev->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00003710 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 goto out;
3712
Shmulik Ladkani9ba2add2012-12-02 01:44:53 +00003713 if (!ipv6_accept_ra(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003715
3716 /* Announcement received after solicitation was sent */
3717 if (idev->if_flags & IF_RA_RCVD)
3718 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003720 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
Cong Wangcaf92bc2013-08-31 13:44:32 +08003721 write_unlock(&idev->lock);
3722 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3723 ndisc_send_rs(dev, &lladdr,
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003724 &in6addr_linklocal_allrouters);
3725 else
Cong Wangcaf92bc2013-08-31 13:44:32 +08003726 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727
Cong Wangcaf92bc2013-08-31 13:44:32 +08003728 write_lock(&idev->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003729 idev->rs_interval = rfc3315_s14_backoff_update(
3730 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003731 /* The wait after the last probe can be shorter */
3732 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3733 idev->cnf.rtr_solicits) ?
3734 idev->cnf.rtr_solicit_delay :
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003735 idev->rs_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 /*
3738 * Note: we do not support deprecated "all on-link"
3739 * assumption any longer.
3740 */
Joe Perches91df42b2012-05-15 14:11:54 +00003741 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 }
3743
3744out:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003745 write_unlock(&idev->lock);
Cong Wangcaf92bc2013-08-31 13:44:32 +08003746put:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003747 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748}
3749
3750/*
3751 * Duplicate Address Detection
3752 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003753static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3754{
3755 unsigned long rand_num;
3756 struct inet6_dev *idev = ifp->idev;
3757
Neil Horman95c385b2007-04-25 17:08:10 -07003758 if (ifp->flags & IFA_F_OPTIMISTIC)
3759 rand_num = 0;
3760 else
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003761 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
Neil Horman95c385b2007-04-25 17:08:10 -07003762
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003763 ifp->dad_probes = idev->cnf.dad_transmits;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003764 addrconf_mod_dad_work(ifp, rand_num);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003765}
3766
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003767static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768{
3769 struct inet6_dev *idev = ifp->idev;
3770 struct net_device *dev = idev->dev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003771 bool bump_id, notify = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772
3773 addrconf_join_solict(dev, &ifp->addr);
3774
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003775 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
3777 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003778 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003779 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
3782 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09003783 idev->cnf.accept_dad < 1 ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003784 !(ifp->flags&IFA_F_TENTATIVE) ||
3785 ifp->flags & IFA_F_NODAD) {
Paolo Abeni764d3be2016-11-22 16:57:40 +01003786 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003787 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003788 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 read_unlock_bh(&idev->lock);
3790
Paolo Abeni764d3be2016-11-22 16:57:40 +01003791 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 return;
3793 }
3794
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003795 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003796 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003797 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003798 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003799 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003800 * - keep it tentative if it is a permanent address.
3801 * - otherwise, kill it.
3802 */
3803 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003804 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003805 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003806 }
Neil Horman95c385b2007-04-25 17:08:10 -07003807
3808 /*
3809 * Optimistic nodes can start receiving
3810 * Frames right away
3811 */
Erik Kline7fd25612014-10-28 18:11:14 +09003812 if (ifp->flags & IFA_F_OPTIMISTIC) {
Neil Horman95c385b2007-04-25 17:08:10 -07003813 ip6_ins_rt(ifp->rt);
Erik Kline7fd25612014-10-28 18:11:14 +09003814 if (ipv6_use_optimistic_addr(idev)) {
3815 /* Because optimistic nodes can use this address,
3816 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3817 */
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003818 notify = true;
Erik Kline7fd25612014-10-28 18:11:14 +09003819 }
3820 }
Neil Horman95c385b2007-04-25 17:08:10 -07003821
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003822 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003824 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 read_unlock_bh(&idev->lock);
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003826 if (notify)
3827 ipv6_ifa_notify(RTM_NEWADDR, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828}
3829
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003830static void addrconf_dad_start(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003832 bool begin_dad = false;
3833
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003834 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003835 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3836 ifp->state = INET6_IFADDR_STATE_PREDAD;
3837 begin_dad = true;
3838 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003839 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003840
3841 if (begin_dad)
3842 addrconf_mod_dad_work(ifp, 0);
3843}
3844
3845static void addrconf_dad_work(struct work_struct *w)
3846{
3847 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3848 struct inet6_ifaddr,
3849 dad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 struct inet6_dev *idev = ifp->idev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003851 bool bump_id, disable_ipv6 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 struct in6_addr mcaddr;
3853
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003854 enum {
3855 DAD_PROCESS,
3856 DAD_BEGIN,
3857 DAD_ABORT,
3858 } action = DAD_PROCESS;
3859
3860 rtnl_lock();
3861
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003862 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003863 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3864 action = DAD_BEGIN;
3865 ifp->state = INET6_IFADDR_STATE_DAD;
3866 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3867 action = DAD_ABORT;
3868 ifp->state = INET6_IFADDR_STATE_POSTDAD;
Mike Manning85b51b12016-08-18 14:39:40 +01003869
3870 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6 &&
3871 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
3872 struct in6_addr addr;
3873
3874 addr.s6_addr32[0] = htonl(0xfe800000);
3875 addr.s6_addr32[1] = 0;
3876
3877 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
3878 ipv6_addr_equal(&ifp->addr, &addr)) {
3879 /* DAD failed for link-local based on MAC */
3880 idev->cnf.disable_ipv6 = 1;
3881
3882 pr_info("%s: IPv6 being disabled!\n",
3883 ifp->idev->dev->name);
3884 disable_ipv6 = true;
3885 }
3886 }
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003887 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003888 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003889
3890 if (action == DAD_BEGIN) {
3891 addrconf_dad_begin(ifp);
3892 goto out;
3893 } else if (action == DAD_ABORT) {
Wei Yongjun751eb6b2016-09-05 16:06:31 +08003894 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003895 addrconf_dad_stop(ifp, 1);
Mike Manning85b51b12016-08-18 14:39:40 +01003896 if (disable_ipv6)
3897 addrconf_ifdown(idev->dev, 0);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003898 goto out;
3899 }
3900
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003901 if (!ifp->dad_probes && addrconf_dad_end(ifp))
Herbert Xuf2344a12010-05-18 15:55:27 -07003902 goto out;
3903
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003904 write_lock_bh(&idev->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003905 if (idev->dead || !(idev->if_flags & IF_READY)) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003906 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 goto out;
3908 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003909
3910 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003911 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3912 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003913 write_unlock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003914 goto out;
3915 }
3916
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003917 if (ifp->dad_probes == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 /*
3919 * DAD was successful
3920 */
3921
Paolo Abeni764d3be2016-11-22 16:57:40 +01003922 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003923 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003924 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003925 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
Paolo Abeni764d3be2016-11-22 16:57:40 +01003927 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928
3929 goto out;
3930 }
3931
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003932 ifp->dad_probes--;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003933 addrconf_mod_dad_work(ifp,
3934 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
stephen hemminger21809fa2010-02-08 19:48:52 +00003935 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003936 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
3938 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
Nicolas Dichtel304d8882015-11-27 18:17:05 +01003940 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941out:
3942 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003943 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944}
3945
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003946/* ifp->idev must be at least read locked */
3947static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3948{
3949 struct inet6_ifaddr *ifpiter;
3950 struct inet6_dev *idev = ifp->idev;
3951
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01003952 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3953 if (ifpiter->scope > IFA_LINK)
3954 break;
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003955 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3956 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3957 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
3958 IFA_F_PERMANENT)
3959 return false;
3960 }
3961 return true;
3962}
3963
Paolo Abeni764d3be2016-11-22 16:57:40 +01003964static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003966 struct net_device *dev = ifp->idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003967 struct in6_addr lladdr;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003968 bool send_rs, send_mld;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003969
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003970 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
3972 /*
3973 * Configure the address for reception. Now it is valid.
3974 */
3975
3976 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3977
Tore Anderson026359b2011-08-28 23:47:33 +00003978 /* If added prefix is link local and we are prepared to process
3979 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 */
3981
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003982 read_lock_bh(&ifp->idev->lock);
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003983 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003984 send_rs = send_mld &&
3985 ipv6_accept_ra(ifp->idev) &&
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003986 ifp->idev->cnf.rtr_solicits != 0 &&
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003987 (dev->flags&IFF_LOOPBACK) == 0;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003988 read_unlock_bh(&ifp->idev->lock);
3989
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003990 /* While dad is in progress mld report's source address is in6_addrany.
3991 * Resend with proper ll now.
3992 */
3993 if (send_mld)
3994 ipv6_mc_dad_complete(ifp->idev);
3995
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003996 if (send_rs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 /*
3998 * If a host as already performed a random delay
3999 * [...] as part of DAD [...] there is no need
4000 * to delay again before sending the first RS
4001 */
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004002 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004003 return;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004004 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004006 write_lock_bh(&ifp->idev->lock);
4007 spin_lock(&ifp->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004008 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4009 ifp->idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004010 ifp->idev->rs_probes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 ifp->idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004012 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004013 spin_unlock(&ifp->lock);
4014 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 }
Paolo Abeni764d3be2016-11-22 16:57:40 +01004016
4017 if (bump_id)
4018 rt_genid_bump_ipv6(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019}
4020
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004021static void addrconf_dad_run(struct inet6_dev *idev)
4022{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004023 struct inet6_ifaddr *ifp;
4024
4025 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00004026 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00004027 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07004028 if (ifp->flags & IFA_F_TENTATIVE &&
4029 ifp->state == INET6_IFADDR_STATE_DAD)
4030 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00004031 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004032 }
4033 read_unlock_bh(&idev->lock);
4034}
4035
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036#ifdef CONFIG_PROC_FS
4037struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08004038 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004040 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041};
4042
Mihai Maruseac1d578302012-01-03 23:31:35 +00004043static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044{
4045 struct inet6_ifaddr *ifa = NULL;
4046 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004047 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00004048 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049
Mihai Maruseac1d578302012-01-03 23:31:35 +00004050 /* initial bucket if pos is 0 */
4051 if (pos == 0) {
4052 state->bucket = 0;
4053 state->offset = 0;
4054 }
4055
4056 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08004057 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00004058 addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004059 if (!net_eq(dev_net(ifa->idev->dev), net))
4060 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004061 /* sync with offset */
4062 if (p < state->offset) {
4063 p++;
4064 continue;
4065 }
4066 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004067 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004068 }
4069
4070 /* prepare for next bucket */
4071 state->offset = 0;
4072 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004074 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075}
4076
stephen hemmingerc2e21292010-03-17 20:31:10 +00004077static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4078 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079{
4080 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004081 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
Sasha Levinb67bfe02013-02-27 17:06:00 -08004083 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004084 if (!net_eq(dev_net(ifa->idev->dev), net))
4085 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004086 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004087 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004088 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004089
4090 while (++state->bucket < IN6_ADDR_HSIZE) {
Mihai Maruseac1d578302012-01-03 23:31:35 +00004091 state->offset = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004092 hlist_for_each_entry_rcu_bh(ifa,
stephen hemmingerc2e21292010-03-17 20:31:10 +00004093 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004094 if (!net_eq(dev_net(ifa->idev->dev), net))
4095 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004096 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004097 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08004098 }
4099 }
4100
stephen hemmingerc2e21292010-03-17 20:31:10 +00004101 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102}
4103
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004105 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106{
stephen hemminger5c578ae2010-03-17 20:31:11 +00004107 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00004108 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109}
4110
4111static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4112{
4113 struct inet6_ifaddr *ifa;
4114
4115 ifa = if6_get_next(seq, v);
4116 ++*pos;
4117 return ifa;
4118}
4119
4120static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004121 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122{
stephen hemminger5c578ae2010-03-17 20:31:11 +00004123 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124}
4125
4126static int if6_seq_show(struct seq_file *seq, void *v)
4127{
4128 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Jiri Pirko971a3512013-12-10 13:56:29 +01004129 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07004130 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 ifp->idev->dev->ifindex,
4132 ifp->prefix_len,
4133 ifp->scope,
Jiri Pirko971a3512013-12-10 13:56:29 +01004134 (u8) ifp->flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 ifp->idev->dev->name);
4136 return 0;
4137}
4138
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004139static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 .start = if6_seq_start,
4141 .next = if6_seq_next,
4142 .show = if6_seq_show,
4143 .stop = if6_seq_stop,
4144};
4145
4146static int if6_seq_open(struct inode *inode, struct file *file)
4147{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004148 return seq_open_net(inode, file, &if6_seq_ops,
4149 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150}
4151
Arjan van de Ven9a321442007-02-12 00:55:35 -08004152static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 .owner = THIS_MODULE,
4154 .open = if6_seq_open,
4155 .read = seq_read,
4156 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004157 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158};
4159
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004160static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161{
Gao fengd4beaa62013-02-18 01:34:54 +00004162 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 return -ENOMEM;
4164 return 0;
4165}
4166
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004167static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08004168{
Gao fengece31ff2013-02-18 01:34:56 +00004169 remove_proc_entry("if_inet6", net->proc_net);
Daniel Lezcano3c400902008-01-10 22:42:49 -08004170}
4171
4172static struct pernet_operations if6_proc_net_ops = {
Ian Morris67ba4152014-08-24 21:53:10 +01004173 .init = if6_proc_net_init,
4174 .exit = if6_proc_net_exit,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004175};
4176
4177int __init if6_proc_init(void)
4178{
4179 return register_pernet_subsys(&if6_proc_net_ops);
4180}
4181
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182void if6_proc_exit(void)
4183{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004184 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185}
4186#endif /* CONFIG_PROC_FS */
4187
Amerigo Wang07a93622012-10-29 16:23:10 +00004188#if IS_ENABLED(CONFIG_IPV6_MIP6)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004189/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004190int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004191{
4192 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004193 struct inet6_ifaddr *ifp = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00004194 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004195
stephen hemminger5c578ae2010-03-17 20:31:11 +00004196 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08004197 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09004198 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08004199 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09004200 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004201 (ifp->flags & IFA_F_HOMEADDRESS)) {
4202 ret = 1;
4203 break;
4204 }
4205 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00004206 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004207 return ret;
4208}
4209#endif
4210
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211/*
4212 * Periodic address status verification
4213 */
4214
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004215static void addrconf_verify_rtnl(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004217 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 int i;
4220
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004221 ASSERT_RTNL();
4222
stephen hemminger5c578ae2010-03-17 20:31:11 +00004223 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004225 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004227 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004229 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230restart:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004231 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233
Yasushi Asanofad8da32013-12-31 12:04:19 +09004234 /* When setting preferred_lft to a value not zero or
4235 * infinity, while valid_lft is infinity
4236 * IFA_F_PERMANENT has a non-infinity life time.
4237 */
4238 if ((ifp->flags & IFA_F_PERMANENT) &&
4239 (ifp->prefered_lft == INFINITY_LIFE_TIME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 continue;
4241
4242 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004243 /* We try to batch several events at once. */
4244 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004246 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4247 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 spin_unlock(&ifp->lock);
4249 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 ipv6_del_addr(ifp);
4251 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004252 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4253 spin_unlock(&ifp->lock);
4254 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00004256 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 int deprecate = 0;
4258
4259 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4260 deprecate = 1;
4261 ifp->flags |= IFA_F_DEPRECATED;
4262 }
4263
Yasushi Asanofad8da32013-12-31 12:04:19 +09004264 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4265 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 next = ifp->tstamp + ifp->valid_lft * HZ;
4267
4268 spin_unlock(&ifp->lock);
4269
4270 if (deprecate) {
4271 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
4273 ipv6_ifa_notify(0, ifp);
4274 in6_ifa_put(ifp);
4275 goto restart;
4276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4278 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00004279 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4280 ifp->idev->cnf.dad_transmits *
Jiri Pirko1f9248e52013-12-07 19:26:53 +01004281 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
stephen hemminger88949cf2010-03-17 20:31:17 +00004282
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 if (age >= ifp->prefered_lft - regen_advance) {
4284 struct inet6_ifaddr *ifpub = ifp->ifpub;
4285 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4286 next = ifp->tstamp + ifp->prefered_lft * HZ;
4287 if (!ifp->regen_count && ifpub) {
4288 ifp->regen_count++;
4289 in6_ifa_hold(ifp);
4290 in6_ifa_hold(ifpub);
4291 spin_unlock(&ifp->lock);
stephen hemminger5c578ae2010-03-17 20:31:11 +00004292
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08004293 spin_lock(&ifpub->lock);
4294 ifpub->regen_count = 0;
4295 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 ipv6_create_tempaddr(ifpub, ifp);
4297 in6_ifa_put(ifpub);
4298 in6_ifa_put(ifp);
4299 goto restart;
4300 }
4301 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4302 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4303 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 } else {
4305 /* ifp->prefered_lft <= ifp->valid_lft */
4306 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4307 next = ifp->tstamp + ifp->prefered_lft * HZ;
4308 spin_unlock(&ifp->lock);
4309 }
4310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 }
4312
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004313 next_sec = round_jiffies_up(next);
4314 next_sched = next;
4315
4316 /* If rounded timeout is accurate enough, accept it. */
4317 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4318 next_sched = next_sec;
4319
4320 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4321 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4322 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4323
dingtianhongba3542e2013-08-17 10:27:04 +08004324 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4325 now, next, next_sec, next_sched);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004326 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
stephen hemminger5c578ae2010-03-17 20:31:11 +00004327 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328}
4329
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004330static void addrconf_verify_work(struct work_struct *w)
4331{
4332 rtnl_lock();
4333 addrconf_verify_rtnl();
4334 rtnl_unlock();
4335}
4336
4337static void addrconf_verify(void)
4338{
4339 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4340}
4341
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004342static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4343 struct in6_addr **peer_pfx)
Thomas Graf461d8832006-09-18 00:09:49 -07004344{
4345 struct in6_addr *pfx = NULL;
4346
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004347 *peer_pfx = NULL;
4348
Thomas Graf461d8832006-09-18 00:09:49 -07004349 if (addr)
4350 pfx = nla_data(addr);
4351
4352 if (local) {
4353 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004354 *peer_pfx = pfx;
4355 pfx = nla_data(local);
Thomas Graf461d8832006-09-18 00:09:49 -07004356 }
4357
4358 return pfx;
4359}
4360
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004361static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07004362 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4363 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4364 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
Jiri Pirko479840f2013-12-06 09:45:21 +01004365 [IFA_FLAGS] = { .len = sizeof(u32) },
Thomas Graf461d8832006-09-18 00:09:49 -07004366};
4367
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368static int
Thomas Graf661d2962013-03-21 07:45:29 +00004369inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004371 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07004372 struct ifaddrmsg *ifm;
4373 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004374 struct in6_addr *pfx, *peer_pfx;
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004375 u32 ifa_flags;
Thomas Grafb933f712006-09-18 00:10:19 -07004376 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
Thomas Grafb933f712006-09-18 00:10:19 -07004378 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4379 if (err < 0)
4380 return err;
4381
4382 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004383 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004384 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 return -EINVAL;
4386
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004387 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4388
4389 /* We ignore other flags so far. */
4390 ifa_flags &= IFA_F_MANAGETEMPADDR;
4391
4392 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4393 ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394}
4395
Jiri Pirko479840f2013-12-06 09:45:21 +01004396static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004397 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004398{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004399 u32 flags;
4400 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004401 unsigned long timeout;
Jiri Pirko53bd6742013-12-06 09:45:22 +01004402 bool was_managetempaddr;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004403 bool had_prefixroute;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09004404
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004405 ASSERT_RTNL();
4406
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004407 if (!valid_lft || (prefered_lft > valid_lft))
4408 return -EINVAL;
4409
Jiri Pirko53bd6742013-12-06 09:45:22 +01004410 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4411 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4412 return -EINVAL;
4413
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004414 timeout = addrconf_timeout_fixup(valid_lft, HZ);
4415 if (addrconf_finite_timeout(timeout)) {
4416 expires = jiffies_to_clock_t(timeout * HZ);
4417 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004418 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004419 } else {
4420 expires = 0;
4421 flags = 0;
4422 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004423 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004424
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004425 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4426 if (addrconf_finite_timeout(timeout)) {
4427 if (timeout == 0)
4428 ifa_flags |= IFA_F_DEPRECATED;
4429 prefered_lft = timeout;
4430 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004431
4432 spin_lock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004433 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004434 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4435 !(ifp->flags & IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004436 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
Thomas Haller761aac72014-01-15 15:36:58 +01004437 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4438 IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004439 ifp->flags |= ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004440 ifp->tstamp = jiffies;
4441 ifp->valid_lft = valid_lft;
4442 ifp->prefered_lft = prefered_lft;
4443
4444 spin_unlock_bh(&ifp->lock);
4445 if (!(ifp->flags&IFA_F_TENTATIVE))
4446 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004447
Thomas Haller761aac72014-01-15 15:36:58 +01004448 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4449 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4450 expires, flags);
Thomas Haller5b84efe2014-01-15 15:36:59 +01004451 } else if (had_prefixroute) {
4452 enum cleanup_prefix_rt_t action;
4453 unsigned long rt_expires;
4454
4455 write_lock_bh(&ifp->idev->lock);
4456 action = check_cleanup_prefix_route(ifp, &rt_expires);
4457 write_unlock_bh(&ifp->idev->lock);
4458
4459 if (action != CLEANUP_PREFIX_RT_NOP) {
4460 cleanup_prefix_route(ifp, rt_expires,
4461 action == CLEANUP_PREFIX_RT_DEL);
4462 }
Thomas Haller761aac72014-01-15 15:36:58 +01004463 }
Jiri Pirko53bd6742013-12-06 09:45:22 +01004464
4465 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4466 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4467 valid_lft = prefered_lft = 0;
4468 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4469 !was_managetempaddr, jiffies);
4470 }
4471
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004472 addrconf_verify_rtnl();
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004473
4474 return 0;
4475}
4476
4477static int
Thomas Graf661d2962013-03-21 07:45:29 +00004478inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004480 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07004481 struct ifaddrmsg *ifm;
4482 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004483 struct in6_addr *pfx, *peer_pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07004484 struct inet6_ifaddr *ifa;
4485 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004486 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
Jiri Pirko479840f2013-12-06 09:45:21 +01004487 u32 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07004488 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489
Thomas Graf461d8832006-09-18 00:09:49 -07004490 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4491 if (err < 0)
4492 return err;
4493
4494 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004495 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004496 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 return -EINVAL;
4498
Thomas Graf461d8832006-09-18 00:09:49 -07004499 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004500 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07004501
4502 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004503 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07004504 preferred_lft = ci->ifa_prefered;
4505 } else {
4506 preferred_lft = INFINITY_LIFE_TIME;
4507 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004508 }
4509
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004510 dev = __dev_get_by_index(net, ifm->ifa_index);
Ian Morris63159f22015-03-29 14:00:04 +01004511 if (!dev)
Thomas Graf7198f8c2006-09-18 00:13:46 -07004512 return -ENODEV;
4513
Jiri Pirko479840f2013-12-06 09:45:21 +01004514 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4515
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004516 /* We ignore other flags so far. */
Thomas Haller761aac72014-01-15 15:36:58 +01004517 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
Madhu Challa93a714d2015-02-25 09:58:35 -08004518 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004519
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08004520 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Ian Morris63159f22015-03-29 14:00:04 +01004521 if (!ifa) {
Thomas Graf7198f8c2006-09-18 00:13:46 -07004522 /*
4523 * It would be best to check for !NLM_F_CREATE here but
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08004524 * userspace already relies on not having to provide this.
Thomas Graf7198f8c2006-09-18 00:13:46 -07004525 */
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004526 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004527 ifm->ifa_prefixlen, ifa_flags,
4528 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004529 }
4530
Thomas Graf7198f8c2006-09-18 00:13:46 -07004531 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4532 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4533 err = -EEXIST;
4534 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004535 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07004536
4537 in6_ifa_put(ifa);
4538
4539 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540}
4541
Jiri Pirko479840f2013-12-06 09:45:21 +01004542static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
Thomas Graf101bb222006-09-18 00:11:52 -07004543 u8 scope, int ifindex)
4544{
4545 struct ifaddrmsg *ifm;
4546
4547 ifm = nlmsg_data(nlh);
4548 ifm->ifa_family = AF_INET6;
4549 ifm->ifa_prefixlen = prefixlen;
4550 ifm->ifa_flags = flags;
4551 ifm->ifa_scope = scope;
4552 ifm->ifa_index = ifindex;
4553}
4554
Thomas Graf85486af2006-09-18 00:11:24 -07004555static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4556 unsigned long tstamp, u32 preferred, u32 valid)
4557{
4558 struct ifa_cacheinfo ci;
4559
Thomas Graf18a31e12010-11-17 04:12:02 +00004560 ci.cstamp = cstamp_delta(cstamp);
4561 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07004562 ci.ifa_prefered = preferred;
4563 ci.ifa_valid = valid;
4564
4565 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4566}
4567
Thomas Graf101bb222006-09-18 00:11:52 -07004568static inline int rt_scope(int ifa_scope)
4569{
4570 if (ifa_scope & IFA_HOST)
4571 return RT_SCOPE_HOST;
4572 else if (ifa_scope & IFA_LINK)
4573 return RT_SCOPE_LINK;
4574 else if (ifa_scope & IFA_SITE)
4575 return RT_SCOPE_SITE;
4576 else
4577 return RT_SCOPE_UNIVERSE;
4578}
4579
Thomas Graf0ab68032006-09-18 00:12:35 -07004580static inline int inet6_ifaddr_msgsize(void)
4581{
Thomas Graf339bf982006-11-10 14:10:15 -08004582 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004583 + nla_total_size(16) /* IFA_LOCAL */
Thomas Graf339bf982006-11-10 14:10:15 -08004584 + nla_total_size(16) /* IFA_ADDRESS */
Jiri Pirko479840f2013-12-06 09:45:21 +01004585 + nla_total_size(sizeof(struct ifa_cacheinfo))
4586 + nla_total_size(4) /* IFA_FLAGS */;
Thomas Graf0ab68032006-09-18 00:12:35 -07004587}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004588
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004590 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07004593 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
Eric W. Biederman15e47302012-09-07 20:12:54 +00004595 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004596 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004597 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004598
Thomas Graf101bb222006-09-18 00:11:52 -07004599 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4600 ifa->idev->dev->ifindex);
4601
Yasushi Asanofad8da32013-12-31 12:04:19 +09004602 if (!((ifa->flags&IFA_F_PERMANENT) &&
4603 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
Thomas Graf85486af2006-09-18 00:11:24 -07004604 preferred = ifa->prefered_lft;
4605 valid = ifa->valid_lft;
4606 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00004608 if (preferred > tval)
4609 preferred -= tval;
4610 else
4611 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00004612 if (valid != INFINITY_LIFE_TIME) {
4613 if (valid > tval)
4614 valid -= tval;
4615 else
4616 valid = 0;
4617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 }
4619 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07004620 preferred = INFINITY_LIFE_TIME;
4621 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 }
Thomas Graf85486af2006-09-18 00:11:24 -07004623
Cong Wang7996c792013-05-22 05:41:06 +00004624 if (!ipv6_addr_any(&ifa->peer_addr)) {
Jiri Benc930345e2015-03-29 16:59:25 +02004625 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4626 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004627 goto error;
4628 } else
Jiri Benc930345e2015-03-29 16:59:25 +02004629 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004630 goto error;
4631
4632 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4633 goto error;
Thomas Graf85486af2006-09-18 00:11:24 -07004634
Jiri Pirko479840f2013-12-06 09:45:21 +01004635 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4636 goto error;
4637
Johannes Berg053c0952015-01-16 22:09:00 +01004638 nlmsg_end(skb, nlh);
4639 return 0;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004640
4641error:
4642 nlmsg_cancel(skb, nlh);
4643 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644}
4645
4646static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004647 u32 portid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004650 u8 scope = RT_SCOPE_UNIVERSE;
4651 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652
Thomas Graf101bb222006-09-18 00:11:52 -07004653 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4654 scope = RT_SCOPE_SITE;
4655
Eric W. Biederman15e47302012-09-07 20:12:54 +00004656 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004657 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004658 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004659
Thomas Graf101bb222006-09-18 00:11:52 -07004660 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004661 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004662 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004663 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4664 nlmsg_cancel(skb, nlh);
4665 return -EMSGSIZE;
4666 }
Thomas Graf85486af2006-09-18 00:11:24 -07004667
Johannes Berg053c0952015-01-16 22:09:00 +01004668 nlmsg_end(skb, nlh);
4669 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670}
4671
4672static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004673 u32 portid, u32 seq, int event, unsigned int 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 = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
Thomas Graf101bb222006-09-18 00:11:52 -07004679 if (ipv6_addr_scope(&ifaca->aca_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_ANYCAST, &ifaca->aca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004688 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_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
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004698enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 UNICAST_ADDR,
4700 MULTICAST_ADDR,
4701 ANYCAST_ADDR,
4702};
4703
Eric Dumazet234b27c2009-11-12 04:11:50 +00004704/* called with rcu_read_lock() */
4705static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4706 struct netlink_callback *cb, enum addr_type_t type,
4707 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 struct ifmcaddr6 *ifmca;
4710 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004711 int err = 1;
4712 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
Eric Dumazet234b27c2009-11-12 04:11:50 +00004714 read_lock_bh(&idev->lock);
4715 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00004716 case UNICAST_ADDR: {
4717 struct inet6_ifaddr *ifa;
4718
Eric Dumazet234b27c2009-11-12 04:11:50 +00004719 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00004720 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4721 if (++ip_idx < s_ip_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004722 continue;
4723 err = inet6_fill_ifaddr(skb, ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004724 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004725 cb->nlh->nlmsg_seq,
4726 RTM_NEWADDR,
4727 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004728 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004729 break;
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004730 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004732 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00004733 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004734 case MULTICAST_ADDR:
4735 /* multicast address */
4736 for (ifmca = idev->mc_list; ifmca;
4737 ifmca = ifmca->next, ip_idx++) {
4738 if (ip_idx < s_ip_idx)
4739 continue;
4740 err = inet6_fill_ifmcaddr(skb, ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004741 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004742 cb->nlh->nlmsg_seq,
4743 RTM_GETMULTICAST,
4744 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004745 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004746 break;
4747 }
4748 break;
4749 case ANYCAST_ADDR:
4750 /* anycast address */
4751 for (ifaca = idev->ac_list; ifaca;
4752 ifaca = ifaca->aca_next, ip_idx++) {
4753 if (ip_idx < s_ip_idx)
4754 continue;
4755 err = inet6_fill_ifacaddr(skb, ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004756 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004757 cb->nlh->nlmsg_seq,
4758 RTM_GETANYCAST,
4759 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004760 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004761 break;
4762 }
4763 break;
4764 default:
4765 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004767 read_unlock_bh(&idev->lock);
4768 *p_ip_idx = ip_idx;
4769 return err;
4770}
4771
4772static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4773 enum addr_type_t type)
4774{
4775 struct net *net = sock_net(skb->sk);
4776 int h, s_h;
4777 int idx, ip_idx;
4778 int s_idx, s_ip_idx;
4779 struct net_device *dev;
4780 struct inet6_dev *idev;
4781 struct hlist_head *head;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004782
4783 s_h = cb->args[0];
4784 s_idx = idx = cb->args[1];
4785 s_ip_idx = ip_idx = cb->args[2];
4786
4787 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004788 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004789 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4790 idx = 0;
4791 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08004792 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet234b27c2009-11-12 04:11:50 +00004793 if (idx < s_idx)
4794 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07004795 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004796 s_ip_idx = 0;
4797 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004798 idev = __in6_dev_get(dev);
4799 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004800 goto cont;
4801
4802 if (in6_dump_addrs(idev, skb, cb, type,
David S. Miller7b46a642015-01-18 23:36:08 -05004803 s_ip_idx, &ip_idx) < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004804 goto done;
4805cont:
4806 idx++;
4807 }
4808 }
4809done:
4810 rcu_read_unlock();
4811 cb->args[0] = h;
4812 cb->args[1] = idx;
4813 cb->args[2] = ip_idx;
4814
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 return skb->len;
4816}
4817
4818static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4819{
4820 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004821
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 return inet6_dump_addr(skb, cb, type);
4823}
4824
4825static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4826{
4827 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004828
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 return inet6_dump_addr(skb, cb, type);
4830}
4831
4832
4833static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4834{
4835 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004836
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 return inet6_dump_addr(skb, cb, type);
4838}
4839
Thomas Graf661d2962013-03-21 07:45:29 +00004840static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004841{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004842 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004843 struct ifaddrmsg *ifm;
4844 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004845 struct in6_addr *addr = NULL, *peer;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004846 struct net_device *dev = NULL;
4847 struct inet6_ifaddr *ifa;
4848 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004849 int err;
4850
Thomas Graf1b29fc22006-09-18 00:10:50 -07004851 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4852 if (err < 0)
4853 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004854
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004855 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
Ian Morris63159f22015-03-29 14:00:04 +01004856 if (!addr) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004857 err = -EINVAL;
4858 goto errout;
4859 }
4860
4861 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004862 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004863 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004864
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004865 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4866 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004867 err = -EADDRNOTAVAIL;
4868 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004869 }
4870
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004871 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4872 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004873 err = -ENOBUFS;
4874 goto errout_ifa;
4875 }
4876
Eric W. Biederman15e47302012-09-07 20:12:54 +00004877 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004878 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004879 if (err < 0) {
4880 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4881 WARN_ON(err == -EMSGSIZE);
4882 kfree_skb(skb);
4883 goto errout_ifa;
4884 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00004885 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004886errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004887 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004888errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004889 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004890}
4891
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4893{
4894 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004895 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07004896 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
Thomas Graf0ab68032006-09-18 00:12:35 -07004898 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01004899 if (!skb)
Thomas Graf5d620262006-08-15 00:35:02 -07004900 goto errout;
4901
4902 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004903 if (err < 0) {
4904 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4905 WARN_ON(err == -EMSGSIZE);
4906 kfree_skb(skb);
4907 goto errout;
4908 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004909 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4910 return;
Thomas Graf5d620262006-08-15 00:35:02 -07004911errout:
4912 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004913 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914}
4915
Dave Jonesb6f99a22007-03-22 12:27:49 -07004916static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 __s32 *array, int bytes)
4918{
Thomas Graf04561c12006-11-14 19:53:58 -08004919 BUG_ON(bytes < (DEVCONF_MAX * 4));
4920
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 memset(array, 0, bytes);
4922 array[DEVCONF_FORWARDING] = cnf->forwarding;
4923 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4924 array[DEVCONF_MTU6] = cnf->mtu6;
4925 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4926 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4927 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4928 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4929 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00004930 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4931 jiffies_to_msecs(cnf->rtr_solicit_interval);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004932 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
4933 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
Thomas Graf93908d12010-11-17 01:44:24 +00004934 array[DEVCONF_RTR_SOLICIT_DELAY] =
4935 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02004937 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4938 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4939 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4940 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4942 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4943 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4944 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4945 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004947 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
Hangbin Liu8013d1d2015-07-30 14:28:42 +08004948 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004949 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004950#ifdef CONFIG_IPV6_ROUTER_PREF
4951 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00004952 array[DEVCONF_RTR_PROBE_INTERVAL] =
4953 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08004954#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004955 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4956#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004957#endif
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +09004958 array[DEVCONF_ACCEPT_RA_RT_TABLE] = cnf->accept_ra_rt_table;
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004959 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004960 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07004961#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4962 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
Erik Kline7fd25612014-10-28 18:11:14 +09004963 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
Neil Horman95c385b2007-04-25 17:08:10 -07004964#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004965#ifdef CONFIG_IPV6_MROUTE
4966 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4967#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09004968 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09004969 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00004970 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00004971 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02004972 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
Ben Greeard9333192014-06-25 14:44:53 -07004973 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
Harout Hedeshianc2943f12015-01-20 10:06:05 -07004974 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
Andy Gospodarek35103d12015-08-13 10:39:01 -04004975 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01004976 /* we omit DEVCONF_STABLE_SECRET for now */
Erik Kline3985e8a2015-07-22 16:38:25 +09004977 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
Johannes Bergabbc3042016-02-04 13:31:19 +01004978 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
Johannes Berg7a02bf82016-02-04 13:31:20 +01004979 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
David Ahernf1705ec2016-02-24 09:25:37 -08004980 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981}
4982
Thomas Grafb382b192010-11-16 04:33:57 +00004983static inline size_t inet6_ifla6_size(void)
4984{
4985 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4986 + nla_total_size(sizeof(struct ifla_cacheinfo))
4987 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4988 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004989 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4990 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
Thomas Grafb382b192010-11-16 04:33:57 +00004991}
4992
Thomas Graf339bf982006-11-10 14:10:15 -08004993static inline size_t inet6_if_nlmsg_size(void)
4994{
4995 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4996 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4997 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4998 + nla_total_size(4) /* IFLA_MTU */
4999 + nla_total_size(4) /* IFLA_LINK */
Andy Gospodarek03443382015-08-13 15:26:35 -04005000 + nla_total_size(1) /* IFLA_OPERSTATE */
Thomas Grafb382b192010-11-16 04:33:57 +00005001 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08005002}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005003
Eric Dumazetbe281e52011-05-19 01:14:23 +00005004static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Jia Heaca05672016-09-30 11:29:03 +08005005 int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005006{
5007 int i;
Jia Heaca05672016-09-30 11:29:03 +08005008 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005009 BUG_ON(pad < 0);
5010
5011 /* Use put_unaligned() because stats may not be aligned for u64. */
Jia Heaca05672016-09-30 11:29:03 +08005012 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5013 for (i = 1; i < ICMP6_MIB_MAX; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00005014 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005015
Jia Heaca05672016-09-30 11:29:03 +08005016 memset(&stats[ICMP6_MIB_MAX], 0, pad);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005017}
5018
WANG Cong698365f2014-05-05 15:55:55 -07005019static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305020 int bytes, size_t syncpoff)
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005021{
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305022 int i, c;
5023 u64 buff[IPSTATS_MIB_MAX];
5024 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5025
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005026 BUG_ON(pad < 0);
5027
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305028 memset(buff, 0, sizeof(buff));
5029 buff[0] = IPSTATS_MIB_MAX;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005030
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305031 for_each_possible_cpu(c) {
5032 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5033 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5034 }
5035
5036 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5037 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005038}
5039
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005040static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5041 int bytes)
5042{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005043 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005044 case IFLA_INET6_STATS:
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305045 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5046 offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005047 break;
5048 case IFLA_INET6_ICMP6STATS:
Jia Heaca05672016-09-30 11:29:03 +08005049 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005050 break;
5051 }
5052}
5053
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005054static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5055 u32 ext_filter_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005057 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08005058 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059
David S. Millerc78679e2012-04-01 20:27:33 -04005060 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5061 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08005063 ci.tstamp = cstamp_delta(idev->tstamp);
5064 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
Jiri Pirko1f9248e52013-12-07 19:26:53 +01005065 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
David S. Millerc78679e2012-04-01 20:27:33 -04005066 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5067 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005068 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
Ian Morris63159f22015-03-29 14:00:04 +01005069 if (!nla)
Thomas Graf04561c12006-11-14 19:53:58 -08005070 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005071 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005073 /* XXX - MC not implemented */
5074
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005075 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5076 return 0;
5077
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005078 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005079 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005080 goto nla_put_failure;
5081 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5082
5083 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005084 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005085 goto nla_put_failure;
5086 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005088 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
Ian Morris63159f22015-03-29 14:00:04 +01005089 if (!nla)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005090 goto nla_put_failure;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005091
5092 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode))
5093 goto nla_put_failure;
5094
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005095 read_lock_bh(&idev->lock);
5096 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5097 read_unlock_bh(&idev->lock);
5098
Thomas Grafb382b192010-11-16 04:33:57 +00005099 return 0;
5100
5101nla_put_failure:
5102 return -EMSGSIZE;
5103}
5104
Arad, Ronenb1974ed2015-10-19 09:23:28 -07005105static size_t inet6_get_link_af_size(const struct net_device *dev,
5106 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005107{
5108 if (!__in6_dev_get(dev))
5109 return 0;
5110
5111 return inet6_ifla6_size();
5112}
5113
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005114static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5115 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005116{
5117 struct inet6_dev *idev = __in6_dev_get(dev);
5118
5119 if (!idev)
5120 return -ENODATA;
5121
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005122 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005123 return -EMSGSIZE;
5124
5125 return 0;
5126}
5127
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005128static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5129{
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005130 struct inet6_ifaddr *ifp;
5131 struct net_device *dev = idev->dev;
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005132 bool clear_token, update_rs = false;
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005133 struct in6_addr ll_addr;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005134
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005135 ASSERT_RTNL();
5136
Ian Morris63159f22015-03-29 14:00:04 +01005137 if (!token)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005138 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005139 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5140 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005141 if (!ipv6_accept_ra(idev))
5142 return -EINVAL;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005143 if (idev->cnf.rtr_solicits == 0)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005144 return -EINVAL;
5145
5146 write_lock_bh(&idev->lock);
5147
5148 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5149 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5150
5151 write_unlock_bh(&idev->lock);
5152
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005153 clear_token = ipv6_addr_any(token);
5154 if (clear_token)
5155 goto update_lft;
5156
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005157 if (!idev->dead && (idev->if_flags & IF_READY) &&
5158 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5159 IFA_F_OPTIMISTIC)) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005160 /* If we're not ready, then normal ifup will take care
5161 * of this. Otherwise, we need to request our rs here.
5162 */
5163 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5164 update_rs = true;
5165 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005166
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005167update_lft:
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005168 write_lock_bh(&idev->lock);
Daniel Borkmannfc403832013-04-09 03:47:15 +00005169
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005170 if (update_rs) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005171 idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005172 idev->rs_interval = rfc3315_s14_backoff_init(
5173 idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005174 idev->rs_probes = 1;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005175 addrconf_mod_rs_timer(idev, idev->rs_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005176 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005177
5178 /* Well, that's kinda nasty ... */
5179 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5180 spin_lock(&ifp->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00005181 if (ifp->tokenized) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005182 ifp->valid_lft = 0;
5183 ifp->prefered_lft = 0;
5184 }
5185 spin_unlock(&ifp->lock);
5186 }
5187
5188 write_unlock_bh(&idev->lock);
Lubomir Rintelb2ed64a2014-10-27 17:39:16 +01005189 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005190 addrconf_verify_rtnl();
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005191 return 0;
5192}
5193
Daniel Borkmann11b1f822015-02-05 14:39:11 +01005194static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5195 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5196 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5197};
5198
5199static int inet6_validate_link_af(const struct net_device *dev,
5200 const struct nlattr *nla)
5201{
5202 struct nlattr *tb[IFLA_INET6_MAX + 1];
5203
5204 if (dev && !__in6_dev_get(dev))
5205 return -EAFNOSUPPORT;
5206
5207 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
5208}
5209
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005210static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5211{
5212 int err = -EINVAL;
5213 struct inet6_dev *idev = __in6_dev_get(dev);
5214 struct nlattr *tb[IFLA_INET6_MAX + 1];
5215
5216 if (!idev)
5217 return -EAFNOSUPPORT;
5218
5219 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
5220 BUG();
5221
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005222 if (tb[IFLA_INET6_TOKEN]) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005223 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005224 if (err)
5225 return err;
5226 }
5227
5228 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5229 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5230
5231 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005232 mode != IN6_ADDR_GEN_MODE_NONE &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01005233 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5234 mode != IN6_ADDR_GEN_MODE_RANDOM)
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005235 return -EINVAL;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005236
5237 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5238 !idev->cnf.stable_secret.initialized &&
5239 !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized)
5240 return -EINVAL;
5241
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005242 idev->addr_gen_mode = mode;
5243 err = 0;
5244 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005245
5246 return err;
5247}
5248
Thomas Grafb382b192010-11-16 04:33:57 +00005249static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005250 u32 portid, u32 seq, int event, unsigned int flags)
Thomas Grafb382b192010-11-16 04:33:57 +00005251{
5252 struct net_device *dev = idev->dev;
5253 struct ifinfomsg *hdr;
5254 struct nlmsghdr *nlh;
5255 void *protoinfo;
5256
Eric W. Biederman15e47302012-09-07 20:12:54 +00005257 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005258 if (!nlh)
Thomas Grafb382b192010-11-16 04:33:57 +00005259 return -EMSGSIZE;
5260
5261 hdr = nlmsg_data(nlh);
5262 hdr->ifi_family = AF_INET6;
5263 hdr->__ifi_pad = 0;
5264 hdr->ifi_type = dev->type;
5265 hdr->ifi_index = dev->ifindex;
5266 hdr->ifi_flags = dev_get_flags(dev);
5267 hdr->ifi_change = 0;
5268
David S. Millerc78679e2012-04-01 20:27:33 -04005269 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5270 (dev->addr_len &&
5271 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5272 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02005273 (dev->ifindex != dev_get_iflink(dev) &&
Andy Gospodarek03443382015-08-13 15:26:35 -04005274 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5275 nla_put_u8(skb, IFLA_OPERSTATE,
5276 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
David S. Millerc78679e2012-04-01 20:27:33 -04005277 goto nla_put_failure;
Thomas Grafb382b192010-11-16 04:33:57 +00005278 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
Ian Morris63159f22015-03-29 14:00:04 +01005279 if (!protoinfo)
Thomas Grafb382b192010-11-16 04:33:57 +00005280 goto nla_put_failure;
5281
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005282 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005283 goto nla_put_failure;
5284
Thomas Graf04561c12006-11-14 19:53:58 -08005285 nla_nest_end(skb, protoinfo);
Johannes Berg053c0952015-01-16 22:09:00 +01005286 nlmsg_end(skb, nlh);
5287 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288
Thomas Graf04561c12006-11-14 19:53:58 -08005289nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005290 nlmsg_cancel(skb, nlh);
5291 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292}
5293
5294static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5295{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005296 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00005297 int h, s_h;
David S. Miller434a8a582009-11-11 18:53:00 -08005298 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 struct net_device *dev;
5300 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00005301 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302
Eric Dumazet84d26972009-11-09 12:11:28 +00005303 s_h = cb->args[0];
5304 s_idx = cb->args[1];
5305
5306 rcu_read_lock();
5307 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5308 idx = 0;
5309 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08005310 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet84d26972009-11-09 12:11:28 +00005311 if (idx < s_idx)
5312 goto cont;
5313 idev = __in6_dev_get(dev);
5314 if (!idev)
5315 goto cont;
5316 if (inet6_fill_ifinfo(skb, idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005317 NETLINK_CB(cb->skb).portid,
Eric Dumazet84d26972009-11-09 12:11:28 +00005318 cb->nlh->nlmsg_seq,
Johannes Berg053c0952015-01-16 22:09:00 +01005319 RTM_NEWLINK, NLM_F_MULTI) < 0)
Eric Dumazet84d26972009-11-09 12:11:28 +00005320 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07005321cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00005322 idx++;
5323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 }
Eric Dumazet84d26972009-11-09 12:11:28 +00005325out:
5326 rcu_read_unlock();
5327 cb->args[1] = idx;
5328 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329
5330 return skb->len;
5331}
5332
5333void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5334{
5335 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005336 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005337 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005338
Thomas Graf339bf982006-11-10 14:10:15 -08005339 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005340 if (!skb)
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005341 goto errout;
5342
5343 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005344 if (err < 0) {
5345 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5346 WARN_ON(err == -EMSGSIZE);
5347 kfree_skb(skb);
5348 goto errout;
5349 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005350 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005351 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005352errout:
5353 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005354 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355}
5356
Thomas Graf339bf982006-11-10 14:10:15 -08005357static inline size_t inet6_prefix_nlmsg_size(void)
5358{
5359 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5360 + nla_total_size(sizeof(struct in6_addr))
5361 + nla_total_size(sizeof(struct prefix_cacheinfo));
5362}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005363
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005365 struct prefix_info *pinfo, u32 portid, u32 seq,
Thomas Graf6051e2f2006-11-14 19:54:19 -08005366 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367{
Thomas Graf6051e2f2006-11-14 19:54:19 -08005368 struct prefixmsg *pmsg;
5369 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 struct prefix_cacheinfo ci;
5371
Eric W. Biederman15e47302012-09-07 20:12:54 +00005372 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005373 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005374 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08005375
5376 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07005378 pmsg->prefix_pad1 = 0;
5379 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 pmsg->prefix_ifindex = idev->dev->ifindex;
5381 pmsg->prefix_len = pinfo->prefix_len;
5382 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07005383 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 pmsg->prefix_flags = 0;
5385 if (pinfo->onlink)
5386 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5387 if (pinfo->autoconf)
5388 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5389
David S. Millerc78679e2012-04-01 20:27:33 -04005390 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5391 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 ci.preferred_time = ntohl(pinfo->prefered);
5393 ci.valid_time = ntohl(pinfo->valid);
David S. Millerc78679e2012-04-01 20:27:33 -04005394 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5395 goto nla_put_failure;
Johannes Berg053c0952015-01-16 22:09:00 +01005396 nlmsg_end(skb, nlh);
5397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398
Thomas Graf6051e2f2006-11-14 19:54:19 -08005399nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005400 nlmsg_cancel(skb, nlh);
5401 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402}
5403
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005404static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 struct prefix_info *pinfo)
5406{
5407 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005408 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005409 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410
Thomas Graf339bf982006-11-10 14:10:15 -08005411 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005412 if (!skb)
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005413 goto errout;
5414
5415 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005416 if (err < 0) {
5417 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5418 WARN_ON(err == -EMSGSIZE);
5419 kfree_skb(skb);
5420 goto errout;
5421 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005422 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5423 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005424errout:
5425 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08005426 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427}
5428
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5430{
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005431 struct net *net = dev_net(ifp->idev->dev);
5432
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005433 if (event)
5434 ASSERT_RTNL();
5435
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5437
5438 switch (event) {
5439 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07005440 /*
5441 * If the address was optimistic
5442 * we inserted the route at the start of
5443 * our DAD process, so we don't need
5444 * to do it again
5445 */
5446 if (!(ifp->rt->rt6i_node))
5447 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 if (ifp->idev->cnf.forwarding)
5449 addrconf_join_anycast(ifp);
Cong Wang7996c792013-05-22 05:41:06 +00005450 if (!ipv6_addr_any(&ifp->peer_addr))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005451 addrconf_prefix_route(&ifp->peer_addr, 128,
5452 ifp->idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 break;
5454 case RTM_DELADDR:
5455 if (ifp->idev->cnf.forwarding)
5456 addrconf_leave_anycast(ifp);
5457 addrconf_leave_solict(ifp->idev, &ifp->addr);
Cong Wang7996c792013-05-22 05:41:06 +00005458 if (!ipv6_addr_any(&ifp->peer_addr)) {
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005459 struct rt6_info *rt;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005460
Nicolas Dichtele7478df2014-09-03 23:59:22 +02005461 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5462 ifp->idev->dev, 0, 0);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07005463 if (rt)
5464 ip6_del_rt(rt);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005465 }
David Ahern38bd10c2016-04-21 20:56:12 -07005466 if (ifp->rt) {
5467 dst_hold(&ifp->rt->dst);
5468 ip6_del_rt(ifp->rt);
5469 }
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02005470 rt_genid_bump_ipv6(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 break;
5472 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005473 atomic_inc(&net->ipv6.dev_addr_genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474}
5475
5476static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5477{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005478 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 if (likely(ifp->idev->dead == 0))
5480 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005481 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482}
5483
5484#ifdef CONFIG_SYSCTL
5485
5486static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005487int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 void __user *buffer, size_t *lenp, loff_t *ppos)
5489{
5490 int *valp = ctl->data;
5491 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005492 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005493 struct ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 int ret;
5495
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005496 /*
5497 * ctl->data points to idev->cnf.forwarding, we should
5498 * not modify it until we get the rtnl lock.
5499 */
5500 lctl = *ctl;
5501 lctl.data = &val;
5502
5503 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08005505 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00005506 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005507 if (ret)
5508 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005509 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510}
5511
Marcelo Leitner77751422015-02-23 11:17:13 -03005512static
5513int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5514 void __user *buffer, size_t *lenp, loff_t *ppos)
5515{
5516 struct inet6_dev *idev = ctl->extra1;
5517 int min_mtu = IPV6_MIN_MTU;
5518 struct ctl_table lctl;
5519
5520 lctl = *ctl;
5521 lctl.extra1 = &min_mtu;
5522 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5523
5524 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5525}
5526
Brian Haley56d417b2009-06-01 03:07:33 -07005527static void dev_disable_change(struct inet6_dev *idev)
5528{
Cong Wang75538c22013-05-29 11:30:50 +08005529 struct netdev_notifier_info info;
5530
Brian Haley56d417b2009-06-01 03:07:33 -07005531 if (!idev || !idev->dev)
5532 return;
5533
Cong Wang75538c22013-05-29 11:30:50 +08005534 netdev_notifier_info_init(&info, idev->dev);
Brian Haley56d417b2009-06-01 03:07:33 -07005535 if (idev->cnf.disable_ipv6)
Cong Wang75538c22013-05-29 11:30:50 +08005536 addrconf_notify(NULL, NETDEV_DOWN, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005537 else
Cong Wang75538c22013-05-29 11:30:50 +08005538 addrconf_notify(NULL, NETDEV_UP, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005539}
5540
5541static void addrconf_disable_change(struct net *net, __s32 newf)
5542{
5543 struct net_device *dev;
5544 struct inet6_dev *idev;
5545
Kefeng Wang79453ab2017-01-19 16:26:21 +08005546 for_each_netdev(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07005547 idev = __in6_dev_get(dev);
5548 if (idev) {
5549 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5550 idev->cnf.disable_ipv6 = newf;
5551 if (changed)
5552 dev_disable_change(idev);
5553 }
Brian Haley56d417b2009-06-01 03:07:33 -07005554 }
Brian Haley56d417b2009-06-01 03:07:33 -07005555}
5556
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005557static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07005558{
5559 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005560 int old;
5561
5562 if (!rtnl_trylock())
5563 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07005564
5565 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005566 old = *p;
5567 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07005568
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005569 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5570 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07005571 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005572 }
Brian Haley56d417b2009-06-01 03:07:33 -07005573
5574 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07005575 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5576 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005577 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07005578 dev_disable_change((struct inet6_dev *)table->extra1);
5579
5580 rtnl_unlock();
5581 return 0;
5582}
5583
5584static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005585int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07005586 void __user *buffer, size_t *lenp, loff_t *ppos)
5587{
5588 int *valp = ctl->data;
5589 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005590 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005591 struct ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07005592 int ret;
5593
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005594 /*
5595 * ctl->data points to idev->cnf.disable_ipv6, we should
5596 * not modify it until we get the rtnl lock.
5597 */
5598 lctl = *ctl;
5599 lctl.data = &val;
5600
5601 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07005602
5603 if (write)
5604 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005605 if (ret)
5606 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07005607 return ret;
5608}
5609
stephen hemmingerc92d5492013-12-17 22:37:14 -08005610static
5611int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5612 void __user *buffer, size_t *lenp, loff_t *ppos)
5613{
5614 int *valp = ctl->data;
5615 int ret;
5616 int old, new;
5617
5618 old = *valp;
5619 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5620 new = *valp;
5621
5622 if (write && old != new) {
5623 struct net *net = ctl->extra2;
5624
5625 if (!rtnl_trylock())
5626 return restart_syscall();
5627
5628 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5629 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5630 NETCONFA_IFINDEX_DEFAULT,
5631 net->ipv6.devconf_dflt);
5632 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5633 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5634 NETCONFA_IFINDEX_ALL,
5635 net->ipv6.devconf_all);
5636 else {
5637 struct inet6_dev *idev = ctl->extra1;
5638
5639 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5640 idev->dev->ifindex,
5641 &idev->cnf);
5642 }
5643 rtnl_unlock();
5644 }
5645
5646 return ret;
5647}
5648
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005649static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5650 void __user *buffer, size_t *lenp,
5651 loff_t *ppos)
5652{
5653 int err;
5654 struct in6_addr addr;
5655 char str[IPV6_MAX_STRLEN];
5656 struct ctl_table lctl = *ctl;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005657 struct net *net = ctl->extra2;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005658 struct ipv6_stable_secret *secret = ctl->data;
5659
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005660 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5661 return -EIO;
5662
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005663 lctl.maxlen = IPV6_MAX_STRLEN;
5664 lctl.data = str;
5665
5666 if (!rtnl_trylock())
5667 return restart_syscall();
5668
5669 if (!write && !secret->initialized) {
5670 err = -EIO;
5671 goto out;
5672 }
5673
WANG Cong5449a5c2015-12-21 10:55:45 -08005674 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
5675 if (err >= sizeof(str)) {
5676 err = -EIO;
5677 goto out;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005678 }
5679
5680 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5681 if (err || !write)
5682 goto out;
5683
5684 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5685 err = -EIO;
5686 goto out;
5687 }
5688
5689 secret->initialized = true;
5690 secret->secret = addr;
5691
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005692 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5693 struct net_device *dev;
5694
5695 for_each_netdev(net, dev) {
5696 struct inet6_dev *idev = __in6_dev_get(dev);
5697
5698 if (idev) {
5699 idev->addr_gen_mode =
5700 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5701 }
5702 }
5703 } else {
5704 struct inet6_dev *idev = ctl->extra1;
5705
5706 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5707 }
5708
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005709out:
5710 rtnl_unlock();
5711
5712 return err;
5713}
stephen hemmingerc92d5492013-12-17 22:37:14 -08005714
Andy Gospodarek35103d12015-08-13 10:39:01 -04005715static
5716int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
5717 int write,
5718 void __user *buffer,
5719 size_t *lenp,
5720 loff_t *ppos)
5721{
5722 int *valp = ctl->data;
5723 int val = *valp;
5724 loff_t pos = *ppos;
5725 struct ctl_table lctl;
5726 int ret;
5727
5728 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
5729 * we should not modify it until we get the rtnl lock.
5730 */
5731 lctl = *ctl;
5732 lctl.data = &val;
5733
5734 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5735
5736 if (write)
5737 ret = addrconf_fixup_linkdown(ctl, valp, val);
5738 if (ret)
5739 *ppos = pos;
5740 return ret;
5741}
5742
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07005743static int minus_one = -1;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07005744static const int one = 1;
5745static const int two_five_five = 255;
5746
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03005747static const struct ctl_table addrconf_sysctl[] = {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005748 {
5749 .procname = "forwarding",
5750 .data = &ipv6_devconf.forwarding,
5751 .maxlen = sizeof(int),
5752 .mode = 0644,
5753 .proc_handler = addrconf_sysctl_forward,
5754 },
5755 {
5756 .procname = "hop_limit",
5757 .data = &ipv6_devconf.hop_limit,
5758 .maxlen = sizeof(int),
5759 .mode = 0644,
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07005760 .proc_handler = proc_dointvec_minmax,
5761 .extra1 = (void *)&one,
5762 .extra2 = (void *)&two_five_five,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005763 },
5764 {
5765 .procname = "mtu",
5766 .data = &ipv6_devconf.mtu6,
5767 .maxlen = sizeof(int),
5768 .mode = 0644,
5769 .proc_handler = addrconf_sysctl_mtu,
5770 },
5771 {
5772 .procname = "accept_ra",
5773 .data = &ipv6_devconf.accept_ra,
5774 .maxlen = sizeof(int),
5775 .mode = 0644,
5776 .proc_handler = proc_dointvec,
5777 },
5778 {
5779 .procname = "accept_redirects",
5780 .data = &ipv6_devconf.accept_redirects,
5781 .maxlen = sizeof(int),
5782 .mode = 0644,
5783 .proc_handler = proc_dointvec,
5784 },
5785 {
5786 .procname = "autoconf",
5787 .data = &ipv6_devconf.autoconf,
5788 .maxlen = sizeof(int),
5789 .mode = 0644,
5790 .proc_handler = proc_dointvec,
5791 },
5792 {
5793 .procname = "dad_transmits",
5794 .data = &ipv6_devconf.dad_transmits,
5795 .maxlen = sizeof(int),
5796 .mode = 0644,
5797 .proc_handler = proc_dointvec,
5798 },
5799 {
5800 .procname = "router_solicitations",
5801 .data = &ipv6_devconf.rtr_solicits,
5802 .maxlen = sizeof(int),
5803 .mode = 0644,
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07005804 .proc_handler = proc_dointvec_minmax,
5805 .extra1 = &minus_one,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005806 },
5807 {
5808 .procname = "router_solicitation_interval",
5809 .data = &ipv6_devconf.rtr_solicit_interval,
5810 .maxlen = sizeof(int),
5811 .mode = 0644,
5812 .proc_handler = proc_dointvec_jiffies,
5813 },
5814 {
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005815 .procname = "router_solicitation_max_interval",
5816 .data = &ipv6_devconf.rtr_solicit_max_interval,
5817 .maxlen = sizeof(int),
5818 .mode = 0644,
5819 .proc_handler = proc_dointvec_jiffies,
5820 },
5821 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005822 .procname = "router_solicitation_delay",
5823 .data = &ipv6_devconf.rtr_solicit_delay,
5824 .maxlen = sizeof(int),
5825 .mode = 0644,
5826 .proc_handler = proc_dointvec_jiffies,
5827 },
5828 {
5829 .procname = "force_mld_version",
5830 .data = &ipv6_devconf.force_mld_version,
5831 .maxlen = sizeof(int),
5832 .mode = 0644,
5833 .proc_handler = proc_dointvec,
5834 },
5835 {
5836 .procname = "mldv1_unsolicited_report_interval",
5837 .data =
5838 &ipv6_devconf.mldv1_unsolicited_report_interval,
5839 .maxlen = sizeof(int),
5840 .mode = 0644,
5841 .proc_handler = proc_dointvec_ms_jiffies,
5842 },
5843 {
5844 .procname = "mldv2_unsolicited_report_interval",
5845 .data =
5846 &ipv6_devconf.mldv2_unsolicited_report_interval,
5847 .maxlen = sizeof(int),
5848 .mode = 0644,
5849 .proc_handler = proc_dointvec_ms_jiffies,
5850 },
5851 {
5852 .procname = "use_tempaddr",
5853 .data = &ipv6_devconf.use_tempaddr,
5854 .maxlen = sizeof(int),
5855 .mode = 0644,
5856 .proc_handler = proc_dointvec,
5857 },
5858 {
5859 .procname = "temp_valid_lft",
5860 .data = &ipv6_devconf.temp_valid_lft,
5861 .maxlen = sizeof(int),
5862 .mode = 0644,
5863 .proc_handler = proc_dointvec,
5864 },
5865 {
5866 .procname = "temp_prefered_lft",
5867 .data = &ipv6_devconf.temp_prefered_lft,
5868 .maxlen = sizeof(int),
5869 .mode = 0644,
5870 .proc_handler = proc_dointvec,
5871 },
5872 {
5873 .procname = "regen_max_retry",
5874 .data = &ipv6_devconf.regen_max_retry,
5875 .maxlen = sizeof(int),
5876 .mode = 0644,
5877 .proc_handler = proc_dointvec,
5878 },
5879 {
5880 .procname = "max_desync_factor",
5881 .data = &ipv6_devconf.max_desync_factor,
5882 .maxlen = sizeof(int),
5883 .mode = 0644,
5884 .proc_handler = proc_dointvec,
5885 },
5886 {
5887 .procname = "max_addresses",
5888 .data = &ipv6_devconf.max_addresses,
5889 .maxlen = sizeof(int),
5890 .mode = 0644,
5891 .proc_handler = proc_dointvec,
5892 },
5893 {
5894 .procname = "accept_ra_defrtr",
5895 .data = &ipv6_devconf.accept_ra_defrtr,
5896 .maxlen = sizeof(int),
5897 .mode = 0644,
5898 .proc_handler = proc_dointvec,
5899 },
5900 {
5901 .procname = "accept_ra_min_hop_limit",
5902 .data = &ipv6_devconf.accept_ra_min_hop_limit,
5903 .maxlen = sizeof(int),
5904 .mode = 0644,
5905 .proc_handler = proc_dointvec,
5906 },
5907 {
5908 .procname = "accept_ra_pinfo",
5909 .data = &ipv6_devconf.accept_ra_pinfo,
5910 .maxlen = sizeof(int),
5911 .mode = 0644,
5912 .proc_handler = proc_dointvec,
5913 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005914#ifdef CONFIG_IPV6_ROUTER_PREF
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005915 {
5916 .procname = "accept_ra_rtr_pref",
5917 .data = &ipv6_devconf.accept_ra_rtr_pref,
5918 .maxlen = sizeof(int),
5919 .mode = 0644,
5920 .proc_handler = proc_dointvec,
5921 },
5922 {
5923 .procname = "router_probe_interval",
5924 .data = &ipv6_devconf.rtr_probe_interval,
5925 .maxlen = sizeof(int),
5926 .mode = 0644,
5927 .proc_handler = proc_dointvec_jiffies,
5928 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08005929#ifdef CONFIG_IPV6_ROUTE_INFO
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005930 {
5931 .procname = "accept_ra_rt_info_max_plen",
5932 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
5933 .maxlen = sizeof(int),
5934 .mode = 0644,
5935 .proc_handler = proc_dointvec,
5936 },
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08005937#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005938#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005939 {
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +09005940 .procname = "accept_ra_rt_table",
5941 .data = &ipv6_devconf.accept_ra_rt_table,
5942 .maxlen = sizeof(int),
5943 .mode = 0644,
5944 .proc_handler = proc_dointvec,
5945 },
5946 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005947 .procname = "proxy_ndp",
5948 .data = &ipv6_devconf.proxy_ndp,
5949 .maxlen = sizeof(int),
5950 .mode = 0644,
5951 .proc_handler = addrconf_sysctl_proxy_ndp,
5952 },
5953 {
5954 .procname = "accept_source_route",
5955 .data = &ipv6_devconf.accept_source_route,
5956 .maxlen = sizeof(int),
5957 .mode = 0644,
5958 .proc_handler = proc_dointvec,
5959 },
Neil Horman95c385b2007-04-25 17:08:10 -07005960#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005961 {
5962 .procname = "optimistic_dad",
5963 .data = &ipv6_devconf.optimistic_dad,
5964 .maxlen = sizeof(int),
5965 .mode = 0644,
5966 .proc_handler = proc_dointvec,
5967 },
5968 {
5969 .procname = "use_optimistic",
5970 .data = &ipv6_devconf.use_optimistic,
5971 .maxlen = sizeof(int),
5972 .mode = 0644,
5973 .proc_handler = proc_dointvec,
5974 },
Neil Horman95c385b2007-04-25 17:08:10 -07005975#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005976#ifdef CONFIG_IPV6_MROUTE
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005977 {
5978 .procname = "mc_forwarding",
5979 .data = &ipv6_devconf.mc_forwarding,
5980 .maxlen = sizeof(int),
5981 .mode = 0444,
5982 .proc_handler = proc_dointvec,
5983 },
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005984#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005985 {
5986 .procname = "disable_ipv6",
5987 .data = &ipv6_devconf.disable_ipv6,
5988 .maxlen = sizeof(int),
5989 .mode = 0644,
5990 .proc_handler = addrconf_sysctl_disable,
5991 },
5992 {
5993 .procname = "accept_dad",
5994 .data = &ipv6_devconf.accept_dad,
5995 .maxlen = sizeof(int),
5996 .mode = 0644,
5997 .proc_handler = proc_dointvec,
5998 },
5999 {
6000 .procname = "force_tllao",
6001 .data = &ipv6_devconf.force_tllao,
6002 .maxlen = sizeof(int),
6003 .mode = 0644,
6004 .proc_handler = proc_dointvec
6005 },
6006 {
6007 .procname = "ndisc_notify",
6008 .data = &ipv6_devconf.ndisc_notify,
6009 .maxlen = sizeof(int),
6010 .mode = 0644,
6011 .proc_handler = proc_dointvec
6012 },
6013 {
6014 .procname = "suppress_frag_ndisc",
6015 .data = &ipv6_devconf.suppress_frag_ndisc,
6016 .maxlen = sizeof(int),
6017 .mode = 0644,
6018 .proc_handler = proc_dointvec
6019 },
6020 {
6021 .procname = "accept_ra_from_local",
6022 .data = &ipv6_devconf.accept_ra_from_local,
6023 .maxlen = sizeof(int),
6024 .mode = 0644,
6025 .proc_handler = proc_dointvec,
6026 },
6027 {
6028 .procname = "accept_ra_mtu",
6029 .data = &ipv6_devconf.accept_ra_mtu,
6030 .maxlen = sizeof(int),
6031 .mode = 0644,
6032 .proc_handler = proc_dointvec,
6033 },
6034 {
6035 .procname = "stable_secret",
6036 .data = &ipv6_devconf.stable_secret,
6037 .maxlen = IPV6_MAX_STRLEN,
6038 .mode = 0600,
6039 .proc_handler = addrconf_sysctl_stable_secret,
6040 },
6041 {
6042 .procname = "use_oif_addrs_only",
6043 .data = &ipv6_devconf.use_oif_addrs_only,
6044 .maxlen = sizeof(int),
6045 .mode = 0644,
6046 .proc_handler = proc_dointvec,
6047 },
6048 {
6049 .procname = "ignore_routes_with_linkdown",
6050 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6051 .maxlen = sizeof(int),
6052 .mode = 0644,
6053 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6054 },
6055 {
6056 .procname = "drop_unicast_in_l2_multicast",
6057 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6058 .maxlen = sizeof(int),
6059 .mode = 0644,
6060 .proc_handler = proc_dointvec,
6061 },
6062 {
6063 .procname = "drop_unsolicited_na",
6064 .data = &ipv6_devconf.drop_unsolicited_na,
6065 .maxlen = sizeof(int),
6066 .mode = 0644,
6067 .proc_handler = proc_dointvec,
6068 },
6069 {
6070 .procname = "keep_addr_on_down",
6071 .data = &ipv6_devconf.keep_addr_on_down,
6072 .maxlen = sizeof(int),
6073 .mode = 0644,
6074 .proc_handler = proc_dointvec,
David Ahernf1705ec2016-02-24 09:25:37 -08006075
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006076 },
6077 {
6078 /* sentinel */
6079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080};
6081
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08006082static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006083 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084{
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006085 int i, ifindex;
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006086 struct ctl_table *table;
Eric W. Biederman6105e292012-04-19 13:41:24 +00006087 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11006088
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006089 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6090 if (!table)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006091 goto out;
6092
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006093 for (i = 0; table[i].data; i++) {
6094 table[i].data += (char *)p - (char *)&ipv6_devconf;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006095 /* If one of these is already set, then it is not safe to
6096 * overwrite either of them: this makes proc_dointvec_minmax
6097 * usable.
6098 */
6099 if (!table[i].extra1 && !table[i].extra2) {
6100 table[i].extra1 = idev; /* embedded; no ref */
6101 table[i].extra2 = net;
6102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104
Eric W. Biederman6105e292012-04-19 13:41:24 +00006105 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006107 p->sysctl_header = register_net_sysctl(net, path, table);
6108 if (!p->sysctl_header)
Eric W. Biederman6105e292012-04-19 13:41:24 +00006109 goto free;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006110
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006111 if (!strcmp(dev_name, "all"))
6112 ifindex = NETCONFA_IFINDEX_ALL;
6113 else if (!strcmp(dev_name, "default"))
6114 ifindex = NETCONFA_IFINDEX_DEFAULT;
6115 else
6116 ifindex = idev->dev->ifindex;
6117 inet6_netconf_notify_devconf(net, NETCONFA_ALL, ifindex, p);
Pavel Emelyanov95897312008-01-10 17:41:45 -08006118 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006120free:
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006121 kfree(table);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006122out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08006123 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124}
6125
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006126static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
6127{
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006128 struct ctl_table *table;
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006129
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006130 if (!p->sysctl_header)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006131 return;
6132
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006133 table = p->sysctl_header->ctl_table_arg;
6134 unregister_net_sysctl_table(p->sysctl_header);
6135 p->sysctl_header = NULL;
6136 kfree(table);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006137}
6138
WANG Conga317a2f2014-07-25 15:25:09 -07006139static int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006140{
WANG Conga317a2f2014-07-25 15:25:09 -07006141 int err;
6142
6143 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6144 return -EINVAL;
6145
6146 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6147 &ndisc_ifinfo_sysctl_change);
6148 if (err)
6149 return err;
6150 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6151 idev, &idev->cnf);
6152 if (err)
6153 neigh_sysctl_unregister(idev->nd_parms);
6154
6155 return err;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006156}
6157
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006158static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006160 __addrconf_sysctl_unregister(&idev->cnf);
6161 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162}
6163
6164
6165#endif
6166
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006167static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006168{
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006169 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006170 struct ipv6_devconf *all, *dflt;
6171
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006172 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006173 if (!all)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006174 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006175
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006176 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006177 if (!dflt)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006178 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006179
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006180 /* these will be inherited by all namespaces */
6181 dflt->autoconf = ipv6_defaults.autoconf;
6182 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006183
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006184 dflt->stable_secret.initialized = false;
6185 all->stable_secret.initialized = false;
6186
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006187 net->ipv6.devconf_all = all;
6188 net->ipv6.devconf_dflt = dflt;
6189
6190#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006191 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006192 if (err < 0)
6193 goto err_reg_all;
6194
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006195 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006196 if (err < 0)
6197 goto err_reg_dflt;
6198#endif
6199 return 0;
6200
6201#ifdef CONFIG_SYSCTL
6202err_reg_dflt:
6203 __addrconf_sysctl_unregister(all);
6204err_reg_all:
6205 kfree(dflt);
6206#endif
6207err_alloc_dflt:
6208 kfree(all);
6209err_alloc_all:
6210 return err;
6211}
6212
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006213static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006214{
6215#ifdef CONFIG_SYSCTL
6216 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
6217 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
6218#endif
zhuyj73cf0e92014-11-26 10:25:58 +08006219 kfree(net->ipv6.devconf_dflt);
6220 kfree(net->ipv6.devconf_all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006221}
6222
6223static struct pernet_operations addrconf_ops = {
6224 .init = addrconf_init_net,
6225 .exit = addrconf_exit_net,
6226};
6227
Daniel Borkmann207895f2015-01-29 12:15:03 +01006228static struct rtnl_af_ops inet6_ops __read_mostly = {
Thomas Grafb382b192010-11-16 04:33:57 +00006229 .family = AF_INET6,
6230 .fill_link_af = inet6_fill_link_af,
6231 .get_link_af_size = inet6_get_link_af_size,
Daniel Borkmann11b1f822015-02-05 14:39:11 +01006232 .validate_link_af = inet6_validate_link_af,
Daniel Borkmannf53adae2013-04-08 04:01:30 +00006233 .set_link_af = inet6_set_link_af,
Thomas Grafb382b192010-11-16 04:33:57 +00006234};
6235
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236/*
6237 * Init / cleanup code
6238 */
6239
6240int __init addrconf_init(void)
6241{
WANG Conga317a2f2014-07-25 15:25:09 -07006242 struct inet6_dev *idev;
stephen hemmingerc2e21292010-03-17 20:31:10 +00006243 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006244
Stephen Hemmingere21e8462010-03-20 16:09:01 -07006245 err = ipv6_addr_label_init();
6246 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00006247 pr_crit("%s: cannot initialize default policy table: %d\n",
6248 __func__, err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006249 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006252 err = register_pernet_subsys(&addrconf_ops);
6253 if (err < 0)
6254 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006255
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006256 addrconf_wq = create_workqueue("ipv6_addrconf");
6257 if (!addrconf_wq) {
6258 err = -ENOMEM;
6259 goto out_nowq;
6260 }
6261
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 /* The addrconf netdev notifier requires that loopback_dev
6263 * has it's ipv6 private information allocated and setup
6264 * before it can bring up and give link-local addresses
6265 * to other devices which are up.
6266 *
6267 * Unfortunately, loopback_dev is not necessarily the first
6268 * entry in the global dev_base list of net devices. In fact,
6269 * it is likely to be the very last entry on that list.
6270 * So this causes the notifier registry below to try and
6271 * give link-local addresses to all devices besides loopback_dev
6272 * first, then loopback_dev, which cases all the non-loopback_dev
6273 * devices to fail to get a link-local address.
6274 *
6275 * So, as a temporary fix, allocate the ipv6 structure for
6276 * loopback_dev first by hand.
6277 * Longer term, all of the dependencies ipv6 has upon the loopback
6278 * device and it being up should be removed.
6279 */
6280 rtnl_lock();
WANG Conga317a2f2014-07-25 15:25:09 -07006281 idev = ipv6_add_dev(init_net.loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282 rtnl_unlock();
WANG Conga317a2f2014-07-25 15:25:09 -07006283 if (IS_ERR(idev)) {
6284 err = PTR_ERR(idev);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006285 goto errlo;
WANG Conga317a2f2014-07-25 15:25:09 -07006286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287
stephen hemmingerc2e21292010-03-17 20:31:10 +00006288 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6289 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6290
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 register_netdevice_notifier(&ipv6_dev_notf);
6292
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006293 addrconf_verify();
Thomas Grafc127ea22007-03-22 11:58:32 -07006294
stephen hemminger3678a9d2013-12-30 10:41:32 -08006295 rtnl_af_register(&inet6_ops);
Thomas Grafb382b192010-11-16 04:33:57 +00006296
Greg Rosec7ac8672011-06-10 01:27:09 +00006297 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6298 NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006299 if (err < 0)
6300 goto errout;
6301
6302 /* Only the first call to __rtnl_register can fail */
Greg Rosec7ac8672011-06-10 01:27:09 +00006303 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
6304 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
6305 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6306 inet6_dump_ifaddr, NULL);
6307 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6308 inet6_dump_ifmcaddr, NULL);
6309 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6310 inet6_dump_ifacaddr, NULL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +00006311 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
Nicolas Dichtel7a674202013-03-05 23:42:06 +00006312 inet6_netconf_dump_devconf, NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006313
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006314 ipv6_addr_label_rtnl_register();
6315
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07006317errout:
Thomas Grafb382b192010-11-16 04:33:57 +00006318 rtnl_af_unregister(&inet6_ops);
Thomas Grafc127ea22007-03-22 11:58:32 -07006319 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006320errlo:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006321 destroy_workqueue(addrconf_wq);
6322out_nowq:
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006323 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006324out_addrlabel:
6325 ipv6_addr_label_cleanup();
6326out:
Thomas Grafc127ea22007-03-22 11:58:32 -07006327 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328}
6329
Daniel Lezcano09f77092007-12-13 05:34:58 -08006330void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006332 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 int i;
6334
6335 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006336 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006337 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338
6339 rtnl_lock();
6340
Thomas Grafb382b192010-11-16 04:33:57 +00006341 __rtnl_af_unregister(&inet6_ops);
6342
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006343 /* clean dev list */
6344 for_each_netdev(&init_net, dev) {
6345 if (__in6_dev_get(dev) == NULL)
6346 continue;
6347 addrconf_ifdown(dev, 1);
6348 }
6349 addrconf_ifdown(init_net.loopback_dev, 2);
6350
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 * Check hash table.
6353 */
stephen hemminger5c578ae2010-03-17 20:31:11 +00006354 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00006355 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6356 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578ae2010-03-17 20:31:11 +00006357 spin_unlock_bh(&addrconf_hash_lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006358 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 rtnl_unlock();
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006360
6361 destroy_workqueue(addrconf_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362}