blob: 6ba6a5dd7ff045edd824dafe6caa69ce5e622270 [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
Joel Scherpelzd860b2e2017-03-22 18:19:04 +0900226 .accept_ra_rt_info_min_plen = 0,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800227 .accept_ra_rt_info_max_plen = 0,
228#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800229#endif
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +0900230 .accept_ra_rt_table = 0,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700231 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700232 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900233 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900234 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200235 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700236 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100237 .stable_secret = {
238 .initialized = false,
Erik Kline3985e8a2015-07-22 16:38:25 +0900239 },
240 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400241 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800242 .keep_addr_on_down = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243};
244
Brian Haleyab32ea52006-09-22 14:15:41 -0700245static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 .forwarding = 0,
247 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
248 .mtu6 = IPV6_MIN_MTU,
249 .accept_ra = 1,
250 .accept_redirects = 1,
251 .autoconf = 1,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200252 .force_mld_version = 0,
253 .mldv1_unsolicited_report_interval = 10 * HZ,
254 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 .dad_transmits = 1,
256 .rtr_solicits = MAX_RTR_SOLICITATIONS,
257 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700258 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 .use_tempaddr = 0,
261 .temp_valid_lft = TEMP_VALID_LIFETIME,
262 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
263 .regen_max_retry = REGEN_MAX_RETRY,
264 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800266 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700267 .accept_ra_from_local = 0,
Hangbin Liu8013d1d2015-07-30 14:28:42 +0800268 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800269 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800270#ifdef CONFIG_IPV6_ROUTER_PREF
271 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800272 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800273#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzd860b2e2017-03-22 18:19:04 +0900274 .accept_ra_rt_info_min_plen = 0,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800275 .accept_ra_rt_info_max_plen = 0,
276#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800277#endif
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +0900278 .accept_ra_rt_table = 0,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700279 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700280 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900281 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900282 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200283 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700284 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100285 .stable_secret = {
286 .initialized = false,
287 },
Erik Kline3985e8a2015-07-22 16:38:25 +0900288 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400289 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800290 .keep_addr_on_down = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291};
292
Mike Manning4bf42a22017-09-25 22:01:36 +0100293/* Check if link is ready: is it up and is a valid qdisc available */
294static inline bool addrconf_link_ready(const struct net_device *dev)
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700295{
Mike Manning4bf42a22017-09-25 22:01:36 +0100296 return netif_oper_up(dev) && !qdisc_tx_is_noop(dev);
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700297}
298
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200299static void addrconf_del_rs_timer(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200301 if (del_timer(&idev->rs_timer))
302 __in6_dev_put(idev);
303}
304
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100305static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200306{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100307 if (cancel_delayed_work(&ifp->dad_work))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 __in6_ifa_put(ifp);
309}
310
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200311static void addrconf_mod_rs_timer(struct inet6_dev *idev,
312 unsigned long when)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200314 if (!timer_pending(&idev->rs_timer))
315 in6_dev_hold(idev);
316 mod_timer(&idev->rs_timer, jiffies + when);
317}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100319static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
320 unsigned long delay)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200321{
Xin Longfded2d72017-06-15 16:33:58 +0800322 in6_ifa_hold(ifp);
323 if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay))
324 in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700327static int snmp6_alloc_dev(struct inet6_dev *idev)
328{
John Stultz827da442013-10-07 15:51:58 -0700329 int i;
330
WANG Cong698365f2014-05-05 15:55:55 -0700331 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
332 if (!idev->stats.ipv6)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700333 goto err_ip;
John Stultz827da442013-10-07 15:51:58 -0700334
335 for_each_possible_cpu(i) {
336 struct ipstats_mib *addrconf_stats;
WANG Cong698365f2014-05-05 15:55:55 -0700337 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
John Stultz827da442013-10-07 15:51:58 -0700338 u64_stats_init(&addrconf_stats->syncp);
John Stultz827da442013-10-07 15:51:58 -0700339 }
340
341
Eric Dumazetbe281e52011-05-19 01:14:23 +0000342 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
343 GFP_KERNEL);
344 if (!idev->stats.icmpv6dev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700345 goto err_icmp;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000346 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
347 GFP_KERNEL);
348 if (!idev->stats.icmpv6msgdev)
David L Stevens14878f72007-09-16 16:52:35 -0700349 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700350
351 return 0;
352
David L Stevens14878f72007-09-16 16:52:35 -0700353err_icmpmsg:
Eric Dumazetbe281e52011-05-19 01:14:23 +0000354 kfree(idev->stats.icmpv6dev);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700355err_icmp:
WANG Cong698365f2014-05-05 15:55:55 -0700356 free_percpu(idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700357err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700358 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700359}
360
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000361static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct inet6_dev *ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700364 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 ASSERT_RTNL();
367
368 if (dev->mtu < IPV6_MIN_MTU)
WANG Conga317a2f2014-07-25 15:25:09 -0700369 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900371 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100372 if (!ndev)
WANG Conga317a2f2014-07-25 15:25:09 -0700373 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Ingo Oeser322f74a2006-03-20 23:01:47 -0800375 rwlock_init(&ndev->lock);
376 ndev->dev = dev;
stephen hemminger502a2ff2010-03-17 20:31:13 +0000377 INIT_LIST_HEAD(&ndev->addr_list);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200378 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
379 (unsigned long)ndev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900380 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100381
382 if (ndev->cnf.stable_secret.initialized)
383 ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
384 else
385 ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64;
386
Ingo Oeser322f74a2006-03-20 23:01:47 -0800387 ndev->cnf.mtu6 = dev->mtu;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800388 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
Ian Morris63159f22015-03-29 14:00:04 +0100389 if (!ndev->nd_parms) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800390 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700391 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800392 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700393 if (ndev->cnf.forwarding)
394 dev_disable_lro(dev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800395 /* We refer to the device */
396 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Ingo Oeser322f74a2006-03-20 23:01:47 -0800398 if (snmp6_alloc_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800399 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000400 "%s: cannot allocate memory for statistics; dev=%s.\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800401 __func__, dev->name);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800402 neigh_parms_release(&nd_tbl, ndev->nd_parms);
Roy Li8603e332011-09-20 15:10:16 -0400403 dev_put(dev);
404 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700405 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Ingo Oeser322f74a2006-03-20 23:01:47 -0800408 if (snmp6_register_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800409 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000410 "%s: cannot create /proc/net/dev_snmp6/%s\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800411 __func__, dev->name);
WANG Conga317a2f2014-07-25 15:25:09 -0700412 goto err_release;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200415 /* One reference from device. */
Ingo Oeser322f74a2006-03-20 23:01:47 -0800416 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900418 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
419 ndev->cnf.accept_dad = -1;
420
Amerigo Wang07a93622012-10-29 16:23:10 +0000421#if IS_ENABLED(CONFIG_IPV6_SIT)
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700422 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
Joe Perchesf3213832012-05-15 14:11:53 +0000423 pr_info("%s: Disabled Multicast RS\n", dev->name);
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700424 ndev->cnf.rtr_solicits = 0;
425 }
426#endif
427
stephen hemminger372e6c82010-03-17 20:31:09 +0000428 INIT_LIST_HEAD(&ndev->tempaddr_list);
Jiri Bohac76506a92016-10-13 18:52:15 +0200429 ndev->desync_factor = U32_MAX;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800430 if ((dev->flags&IFF_LOOPBACK) ||
431 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700432 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700433 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700434 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800435 ndev->cnf.use_tempaddr = -1;
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200436 } else
437 ipv6_regen_rndid(ndev);
David S. Miller5d9efa72013-10-28 20:07:50 -0400438
Daniel Borkmann914faa12013-04-09 03:47:14 +0000439 ndev->token = in6addr_any;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800440
Mike Manning4bf42a22017-09-25 22:01:36 +0100441 if (netif_running(dev) && addrconf_link_ready(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700442 ndev->if_flags |= IF_READY;
443
Ingo Oeser322f74a2006-03-20 23:01:47 -0800444 ipv6_mc_init_dev(ndev);
445 ndev->tstamp = jiffies;
WANG Conga317a2f2014-07-25 15:25:09 -0700446 err = addrconf_sysctl_register(ndev);
447 if (err) {
448 ipv6_mc_destroy_dev(ndev);
Sabrina Dubroca2a189f92015-11-04 14:47:53 +0100449 snmp6_unregister_dev(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700450 goto err_release;
451 }
David L Stevens30c4cf52007-01-04 12:31:14 -0800452 /* protected by rtnl_lock */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000453 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800454
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000455 /* Join interface-local all-node multicast group */
456 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
457
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800458 /* Join all-node multicast group */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900459 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800460
Li Weid6ddef92012-03-05 14:45:17 +0000461 /* Join all-router multicast group if forwarding is set */
Li Wei8b2aaed2012-03-07 14:58:07 +0000462 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
Li Weid6ddef92012-03-05 14:45:17 +0000463 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700466
467err_release:
468 neigh_parms_release(&nd_tbl, ndev->nd_parms);
469 ndev->dead = 1;
470 in6_dev_finish_destroy(ndev);
471 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000474static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 struct inet6_dev *idev;
477
478 ASSERT_RTNL();
479
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700480 idev = __in6_dev_get(dev);
481 if (!idev) {
482 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -0700483 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return NULL;
485 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (dev->flags&IFF_UP)
488 ipv6_mc_up(idev);
489 return idev;
490}
491
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000492static int inet6_netconf_msgsize_devconf(int type)
493{
494 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
495 + nla_total_size(4); /* NETCONFA_IFINDEX */
Zhang Shengju136ba622016-03-10 08:55:50 +0000496 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000497
Zhang Shengju136ba622016-03-10 08:55:50 +0000498 if (type == NETCONFA_ALL)
499 all = true;
500
501 if (all || type == NETCONFA_FORWARDING)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000502 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500503#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000504 if (all || type == NETCONFA_MC_FORWARDING)
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000505 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500506#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000507 if (all || type == NETCONFA_PROXY_NEIGH)
stephen hemmingerc92d5492013-12-17 22:37:14 -0800508 size += nla_total_size(4);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000509
Zhang Shengju136ba622016-03-10 08:55:50 +0000510 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
Andy Gospodarek35103d12015-08-13 10:39:01 -0400511 size += nla_total_size(4);
512
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000513 return size;
514}
515
516static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
517 struct ipv6_devconf *devconf, u32 portid,
518 u32 seq, int event, unsigned int flags,
519 int type)
520{
521 struct nlmsghdr *nlh;
522 struct netconfmsg *ncm;
Zhang Shengju136ba622016-03-10 08:55:50 +0000523 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000524
525 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
526 flags);
Ian Morris63159f22015-03-29 14:00:04 +0100527 if (!nlh)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000528 return -EMSGSIZE;
529
Zhang Shengju136ba622016-03-10 08:55:50 +0000530 if (type == NETCONFA_ALL)
531 all = true;
532
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000533 ncm = nlmsg_data(nlh);
534 ncm->ncm_family = AF_INET6;
535
536 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
537 goto nla_put_failure;
538
Zhang Shengju136ba622016-03-10 08:55:50 +0000539 if ((all || type == NETCONFA_FORWARDING) &&
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000540 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
541 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500542#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000543 if ((all || type == NETCONFA_MC_FORWARDING) &&
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000544 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
545 devconf->mc_forwarding) < 0)
546 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500547#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000548 if ((all || type == NETCONFA_PROXY_NEIGH) &&
stephen hemmingerc92d5492013-12-17 22:37:14 -0800549 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
550 goto nla_put_failure;
551
Zhang Shengju136ba622016-03-10 08:55:50 +0000552 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
Andy Gospodarek35103d12015-08-13 10:39:01 -0400553 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
554 devconf->ignore_routes_with_linkdown) < 0)
555 goto nla_put_failure;
556
Johannes Berg053c0952015-01-16 22:09:00 +0100557 nlmsg_end(skb, nlh);
558 return 0;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000559
560nla_put_failure:
561 nlmsg_cancel(skb, nlh);
562 return -EMSGSIZE;
563}
564
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000565void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
566 struct ipv6_devconf *devconf)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000567{
568 struct sk_buff *skb;
569 int err = -ENOBUFS;
570
Eric Dumazet927265b2016-07-08 05:46:04 +0200571 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100572 if (!skb)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000573 goto errout;
574
575 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
576 RTM_NEWNETCONF, 0, type);
577 if (err < 0) {
578 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
579 WARN_ON(err == -EMSGSIZE);
580 kfree_skb(skb);
581 goto errout;
582 }
Eric Dumazet927265b2016-07-08 05:46:04 +0200583 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000584 return;
585errout:
Cong Dingbd779022012-12-18 12:08:56 +0000586 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000587}
588
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000589static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
590 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
591 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
stephen hemmingerc92d5492013-12-17 22:37:14 -0800592 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
Andy Gospodarek35103d12015-08-13 10:39:01 -0400593 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000594};
595
596static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
Thomas Graf661d2962013-03-21 07:45:29 +0000597 struct nlmsghdr *nlh)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000598{
599 struct net *net = sock_net(in_skb->sk);
600 struct nlattr *tb[NETCONFA_MAX+1];
601 struct netconfmsg *ncm;
602 struct sk_buff *skb;
603 struct ipv6_devconf *devconf;
604 struct inet6_dev *in6_dev;
605 struct net_device *dev;
606 int ifindex;
607 int err;
608
609 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
610 devconf_ipv6_policy);
611 if (err < 0)
612 goto errout;
613
Anton Protopopova97eb332016-02-16 21:43:16 -0500614 err = -EINVAL;
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000615 if (!tb[NETCONFA_IFINDEX])
616 goto errout;
617
618 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
619 switch (ifindex) {
620 case NETCONFA_IFINDEX_ALL:
621 devconf = net->ipv6.devconf_all;
622 break;
623 case NETCONFA_IFINDEX_DEFAULT:
624 devconf = net->ipv6.devconf_dflt;
625 break;
626 default:
627 dev = __dev_get_by_index(net, ifindex);
Ian Morris63159f22015-03-29 14:00:04 +0100628 if (!dev)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000629 goto errout;
630 in6_dev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +0100631 if (!in6_dev)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000632 goto errout;
633 devconf = &in6_dev->cnf;
634 break;
635 }
636
637 err = -ENOBUFS;
Zhang Shengju136ba622016-03-10 08:55:50 +0000638 skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +0100639 if (!skb)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000640 goto errout;
641
642 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
643 NETLINK_CB(in_skb).portid,
644 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
Zhang Shengju136ba622016-03-10 08:55:50 +0000645 NETCONFA_ALL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000646 if (err < 0) {
647 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
648 WARN_ON(err == -EMSGSIZE);
649 kfree_skb(skb);
650 goto errout;
651 }
652 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
653errout:
654 return err;
655}
656
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000657static int inet6_netconf_dump_devconf(struct sk_buff *skb,
658 struct netlink_callback *cb)
659{
660 struct net *net = sock_net(skb->sk);
661 int h, s_h;
662 int idx, s_idx;
663 struct net_device *dev;
664 struct inet6_dev *idev;
665 struct hlist_head *head;
666
667 s_h = cb->args[0];
668 s_idx = idx = cb->args[1];
669
670 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
671 idx = 0;
672 head = &net->dev_index_head[h];
673 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000674 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
675 net->dev_base_seq;
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000676 hlist_for_each_entry_rcu(dev, head, index_hlist) {
677 if (idx < s_idx)
678 goto cont;
679 idev = __in6_dev_get(dev);
680 if (!idev)
681 goto cont;
682
683 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
684 &idev->cnf,
685 NETLINK_CB(cb->skb).portid,
686 cb->nlh->nlmsg_seq,
687 RTM_NEWNETCONF,
688 NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000689 NETCONFA_ALL) < 0) {
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000690 rcu_read_unlock();
691 goto done;
692 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000693 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000694cont:
695 idx++;
696 }
697 rcu_read_unlock();
698 }
699 if (h == NETDEV_HASHENTRIES) {
700 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
701 net->ipv6.devconf_all,
702 NETLINK_CB(cb->skb).portid,
703 cb->nlh->nlmsg_seq,
704 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000705 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000706 goto done;
707 else
708 h++;
709 }
710 if (h == NETDEV_HASHENTRIES + 1) {
711 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
712 net->ipv6.devconf_dflt,
713 NETLINK_CB(cb->skb).portid,
714 cb->nlh->nlmsg_seq,
715 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000716 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000717 goto done;
718 else
719 h++;
720 }
721done:
722 cb->args[0] = h;
723 cb->args[1] = idx;
724
725 return skb->len;
726}
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728#ifdef CONFIG_SYSCTL
729static void dev_forward_change(struct inet6_dev *idev)
730{
731 struct net_device *dev;
732 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 if (!idev)
735 return;
736 dev = idev->dev;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700737 if (idev->cnf.forwarding)
738 dev_disable_lro(dev);
Amerigo Wang1a940832012-10-28 17:43:53 +0000739 if (dev->flags & IFF_MULTICAST) {
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000740 if (idev->cnf.forwarding) {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900741 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000742 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
743 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
744 } else {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900745 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000746 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
747 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
stephen hemminger502a2ff2010-03-17 20:31:13 +0000750
751 list_for_each_entry(ifa, &idev->addr_list, if_list) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800752 if (ifa->flags&IFA_F_TENTATIVE)
753 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (idev->cnf.forwarding)
755 addrconf_join_anycast(ifa);
756 else
757 addrconf_leave_anycast(ifa);
758 }
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000759 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
760 dev->ifindex, &idev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
763
Pavel Emelyanove1869322008-01-10 17:43:50 -0800764static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 struct net_device *dev;
767 struct inet6_dev *idev;
768
Ben Hutchings4acd4942012-08-14 08:54:51 +0000769 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 idev = __in6_dev_get(dev);
771 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800772 int changed = (!idev->cnf.forwarding) ^ (!newf);
773 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (changed)
775 dev_forward_change(idev);
776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800779
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000780static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800781{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800782 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000783 int old;
784
785 if (!rtnl_trylock())
786 return restart_syscall();
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800787
788 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000789 old = *p;
790 *p = newf;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800791
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000792 if (p == &net->ipv6.devconf_dflt->forwarding) {
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000793 if ((!newf) ^ (!old))
794 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
795 NETCONFA_IFINDEX_DEFAULT,
796 net->ipv6.devconf_dflt);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000797 rtnl_unlock();
798 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +0000799 }
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000800
Pavel Emelyanove1869322008-01-10 17:43:50 -0800801 if (p == &net->ipv6.devconf_all->forwarding) {
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200802 int old_dflt = net->ipv6.devconf_dflt->forwarding;
803
Pavel Emelyanove1869322008-01-10 17:43:50 -0800804 net->ipv6.devconf_dflt->forwarding = newf;
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200805 if ((!newf) ^ (!old_dflt))
806 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
807 NETCONFA_IFINDEX_DEFAULT,
808 net->ipv6.devconf_dflt);
809
Pavel Emelyanove1869322008-01-10 17:43:50 -0800810 addrconf_forward_change(net, newf);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000811 if ((!newf) ^ (!old))
812 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
813 NETCONFA_IFINDEX_ALL,
814 net->ipv6.devconf_all);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000815 } else if ((!newf) ^ (!old))
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800816 dev_forward_change((struct inet6_dev *)table->extra1);
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700817 rtnl_unlock();
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800818
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000819 if (newf)
Daniel Lezcano7b4da532008-03-04 13:47:14 -0800820 rt6_purge_dflt_routers(net);
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000821 return 1;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800822}
Andy Gospodarek35103d12015-08-13 10:39:01 -0400823
824static void addrconf_linkdown_change(struct net *net, __s32 newf)
825{
826 struct net_device *dev;
827 struct inet6_dev *idev;
828
829 for_each_netdev(net, dev) {
830 idev = __in6_dev_get(dev);
831 if (idev) {
832 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
833
834 idev->cnf.ignore_routes_with_linkdown = newf;
835 if (changed)
836 inet6_netconf_notify_devconf(dev_net(dev),
837 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
838 dev->ifindex,
839 &idev->cnf);
840 }
841 }
842}
843
844static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
845{
846 struct net *net;
847 int old;
848
849 if (!rtnl_trylock())
850 return restart_syscall();
851
852 net = (struct net *)table->extra2;
853 old = *p;
854 *p = newf;
855
856 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
857 if ((!newf) ^ (!old))
858 inet6_netconf_notify_devconf(net,
859 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
860 NETCONFA_IFINDEX_DEFAULT,
861 net->ipv6.devconf_dflt);
862 rtnl_unlock();
863 return 0;
864 }
865
866 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
867 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
868 addrconf_linkdown_change(net, newf);
869 if ((!newf) ^ (!old))
870 inet6_netconf_notify_devconf(net,
871 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
872 NETCONFA_IFINDEX_ALL,
873 net->ipv6.devconf_all);
874 }
875 rtnl_unlock();
876
877 return 1;
878}
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880#endif
881
stephen hemminger5c578ae2010-03-17 20:31:11 +0000882/* Nobody refers to this ifaddr, destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
884{
stephen hemmingerc2e21292010-03-17 20:31:10 +0000885 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887#ifdef NET_REFCNT_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +0000888 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889#endif
890
891 in6_dev_put(ifp->idev);
892
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100893 if (cancel_delayed_work(&ifp->dad_work))
894 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
895 ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Herbert Xue9d3e082010-05-18 15:36:06 -0700897 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
Joe Perchesf3213832012-05-15 14:11:53 +0000898 pr_warn("Freeing alive inet6 address %p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return;
900 }
Amerigo Wang94e187c2012-10-29 00:13:19 +0000901 ip6_rt_put(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Lai Jiangshane5785982011-03-15 18:00:14 +0800903 kfree_rcu(ifp, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
Brian Haleye55ffac2006-07-10 15:25:51 -0700906static void
907ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
908{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000909 struct list_head *p;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700910 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700911
912 /*
913 * Each device address list is sorted in order of scope -
914 * global before linklocal.
915 */
stephen hemminger502a2ff2010-03-17 20:31:13 +0000916 list_for_each(p, &idev->addr_list) {
917 struct inet6_ifaddr *ifa
918 = list_entry(p, struct inet6_ifaddr, if_list);
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700919 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700920 break;
921 }
922
David S. Millerb54c9b92010-03-25 21:25:30 -0700923 list_add_tail(&ifp->if_list, p);
Brian Haleye55ffac2006-07-10 15:25:51 -0700924}
925
Eric Dumazetddbe5032012-07-18 08:11:12 +0000926static u32 inet6_addr_hash(const struct in6_addr *addr)
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900927{
Eric Dumazetddbe5032012-07-18 08:11:12 +0000928 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900929}
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931/* On success it returns ifp with increased reference count */
932
933static struct inet6_ifaddr *
Jiri Pirko3f8f5292013-08-01 10:41:27 +0200934ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
935 const struct in6_addr *peer_addr, int pfxlen,
Jiri Benc8a226b22013-08-01 10:41:28 +0200936 int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
938 struct inet6_ifaddr *ifa = NULL;
939 struct rt6_info *rt;
stephen hemminger3a88a812010-03-17 20:31:12 +0000940 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 int err = 0;
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +0900942 int addr_type = ipv6_addr_type(addr);
943
944 if (addr_type == IPV6_ADDR_ANY ||
945 addr_type & IPV6_ADDR_MULTICAST ||
946 (!(idev->dev->flags & IFF_LOOPBACK) &&
947 addr_type & IPV6_ADDR_LOOPBACK))
948 return ERR_PTR(-EADDRNOTAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700950 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 if (idev->dead) {
952 err = -ENODEV; /*XXX*/
953 goto out2;
954 }
955
Brian Haley56d417b2009-06-01 03:07:33 -0700956 if (idev->cnf.disable_ipv6) {
Brian Haley9bdd8d42009-03-18 18:22:48 -0700957 err = -EACCES;
958 goto out2;
959 }
960
stephen hemminger5c578ae2010-03-17 20:31:11 +0000961 spin_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 /* Ignore adding duplicate addresses on an interface */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900964 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
dingtianhongba3542e2013-08-17 10:27:04 +0800965 ADBG("ipv6_add_addr: already assigned\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 err = -EEXIST;
967 goto out;
968 }
969
Ingo Oeser322f74a2006-03-20 23:01:47 -0800970 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Ian Morris63159f22015-03-29 14:00:04 +0100972 if (!ifa) {
dingtianhongba3542e2013-08-17 10:27:04 +0800973 ADBG("ipv6_add_addr: malloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 err = -ENOBUFS;
975 goto out;
976 }
977
David S. Miller8f031512011-12-06 16:48:14 -0500978 rt = addrconf_dst_alloc(idev, addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (IS_ERR(rt)) {
980 err = PTR_ERR(rt);
981 goto out;
982 }
983
Jiri Pirkobba24892013-12-07 19:26:57 +0100984 neigh_parms_data_state_setall(idev->nd_parms);
985
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000986 ifa->addr = *addr;
Jiri Pirko3f8f5292013-08-01 10:41:27 +0200987 if (peer_addr)
988 ifa->peer_addr = *peer_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 spin_lock_init(&ifa->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100991 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
stephen hemmingerc2e21292010-03-17 20:31:10 +0000992 INIT_HLIST_NODE(&ifa->addr_lst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 ifa->scope = scope;
994 ifa->prefix_len = pfxlen;
Mahesh Bandewareb5562d2017-05-12 17:03:39 -0700995 ifa->flags = flags;
996 /* No need to add the TENTATIVE flag for addresses with NODAD */
997 if (!(flags & IFA_F_NODAD))
998 ifa->flags |= IFA_F_TENTATIVE;
Jiri Benc8a226b22013-08-01 10:41:28 +0200999 ifa->valid_lft = valid_lft;
1000 ifa->prefered_lft = prefered_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 ifa->cstamp = ifa->tstamp = jiffies;
Daniel Borkmann617fe292013-04-09 03:47:16 +00001002 ifa->tokenized = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Keir Fraser57f5f542006-08-29 02:43:49 -07001004 ifa->rt = rt;
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 ifa->idev = idev;
1007 in6_dev_hold(idev);
1008 /* For caller */
1009 in6_ifa_hold(ifa);
1010
1011 /* Add to big hash table */
Eric Dumazetddbe5032012-07-18 08:11:12 +00001012 hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
stephen hemminger5c578ae2010-03-17 20:31:11 +00001014 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
stephen hemminger5c578ae2010-03-17 20:31:11 +00001015 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 write_lock(&idev->lock);
1018 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -07001019 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (ifa->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001022 list_add(&ifa->tmp_list, &idev->tempaddr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 in6_ifa_hold(ifa);
1024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 in6_ifa_hold(ifa);
1027 write_unlock(&idev->lock);
1028out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001029 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -07001031 if (likely(err == 0))
Cong Wangf88c91d2013-04-14 23:18:43 +08001032 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 else {
1034 kfree(ifa);
1035 ifa = ERR_PTR(err);
1036 }
1037
1038 return ifa;
1039out:
stephen hemminger5c578ae2010-03-17 20:31:11 +00001040 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 goto out2;
1042}
1043
Thomas Haller5b84efe2014-01-15 15:36:59 +01001044enum cleanup_prefix_rt_t {
1045 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1046 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1047 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1048};
1049
1050/*
1051 * Check, whether the prefix for ifp would still need a prefix route
1052 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1053 * constants.
1054 *
1055 * 1) we don't purge prefix if address was not permanent.
1056 * prefix is managed by its own lifetime.
1057 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1058 * 3) if there are no addresses, delete prefix.
1059 * 4) if there are still other permanent address(es),
1060 * corresponding prefix is still permanent.
1061 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1062 * don't purge the prefix, assume user space is managing it.
1063 * 6) otherwise, update prefix lifetime to the
1064 * longest valid lifetime among the corresponding
1065 * addresses on the device.
1066 * Note: subsequent RA will update lifetime.
1067 **/
1068static enum cleanup_prefix_rt_t
1069check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1070{
1071 struct inet6_ifaddr *ifa;
1072 struct inet6_dev *idev = ifp->idev;
1073 unsigned long lifetime;
1074 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1075
1076 *expires = jiffies;
1077
1078 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1079 if (ifa == ifp)
1080 continue;
Zhiqiang Liue52cb572019-02-11 10:57:46 +08001081 if (ifa->prefix_len != ifp->prefix_len ||
1082 !ipv6_prefix_equal(&ifa->addr, &ifp->addr,
Thomas Haller5b84efe2014-01-15 15:36:59 +01001083 ifp->prefix_len))
1084 continue;
1085 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1086 return CLEANUP_PREFIX_RT_NOP;
1087
1088 action = CLEANUP_PREFIX_RT_EXPIRE;
1089
1090 spin_lock(&ifa->lock);
1091
1092 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1093 /*
1094 * Note: Because this address is
1095 * not permanent, lifetime <
1096 * LONG_MAX / HZ here.
1097 */
1098 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1099 *expires = ifa->tstamp + lifetime * HZ;
1100 spin_unlock(&ifa->lock);
1101 }
1102
1103 return action;
1104}
1105
1106static void
1107cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
1108{
1109 struct rt6_info *rt;
1110
1111 rt = addrconf_get_prefix_route(&ifp->addr,
1112 ifp->prefix_len,
1113 ifp->idev->dev,
1114 0, RTF_GATEWAY | RTF_DEFAULT);
1115 if (rt) {
1116 if (del_rt)
1117 ip6_del_rt(rt);
1118 else {
1119 if (!(rt->rt6i_flags & RTF_EXPIRES))
1120 rt6_set_expires(rt, expires);
1121 ip6_rt_put(rt);
1122 }
1123 }
1124}
1125
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127/* This function wants to get referenced ifp and releases it before return */
1128
1129static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1130{
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001131 int state;
Thomas Haller5b84efe2014-01-15 15:36:59 +01001132 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1133 unsigned long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001135 ASSERT_RTNL();
1136
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001137 spin_lock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001138 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -07001139 ifp->state = INET6_IFADDR_STATE_DEAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001140 spin_unlock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001141
1142 if (state == INET6_IFADDR_STATE_DEAD)
1143 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
stephen hemminger5c578ae2010-03-17 20:31:11 +00001145 spin_lock_bh(&addrconf_hash_lock);
1146 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578ae2010-03-17 20:31:11 +00001147 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Thomas Haller5b84efe2014-01-15 15:36:59 +01001149 write_lock_bh(&ifp->idev->lock);
David S. Miller5d9efa72013-10-28 20:07:50 -04001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001152 list_del(&ifp->tmp_list);
1153 if (ifp->ifpub) {
1154 in6_ifa_put(ifp->ifpub);
1155 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
stephen hemminger372e6c82010-03-17 20:31:09 +00001157 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Thomas Haller5b84efe2014-01-15 15:36:59 +01001160 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1161 action = check_cleanup_prefix_route(ifp, &expires);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001162
Thomas Haller5b84efe2014-01-15 15:36:59 +01001163 list_del_init(&ifp->if_list);
1164 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Thomas Haller5b84efe2014-01-15 15:36:59 +01001166 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001168 addrconf_del_dad_work(ifp);
Andrey Vaginb2238562008-07-08 15:13:31 -07001169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 ipv6_ifa_notify(RTM_DELADDR, ifp);
1171
Cong Wangf88c91d2013-04-14 23:18:43 +08001172 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Thomas Haller5b84efe2014-01-15 15:36:59 +01001174 if (action != CLEANUP_PREFIX_RT_NOP) {
1175 cleanup_prefix_route(ifp, expires,
1176 action == CLEANUP_PREFIX_RT_DEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
1178
Daniel Walterc3968a82011-04-13 21:10:57 +00001179 /* clean up prefsrc entries */
1180 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001181out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 in6_ifa_put(ifp);
1183}
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1186{
1187 struct inet6_dev *idev = ifp->idev;
1188 struct in6_addr addr, *tmpaddr;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001189 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001190 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 int tmp_plen;
1192 int ret = 0;
Neil Horman95c385b2007-04-25 17:08:10 -07001193 u32 addr_flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001194 unsigned long now = jiffies;
Jiri Bohac76506a92016-10-13 18:52:15 +02001195 long max_desync_factor;
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001196 s32 cnf_temp_preferred_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Jiri Pirko53bd6742013-12-06 09:45:22 +01001198 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (ift) {
1200 spin_lock_bh(&ift->lock);
1201 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1202 spin_unlock_bh(&ift->lock);
1203 tmpaddr = &addr;
1204 } else {
1205 tmpaddr = NULL;
1206 }
1207retry:
1208 in6_dev_hold(idev);
1209 if (idev->cnf.use_tempaddr <= 0) {
Jiri Pirko53bd6742013-12-06 09:45:22 +01001210 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001211 pr_info("%s: use_tempaddr is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 in6_dev_put(idev);
1213 ret = -1;
1214 goto out;
1215 }
1216 spin_lock_bh(&ifp->lock);
1217 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1218 idev->cnf.use_tempaddr = -1; /*XXX*/
1219 spin_unlock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01001220 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001221 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1222 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 in6_dev_put(idev);
1224 ret = -1;
1225 goto out;
1226 }
1227 in6_ifa_hold(ifp);
1228 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
Jiri Bohac9d6280d2016-10-13 18:50:02 +02001229 ipv6_try_regen_rndid(idev, tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001231 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001233 regen_advance = idev->cnf.regen_max_retry *
Ian Morris67ba4152014-08-24 21:53:10 +01001234 idev->cnf.dad_transmits *
1235 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
Jiri Bohac76506a92016-10-13 18:52:15 +02001236
1237 /* recalculate max_desync_factor each time and update
1238 * idev->desync_factor if it's larger
1239 */
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001240 cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
Jiri Bohac76506a92016-10-13 18:52:15 +02001241 max_desync_factor = min_t(__u32,
1242 idev->cnf.max_desync_factor,
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001243 cnf_temp_preferred_lft - regen_advance);
Jiri Bohac76506a92016-10-13 18:52:15 +02001244
1245 if (unlikely(idev->desync_factor > max_desync_factor)) {
1246 if (max_desync_factor > 0) {
1247 get_random_bytes(&idev->desync_factor,
1248 sizeof(idev->desync_factor));
1249 idev->desync_factor %= max_desync_factor;
1250 } else {
1251 idev->desync_factor = 0;
1252 }
1253 }
1254
1255 tmp_valid_lft = min_t(__u32,
1256 ifp->valid_lft,
1257 idev->cnf.temp_valid_lft + age);
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001258 tmp_prefered_lft = cnf_temp_preferred_lft + age -
Jiri Bohac76506a92016-10-13 18:52:15 +02001259 idev->desync_factor;
Jiri Bohac76506a92016-10-13 18:52:15 +02001260 tmp_prefered_lft = min_t(__u32, ifp->prefered_lft, tmp_prefered_lft);
1261 tmp_plen = ifp->prefix_len;
1262 tmp_tstamp = ifp->tstamp;
1263 spin_unlock_bh(&ifp->lock);
1264
Jiri Pirko53bd6742013-12-06 09:45:22 +01001265 write_unlock_bh(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -07001266
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001267 /* A temporary address is created only if this calculated Preferred
1268 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1269 * an implementation must not create a temporary address with a zero
1270 * Preferred Lifetime.
Heiner Kallweitecab6702014-03-12 22:13:19 +01001271 * Use age calculation as in addrconf_verify to avoid unnecessary
1272 * temporary addresses being generated.
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001273 */
Heiner Kallweitecab6702014-03-12 22:13:19 +01001274 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1275 if (tmp_prefered_lft <= regen_advance + age) {
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001276 in6_ifa_put(ifp);
1277 in6_dev_put(idev);
1278 ret = -1;
1279 goto out;
1280 }
1281
Neil Horman95c385b2007-04-25 17:08:10 -07001282 addr_flags = IFA_F_TEMPORARY;
1283 /* set in addrconf_prefix_rcv() */
1284 if (ifp->flags & IFA_F_OPTIMISTIC)
1285 addr_flags |= IFA_F_OPTIMISTIC;
1286
Hannes Frederic Sowa4b08a8f2013-08-16 13:02:27 +02001287 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1288 ipv6_addr_scope(&addr), addr_flags,
1289 tmp_valid_lft, tmp_prefered_lft);
1290 if (IS_ERR(ift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 in6_ifa_put(ifp);
1292 in6_dev_put(idev);
Joe Perchesf3213832012-05-15 14:11:53 +00001293 pr_info("%s: retry temporary address regeneration\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 tmpaddr = &addr;
Jiri Pirko53bd6742013-12-06 09:45:22 +01001295 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 goto retry;
1297 }
1298
1299 spin_lock_bh(&ift->lock);
1300 ift->ifpub = ifp;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001301 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 ift->tstamp = tmp_tstamp;
1303 spin_unlock_bh(&ift->lock);
1304
David S. Millercf22f9a2012-04-14 21:37:40 -04001305 addrconf_dad_start(ift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 in6_ifa_put(ift);
1307 in6_dev_put(idev);
1308out:
1309 return ret;
1310}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001313 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001315enum {
1316 IPV6_SADDR_RULE_INIT = 0,
1317 IPV6_SADDR_RULE_LOCAL,
1318 IPV6_SADDR_RULE_SCOPE,
1319 IPV6_SADDR_RULE_PREFERRED,
1320#ifdef CONFIG_IPV6_MIP6
1321 IPV6_SADDR_RULE_HOA,
1322#endif
1323 IPV6_SADDR_RULE_OIF,
1324 IPV6_SADDR_RULE_LABEL,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001325 IPV6_SADDR_RULE_PRIVACY,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001326 IPV6_SADDR_RULE_ORCHID,
1327 IPV6_SADDR_RULE_PREFIX,
Erik Kline7fd25612014-10-28 18:11:14 +09001328#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1329 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1330#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001331 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001332};
1333
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001334struct ipv6_saddr_score {
1335 int rule;
1336 int addr_type;
1337 struct inet6_ifaddr *ifa;
1338 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1339 int scopedist;
1340 int matchlen;
1341};
1342
1343struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001344 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001345 int ifindex;
1346 int scope;
1347 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001348 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001349};
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001350
Dave Jonesb6f99a22007-03-22 12:27:49 -07001351static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Ulrich Weber45bb0062010-02-25 23:28:58 +00001353 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001354 return 1;
1355 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
Erik Kline7fd25612014-10-28 18:11:14 +09001358static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
1359{
1360#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1361 return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
1362#else
1363 return false;
1364#endif
1365}
1366
Benjamin Thery3de23252008-05-28 14:51:24 +02001367static int ipv6_get_saddr_eval(struct net *net,
1368 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001369 struct ipv6_saddr_dst *dst,
1370 int i)
1371{
1372 int ret;
1373
1374 if (i <= score->rule) {
1375 switch (i) {
1376 case IPV6_SADDR_RULE_SCOPE:
1377 ret = score->scopedist;
1378 break;
1379 case IPV6_SADDR_RULE_PREFIX:
1380 ret = score->matchlen;
1381 break;
1382 default:
1383 ret = !!test_bit(i, score->scorebits);
1384 }
1385 goto out;
1386 }
1387
1388 switch (i) {
1389 case IPV6_SADDR_RULE_INIT:
1390 /* Rule 0: remember if hiscore is not ready yet */
1391 ret = !!score->ifa;
1392 break;
1393 case IPV6_SADDR_RULE_LOCAL:
1394 /* Rule 1: Prefer same address */
1395 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1396 break;
1397 case IPV6_SADDR_RULE_SCOPE:
1398 /* Rule 2: Prefer appropriate scope
1399 *
1400 * ret
1401 * ^
1402 * -1 | d 15
1403 * ---+--+-+---> scope
1404 * |
1405 * | d is scope of the destination.
1406 * B-d | \
1407 * | \ <- smaller scope is better if
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08001408 * B-15 | \ if scope is enough for destination.
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001409 * | ret = B - scope (-1 <= scope >= d <= 15).
1410 * d-C-1 | /
1411 * |/ <- greater is better
1412 * -C / if scope is not enough for destination.
1413 * /| ret = scope - C (-1 <= d < scope <= 15).
1414 *
1415 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1416 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1417 * Assume B = 0 and we get C > 29.
1418 */
1419 ret = __ipv6_addr_src_scope(score->addr_type);
1420 if (ret >= dst->scope)
1421 ret = -ret;
1422 else
1423 ret -= 128; /* 30 is enough */
1424 score->scopedist = ret;
1425 break;
1426 case IPV6_SADDR_RULE_PREFERRED:
Erik Kline7fd25612014-10-28 18:11:14 +09001427 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001428 /* Rule 3: Avoid deprecated and optimistic addresses */
Erik Kline7fd25612014-10-28 18:11:14 +09001429 u8 avoid = IFA_F_DEPRECATED;
1430
1431 if (!ipv6_use_optimistic_addr(score->ifa->idev))
1432 avoid |= IFA_F_OPTIMISTIC;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001433 ret = ipv6_saddr_preferred(score->addr_type) ||
Erik Kline7fd25612014-10-28 18:11:14 +09001434 !(score->ifa->flags & avoid);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001435 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001436 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001437#ifdef CONFIG_IPV6_MIP6
1438 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001439 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001440 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001441 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1442 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001443 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001444 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001445#endif
1446 case IPV6_SADDR_RULE_OIF:
1447 /* Rule 5: Prefer outgoing interface */
1448 ret = (!dst->ifindex ||
1449 dst->ifindex == score->ifa->idev->dev->ifindex);
1450 break;
1451 case IPV6_SADDR_RULE_LABEL:
1452 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001453 ret = ipv6_addr_label(net,
1454 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001455 score->ifa->idev->dev->ifindex) == dst->label;
1456 break;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001457 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001458 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001459 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001460 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001461 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001462 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1463 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1464 score->ifa->idev->cnf.use_tempaddr >= 2;
1465 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001466 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001467 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001468 case IPV6_SADDR_RULE_ORCHID:
1469 /* Rule 8-: Prefer ORCHID vs ORCHID or
1470 * non-ORCHID vs non-ORCHID
1471 */
1472 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1473 ipv6_addr_orchid(dst->addr));
1474 break;
1475 case IPV6_SADDR_RULE_PREFIX:
1476 /* Rule 8: Use longest matching prefix */
YOSHIFUJI Hideaki / 吉藤英明91b4b042012-09-10 18:41:07 +00001477 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1478 if (ret > score->ifa->prefix_len)
1479 ret = score->ifa->prefix_len;
1480 score->matchlen = ret;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001481 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001482#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1483 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1484 /* Optimistic addresses still have lower precedence than other
1485 * preferred addresses.
1486 */
1487 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1488 break;
1489#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001490 default:
1491 ret = 0;
1492 }
1493
1494 if (ret)
1495 __set_bit(i, score->scorebits);
1496 score->rule = i;
1497out:
1498 return ret;
1499}
1500
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001501static int __ipv6_dev_get_saddr(struct net *net,
1502 struct ipv6_saddr_dst *dst,
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001503 struct inet6_dev *idev,
1504 struct ipv6_saddr_score *scores,
1505 int hiscore_idx)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001506{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001507 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001508
1509 read_lock_bh(&idev->lock);
1510 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1511 int i;
1512
1513 /*
1514 * - Tentative Address (RFC2462 section 5.4)
1515 * - A tentative address is not considered
1516 * "assigned to an interface" in the traditional
1517 * sense, unless it is also flagged as optimistic.
1518 * - Candidate Source Address (section 4)
1519 * - In any case, anycast addresses, multicast
1520 * addresses, and the unspecified address MUST
1521 * NOT be included in a candidate set.
1522 */
1523 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1524 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1525 continue;
1526
1527 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1528
1529 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1530 score->addr_type & IPV6_ADDR_MULTICAST)) {
1531 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1532 idev->dev->name);
1533 continue;
1534 }
1535
1536 score->rule = -1;
1537 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1538
1539 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1540 int minihiscore, miniscore;
1541
1542 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1543 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1544
1545 if (minihiscore > miniscore) {
1546 if (i == IPV6_SADDR_RULE_SCOPE &&
1547 score->scopedist > 0) {
1548 /*
1549 * special case:
1550 * each remaining entry
1551 * has too small (not enough)
1552 * scope, because ifa entries
1553 * are sorted by their scope
1554 * values.
1555 */
1556 goto out;
1557 }
1558 break;
1559 } else if (minihiscore < miniscore) {
1560 if (hiscore->ifa)
1561 in6_ifa_put(hiscore->ifa);
1562
1563 in6_ifa_hold(score->ifa);
1564
1565 swap(hiscore, score);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001566 hiscore_idx = 1 - hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001567
1568 /* restore our iterator */
1569 score->ifa = hiscore->ifa;
1570
1571 break;
1572 }
1573 }
1574 }
1575out:
1576 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001577 return hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001578}
1579
David Ahernafbac6012016-06-16 16:24:26 -07001580static int ipv6_get_saddr_master(struct net *net,
1581 const struct net_device *dst_dev,
1582 const struct net_device *master,
1583 struct ipv6_saddr_dst *dst,
1584 struct ipv6_saddr_score *scores,
1585 int hiscore_idx)
1586{
1587 struct inet6_dev *idev;
1588
1589 idev = __in6_dev_get(dst_dev);
1590 if (idev)
1591 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1592 scores, hiscore_idx);
1593
1594 idev = __in6_dev_get(master);
1595 if (idev)
1596 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1597 scores, hiscore_idx);
1598
1599 return hiscore_idx;
1600}
1601
Patrick McHardyb3f644f2012-08-26 19:14:14 +02001602int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001603 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001604 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001606 struct ipv6_saddr_score scores[2], *hiscore;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001607 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001608 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001609 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001610 int dst_type;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001611 bool use_oif_addr = false;
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001612 int hiscore_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001614 dst_type = __ipv6_addr_type(daddr);
1615 dst.addr = daddr;
1616 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1617 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001618 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001619 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001620
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001621 scores[hiscore_idx].rule = -1;
1622 scores[hiscore_idx].ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001624 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001625
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001626 /* Candidate Source Address (section 4)
1627 * - multicast and link-local destination address,
1628 * the set of candidate source address MUST only
1629 * include addresses assigned to interfaces
1630 * belonging to the same link as the outgoing
1631 * interface.
1632 * (- For site-local destination addresses, the
1633 * set of candidate source addresses MUST only
1634 * include addresses assigned to interfaces
1635 * belonging to the same site as the outgoing
1636 * interface.)
Erik Kline3985e8a2015-07-22 16:38:25 +09001637 * - "It is RECOMMENDED that the candidate source addresses
1638 * be the set of unicast addresses assigned to the
1639 * interface that will be used to send to the destination
1640 * (the 'outgoing' interface)." (RFC 6724)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001641 */
1642 if (dst_dev) {
Erik Kline3985e8a2015-07-22 16:38:25 +09001643 idev = __in6_dev_get(dst_dev);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001644 if ((dst_type & IPV6_ADDR_MULTICAST) ||
Erik Kline3985e8a2015-07-22 16:38:25 +09001645 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1646 (idev && idev->cnf.use_oif_addrs_only)) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001647 use_oif_addr = true;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001648 }
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001649 }
1650
1651 if (use_oif_addr) {
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001652 if (idev)
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001653 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001654 } else {
David Ahernafbac6012016-06-16 16:24:26 -07001655 const struct net_device *master;
1656 int master_idx = 0;
1657
1658 /* if dst_dev exists and is enslaved to an L3 device, then
1659 * prefer addresses from dst_dev and then the master over
1660 * any other enslaved devices in the L3 domain.
1661 */
1662 master = l3mdev_master_dev_rcu(dst_dev);
1663 if (master) {
1664 master_idx = master->ifindex;
1665
1666 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1667 master, &dst,
1668 scores, hiscore_idx);
1669
1670 if (scores[hiscore_idx].ifa)
1671 goto out;
1672 }
1673
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001674 for_each_netdev_rcu(net, dev) {
David Ahernafbac6012016-06-16 16:24:26 -07001675 /* only consider addresses on devices in the
1676 * same L3 domain
1677 */
1678 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1679 continue;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001680 idev = __in6_dev_get(dev);
1681 if (!idev)
1682 continue;
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001683 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001684 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001685 }
David Ahernafbac6012016-06-16 16:24:26 -07001686
1687out:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001688 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001690 hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001691 if (!hiscore->ifa)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001692 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001693
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001694 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001695 in6_ifa_put(hiscore->ifa);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001696 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001698EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Amerigo Wang89657792013-06-29 21:30:49 +08001700int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001701 u32 banned_flags)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001702{
1703 struct inet6_ifaddr *ifp;
1704 int err = -EADDRNOTAVAIL;
1705
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01001706 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1707 if (ifp->scope > IFA_LINK)
1708 break;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001709 if (ifp->scope == IFA_LINK &&
1710 !(ifp->flags & banned_flags)) {
1711 *addr = ifp->addr;
1712 err = 0;
1713 break;
1714 }
1715 }
1716 return err;
1717}
1718
Neil Horman95c385b2007-04-25 17:08:10 -07001719int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001720 u32 banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
1722 struct inet6_dev *idev;
1723 int err = -EADDRNOTAVAIL;
1724
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001725 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001726 idev = __in6_dev_get(dev);
1727 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 read_lock_bh(&idev->lock);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001729 err = __ipv6_get_lladdr(idev, addr, banned_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 read_unlock_bh(&idev->lock);
1731 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001732 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 return err;
1734}
1735
1736static int ipv6_count_addresses(struct inet6_dev *idev)
1737{
1738 int cnt = 0;
1739 struct inet6_ifaddr *ifp;
1740
1741 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001742 list_for_each_entry(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 cnt++;
1744 read_unlock_bh(&idev->lock);
1745 return cnt;
1746}
1747
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001748int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Florian Westphal2a7851b2013-05-17 03:56:10 +00001749 const struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Erik Klinec58da4c2015-02-04 20:01:23 +09001751 return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1752}
1753EXPORT_SYMBOL(ipv6_chk_addr);
1754
1755int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1756 const struct net_device *dev, int strict,
1757 u32 banned_flags)
1758{
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001759 struct inet6_ifaddr *ifp;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001760 unsigned int hash = inet6_addr_hash(addr);
Erik Klinec58da4c2015-02-04 20:01:23 +09001761 u32 ifp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
stephen hemminger5c578ae2010-03-17 20:31:11 +00001763 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001764 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001765 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001766 continue;
Erik Klinec58da4c2015-02-04 20:01:23 +09001767 /* Decouple optimistic from tentative for evaluation here.
1768 * Ban optimistic addresses explicitly, when required.
1769 */
1770 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1771 ? (ifp->flags&~IFA_F_TENTATIVE)
1772 : ifp->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (ipv6_addr_equal(&ifp->addr, addr) &&
Erik Klinec58da4c2015-02-04 20:01:23 +09001774 !(ifp_flags&banned_flags) &&
Ian Morris63159f22015-03-29 14:00:04 +01001775 (!dev || ifp->idev->dev == dev ||
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001776 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1777 rcu_read_unlock_bh();
1778 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
1780 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001781
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001782 rcu_read_unlock_bh();
1783 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
Erik Klinec58da4c2015-02-04 20:01:23 +09001785EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001786
David S. Miller3e81c6d2010-03-20 16:18:00 -07001787static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1788 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
Eric Dumazetddbe5032012-07-18 08:11:12 +00001790 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001791 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Sasha Levinb67bfe02013-02-27 17:06:00 -08001793 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001794 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001795 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001797 if (!dev || ifp->idev->dev == dev)
David S. Miller3e81c6d2010-03-20 16:18:00 -07001798 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
1800 }
David S. Miller3e81c6d2010-03-20 16:18:00 -07001801 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001804/* Compares an address/prefix_len with addresses on device @dev.
1805 * If one is found it returns true.
1806 */
1807bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1808 const unsigned int prefix_len, struct net_device *dev)
1809{
1810 struct inet6_dev *idev;
1811 struct inet6_ifaddr *ifa;
1812 bool ret = false;
1813
1814 rcu_read_lock();
1815 idev = __in6_dev_get(dev);
1816 if (idev) {
1817 read_lock_bh(&idev->lock);
1818 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1819 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1820 if (ret)
1821 break;
1822 }
1823 read_unlock_bh(&idev->lock);
1824 }
1825 rcu_read_unlock();
1826
1827 return ret;
1828}
1829EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1830
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001831int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001832{
1833 struct inet6_dev *idev;
1834 struct inet6_ifaddr *ifa;
1835 int onlink;
1836
1837 onlink = 0;
1838 rcu_read_lock();
1839 idev = __in6_dev_get(dev);
1840 if (idev) {
1841 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001842 list_for_each_entry(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001843 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1844 ifa->prefix_len);
1845 if (onlink)
1846 break;
1847 }
1848 read_unlock_bh(&idev->lock);
1849 }
1850 rcu_read_unlock();
1851 return onlink;
1852}
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001853EXPORT_SYMBOL(ipv6_chk_prefix);
1854
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001855struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001856 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
David S. Millerb79d1d52010-03-25 21:39:21 -07001858 struct inet6_ifaddr *ifp, *result = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001859 unsigned int hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
stephen hemminger5c578ae2010-03-17 20:31:11 +00001861 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001862 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001863 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001864 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001866 if (!dev || ifp->idev->dev == dev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07001868 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 in6_ifa_hold(ifp);
1870 break;
1871 }
1872 }
1873 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001874 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
David S. Millerb79d1d52010-03-25 21:39:21 -07001876 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879/* Gets referenced address, destroys ifaddr */
1880
Brian Haleycc411d02009-09-09 14:41:32 +00001881static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
Lubomir Rintel3d171f32016-01-08 13:47:23 +01001883 if (dad_failed)
1884 ifp->flags |= IFA_F_DADFAILED;
1885
Sabrina Dubrocad2c95122017-06-29 16:56:54 +02001886 if (ifp->flags&IFA_F_TEMPORARY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 struct inet6_ifaddr *ifpub;
1888 spin_lock_bh(&ifp->lock);
1889 ifpub = ifp->ifpub;
1890 if (ifpub) {
1891 in6_ifa_hold(ifpub);
1892 spin_unlock_bh(&ifp->lock);
1893 ipv6_create_tempaddr(ifpub, ifp);
1894 in6_ifa_put(ifpub);
1895 } else {
1896 spin_unlock_bh(&ifp->lock);
1897 }
1898 ipv6_del_addr(ifp);
Sabrina Dubrocad2c95122017-06-29 16:56:54 +02001899 } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
1900 spin_lock_bh(&ifp->lock);
1901 addrconf_del_dad_work(ifp);
1902 ifp->flags |= IFA_F_TENTATIVE;
1903 spin_unlock_bh(&ifp->lock);
1904 if (dad_failed)
1905 ipv6_ifa_notify(0, ifp);
1906 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001907 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
1911
Herbert Xuf2344a12010-05-18 15:55:27 -07001912static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1913{
1914 int err = -ENOENT;
1915
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001916 spin_lock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001917 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1918 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1919 err = 0;
1920 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001921 spin_unlock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001922
1923 return err;
1924}
1925
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001926void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1927{
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001928 struct inet6_dev *idev = ifp->idev;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001929 struct net *net = dev_net(ifp->idev->dev);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001930
Ursula Braun853dc2e2010-10-24 23:06:43 +00001931 if (addrconf_dad_end(ifp)) {
1932 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07001933 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00001934 }
Herbert Xuf2344a12010-05-18 15:55:27 -07001935
Joe Perchese87cc472012-05-13 21:56:26 +00001936 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1937 ifp->idev->dev->name, &ifp->addr);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001938
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001939 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001940
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001941 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
1942 int scope = ifp->scope;
1943 u32 flags = ifp->flags;
1944 struct in6_addr new_addr;
1945 struct inet6_ifaddr *ifp2;
1946 u32 valid_lft, preferred_lft;
1947 int pfxlen = ifp->prefix_len;
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001948 int retries = ifp->stable_privacy_retry + 1;
1949
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001950 if (retries > net->ipv6.sysctl.idgen_retries) {
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001951 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
1952 ifp->idev->dev->name);
1953 goto errdad;
1954 }
1955
1956 new_addr = ifp->addr;
1957 if (ipv6_generate_stable_address(&new_addr, retries,
1958 idev))
1959 goto errdad;
1960
1961 valid_lft = ifp->valid_lft;
1962 preferred_lft = ifp->prefered_lft;
1963
1964 spin_unlock_bh(&ifp->lock);
1965
1966 if (idev->cnf.max_addresses &&
1967 ipv6_count_addresses(idev) >=
1968 idev->cnf.max_addresses)
1969 goto lock_errdad;
1970
1971 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
1972 ifp->idev->dev->name);
1973
1974 ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
1975 scope, flags, valid_lft,
1976 preferred_lft);
1977 if (IS_ERR(ifp2))
1978 goto lock_errdad;
1979
1980 spin_lock_bh(&ifp2->lock);
1981 ifp2->stable_privacy_retry = retries;
1982 ifp2->state = INET6_IFADDR_STATE_PREDAD;
1983 spin_unlock_bh(&ifp2->lock);
1984
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001985 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001986 in6_ifa_put(ifp2);
1987lock_errdad:
1988 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001989 }
1990
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001991errdad:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001992 /* transition from _POSTDAD to _ERRDAD */
1993 ifp->state = INET6_IFADDR_STATE_ERRDAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001994 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001995
1996 addrconf_mod_dad_work(ifp, 0);
Wei Yongjun751eb6b2016-09-05 16:06:31 +08001997 in6_ifa_put(ifp);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001998}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002000/* Join to solicited addr multicast group.
2001 * caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002002void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
2004 struct in6_addr maddr;
2005
2006 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2007 return;
2008
2009 addrconf_addr_solict_mult(addr, &maddr);
2010 ipv6_dev_mc_inc(dev, &maddr);
2011}
2012
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002013/* caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002014void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
2016 struct in6_addr maddr;
2017
2018 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2019 return;
2020
2021 addrconf_addr_solict_mult(addr, &maddr);
2022 __ipv6_dev_mc_dec(idev, &maddr);
2023}
2024
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002025/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002026static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
2028 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002029
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002030 if (ifp->prefix_len >= 127) /* RFC 6164 */
Bjørn Mork2bda8a02011-07-05 23:04:13 +00002031 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2033 if (ipv6_addr_any(&addr))
2034 return;
WANG Cong013b4d92014-09-11 15:35:11 -07002035 __ipv6_dev_ac_inc(ifp->idev, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036}
2037
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002038/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002039static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
2041 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002042
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002043 if (ifp->prefix_len >= 127) /* RFC 6164 */
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00002044 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2046 if (ipv6_addr_any(&addr))
2047 return;
2048 __ipv6_dev_ac_dec(ifp->idev, &addr);
2049}
2050
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002051static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
2052{
Alexander Aring5241c2d2015-12-14 20:55:22 +01002053 if (dev->addr_len != EUI64_ADDR_LEN)
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002054 return -1;
Alexander Aring5241c2d2015-12-14 20:55:22 +01002055 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
YOSHIFUJI Hideaki / 吉藤英明5e98a362013-01-28 10:44:29 +00002056 eui[0] ^= 2;
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002057 return 0;
2058}
2059
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002060static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2061{
2062 union fwnet_hwaddr *ha;
2063
2064 if (dev->addr_len != FWNET_ALEN)
2065 return -1;
2066
2067 ha = (union fwnet_hwaddr *)dev->dev_addr;
2068
2069 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2070 eui[0] ^= 2;
2071 return 0;
2072}
2073
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002074static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2075{
2076 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2077 if (dev->addr_len != ARCNET_ALEN)
2078 return -1;
2079 memset(eui, 0, 7);
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002080 eui[7] = *(u8 *)dev->dev_addr;
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002081 return 0;
2082}
2083
2084static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2085{
2086 if (dev->addr_len != INFINIBAND_ALEN)
2087 return -1;
2088 memcpy(eui, dev->dev_addr + 12, 8);
2089 eui[0] |= 2;
2090 return 0;
2091}
2092
stephen hemmingerc61393e2010-10-04 20:17:53 +00002093static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002094{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00002095 if (addr == 0)
2096 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002097 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2098 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2099 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2100 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2101 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2102 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2103 eui[1] = 0;
2104 eui[2] = 0x5E;
2105 eui[3] = 0xFE;
2106 memcpy(eui + 4, &addr, 4);
2107 return 0;
2108}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002109
2110static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2111{
2112 if (dev->priv_flags & IFF_ISATAP)
2113 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2114 return -1;
2115}
2116
stephen hemmingeraee80b52011-06-08 10:44:30 +00002117static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2118{
2119 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2120}
2121
Nicolas Dichtele8377352013-08-20 12:16:06 +02002122static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2123{
2124 memcpy(eui, dev->perm_addr, 3);
2125 memcpy(eui + 5, dev->perm_addr + 3, 3);
2126 eui[3] = 0xFF;
2127 eui[4] = 0xFE;
2128 eui[0] ^= 2;
2129 return 0;
2130}
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2133{
2134 switch (dev->type) {
2135 case ARPHRD_ETHER:
2136 case ARPHRD_FDDI:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002137 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002139 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002141 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002142 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002143 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002144 case ARPHRD_IPGRE:
2145 return addrconf_ifid_gre(eui, dev);
Jukka Rissanene74bccb2013-12-11 17:05:36 +02002146 case ARPHRD_6LOWPAN:
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002147 return addrconf_ifid_eui64(eui, dev);
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002148 case ARPHRD_IEEE1394:
2149 return addrconf_ifid_ieee1394(eui, dev);
Nicolas Dichtele8377352013-08-20 12:16:06 +02002150 case ARPHRD_TUNNEL6:
2151 return addrconf_ifid_ip6tnl(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 }
2153 return -1;
2154}
2155
2156static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2157{
2158 int err = -1;
2159 struct inet6_ifaddr *ifp;
2160
2161 read_lock_bh(&idev->lock);
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01002162 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2163 if (ifp->scope > IFA_LINK)
2164 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2166 memcpy(eui, ifp->addr.s6_addr+8, 8);
2167 err = 0;
2168 break;
2169 }
2170 }
2171 read_unlock_bh(&idev->lock);
2172 return err;
2173}
2174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002176static void ipv6_regen_rndid(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08002179 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 /*
2183 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2184 * check if generated address is not inappropriate
2185 *
2186 * - Reserved subnet anycast (RFC 2526)
2187 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002188 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 * 00-00-5E-FE-xx-xx-xx-xx
2190 * - value 0
2191 * - XXX: already assigned to an address on the device
2192 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002193 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2195 (idev->rndid[7]&0x80))
2196 goto regen;
2197 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2198 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2199 goto regen;
2200 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2201 goto regen;
2202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203}
2204
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002205static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002206{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002208 ipv6_regen_rndid(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +09002211u32 addrconf_rt_table(const struct net_device *dev, u32 default_table) {
2212 /* Determines into what table to put autoconf PIO/RIO/default routes
2213 * learned on this device.
2214 *
2215 * - If 0, use the same table for every device. This puts routes into
2216 * one of RT_TABLE_{PREFIX,INFO,DFLT} depending on the type of route
2217 * (but note that these three are currently all equal to
2218 * RT6_TABLE_MAIN).
2219 * - If > 0, use the specified table.
2220 * - If < 0, put routes into table dev->ifindex + (-rt_table).
2221 */
2222 struct inet6_dev *idev = in6_dev_get(dev);
2223 u32 table;
2224 int sysctl = idev->cnf.accept_ra_rt_table;
2225 if (sysctl == 0) {
2226 table = default_table;
2227 } else if (sysctl > 0) {
2228 table = (u32) sysctl;
2229 } else {
2230 table = (unsigned) dev->ifindex + (-sysctl);
2231 }
2232 in6_dev_put(idev);
2233 return table;
2234}
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236/*
2237 * Add prefix route.
2238 */
2239
2240static void
2241addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002242 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
Thomas Graf86872cb2006-08-22 00:01:08 -07002244 struct fib6_config cfg = {
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +09002245 .fc_table = l3mdev_fib_table(dev) ? : addrconf_rt_table(dev, RT6_TABLE_PREFIX),
Thomas Graf86872cb2006-08-22 00:01:08 -07002246 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2247 .fc_ifindex = dev->ifindex,
2248 .fc_expires = expires,
2249 .fc_dst_len = plen,
2250 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002251 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07002252 .fc_protocol = RTPROT_KERNEL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002253 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002255 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257 /* Prevent useless cloning on PtP SIT.
2258 This thing is done here expecting that the whole
2259 class of non-broadcast devices need not cloning.
2260 */
Amerigo Wang07a93622012-10-29 16:23:10 +00002261#if IS_ENABLED(CONFIG_IPV6_SIT)
Thomas Graf86872cb2006-08-22 00:01:08 -07002262 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2263 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002264#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Thomas Graf86872cb2006-08-22 00:01:08 -07002266 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267}
2268
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002269
2270static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2271 int plen,
2272 const struct net_device *dev,
2273 u32 flags, u32 noflags)
2274{
2275 struct fib6_node *fn;
2276 struct rt6_info *rt = NULL;
2277 struct fib6_table *table;
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +09002278 u32 tb_id = l3mdev_fib_table(dev) ? : addrconf_rt_table(dev, RT6_TABLE_PREFIX);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002279
David Ahernca254492015-10-12 11:47:10 -07002280 table = fib6_get_table(dev_net(dev), tb_id);
Ian Morris63159f22015-03-29 14:00:04 +01002281 if (!table)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002282 return NULL;
2283
Li RongQing5744dd92012-09-11 21:59:01 +00002284 read_lock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002285 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2286 if (!fn)
2287 goto out;
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07002288
2289 noflags |= RTF_CACHE;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002290 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002291 if (rt->dst.dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002292 continue;
2293 if ((rt->rt6i_flags & flags) != flags)
2294 continue;
Romain Kuntz85da53b2013-01-09 15:02:26 +01002295 if ((rt->rt6i_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002296 continue;
2297 dst_hold(&rt->dst);
2298 break;
2299 }
2300out:
Li RongQing5744dd92012-09-11 21:59:01 +00002301 read_unlock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002302 return rt;
2303}
2304
2305
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306/* Create "default" multicast route to the interface */
2307
2308static void addrconf_add_mroute(struct net_device *dev)
2309{
Thomas Graf86872cb2006-08-22 00:01:08 -07002310 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002311 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002312 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2313 .fc_ifindex = dev->ifindex,
2314 .fc_dst_len = 8,
2315 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002316 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002317 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Thomas Graf86872cb2006-08-22 00:01:08 -07002319 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2320
2321 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322}
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2325{
2326 struct inet6_dev *idev;
2327
2328 ASSERT_RTNL();
2329
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002330 idev = ipv6_find_idev(dev);
2331 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00002332 return ERR_PTR(-ENOBUFS);
2333
2334 if (idev->cnf.disable_ipv6)
2335 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337 /* Add default multicast route */
David Ahernba46ee42016-06-13 13:44:18 -07002338 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
Li Wei4af04ab2011-12-06 21:23:45 +00002339 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return idev;
2342}
2343
Jiri Pirko53bd6742013-12-06 09:45:22 +01002344static void manage_tempaddrs(struct inet6_dev *idev,
2345 struct inet6_ifaddr *ifp,
2346 __u32 valid_lft, __u32 prefered_lft,
2347 bool create, unsigned long now)
2348{
2349 u32 flags;
2350 struct inet6_ifaddr *ift;
2351
2352 read_lock_bh(&idev->lock);
2353 /* update all temporary addresses in the list */
2354 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2355 int age, max_valid, max_prefered;
2356
2357 if (ifp != ift->ifpub)
2358 continue;
2359
2360 /* RFC 4941 section 3.3:
2361 * If a received option will extend the lifetime of a public
2362 * address, the lifetimes of temporary addresses should
2363 * be extended, subject to the overall constraint that no
2364 * temporary addresses should ever remain "valid" or "preferred"
2365 * for a time longer than (TEMP_VALID_LIFETIME) or
2366 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2367 */
2368 age = (now - ift->cstamp) / HZ;
2369 max_valid = idev->cnf.temp_valid_lft - age;
2370 if (max_valid < 0)
2371 max_valid = 0;
2372
2373 max_prefered = idev->cnf.temp_prefered_lft -
Jiri Bohac76506a92016-10-13 18:52:15 +02002374 idev->desync_factor - age;
Jiri Pirko53bd6742013-12-06 09:45:22 +01002375 if (max_prefered < 0)
2376 max_prefered = 0;
2377
2378 if (valid_lft > max_valid)
2379 valid_lft = max_valid;
2380
2381 if (prefered_lft > max_prefered)
2382 prefered_lft = max_prefered;
2383
2384 spin_lock(&ift->lock);
2385 flags = ift->flags;
2386 ift->valid_lft = valid_lft;
2387 ift->prefered_lft = prefered_lft;
2388 ift->tstamp = now;
2389 if (prefered_lft > 0)
2390 ift->flags &= ~IFA_F_DEPRECATED;
2391
2392 spin_unlock(&ift->lock);
2393 if (!(flags&IFA_F_TENTATIVE))
2394 ipv6_ifa_notify(0, ift);
2395 }
2396
2397 if ((create || list_empty(&idev->tempaddr_list)) &&
2398 idev->cnf.use_tempaddr > 0) {
2399 /* When a new public address is created as described
2400 * in [ADDRCONF], also create a new temporary address.
2401 * Also create a temporary address if it's enabled but
2402 * no temporary address currently exists.
2403 */
2404 read_unlock_bh(&idev->lock);
2405 ipv6_create_tempaddr(ifp, NULL);
2406 } else {
2407 read_unlock_bh(&idev->lock);
2408 }
2409}
2410
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002411static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2412{
2413 return idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2414 idev->addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2415}
2416
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002417int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2418 const struct prefix_info *pinfo,
2419 struct inet6_dev *in6_dev,
2420 const struct in6_addr *addr, int addr_type,
2421 u32 addr_flags, bool sllao, bool tokenized,
2422 __u32 valid_lft, u32 prefered_lft)
Alexander Aring4f672232016-06-15 21:20:22 +02002423{
2424 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2425 int create = 0, update_lft = 0;
2426
2427 if (!ifp && valid_lft) {
2428 int max_addresses = in6_dev->cnf.max_addresses;
2429
2430#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2431 if (in6_dev->cnf.optimistic_dad &&
2432 !net->ipv6.devconf_all->forwarding && sllao)
2433 addr_flags |= IFA_F_OPTIMISTIC;
2434#endif
2435
2436 /* Do not allow to create too much of autoconfigured
2437 * addresses; this would be too easy way to crash kernel.
2438 */
2439 if (!max_addresses ||
2440 ipv6_count_addresses(in6_dev) < max_addresses)
2441 ifp = ipv6_add_addr(in6_dev, addr, NULL,
2442 pinfo->prefix_len,
2443 addr_type&IPV6_ADDR_SCOPE_MASK,
2444 addr_flags, valid_lft,
2445 prefered_lft);
2446
2447 if (IS_ERR_OR_NULL(ifp))
2448 return -1;
2449
2450 update_lft = 0;
2451 create = 1;
2452 spin_lock_bh(&ifp->lock);
2453 ifp->flags |= IFA_F_MANAGETEMPADDR;
2454 ifp->cstamp = jiffies;
2455 ifp->tokenized = tokenized;
2456 spin_unlock_bh(&ifp->lock);
2457 addrconf_dad_start(ifp);
2458 }
2459
2460 if (ifp) {
2461 u32 flags;
2462 unsigned long now;
2463 u32 stored_lft;
2464
2465 /* update lifetime (RFC2462 5.5.3 e) */
2466 spin_lock_bh(&ifp->lock);
2467 now = jiffies;
2468 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2469 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2470 else
2471 stored_lft = 0;
2472 if (!update_lft && !create && stored_lft) {
2473 const u32 minimum_lft = min_t(u32,
2474 stored_lft, MIN_VALID_LIFETIME);
2475 valid_lft = max(valid_lft, minimum_lft);
2476
2477 /* RFC4862 Section 5.5.3e:
2478 * "Note that the preferred lifetime of the
2479 * corresponding address is always reset to
2480 * the Preferred Lifetime in the received
2481 * Prefix Information option, regardless of
2482 * whether the valid lifetime is also reset or
2483 * ignored."
2484 *
2485 * So we should always update prefered_lft here.
2486 */
2487 update_lft = 1;
2488 }
2489
2490 if (update_lft) {
2491 ifp->valid_lft = valid_lft;
2492 ifp->prefered_lft = prefered_lft;
2493 ifp->tstamp = now;
2494 flags = ifp->flags;
2495 ifp->flags &= ~IFA_F_DEPRECATED;
2496 spin_unlock_bh(&ifp->lock);
2497
2498 if (!(flags&IFA_F_TENTATIVE))
2499 ipv6_ifa_notify(0, ifp);
2500 } else
2501 spin_unlock_bh(&ifp->lock);
2502
2503 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2504 create, now);
2505
2506 in6_ifa_put(ifp);
2507 addrconf_verify();
2508 }
2509
2510 return 0;
2511}
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002512EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
Alexander Aring4f672232016-06-15 21:20:22 +02002513
Neil Hormane6bff992012-01-04 10:49:15 +00002514void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515{
2516 struct prefix_info *pinfo;
2517 __u32 valid_lft;
2518 __u32 prefered_lft;
Alexander Aring4f672232016-06-15 21:20:22 +02002519 int addr_type, err;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002520 u32 addr_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07002522 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 if (len < sizeof(struct prefix_info)) {
dingtianhongba3542e2013-08-17 10:27:04 +08002527 ADBG("addrconf: prefix option too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 return;
2529 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 /*
2532 * Validation checks ([ADDRCONF], page 19)
2533 */
2534
2535 addr_type = ipv6_addr_type(&pinfo->prefix);
2536
2537 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2538 return;
2539
2540 valid_lft = ntohl(pinfo->valid);
2541 prefered_lft = ntohl(pinfo->prefered);
2542
2543 if (prefered_lft > valid_lft) {
Joe Perchese87cc472012-05-13 21:56:26 +00002544 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 return;
2546 }
2547
2548 in6_dev = in6_dev_get(dev);
2549
Ian Morris63159f22015-03-29 14:00:04 +01002550 if (!in6_dev) {
Joe Perchese87cc472012-05-13 21:56:26 +00002551 net_dbg_ratelimited("addrconf: device %s not configured\n",
2552 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 return;
2554 }
2555
2556 /*
2557 * Two things going on here:
2558 * 1) Add routes for on-link prefixes
2559 * 2) Configure prefixes with the auto flag set
2560 */
2561
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002562 if (pinfo->onlink) {
2563 struct rt6_info *rt;
2564 unsigned long rt_expires;
2565
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002566 /* Avoid arithmetic overflow. Really, we could
2567 * save rt_expires in seconds, likely valid_lft,
2568 * but it would require division in fib gc, that it
2569 * not good.
2570 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002571 if (HZ > USER_HZ)
2572 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2573 else
2574 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08002575
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002576 if (addrconf_finite_timeout(rt_expires))
2577 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002579 rt = addrconf_get_prefix_route(&pinfo->prefix,
2580 pinfo->prefix_len,
2581 dev,
2582 RTF_ADDRCONF | RTF_PREFIX_RT,
2583 RTF_GATEWAY | RTF_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002585 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002586 /* Autoconf prefix route */
2587 if (valid_lft == 0) {
2588 ip6_del_rt(rt);
2589 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002590 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002591 /* not infinity */
Gao feng1716a962012-04-06 00:13:10 +00002592 rt6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002593 } else {
Gao feng1716a962012-04-06 00:13:10 +00002594 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 }
2596 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002597 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002598 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2599 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002600 /* not infinity */
2601 flags |= RTF_EXPIRES;
2602 expires = jiffies_to_clock_t(rt_expires);
2603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002605 dev, expires, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00002607 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 }
2609
2610 /* Try to figure out our local address for this prefix */
2611
2612 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 struct in6_addr addr;
Alexander Aringf997c552016-06-15 21:20:23 +02002614 bool tokenized = false, dev_addr_generated = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 if (pinfo->prefix_len == 64) {
2617 memcpy(&addr, &pinfo->prefix, 8);
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002618
2619 if (!ipv6_addr_any(&in6_dev->token)) {
2620 read_lock_bh(&in6_dev->lock);
2621 memcpy(addr.s6_addr + 8,
2622 in6_dev->token.s6_addr + 8, 8);
2623 read_unlock_bh(&in6_dev->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00002624 tokenized = true;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002625 } else if (is_addr_mode_generate_stable(in6_dev) &&
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002626 !ipv6_generate_stable_address(&addr, 0,
2627 in6_dev)) {
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002628 addr_flags |= IFA_F_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002629 goto ok;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002630 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2631 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
Alexander Aring4f672232016-06-15 21:20:22 +02002632 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002633 } else {
2634 dev_addr_generated = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 }
2636 goto ok;
2637 }
Joe Perchese87cc472012-05-13 21:56:26 +00002638 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2639 pinfo->prefix_len);
Alexander Aring4f672232016-06-15 21:20:22 +02002640 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
2642ok:
Alexander Aring4f672232016-06-15 21:20:22 +02002643 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2644 &addr, addr_type,
2645 addr_flags, sllao,
2646 tokenized, valid_lft,
2647 prefered_lft);
2648 if (err)
2649 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002650
2651 /* Ignore error case here because previous prefix add addr was
2652 * successful which will be notified.
2653 */
2654 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2655 addr_type, addr_flags, sllao,
2656 tokenized, valid_lft,
2657 prefered_lft,
2658 dev_addr_generated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 }
2660 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
Alexander Aring4f672232016-06-15 21:20:22 +02002661put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 in6_dev_put(in6_dev);
2663}
2664
2665/*
2666 * Set destination address.
2667 * Special case for SIT interfaces where we create a new "virtual"
2668 * device.
2669 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002670int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
2672 struct in6_ifreq ireq;
2673 struct net_device *dev;
2674 int err = -EINVAL;
2675
2676 rtnl_lock();
2677
2678 err = -EFAULT;
2679 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2680 goto err_exit;
2681
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002682 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
2684 err = -ENODEV;
Ian Morris63159f22015-03-29 14:00:04 +01002685 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 goto err_exit;
2687
Amerigo Wang07a93622012-10-29 16:23:10 +00002688#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002690 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 struct ip_tunnel_parm p;
2693
2694 err = -EADDRNOTAVAIL;
2695 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2696 goto err_exit;
2697
2698 memset(&p, 0, sizeof(p));
2699 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2700 p.iph.saddr = 0;
2701 p.iph.version = 4;
2702 p.iph.ihl = 5;
2703 p.iph.protocol = IPPROTO_IPV6;
2704 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002705 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002707 if (ops->ndo_do_ioctl) {
2708 mm_segment_t oldfs = get_fs();
2709
2710 set_fs(KERNEL_DS);
2711 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2712 set_fs(oldfs);
2713 } else
2714 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
2716 if (err == 0) {
2717 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002718 dev = __dev_get_by_name(net, p.name);
2719 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 goto err_exit;
2721 err = dev_open(dev);
2722 }
2723 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002724#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
2726err_exit:
2727 rtnl_unlock();
2728 return err;
2729}
2730
Madhu Challa93a714d2015-02-25 09:58:35 -08002731static int ipv6_mc_config(struct sock *sk, bool join,
2732 const struct in6_addr *addr, int ifindex)
2733{
2734 int ret;
2735
2736 ASSERT_RTNL();
2737
2738 lock_sock(sk);
2739 if (join)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002740 ret = ipv6_sock_mc_join(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002741 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002742 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002743 release_sock(sk);
2744
2745 return ret;
2746}
2747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748/*
2749 * Manual configuration of address on an interface
2750 */
Jiri Pirko479840f2013-12-06 09:45:21 +01002751static int inet6_addr_add(struct net *net, int ifindex,
2752 const struct in6_addr *pfx,
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002753 const struct in6_addr *peer_pfx,
Jiri Pirko479840f2013-12-06 09:45:21 +01002754 unsigned int plen, __u32 ifa_flags,
2755 __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756{
2757 struct inet6_ifaddr *ifp;
2758 struct inet6_dev *idev;
2759 struct net_device *dev;
Madhu Challa93a714d2015-02-25 09:58:35 -08002760 unsigned long timeout;
2761 clock_t expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002763 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002766
Thomas Graf24ef0da2008-05-28 16:54:22 +02002767 if (plen > 128)
2768 return -EINVAL;
2769
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002770 /* check the lifetime */
2771 if (!valid_lft || prefered_lft > valid_lft)
2772 return -EINVAL;
2773
Jiri Pirko53bd6742013-12-06 09:45:22 +01002774 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2775 return -EINVAL;
2776
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002777 dev = __dev_get_by_index(net, ifindex);
2778 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002780
Brian Haley64e724f2010-07-20 10:34:30 +00002781 idev = addrconf_add_dev(dev);
2782 if (IS_ERR(idev))
2783 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Madhu Challa93a714d2015-02-25 09:58:35 -08002785 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2786 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2787 true, pfx, ifindex);
2788
2789 if (ret < 0)
2790 return ret;
2791 }
2792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 scope = ipv6_addr_scope(pfx);
2794
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002795 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2796 if (addrconf_finite_timeout(timeout)) {
2797 expires = jiffies_to_clock_t(timeout * HZ);
2798 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002799 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002800 } else {
2801 expires = 0;
2802 flags = 0;
2803 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002804 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002805
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002806 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2807 if (addrconf_finite_timeout(timeout)) {
2808 if (timeout == 0)
2809 ifa_flags |= IFA_F_DEPRECATED;
2810 prefered_lft = timeout;
2811 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002812
Jiri Benc8a226b22013-08-01 10:41:28 +02002813 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2814 valid_lft, prefered_lft);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002815
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 if (!IS_ERR(ifp)) {
Thomas Haller761aac72014-01-15 15:36:58 +01002817 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2818 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2819 expires, flags);
2820 }
2821
Neil Horman95c385b2007-04-25 17:08:10 -07002822 /*
2823 * Note that section 3.1 of RFC 4429 indicates
2824 * that the Optimistic flag should not be set for
2825 * manually configured addresses
2826 */
David S. Millercf22f9a2012-04-14 21:37:40 -04002827 addrconf_dad_start(ifp);
Jiri Pirko53bd6742013-12-06 09:45:22 +01002828 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2829 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2830 true, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002832 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 return 0;
Madhu Challa93a714d2015-02-25 09:58:35 -08002834 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2835 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2836 false, pfx, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 }
2838
2839 return PTR_ERR(ifp);
2840}
2841
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002842static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2843 const struct in6_addr *pfx, unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
2845 struct inet6_ifaddr *ifp;
2846 struct inet6_dev *idev;
2847 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002848
Thomas Graf24ef0da2008-05-28 16:54:22 +02002849 if (plen > 128)
2850 return -EINVAL;
2851
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002852 dev = __dev_get_by_index(net, ifindex);
2853 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 return -ENODEV;
2855
Ian Morrise5d08d72014-11-23 21:28:43 +00002856 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01002857 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 return -ENXIO;
2859
2860 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002861 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 if (ifp->prefix_len == plen &&
2863 ipv6_addr_equal(pfx, &ifp->addr)) {
2864 in6_ifa_hold(ifp);
2865 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002866
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002867 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2868 (ifa_flags & IFA_F_MANAGETEMPADDR))
2869 manage_tempaddrs(idev, ifp, 0, 0, false,
2870 jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 ipv6_del_addr(ifp);
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002872 addrconf_verify_rtnl();
Madhu Challa93a714d2015-02-25 09:58:35 -08002873 if (ipv6_addr_is_multicast(pfx)) {
2874 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2875 false, pfx, dev->ifindex);
2876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return 0;
2878 }
2879 }
2880 read_unlock_bh(&idev->lock);
2881 return -EADDRNOTAVAIL;
2882}
2883
2884
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002885int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
2887 struct in6_ifreq ireq;
2888 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002889
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002890 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2894 return -EFAULT;
2895
2896 rtnl_lock();
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002897 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002898 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2899 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 rtnl_unlock();
2901 return err;
2902}
2903
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002904int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
2906 struct in6_ifreq ireq;
2907 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002908
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002909 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 return -EPERM;
2911
2912 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2913 return -EFAULT;
2914
2915 rtnl_lock();
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002916 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002917 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 rtnl_unlock();
2919 return err;
2920}
2921
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002922static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2923 int plen, int scope)
2924{
2925 struct inet6_ifaddr *ifp;
2926
Jiri Benc8a226b22013-08-01 10:41:28 +02002927 ifp = ipv6_add_addr(idev, addr, NULL, plen,
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01002928 scope, IFA_F_PERMANENT,
2929 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002930 if (!IS_ERR(ifp)) {
2931 spin_lock_bh(&ifp->lock);
2932 ifp->flags &= ~IFA_F_TENTATIVE;
2933 spin_unlock_bh(&ifp->lock);
Paolo Abeni764d3be2016-11-22 16:57:40 +01002934 rt_genid_bump_ipv6(dev_net(idev->dev));
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002935 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2936 in6_ifa_put(ifp);
2937 }
2938}
2939
Amerigo Wang07a93622012-10-29 16:23:10 +00002940#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941static void sit_add_v4_addrs(struct inet6_dev *idev)
2942{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 struct in6_addr addr;
2944 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002945 struct net *net = dev_net(idev->dev);
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002946 int scope, plen;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002947 u32 pflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
2949 ASSERT_RTNL();
2950
2951 memset(&addr, 0, sizeof(struct in6_addr));
2952 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2953
2954 if (idev->dev->flags&IFF_POINTOPOINT) {
2955 addr.s6_addr32[0] = htonl(0xfe800000);
2956 scope = IFA_LINK;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002957 plen = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 } else {
2959 scope = IPV6_ADDR_COMPATv4;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002960 plen = 96;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002961 pflags |= RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 }
2963
2964 if (addr.s6_addr32[3]) {
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002965 add_addr(idev, &addr, plen, scope);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002966 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 return;
2968 }
2969
Benjamin Thery6fda7352008-03-05 10:47:47 -08002970 for_each_netdev(net, dev) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002971 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 if (in_dev && (dev->flags & IFF_UP)) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002973 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
2975 int flag = scope;
2976
2977 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
2979 addr.s6_addr32[3] = ifa->ifa_local;
2980
2981 if (ifa->ifa_scope == RT_SCOPE_LINK)
2982 continue;
2983 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2984 if (idev->dev->flags&IFF_POINTOPOINT)
2985 continue;
2986 flag |= IFA_HOST;
2987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002989 add_addr(idev, &addr, plen, flag);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002990 addrconf_prefix_route(&addr, plen, idev->dev, 0,
2991 pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 }
2993 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002996#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
2998static void init_loopback(struct net_device *dev)
2999{
3000 struct inet6_dev *idev;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303001 struct net_device *sp_dev;
3002 struct inet6_ifaddr *sp_ifa;
3003 struct rt6_info *sp_rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
3005 /* ::1 */
3006
3007 ASSERT_RTNL();
3008
Ian Morrise5d08d72014-11-23 21:28:43 +00003009 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003010 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003011 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 return;
3013 }
3014
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003015 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303016
3017 /* Add routes to other interface's IPv6 addresses */
3018 for_each_netdev(dev_net(dev), sp_dev) {
3019 if (!strcmp(sp_dev->name, dev->name))
3020 continue;
3021
3022 idev = __in6_dev_get(sp_dev);
3023 if (!idev)
3024 continue;
3025
3026 read_lock_bh(&idev->lock);
3027 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
3028
3029 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
3030 continue;
3031
Gao feng33d99112014-01-24 16:29:11 +08003032 if (sp_ifa->rt) {
3033 /* This dst has been added to garbage list when
3034 * lo device down, release this obsolete dst and
3035 * reallocate a new router for ifa.
3036 */
Nicolas Dichtela2204452016-10-12 10:10:40 +02003037 if (!atomic_read(&sp_ifa->rt->rt6i_ref)) {
Gao feng33d99112014-01-24 16:29:11 +08003038 ip6_rt_put(sp_ifa->rt);
3039 sp_ifa->rt = NULL;
3040 } else {
3041 continue;
3042 }
3043 }
Gao fenga881ae12013-06-16 11:14:30 +08003044
François-Xavier Le Bail57ec0af2013-12-02 11:28:49 +01003045 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303046
3047 /* Failure cases are ignored */
Gao feng534c8772013-06-02 22:16:21 +00003048 if (!IS_ERR(sp_rt)) {
3049 sp_ifa->rt = sp_rt;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303050 ip6_ins_rt(sp_rt);
Gao feng534c8772013-06-02 22:16:21 +00003051 }
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303052 }
3053 read_unlock_bh(&idev->lock);
3054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055}
3056
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003057void addrconf_add_linklocal(struct inet6_dev *idev,
3058 const struct in6_addr *addr, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003060 struct inet6_ifaddr *ifp;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003061 u32 addr_flags = flags | IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Neil Horman95c385b2007-04-25 17:08:10 -07003063#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3064 if (idev->cnf.optimistic_dad &&
David Miller702beb82008-07-20 18:17:02 -07003065 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
Neil Horman95c385b2007-04-25 17:08:10 -07003066 addr_flags |= IFA_F_OPTIMISTIC;
3067#endif
3068
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01003069 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
3070 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003072 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
David S. Millercf22f9a2012-04-14 21:37:40 -04003073 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 in6_ifa_put(ifp);
3075 }
3076}
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003077EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003079static bool ipv6_reserved_interfaceid(struct in6_addr address)
3080{
3081 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3082 return true;
3083
3084 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3085 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3086 return true;
3087
3088 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3089 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3090 return true;
3091
3092 return false;
3093}
3094
3095static int ipv6_generate_stable_address(struct in6_addr *address,
3096 u8 dad_count,
3097 const struct inet6_dev *idev)
3098{
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003099 static DEFINE_SPINLOCK(lock);
3100 static __u32 digest[SHA_DIGEST_WORDS];
3101 static __u32 workspace[SHA_WORKSPACE_WORDS];
3102
3103 static union {
3104 char __data[SHA_MESSAGE_BYTES];
3105 struct {
3106 struct in6_addr secret;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003107 __be32 prefix[2];
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003108 unsigned char hwaddr[MAX_ADDR_LEN];
3109 u8 dad_count;
3110 } __packed;
3111 } data;
3112
3113 struct in6_addr secret;
3114 struct in6_addr temp;
3115 struct net *net = dev_net(idev->dev);
3116
3117 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3118
3119 if (idev->cnf.stable_secret.initialized)
3120 secret = idev->cnf.stable_secret.secret;
3121 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3122 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3123 else
3124 return -1;
3125
3126retry:
3127 spin_lock_bh(&lock);
3128
3129 sha_init(digest);
3130 memset(&data, 0, sizeof(data));
3131 memset(workspace, 0, sizeof(workspace));
3132 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003133 data.prefix[0] = address->s6_addr32[0];
3134 data.prefix[1] = address->s6_addr32[1];
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003135 data.secret = secret;
3136 data.dad_count = dad_count;
3137
3138 sha_transform(digest, data.__data, workspace);
3139
3140 temp = *address;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003141 temp.s6_addr32[2] = (__force __be32)digest[0];
3142 temp.s6_addr32[3] = (__force __be32)digest[1];
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003143
3144 spin_unlock_bh(&lock);
3145
3146 if (ipv6_reserved_interfaceid(temp)) {
3147 dad_count++;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01003148 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003149 return -1;
3150 goto retry;
3151 }
3152
3153 *address = temp;
3154 return 0;
3155}
3156
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003157static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3158{
3159 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3160
3161 if (s->initialized)
3162 return;
3163 s = &idev->cnf.stable_secret;
3164 get_random_bytes(&s->secret, sizeof(s->secret));
3165 s->initialized = true;
3166}
3167
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003168static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3169{
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003170 struct in6_addr addr;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003171
David Ahernca254492015-10-12 11:47:10 -07003172 /* no link local addresses on L3 master devices */
3173 if (netif_is_l3_master(idev->dev))
3174 return;
3175
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003176 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3177
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003178 switch (idev->addr_gen_mode) {
3179 case IN6_ADDR_GEN_MODE_RANDOM:
3180 ipv6_gen_mode_random_init(idev);
3181 /* fallthrough */
3182 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003183 if (!ipv6_generate_stable_address(&addr, 0, idev))
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003184 addrconf_add_linklocal(idev, &addr,
3185 IFA_F_STABLE_PRIVACY);
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003186 else if (prefix_route)
3187 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003188 break;
3189 case IN6_ADDR_GEN_MODE_EUI64:
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003190 /* addrconf_add_linklocal also adds a prefix_route and we
3191 * only need to care about prefix routes if ipv6_generate_eui64
3192 * couldn't generate one.
3193 */
3194 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003195 addrconf_add_linklocal(idev, &addr, 0);
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003196 else if (prefix_route)
3197 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003198 break;
3199 case IN6_ADDR_GEN_MODE_NONE:
3200 default:
3201 /* will not add any link local address */
3202 break;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003203 }
3204}
3205
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206static void addrconf_dev_config(struct net_device *dev)
3207{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003208 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
3210 ASSERT_RTNL();
3211
Herbert Xu74235a22007-06-14 13:02:55 -07003212 if ((dev->type != ARPHRD_ETHER) &&
3213 (dev->type != ARPHRD_FDDI) &&
Herbert Xu74235a22007-06-14 13:02:55 -07003214 (dev->type != ARPHRD_ARCNET) &&
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00003215 (dev->type != ARPHRD_INFINIBAND) &&
Nicolas Dichtele8377352013-08-20 12:16:06 +02003216 (dev->type != ARPHRD_IEEE1394) &&
Jukka Rissanene74bccb2013-12-11 17:05:36 +02003217 (dev->type != ARPHRD_TUNNEL6) &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003218 (dev->type != ARPHRD_6LOWPAN) &&
3219 (dev->type != ARPHRD_NONE)) {
Herbert Xu74235a22007-06-14 13:02:55 -07003220 /* Alas, we support only Ethernet autoconfiguration. */
3221 return;
3222 }
3223
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00003225 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 return;
3227
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003228 /* this device type has no EUI support */
3229 if (dev->type == ARPHRD_NONE &&
3230 idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3231 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3232
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003233 addrconf_addr_gen(idev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234}
3235
Amerigo Wang07a93622012-10-29 16:23:10 +00003236#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237static void addrconf_sit_config(struct net_device *dev)
3238{
3239 struct inet6_dev *idev;
3240
3241 ASSERT_RTNL();
3242
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003243 /*
3244 * Configure the tunnel with one of our IPv4
3245 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 * our v4 addrs in the tunnel
3247 */
3248
Ian Morrise5d08d72014-11-23 21:28:43 +00003249 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003250 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003251 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 return;
3253 }
3254
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003255 if (dev->priv_flags & IFF_ISATAP) {
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003256 addrconf_addr_gen(idev, false);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003257 return;
3258 }
3259
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 sit_add_v4_addrs(idev);
3261
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00003262 if (dev->flags&IFF_POINTOPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003265#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
Amerigo Wang07a93622012-10-29 16:23:10 +00003267#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003268static void addrconf_gre_config(struct net_device *dev)
3269{
3270 struct inet6_dev *idev;
stephen hemmingeraee80b52011-06-08 10:44:30 +00003271
stephen hemmingeraee80b52011-06-08 10:44:30 +00003272 ASSERT_RTNL();
3273
Ian Morrise5d08d72014-11-23 21:28:43 +00003274 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003275 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003276 pr_debug("%s: add_dev failed\n", __func__);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003277 return;
3278 }
3279
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003280 addrconf_addr_gen(idev, true);
Hannes Frederic Sowad9e4ce62015-10-08 18:19:39 +02003281 if (dev->flags & IFF_POINTOPOINT)
3282 addrconf_add_mroute(dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003283}
3284#endif
3285
David Ahernf1705ec2016-02-24 09:25:37 -08003286static int fixup_permanent_addr(struct inet6_dev *idev,
3287 struct inet6_ifaddr *ifp)
3288{
David Ahern62817c32017-04-25 09:17:29 -07003289 /* rt6i_ref == 0 means the host route was removed from the
3290 * FIB, for example, if 'lo' device is taken down. In that
3291 * case regenerate the host route.
3292 */
3293 if (!ifp->rt || !atomic_read(&ifp->rt->rt6i_ref)) {
3294 struct rt6_info *rt, *prev;
David Ahernf1705ec2016-02-24 09:25:37 -08003295
3296 rt = addrconf_dst_alloc(idev, &ifp->addr, false);
3297 if (unlikely(IS_ERR(rt)))
3298 return PTR_ERR(rt);
3299
David Ahern62817c32017-04-25 09:17:29 -07003300 /* ifp->rt can be accessed outside of rtnl */
3301 spin_lock(&ifp->lock);
3302 prev = ifp->rt;
David Ahernf1705ec2016-02-24 09:25:37 -08003303 ifp->rt = rt;
David Ahern62817c32017-04-25 09:17:29 -07003304 spin_unlock(&ifp->lock);
3305
3306 ip6_rt_put(prev);
David Ahernf1705ec2016-02-24 09:25:37 -08003307 }
3308
3309 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3310 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3311 idev->dev, 0, 0);
3312 }
3313
David Ahern0f4ac292017-05-02 14:43:44 -07003314 if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3315 addrconf_dad_start(ifp);
David Ahernf1705ec2016-02-24 09:25:37 -08003316
3317 return 0;
3318}
3319
3320static void addrconf_permanent_addr(struct net_device *dev)
3321{
3322 struct inet6_ifaddr *ifp, *tmp;
3323 struct inet6_dev *idev;
3324
3325 idev = __in6_dev_get(dev);
3326 if (!idev)
3327 return;
3328
3329 write_lock_bh(&idev->lock);
3330
3331 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3332 if ((ifp->flags & IFA_F_PERMANENT) &&
3333 fixup_permanent_addr(idev, ifp) < 0) {
3334 write_unlock_bh(&idev->lock);
Eric Dumazetd87890d2017-10-30 22:47:09 -07003335 in6_ifa_hold(ifp);
David Ahernf1705ec2016-02-24 09:25:37 -08003336 ipv6_del_addr(ifp);
3337 write_lock_bh(&idev->lock);
3338
3339 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3340 idev->dev->name, &ifp->addr);
3341 }
3342 }
3343
3344 write_unlock_bh(&idev->lock);
3345}
3346
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003347static int addrconf_notify(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00003348 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349{
Jiri Pirko351638e2013-05-28 01:30:21 +00003350 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
David Ahern4f7f34e2016-04-07 11:10:41 -07003351 struct netdev_notifier_changeupper_info *info;
Eric Dumazet748e2d92012-08-22 21:50:59 +00003352 struct inet6_dev *idev = __in6_dev_get(dev);
WANG Cong0526ff32017-06-21 14:34:58 -07003353 struct net *net = dev_net(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003354 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07003355 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003357 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003358 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07003359 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003360 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -07003361 if (IS_ERR(idev))
3362 return notifier_from_errno(PTR_ERR(idev));
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003363 }
3364 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003365
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003366 case NETDEV_CHANGEMTU:
3367 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3368 if (dev->mtu < IPV6_MIN_MTU) {
WANG Cong0526ff32017-06-21 14:34:58 -07003369 addrconf_ifdown(dev, dev != net->loopback_dev);
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003370 break;
3371 }
3372
3373 if (idev) {
3374 rt6_mtu_change(dev, dev->mtu);
3375 idev->cnf.mtu6 = dev->mtu;
3376 break;
3377 }
3378
3379 /* allocate new idev */
3380 idev = ipv6_add_dev(dev);
3381 if (IS_ERR(idev))
3382 break;
3383
3384 /* device is still not ready */
3385 if (!(idev->if_flags & IF_READY))
3386 break;
3387
3388 run_pending = 1;
3389
3390 /* fall through */
3391
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003393 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07003394 if (dev->flags & IFF_SLAVE)
3395 break;
3396
WANG Cong3ce62a82014-09-11 15:07:16 -07003397 if (idev && idev->cnf.disable_ipv6)
3398 break;
3399
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003400 if (event == NETDEV_UP) {
David Ahern38bd10c2016-04-21 20:56:12 -07003401 /* restore routes for permanent addresses */
3402 addrconf_permanent_addr(dev);
3403
Mike Manning4bf42a22017-09-25 22:01:36 +01003404 if (!addrconf_link_ready(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003405 /* device is not ready yet. */
Joe Perchesf3213832012-05-15 14:11:53 +00003406 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003407 dev->name);
3408 break;
3409 }
Kristian Slavov99081042006-02-08 16:10:53 -08003410
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11003411 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3412 idev = ipv6_add_dev(dev);
3413
WANG Conga317a2f2014-07-25 15:25:09 -07003414 if (!IS_ERR_OR_NULL(idev)) {
Kristian Slavov99081042006-02-08 16:10:53 -08003415 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08003416 run_pending = 1;
3417 }
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003418 } else if (event == NETDEV_CHANGE) {
Mike Manning4bf42a22017-09-25 22:01:36 +01003419 if (!addrconf_link_ready(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003420 /* device is still not ready. */
3421 break;
3422 }
3423
3424 if (idev) {
Linus Lüssing1cadd392017-05-23 21:53:52 -04003425 if (idev->if_flags & IF_READY) {
3426 /* device is already configured -
3427 * but resend MLD reports, we might
3428 * have roamed and need to update
3429 * multicast snooping switches
3430 */
3431 ipv6_mc_up(idev);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003432 break;
Linus Lüssing1cadd392017-05-23 21:53:52 -04003433 }
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003434 idev->if_flags |= IF_READY;
3435 }
3436
Joe Perchesf3213832012-05-15 14:11:53 +00003437 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3438 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003439
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003440 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003441 }
3442
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003443 switch (dev->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +00003444#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 case ARPHRD_SIT:
3446 addrconf_sit_config(dev);
3447 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07003448#endif
Amerigo Wang07a93622012-10-29 16:23:10 +00003449#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003450 case ARPHRD_IPGRE:
3451 addrconf_gre_config(dev);
3452 break;
3453#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 case ARPHRD_LOOPBACK:
3455 init_loopback(dev);
3456 break;
3457
3458 default:
3459 addrconf_dev_config(dev);
3460 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003461 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003462
WANG Conga317a2f2014-07-25 15:25:09 -07003463 if (!IS_ERR_OR_NULL(idev)) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003464 if (run_pending)
3465 addrconf_dad_run(idev);
3466
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003467 /*
3468 * If the MTU changed during the interface down,
3469 * when the interface up, the changed MTU must be
3470 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003472 if (idev->cnf.mtu6 != dev->mtu &&
3473 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 rt6_mtu_change(dev, dev->mtu);
3475 idev->cnf.mtu6 = dev->mtu;
3476 }
3477 idev->tstamp = jiffies;
3478 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003479
3480 /*
3481 * If the changed mtu during down is lower than
3482 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 */
3484 if (dev->mtu < IPV6_MIN_MTU)
WANG Cong0526ff32017-06-21 14:34:58 -07003485 addrconf_ifdown(dev, dev != net->loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 }
3487 break;
3488
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 case NETDEV_DOWN:
3490 case NETDEV_UNREGISTER:
3491 /*
3492 * Remove all addresses from this interface.
3493 */
3494 addrconf_ifdown(dev, event != NETDEV_DOWN);
3495 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003496
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07003499 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003500 addrconf_sysctl_unregister(idev);
WANG Conga317a2f2014-07-25 15:25:09 -07003501 err = addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07003502 if (err)
3503 return notifier_from_errno(err);
WANG Conga317a2f2014-07-25 15:25:09 -07003504 err = snmp6_register_dev(idev);
3505 if (err) {
3506 addrconf_sysctl_unregister(idev);
3507 return notifier_from_errno(err);
3508 }
Stephen Hemminger5632c512007-04-28 21:16:39 -07003509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003511
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003512 case NETDEV_PRE_TYPE_CHANGE:
3513 case NETDEV_POST_TYPE_CHANGE:
Andrew Lunn3ef09522015-12-03 21:12:32 +01003514 if (idev)
3515 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07003516 break;
David Ahern4f7f34e2016-04-07 11:10:41 -07003517
3518 case NETDEV_CHANGEUPPER:
3519 info = ptr;
3520
3521 /* flush all routes if dev is linked to or unlinked from
3522 * an L3 master device (e.g., VRF)
3523 */
3524 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3525 addrconf_ifdown(dev, 0);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527
3528 return NOTIFY_OK;
3529}
3530
3531/*
3532 * addrconf module should be notified of a device going up
3533 */
3534static struct notifier_block ipv6_dev_notf = {
3535 .notifier_call = addrconf_notify,
WANG Cong8795ee72017-05-08 10:12:13 -07003536 .priority = ADDRCONF_NOTIFY_PRIORITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537};
3538
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003539static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07003540{
3541 struct inet6_dev *idev;
3542 ASSERT_RTNL();
3543
3544 idev = __in6_dev_get(dev);
3545
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003546 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003547 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003548 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003549 ipv6_mc_unmap(idev);
3550}
3551
David Ahern70af9212016-04-08 12:01:21 -07003552static bool addr_is_local(const struct in6_addr *addr)
3553{
3554 return ipv6_addr_type(addr) &
3555 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3556}
3557
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558static int addrconf_ifdown(struct net_device *dev, int how)
3559{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003560 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003561 struct inet6_dev *idev;
David Ahernf1705ec2016-02-24 09:25:37 -08003562 struct inet6_ifaddr *ifa, *tmp;
3563 struct list_head del_list;
3564 int _keep_addr;
3565 bool keep_addr;
David S. Miller73a8bd72011-01-23 23:27:15 -08003566 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567
3568 ASSERT_RTNL();
3569
David S. Miller73a8bd72011-01-23 23:27:15 -08003570 rt6_ifdown(net, dev);
3571 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572
3573 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003574 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 return -ENODEV;
3576
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003577 /*
3578 * Step 1: remove reference to ipv6 device from parent device.
3579 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003581 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003583
3584 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003585 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
3587 /* Step 1.5: remove snmp6 entry */
3588 snmp6_unregister_dev(idev);
3589
3590 }
3591
David Ahernf1705ec2016-02-24 09:25:37 -08003592 /* aggregate the system setting and interface setting */
3593 _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3594 if (!_keep_addr)
3595 _keep_addr = idev->cnf.keep_addr_on_down;
3596
3597 /* combine the user config with event to determine if permanent
3598 * addresses are to be removed from address hash table
3599 */
Mike Manningbc561632016-08-12 12:02:38 +01003600 keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003601
David S. Miller73a8bd72011-01-23 23:27:15 -08003602 /* Step 2: clear hash table */
3603 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3604 struct hlist_head *h = &inet6_addr_lst[i];
David S. Miller73a8bd72011-01-23 23:27:15 -08003605
3606 spin_lock_bh(&addrconf_hash_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01003607restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08003608 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
David S. Miller73a8bd72011-01-23 23:27:15 -08003609 if (ifa->idev == idev) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003610 addrconf_del_dad_work(ifa);
David Ahernf1705ec2016-02-24 09:25:37 -08003611 /* combined flag + permanent flag decide if
3612 * address is retained on a down event
3613 */
3614 if (!keep_addr ||
David Ahern70af9212016-04-08 12:01:21 -07003615 !(ifa->flags & IFA_F_PERMANENT) ||
3616 addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003617 hlist_del_init_rcu(&ifa->addr_lst);
3618 goto restart;
3619 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003620 }
3621 }
3622 spin_unlock_bh(&addrconf_hash_lock);
3623 }
3624
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 write_lock_bh(&idev->lock);
3626
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003627 addrconf_del_rs_timer(idev);
3628
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003629 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003630 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003631 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003633 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00003634 while (!list_empty(&idev->tempaddr_list)) {
3635 ifa = list_first_entry(&idev->tempaddr_list,
3636 struct inet6_ifaddr, tmp_list);
3637 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 write_unlock_bh(&idev->lock);
3639 spin_lock_bh(&ifa->lock);
3640
3641 if (ifa->ifpub) {
3642 in6_ifa_put(ifa->ifpub);
3643 ifa->ifpub = NULL;
3644 }
3645 spin_unlock_bh(&ifa->lock);
3646 in6_ifa_put(ifa);
3647 write_lock_bh(&idev->lock);
3648 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003649
David Ahernf1705ec2016-02-24 09:25:37 -08003650 /* re-combine the user config with event to determine if permanent
3651 * addresses are to be removed from the interface list
3652 */
Mike Manningbc561632016-08-12 12:02:38 +01003653 keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003654
3655 INIT_LIST_HEAD(&del_list);
3656 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
David Ahern38bd10c2016-04-21 20:56:12 -07003657 struct rt6_info *rt = NULL;
Rabin Vincentc86872a42017-04-10 08:36:39 +02003658 bool keep;
David Ahern38bd10c2016-04-21 20:56:12 -07003659
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003660 addrconf_del_dad_work(ifa);
stephen hemminger84e8b802010-03-02 13:32:46 +00003661
Rabin Vincentc86872a42017-04-10 08:36:39 +02003662 keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3663 !addr_is_local(&ifa->addr);
3664 if (!keep)
3665 list_move(&ifa->if_list, &del_list);
3666
David S. Miller73a8bd72011-01-23 23:27:15 -08003667 write_unlock_bh(&idev->lock);
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003668 spin_lock_bh(&ifa->lock);
David Ahernf1705ec2016-02-24 09:25:37 -08003669
Rabin Vincentc86872a42017-04-10 08:36:39 +02003670 if (keep) {
David Ahernf1705ec2016-02-24 09:25:37 -08003671 /* set state to skip the notifier below */
3672 state = INET6_IFADDR_STATE_DEAD;
David Ahern0f4ac292017-05-02 14:43:44 -07003673 ifa->state = INET6_IFADDR_STATE_PREDAD;
David Ahernf1705ec2016-02-24 09:25:37 -08003674 if (!(ifa->flags & IFA_F_NODAD))
3675 ifa->flags |= IFA_F_TENTATIVE;
David Ahern38bd10c2016-04-21 20:56:12 -07003676
3677 rt = ifa->rt;
3678 ifa->rt = NULL;
David Ahernf1705ec2016-02-24 09:25:37 -08003679 } else {
3680 state = ifa->state;
3681 ifa->state = INET6_IFADDR_STATE_DEAD;
David Ahernf1705ec2016-02-24 09:25:37 -08003682 }
3683
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003684 spin_unlock_bh(&ifa->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003685
David Ahern38bd10c2016-04-21 20:56:12 -07003686 if (rt)
3687 ip6_del_rt(rt);
3688
David S. Miller73a8bd72011-01-23 23:27:15 -08003689 if (state != INET6_IFADDR_STATE_DEAD) {
3690 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Cong Wangf88c91d2013-04-14 23:18:43 +08003691 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
Mike Manningea06f712016-07-22 18:32:11 +01003692 } else {
3693 if (idev->cnf.forwarding)
3694 addrconf_leave_anycast(ifa);
3695 addrconf_leave_solict(ifa->idev, &ifa->addr);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00003696 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003697
David S. Miller73a8bd72011-01-23 23:27:15 -08003698 write_lock_bh(&idev->lock);
3699 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003700
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 write_unlock_bh(&idev->lock);
3702
David Ahernf1705ec2016-02-24 09:25:37 -08003703 /* now clean up addresses to be removed */
3704 while (!list_empty(&del_list)) {
3705 ifa = list_first_entry(&del_list,
3706 struct inet6_ifaddr, if_list);
3707 list_del(&ifa->if_list);
3708
3709 in6_ifa_put(ifa);
3710 }
3711
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003712 /* Step 5: Discard anycast and multicast list */
3713 if (how) {
3714 ipv6_ac_destroy_dev(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 ipv6_mc_destroy_dev(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003716 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 ipv6_mc_down(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003721
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003722 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003723 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003724 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 neigh_parms_release(&nd_tbl, idev->nd_parms);
3726 neigh_ifdown(&nd_tbl, dev);
3727 in6_dev_put(idev);
3728 }
3729 return 0;
3730}
3731
3732static void addrconf_rs_timer(unsigned long data)
3733{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003734 struct inet6_dev *idev = (struct inet6_dev *)data;
Cong Wangcaf92bc2013-08-31 13:44:32 +08003735 struct net_device *dev = idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003736 struct in6_addr lladdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003738 write_lock(&idev->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00003739 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 goto out;
3741
Shmulik Ladkani9ba2add2012-12-02 01:44:53 +00003742 if (!ipv6_accept_ra(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003744
3745 /* Announcement received after solicitation was sent */
3746 if (idev->if_flags & IF_RA_RCVD)
3747 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003749 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
Cong Wangcaf92bc2013-08-31 13:44:32 +08003750 write_unlock(&idev->lock);
3751 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3752 ndisc_send_rs(dev, &lladdr,
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003753 &in6addr_linklocal_allrouters);
3754 else
Cong Wangcaf92bc2013-08-31 13:44:32 +08003755 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
Cong Wangcaf92bc2013-08-31 13:44:32 +08003757 write_lock(&idev->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003758 idev->rs_interval = rfc3315_s14_backoff_update(
3759 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003760 /* The wait after the last probe can be shorter */
3761 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3762 idev->cnf.rtr_solicits) ?
3763 idev->cnf.rtr_solicit_delay :
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003764 idev->rs_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 /*
3767 * Note: we do not support deprecated "all on-link"
3768 * assumption any longer.
3769 */
Joe Perches91df42b2012-05-15 14:11:54 +00003770 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 }
3772
3773out:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003774 write_unlock(&idev->lock);
Cong Wangcaf92bc2013-08-31 13:44:32 +08003775put:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003776 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777}
3778
3779/*
3780 * Duplicate Address Detection
3781 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003782static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3783{
3784 unsigned long rand_num;
3785 struct inet6_dev *idev = ifp->idev;
3786
Neil Horman95c385b2007-04-25 17:08:10 -07003787 if (ifp->flags & IFA_F_OPTIMISTIC)
3788 rand_num = 0;
3789 else
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003790 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
Neil Horman95c385b2007-04-25 17:08:10 -07003791
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003792 ifp->dad_probes = idev->cnf.dad_transmits;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003793 addrconf_mod_dad_work(ifp, rand_num);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003794}
3795
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003796static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797{
3798 struct inet6_dev *idev = ifp->idev;
3799 struct net_device *dev = idev->dev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003800 bool bump_id, notify = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
3802 addrconf_join_solict(dev, &ifp->addr);
3803
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003804 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
3806 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003807 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003808 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810
3811 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09003812 idev->cnf.accept_dad < 1 ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003813 !(ifp->flags&IFA_F_TENTATIVE) ||
3814 ifp->flags & IFA_F_NODAD) {
Paolo Abeni764d3be2016-11-22 16:57:40 +01003815 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003816 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003817 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 read_unlock_bh(&idev->lock);
3819
Paolo Abeni764d3be2016-11-22 16:57:40 +01003820 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 return;
3822 }
3823
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003824 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003825 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003826 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003827 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003828 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003829 * - keep it tentative if it is a permanent address.
3830 * - otherwise, kill it.
3831 */
3832 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003833 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003834 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003835 }
Neil Horman95c385b2007-04-25 17:08:10 -07003836
3837 /*
3838 * Optimistic nodes can start receiving
3839 * Frames right away
3840 */
Erik Kline7fd25612014-10-28 18:11:14 +09003841 if (ifp->flags & IFA_F_OPTIMISTIC) {
Neil Horman95c385b2007-04-25 17:08:10 -07003842 ip6_ins_rt(ifp->rt);
Erik Kline7fd25612014-10-28 18:11:14 +09003843 if (ipv6_use_optimistic_addr(idev)) {
3844 /* Because optimistic nodes can use this address,
3845 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3846 */
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003847 notify = true;
Erik Kline7fd25612014-10-28 18:11:14 +09003848 }
3849 }
Neil Horman95c385b2007-04-25 17:08:10 -07003850
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003851 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003853 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 read_unlock_bh(&idev->lock);
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003855 if (notify)
3856 ipv6_ifa_notify(RTM_NEWADDR, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857}
3858
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003859static void addrconf_dad_start(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003861 bool begin_dad = false;
3862
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003863 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003864 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3865 ifp->state = INET6_IFADDR_STATE_PREDAD;
3866 begin_dad = true;
3867 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003868 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003869
3870 if (begin_dad)
3871 addrconf_mod_dad_work(ifp, 0);
3872}
3873
3874static void addrconf_dad_work(struct work_struct *w)
3875{
3876 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3877 struct inet6_ifaddr,
3878 dad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 struct inet6_dev *idev = ifp->idev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003880 bool bump_id, disable_ipv6 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 struct in6_addr mcaddr;
3882
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003883 enum {
3884 DAD_PROCESS,
3885 DAD_BEGIN,
3886 DAD_ABORT,
3887 } action = DAD_PROCESS;
3888
3889 rtnl_lock();
3890
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003891 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003892 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3893 action = DAD_BEGIN;
3894 ifp->state = INET6_IFADDR_STATE_DAD;
3895 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3896 action = DAD_ABORT;
3897 ifp->state = INET6_IFADDR_STATE_POSTDAD;
Mike Manning85b51b12016-08-18 14:39:40 +01003898
3899 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6 &&
3900 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
3901 struct in6_addr addr;
3902
3903 addr.s6_addr32[0] = htonl(0xfe800000);
3904 addr.s6_addr32[1] = 0;
3905
3906 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
3907 ipv6_addr_equal(&ifp->addr, &addr)) {
3908 /* DAD failed for link-local based on MAC */
3909 idev->cnf.disable_ipv6 = 1;
3910
3911 pr_info("%s: IPv6 being disabled!\n",
3912 ifp->idev->dev->name);
3913 disable_ipv6 = true;
3914 }
3915 }
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003916 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003917 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003918
3919 if (action == DAD_BEGIN) {
3920 addrconf_dad_begin(ifp);
3921 goto out;
3922 } else if (action == DAD_ABORT) {
Wei Yongjun751eb6b2016-09-05 16:06:31 +08003923 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003924 addrconf_dad_stop(ifp, 1);
Mike Manning85b51b12016-08-18 14:39:40 +01003925 if (disable_ipv6)
3926 addrconf_ifdown(idev->dev, 0);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003927 goto out;
3928 }
3929
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003930 if (!ifp->dad_probes && addrconf_dad_end(ifp))
Herbert Xuf2344a12010-05-18 15:55:27 -07003931 goto out;
3932
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003933 write_lock_bh(&idev->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003934 if (idev->dead || !(idev->if_flags & IF_READY)) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003935 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 goto out;
3937 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003938
3939 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003940 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3941 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003942 write_unlock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003943 goto out;
3944 }
3945
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003946 if (ifp->dad_probes == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 /*
3948 * DAD was successful
3949 */
3950
Paolo Abeni764d3be2016-11-22 16:57:40 +01003951 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003952 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003953 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003954 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
Paolo Abeni764d3be2016-11-22 16:57:40 +01003956 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
3958 goto out;
3959 }
3960
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003961 ifp->dad_probes--;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003962 addrconf_mod_dad_work(ifp,
3963 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
stephen hemminger21809fa2010-02-08 19:48:52 +00003964 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003965 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966
3967 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
Nicolas Dichtel304d8882015-11-27 18:17:05 +01003969 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970out:
3971 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003972 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973}
3974
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003975/* ifp->idev must be at least read locked */
3976static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3977{
3978 struct inet6_ifaddr *ifpiter;
3979 struct inet6_dev *idev = ifp->idev;
3980
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01003981 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3982 if (ifpiter->scope > IFA_LINK)
3983 break;
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003984 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3985 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3986 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
3987 IFA_F_PERMANENT)
3988 return false;
3989 }
3990 return true;
3991}
3992
Paolo Abeni764d3be2016-11-22 16:57:40 +01003993static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003995 struct net_device *dev = ifp->idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003996 struct in6_addr lladdr;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003997 bool send_rs, send_mld;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003998
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003999 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
4001 /*
4002 * Configure the address for reception. Now it is valid.
4003 */
4004
4005 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4006
Tore Anderson026359b2011-08-28 23:47:33 +00004007 /* If added prefix is link local and we are prepared to process
4008 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 */
4010
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004011 read_lock_bh(&ifp->idev->lock);
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004012 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004013 send_rs = send_mld &&
4014 ipv6_accept_ra(ifp->idev) &&
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004015 ifp->idev->cnf.rtr_solicits != 0 &&
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004016 (dev->flags&IFF_LOOPBACK) == 0;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004017 read_unlock_bh(&ifp->idev->lock);
4018
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004019 /* While dad is in progress mld report's source address is in6_addrany.
4020 * Resend with proper ll now.
4021 */
4022 if (send_mld)
4023 ipv6_mc_dad_complete(ifp->idev);
4024
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004025 if (send_rs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 /*
4027 * If a host as already performed a random delay
4028 * [...] as part of DAD [...] there is no need
4029 * to delay again before sending the first RS
4030 */
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004031 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004032 return;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004033 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004035 write_lock_bh(&ifp->idev->lock);
4036 spin_lock(&ifp->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004037 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4038 ifp->idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004039 ifp->idev->rs_probes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 ifp->idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004041 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004042 spin_unlock(&ifp->lock);
4043 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 }
Paolo Abeni764d3be2016-11-22 16:57:40 +01004045
4046 if (bump_id)
4047 rt_genid_bump_ipv6(dev_net(dev));
Marcus Huewed385ed72017-05-23 21:53:49 -04004048
4049 /* Make sure that a new temporary address will be created
4050 * before this temporary address becomes deprecated.
4051 */
4052 if (ifp->flags & IFA_F_TEMPORARY)
4053 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054}
4055
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004056static void addrconf_dad_run(struct inet6_dev *idev)
4057{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004058 struct inet6_ifaddr *ifp;
4059
4060 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00004061 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00004062 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07004063 if (ifp->flags & IFA_F_TENTATIVE &&
4064 ifp->state == INET6_IFADDR_STATE_DAD)
4065 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00004066 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004067 }
4068 read_unlock_bh(&idev->lock);
4069}
4070
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071#ifdef CONFIG_PROC_FS
4072struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08004073 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004075 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076};
4077
Mihai Maruseac1d578302012-01-03 23:31:35 +00004078static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079{
4080 struct inet6_ifaddr *ifa = NULL;
4081 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004082 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00004083 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
Mihai Maruseac1d578302012-01-03 23:31:35 +00004085 /* initial bucket if pos is 0 */
4086 if (pos == 0) {
4087 state->bucket = 0;
4088 state->offset = 0;
4089 }
4090
4091 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08004092 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00004093 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 /* sync with offset */
4097 if (p < state->offset) {
4098 p++;
4099 continue;
4100 }
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004101 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004102 }
4103
4104 /* prepare for next bucket */
4105 state->offset = 0;
4106 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004108 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109}
4110
stephen hemmingerc2e21292010-03-17 20:31:10 +00004111static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4112 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113{
4114 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004115 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
Sasha Levinb67bfe02013-02-27 17:06:00 -08004117 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004118 if (!net_eq(dev_net(ifa->idev->dev), net))
4119 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004120 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004121 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004122 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004123
Jeff Barnhillea143552018-09-21 00:45:27 +00004124 state->offset = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004125 while (++state->bucket < IN6_ADDR_HSIZE) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08004126 hlist_for_each_entry_rcu_bh(ifa,
stephen hemmingerc2e21292010-03-17 20:31:10 +00004127 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004128 if (!net_eq(dev_net(ifa->idev->dev), net))
4129 continue;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004130 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08004131 }
4132 }
4133
stephen hemmingerc2e21292010-03-17 20:31:10 +00004134 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135}
4136
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004138 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139{
stephen hemminger5c578ae2010-03-17 20:31:11 +00004140 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00004141 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142}
4143
4144static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4145{
4146 struct inet6_ifaddr *ifa;
4147
4148 ifa = if6_get_next(seq, v);
4149 ++*pos;
4150 return ifa;
4151}
4152
4153static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004154 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155{
stephen hemminger5c578ae2010-03-17 20:31:11 +00004156 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157}
4158
4159static int if6_seq_show(struct seq_file *seq, void *v)
4160{
4161 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Jiri Pirko971a3512013-12-10 13:56:29 +01004162 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07004163 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 ifp->idev->dev->ifindex,
4165 ifp->prefix_len,
4166 ifp->scope,
Jiri Pirko971a3512013-12-10 13:56:29 +01004167 (u8) ifp->flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 ifp->idev->dev->name);
4169 return 0;
4170}
4171
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004172static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 .start = if6_seq_start,
4174 .next = if6_seq_next,
4175 .show = if6_seq_show,
4176 .stop = if6_seq_stop,
4177};
4178
4179static int if6_seq_open(struct inode *inode, struct file *file)
4180{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004181 return seq_open_net(inode, file, &if6_seq_ops,
4182 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183}
4184
Arjan van de Ven9a321442007-02-12 00:55:35 -08004185static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 .owner = THIS_MODULE,
4187 .open = if6_seq_open,
4188 .read = seq_read,
4189 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004190 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191};
4192
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004193static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194{
Gao fengd4beaa62013-02-18 01:34:54 +00004195 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 return -ENOMEM;
4197 return 0;
4198}
4199
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004200static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08004201{
Gao fengece31ff2013-02-18 01:34:56 +00004202 remove_proc_entry("if_inet6", net->proc_net);
Daniel Lezcano3c400902008-01-10 22:42:49 -08004203}
4204
4205static struct pernet_operations if6_proc_net_ops = {
Ian Morris67ba4152014-08-24 21:53:10 +01004206 .init = if6_proc_net_init,
4207 .exit = if6_proc_net_exit,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004208};
4209
4210int __init if6_proc_init(void)
4211{
4212 return register_pernet_subsys(&if6_proc_net_ops);
4213}
4214
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215void if6_proc_exit(void)
4216{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004217 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218}
4219#endif /* CONFIG_PROC_FS */
4220
Amerigo Wang07a93622012-10-29 16:23:10 +00004221#if IS_ENABLED(CONFIG_IPV6_MIP6)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004222/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004223int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004224{
4225 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004226 struct inet6_ifaddr *ifp = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00004227 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004228
stephen hemminger5c578ae2010-03-17 20:31:11 +00004229 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08004230 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09004231 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08004232 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09004233 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004234 (ifp->flags & IFA_F_HOMEADDRESS)) {
4235 ret = 1;
4236 break;
4237 }
4238 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00004239 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004240 return ret;
4241}
4242#endif
4243
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244/*
4245 * Periodic address status verification
4246 */
4247
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004248static void addrconf_verify_rtnl(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004250 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 int i;
4253
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004254 ASSERT_RTNL();
4255
stephen hemminger5c578ae2010-03-17 20:31:11 +00004256 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004258 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004260 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004262 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263restart:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004264 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
Yasushi Asanofad8da32013-12-31 12:04:19 +09004267 /* When setting preferred_lft to a value not zero or
4268 * infinity, while valid_lft is infinity
4269 * IFA_F_PERMANENT has a non-infinity life time.
4270 */
4271 if ((ifp->flags & IFA_F_PERMANENT) &&
4272 (ifp->prefered_lft == INFINITY_LIFE_TIME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 continue;
4274
4275 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004276 /* We try to batch several events at once. */
4277 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004279 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4280 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 spin_unlock(&ifp->lock);
4282 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 ipv6_del_addr(ifp);
4284 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004285 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4286 spin_unlock(&ifp->lock);
4287 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00004289 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 int deprecate = 0;
4291
4292 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4293 deprecate = 1;
4294 ifp->flags |= IFA_F_DEPRECATED;
4295 }
4296
Yasushi Asanofad8da32013-12-31 12:04:19 +09004297 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4298 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 next = ifp->tstamp + ifp->valid_lft * HZ;
4300
4301 spin_unlock(&ifp->lock);
4302
4303 if (deprecate) {
4304 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
4306 ipv6_ifa_notify(0, ifp);
4307 in6_ifa_put(ifp);
4308 goto restart;
4309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4311 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00004312 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4313 ifp->idev->cnf.dad_transmits *
Jiri Pirko1f9248e52013-12-07 19:26:53 +01004314 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
stephen hemminger88949cf2010-03-17 20:31:17 +00004315
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 if (age >= ifp->prefered_lft - regen_advance) {
4317 struct inet6_ifaddr *ifpub = ifp->ifpub;
4318 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4319 next = ifp->tstamp + ifp->prefered_lft * HZ;
4320 if (!ifp->regen_count && ifpub) {
4321 ifp->regen_count++;
4322 in6_ifa_hold(ifp);
4323 in6_ifa_hold(ifpub);
4324 spin_unlock(&ifp->lock);
stephen hemminger5c578ae2010-03-17 20:31:11 +00004325
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08004326 spin_lock(&ifpub->lock);
4327 ifpub->regen_count = 0;
4328 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 ipv6_create_tempaddr(ifpub, ifp);
4330 in6_ifa_put(ifpub);
4331 in6_ifa_put(ifp);
4332 goto restart;
4333 }
4334 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4335 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4336 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 } else {
4338 /* ifp->prefered_lft <= ifp->valid_lft */
4339 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4340 next = ifp->tstamp + ifp->prefered_lft * HZ;
4341 spin_unlock(&ifp->lock);
4342 }
4343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 }
4345
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004346 next_sec = round_jiffies_up(next);
4347 next_sched = next;
4348
4349 /* If rounded timeout is accurate enough, accept it. */
4350 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4351 next_sched = next_sec;
4352
4353 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4354 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4355 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4356
dingtianhongba3542e2013-08-17 10:27:04 +08004357 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4358 now, next, next_sec, next_sched);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004359 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
stephen hemminger5c578ae2010-03-17 20:31:11 +00004360 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361}
4362
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004363static void addrconf_verify_work(struct work_struct *w)
4364{
4365 rtnl_lock();
4366 addrconf_verify_rtnl();
4367 rtnl_unlock();
4368}
4369
4370static void addrconf_verify(void)
4371{
4372 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4373}
4374
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004375static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4376 struct in6_addr **peer_pfx)
Thomas Graf461d8832006-09-18 00:09:49 -07004377{
4378 struct in6_addr *pfx = NULL;
4379
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004380 *peer_pfx = NULL;
4381
Thomas Graf461d8832006-09-18 00:09:49 -07004382 if (addr)
4383 pfx = nla_data(addr);
4384
4385 if (local) {
4386 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004387 *peer_pfx = pfx;
4388 pfx = nla_data(local);
Thomas Graf461d8832006-09-18 00:09:49 -07004389 }
4390
4391 return pfx;
4392}
4393
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004394static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07004395 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4396 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4397 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
Jiri Pirko479840f2013-12-06 09:45:21 +01004398 [IFA_FLAGS] = { .len = sizeof(u32) },
Thomas Graf461d8832006-09-18 00:09:49 -07004399};
4400
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401static int
Thomas Graf661d2962013-03-21 07:45:29 +00004402inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004404 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07004405 struct ifaddrmsg *ifm;
4406 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004407 struct in6_addr *pfx, *peer_pfx;
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004408 u32 ifa_flags;
Thomas Grafb933f712006-09-18 00:10:19 -07004409 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
Thomas Grafb933f712006-09-18 00:10:19 -07004411 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4412 if (err < 0)
4413 return err;
4414
4415 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004416 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004417 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 return -EINVAL;
4419
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004420 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4421
4422 /* We ignore other flags so far. */
4423 ifa_flags &= IFA_F_MANAGETEMPADDR;
4424
4425 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4426 ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427}
4428
Jiri Pirko479840f2013-12-06 09:45:21 +01004429static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004430 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004431{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004432 u32 flags;
4433 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004434 unsigned long timeout;
Jiri Pirko53bd6742013-12-06 09:45:22 +01004435 bool was_managetempaddr;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004436 bool had_prefixroute;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09004437
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004438 ASSERT_RTNL();
4439
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004440 if (!valid_lft || (prefered_lft > valid_lft))
4441 return -EINVAL;
4442
Jiri Pirko53bd6742013-12-06 09:45:22 +01004443 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4444 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4445 return -EINVAL;
4446
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004447 timeout = addrconf_timeout_fixup(valid_lft, HZ);
4448 if (addrconf_finite_timeout(timeout)) {
4449 expires = jiffies_to_clock_t(timeout * HZ);
4450 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004451 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004452 } else {
4453 expires = 0;
4454 flags = 0;
4455 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004456 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004457
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004458 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4459 if (addrconf_finite_timeout(timeout)) {
4460 if (timeout == 0)
4461 ifa_flags |= IFA_F_DEPRECATED;
4462 prefered_lft = timeout;
4463 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004464
4465 spin_lock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004466 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004467 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4468 !(ifp->flags & IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004469 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
Thomas Haller761aac72014-01-15 15:36:58 +01004470 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4471 IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004472 ifp->flags |= ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004473 ifp->tstamp = jiffies;
4474 ifp->valid_lft = valid_lft;
4475 ifp->prefered_lft = prefered_lft;
4476
4477 spin_unlock_bh(&ifp->lock);
4478 if (!(ifp->flags&IFA_F_TENTATIVE))
4479 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004480
Thomas Haller761aac72014-01-15 15:36:58 +01004481 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4482 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4483 expires, flags);
Thomas Haller5b84efe2014-01-15 15:36:59 +01004484 } else if (had_prefixroute) {
4485 enum cleanup_prefix_rt_t action;
4486 unsigned long rt_expires;
4487
4488 write_lock_bh(&ifp->idev->lock);
4489 action = check_cleanup_prefix_route(ifp, &rt_expires);
4490 write_unlock_bh(&ifp->idev->lock);
4491
4492 if (action != CLEANUP_PREFIX_RT_NOP) {
4493 cleanup_prefix_route(ifp, rt_expires,
4494 action == CLEANUP_PREFIX_RT_DEL);
4495 }
Thomas Haller761aac72014-01-15 15:36:58 +01004496 }
Jiri Pirko53bd6742013-12-06 09:45:22 +01004497
4498 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4499 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4500 valid_lft = prefered_lft = 0;
4501 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4502 !was_managetempaddr, jiffies);
4503 }
4504
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004505 addrconf_verify_rtnl();
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004506
4507 return 0;
4508}
4509
4510static int
Thomas Graf661d2962013-03-21 07:45:29 +00004511inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004513 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07004514 struct ifaddrmsg *ifm;
4515 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004516 struct in6_addr *pfx, *peer_pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07004517 struct inet6_ifaddr *ifa;
4518 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004519 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
Jiri Pirko479840f2013-12-06 09:45:21 +01004520 u32 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07004521 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522
Thomas Graf461d8832006-09-18 00:09:49 -07004523 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4524 if (err < 0)
4525 return err;
4526
4527 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004528 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004529 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 return -EINVAL;
4531
Thomas Graf461d8832006-09-18 00:09:49 -07004532 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004533 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07004534
4535 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004536 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07004537 preferred_lft = ci->ifa_prefered;
4538 } else {
4539 preferred_lft = INFINITY_LIFE_TIME;
4540 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004541 }
4542
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004543 dev = __dev_get_by_index(net, ifm->ifa_index);
Ian Morris63159f22015-03-29 14:00:04 +01004544 if (!dev)
Thomas Graf7198f8c2006-09-18 00:13:46 -07004545 return -ENODEV;
4546
Jiri Pirko479840f2013-12-06 09:45:21 +01004547 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4548
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004549 /* We ignore other flags so far. */
Thomas Haller761aac72014-01-15 15:36:58 +01004550 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
Madhu Challa93a714d2015-02-25 09:58:35 -08004551 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004552
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08004553 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Ian Morris63159f22015-03-29 14:00:04 +01004554 if (!ifa) {
Thomas Graf7198f8c2006-09-18 00:13:46 -07004555 /*
4556 * It would be best to check for !NLM_F_CREATE here but
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08004557 * userspace already relies on not having to provide this.
Thomas Graf7198f8c2006-09-18 00:13:46 -07004558 */
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004559 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004560 ifm->ifa_prefixlen, ifa_flags,
4561 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004562 }
4563
Thomas Graf7198f8c2006-09-18 00:13:46 -07004564 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4565 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4566 err = -EEXIST;
4567 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004568 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07004569
4570 in6_ifa_put(ifa);
4571
4572 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573}
4574
Jiri Pirko479840f2013-12-06 09:45:21 +01004575static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
Thomas Graf101bb222006-09-18 00:11:52 -07004576 u8 scope, int ifindex)
4577{
4578 struct ifaddrmsg *ifm;
4579
4580 ifm = nlmsg_data(nlh);
4581 ifm->ifa_family = AF_INET6;
4582 ifm->ifa_prefixlen = prefixlen;
4583 ifm->ifa_flags = flags;
4584 ifm->ifa_scope = scope;
4585 ifm->ifa_index = ifindex;
4586}
4587
Thomas Graf85486af2006-09-18 00:11:24 -07004588static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4589 unsigned long tstamp, u32 preferred, u32 valid)
4590{
4591 struct ifa_cacheinfo ci;
4592
Thomas Graf18a31e12010-11-17 04:12:02 +00004593 ci.cstamp = cstamp_delta(cstamp);
4594 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07004595 ci.ifa_prefered = preferred;
4596 ci.ifa_valid = valid;
4597
4598 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4599}
4600
Thomas Graf101bb222006-09-18 00:11:52 -07004601static inline int rt_scope(int ifa_scope)
4602{
4603 if (ifa_scope & IFA_HOST)
4604 return RT_SCOPE_HOST;
4605 else if (ifa_scope & IFA_LINK)
4606 return RT_SCOPE_LINK;
4607 else if (ifa_scope & IFA_SITE)
4608 return RT_SCOPE_SITE;
4609 else
4610 return RT_SCOPE_UNIVERSE;
4611}
4612
Thomas Graf0ab68032006-09-18 00:12:35 -07004613static inline int inet6_ifaddr_msgsize(void)
4614{
Thomas Graf339bf982006-11-10 14:10:15 -08004615 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004616 + nla_total_size(16) /* IFA_LOCAL */
Thomas Graf339bf982006-11-10 14:10:15 -08004617 + nla_total_size(16) /* IFA_ADDRESS */
Jiri Pirko479840f2013-12-06 09:45:21 +01004618 + nla_total_size(sizeof(struct ifa_cacheinfo))
4619 + nla_total_size(4) /* IFA_FLAGS */;
Thomas Graf0ab68032006-09-18 00:12:35 -07004620}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004621
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004623 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07004626 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627
Eric W. Biederman15e47302012-09-07 20:12:54 +00004628 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004629 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004630 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004631
Thomas Graf101bb222006-09-18 00:11:52 -07004632 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4633 ifa->idev->dev->ifindex);
4634
Yasushi Asanofad8da32013-12-31 12:04:19 +09004635 if (!((ifa->flags&IFA_F_PERMANENT) &&
4636 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
Thomas Graf85486af2006-09-18 00:11:24 -07004637 preferred = ifa->prefered_lft;
4638 valid = ifa->valid_lft;
4639 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00004641 if (preferred > tval)
4642 preferred -= tval;
4643 else
4644 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00004645 if (valid != INFINITY_LIFE_TIME) {
4646 if (valid > tval)
4647 valid -= tval;
4648 else
4649 valid = 0;
4650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 }
4652 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07004653 preferred = INFINITY_LIFE_TIME;
4654 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 }
Thomas Graf85486af2006-09-18 00:11:24 -07004656
Cong Wang7996c792013-05-22 05:41:06 +00004657 if (!ipv6_addr_any(&ifa->peer_addr)) {
Jiri Benc930345e2015-03-29 16:59:25 +02004658 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4659 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004660 goto error;
4661 } else
Jiri Benc930345e2015-03-29 16:59:25 +02004662 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004663 goto error;
4664
4665 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4666 goto error;
Thomas Graf85486af2006-09-18 00:11:24 -07004667
Jiri Pirko479840f2013-12-06 09:45:21 +01004668 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4669 goto error;
4670
Johannes Berg053c0952015-01-16 22:09:00 +01004671 nlmsg_end(skb, nlh);
4672 return 0;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004673
4674error:
4675 nlmsg_cancel(skb, nlh);
4676 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677}
4678
4679static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004680 u32 portid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004683 u8 scope = RT_SCOPE_UNIVERSE;
4684 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
Thomas Graf101bb222006-09-18 00:11:52 -07004686 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4687 scope = RT_SCOPE_SITE;
4688
Eric W. Biederman15e47302012-09-07 20:12:54 +00004689 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004690 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004691 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004692
Thomas Graf101bb222006-09-18 00:11:52 -07004693 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004694 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004695 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004696 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4697 nlmsg_cancel(skb, nlh);
4698 return -EMSGSIZE;
4699 }
Thomas Graf85486af2006-09-18 00:11:24 -07004700
Johannes Berg053c0952015-01-16 22:09:00 +01004701 nlmsg_end(skb, nlh);
4702 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703}
4704
4705static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004706 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004709 u8 scope = RT_SCOPE_UNIVERSE;
4710 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711
Thomas Graf101bb222006-09-18 00:11:52 -07004712 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4713 scope = RT_SCOPE_SITE;
4714
Eric W. Biederman15e47302012-09-07 20:12:54 +00004715 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004716 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004717 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004718
Thomas Graf101bb222006-09-18 00:11:52 -07004719 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004720 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004721 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004722 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4723 nlmsg_cancel(skb, nlh);
4724 return -EMSGSIZE;
4725 }
Thomas Graf85486af2006-09-18 00:11:24 -07004726
Johannes Berg053c0952015-01-16 22:09:00 +01004727 nlmsg_end(skb, nlh);
4728 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729}
4730
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004731enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 UNICAST_ADDR,
4733 MULTICAST_ADDR,
4734 ANYCAST_ADDR,
4735};
4736
Eric Dumazet234b27c2009-11-12 04:11:50 +00004737/* called with rcu_read_lock() */
4738static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4739 struct netlink_callback *cb, enum addr_type_t type,
4740 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 struct ifmcaddr6 *ifmca;
4743 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004744 int err = 1;
4745 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746
Eric Dumazet234b27c2009-11-12 04:11:50 +00004747 read_lock_bh(&idev->lock);
4748 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00004749 case UNICAST_ADDR: {
4750 struct inet6_ifaddr *ifa;
4751
Eric Dumazet234b27c2009-11-12 04:11:50 +00004752 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00004753 list_for_each_entry(ifa, &idev->addr_list, if_list) {
David Ahernf86c55c62018-10-19 10:00:19 -07004754 if (ip_idx < s_ip_idx)
4755 goto next;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004756 err = inet6_fill_ifaddr(skb, ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004757 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004758 cb->nlh->nlmsg_seq,
4759 RTM_NEWADDR,
4760 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004761 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004762 break;
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004763 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
David Ahernf86c55c62018-10-19 10:00:19 -07004764next:
4765 ip_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004767 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00004768 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004769 case MULTICAST_ADDR:
4770 /* multicast address */
4771 for (ifmca = idev->mc_list; ifmca;
4772 ifmca = ifmca->next, ip_idx++) {
4773 if (ip_idx < s_ip_idx)
4774 continue;
4775 err = inet6_fill_ifmcaddr(skb, ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004776 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004777 cb->nlh->nlmsg_seq,
4778 RTM_GETMULTICAST,
4779 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004780 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004781 break;
4782 }
4783 break;
4784 case ANYCAST_ADDR:
4785 /* anycast address */
4786 for (ifaca = idev->ac_list; ifaca;
4787 ifaca = ifaca->aca_next, ip_idx++) {
4788 if (ip_idx < s_ip_idx)
4789 continue;
4790 err = inet6_fill_ifacaddr(skb, ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004791 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004792 cb->nlh->nlmsg_seq,
4793 RTM_GETANYCAST,
4794 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004795 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004796 break;
4797 }
4798 break;
4799 default:
4800 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004802 read_unlock_bh(&idev->lock);
4803 *p_ip_idx = ip_idx;
4804 return err;
4805}
4806
4807static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4808 enum addr_type_t type)
4809{
4810 struct net *net = sock_net(skb->sk);
4811 int h, s_h;
4812 int idx, ip_idx;
4813 int s_idx, s_ip_idx;
4814 struct net_device *dev;
4815 struct inet6_dev *idev;
4816 struct hlist_head *head;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004817
4818 s_h = cb->args[0];
4819 s_idx = idx = cb->args[1];
4820 s_ip_idx = ip_idx = cb->args[2];
4821
4822 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004823 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004824 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4825 idx = 0;
4826 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08004827 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet234b27c2009-11-12 04:11:50 +00004828 if (idx < s_idx)
4829 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07004830 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004831 s_ip_idx = 0;
4832 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004833 idev = __in6_dev_get(dev);
4834 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004835 goto cont;
4836
4837 if (in6_dump_addrs(idev, skb, cb, type,
David S. Miller7b46a642015-01-18 23:36:08 -05004838 s_ip_idx, &ip_idx) < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004839 goto done;
4840cont:
4841 idx++;
4842 }
4843 }
4844done:
4845 rcu_read_unlock();
4846 cb->args[0] = h;
4847 cb->args[1] = idx;
4848 cb->args[2] = ip_idx;
4849
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 return skb->len;
4851}
4852
4853static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4854{
4855 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004856
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 return inet6_dump_addr(skb, cb, type);
4858}
4859
4860static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4861{
4862 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004863
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 return inet6_dump_addr(skb, cb, type);
4865}
4866
4867
4868static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4869{
4870 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004871
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 return inet6_dump_addr(skb, cb, type);
4873}
4874
Thomas Graf661d2962013-03-21 07:45:29 +00004875static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004876{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004877 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004878 struct ifaddrmsg *ifm;
4879 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004880 struct in6_addr *addr = NULL, *peer;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004881 struct net_device *dev = NULL;
4882 struct inet6_ifaddr *ifa;
4883 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004884 int err;
4885
Thomas Graf1b29fc22006-09-18 00:10:50 -07004886 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4887 if (err < 0)
4888 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004889
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004890 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
Ian Morris63159f22015-03-29 14:00:04 +01004891 if (!addr) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004892 err = -EINVAL;
4893 goto errout;
4894 }
4895
4896 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004897 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004898 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004899
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004900 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4901 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004902 err = -EADDRNOTAVAIL;
4903 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004904 }
4905
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004906 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4907 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004908 err = -ENOBUFS;
4909 goto errout_ifa;
4910 }
4911
Eric W. Biederman15e47302012-09-07 20:12:54 +00004912 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004913 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004914 if (err < 0) {
4915 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4916 WARN_ON(err == -EMSGSIZE);
4917 kfree_skb(skb);
4918 goto errout_ifa;
4919 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00004920 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004921errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004922 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004923errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004924 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004925}
4926
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4928{
4929 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004930 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07004931 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
Thomas Graf0ab68032006-09-18 00:12:35 -07004933 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01004934 if (!skb)
Thomas Graf5d620262006-08-15 00:35:02 -07004935 goto errout;
4936
4937 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004938 if (err < 0) {
4939 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4940 WARN_ON(err == -EMSGSIZE);
4941 kfree_skb(skb);
4942 goto errout;
4943 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004944 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4945 return;
Thomas Graf5d620262006-08-15 00:35:02 -07004946errout:
4947 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004948 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949}
4950
Dave Jonesb6f99a22007-03-22 12:27:49 -07004951static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 __s32 *array, int bytes)
4953{
Thomas Graf04561c12006-11-14 19:53:58 -08004954 BUG_ON(bytes < (DEVCONF_MAX * 4));
4955
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 memset(array, 0, bytes);
4957 array[DEVCONF_FORWARDING] = cnf->forwarding;
4958 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4959 array[DEVCONF_MTU6] = cnf->mtu6;
4960 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4961 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4962 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4963 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4964 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00004965 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4966 jiffies_to_msecs(cnf->rtr_solicit_interval);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004967 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
4968 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
Thomas Graf93908d12010-11-17 01:44:24 +00004969 array[DEVCONF_RTR_SOLICIT_DELAY] =
4970 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02004972 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4973 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4974 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4975 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4977 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4978 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4979 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4980 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004982 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
Hangbin Liu8013d1d2015-07-30 14:28:42 +08004983 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004984 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004985#ifdef CONFIG_IPV6_ROUTER_PREF
4986 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00004987 array[DEVCONF_RTR_PROBE_INTERVAL] =
4988 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08004989#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzd860b2e2017-03-22 18:19:04 +09004990 array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004991 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4992#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004993#endif
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +09004994 array[DEVCONF_ACCEPT_RA_RT_TABLE] = cnf->accept_ra_rt_table;
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004995 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004996 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07004997#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4998 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
Erik Kline7fd25612014-10-28 18:11:14 +09004999 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
Neil Horman95c385b2007-04-25 17:08:10 -07005000#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005001#ifdef CONFIG_IPV6_MROUTE
5002 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
5003#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09005004 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09005005 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00005006 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00005007 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02005008 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
Ben Greeard9333192014-06-25 14:44:53 -07005009 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
Harout Hedeshianc2943f12015-01-20 10:06:05 -07005010 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
Andy Gospodarek35103d12015-08-13 10:39:01 -04005011 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005012 /* we omit DEVCONF_STABLE_SECRET for now */
Erik Kline3985e8a2015-07-22 16:38:25 +09005013 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
Johannes Bergabbc3042016-02-04 13:31:19 +01005014 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
Johannes Berg7a02bf82016-02-04 13:31:20 +01005015 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
David Ahernf1705ec2016-02-24 09:25:37 -08005016 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017}
5018
Thomas Grafb382b192010-11-16 04:33:57 +00005019static inline size_t inet6_ifla6_size(void)
5020{
5021 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5022 + nla_total_size(sizeof(struct ifla_cacheinfo))
5023 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5024 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005025 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5026 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
Thomas Grafb382b192010-11-16 04:33:57 +00005027}
5028
Thomas Graf339bf982006-11-10 14:10:15 -08005029static inline size_t inet6_if_nlmsg_size(void)
5030{
5031 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5032 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5033 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5034 + nla_total_size(4) /* IFLA_MTU */
5035 + nla_total_size(4) /* IFLA_LINK */
Andy Gospodarek03443382015-08-13 15:26:35 -04005036 + nla_total_size(1) /* IFLA_OPERSTATE */
Thomas Grafb382b192010-11-16 04:33:57 +00005037 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08005038}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005039
Eric Dumazetbe281e52011-05-19 01:14:23 +00005040static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Jia Heaca05672016-09-30 11:29:03 +08005041 int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005042{
5043 int i;
Jia Heaca05672016-09-30 11:29:03 +08005044 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005045 BUG_ON(pad < 0);
5046
5047 /* Use put_unaligned() because stats may not be aligned for u64. */
Jia Heaca05672016-09-30 11:29:03 +08005048 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5049 for (i = 1; i < ICMP6_MIB_MAX; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00005050 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005051
Jia Heaca05672016-09-30 11:29:03 +08005052 memset(&stats[ICMP6_MIB_MAX], 0, pad);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005053}
5054
WANG Cong698365f2014-05-05 15:55:55 -07005055static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305056 int bytes, size_t syncpoff)
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005057{
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305058 int i, c;
5059 u64 buff[IPSTATS_MIB_MAX];
5060 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5061
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005062 BUG_ON(pad < 0);
5063
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305064 memset(buff, 0, sizeof(buff));
5065 buff[0] = IPSTATS_MIB_MAX;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005066
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305067 for_each_possible_cpu(c) {
5068 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5069 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5070 }
5071
5072 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5073 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005074}
5075
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005076static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5077 int bytes)
5078{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005079 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005080 case IFLA_INET6_STATS:
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305081 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5082 offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005083 break;
5084 case IFLA_INET6_ICMP6STATS:
Jia Heaca05672016-09-30 11:29:03 +08005085 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005086 break;
5087 }
5088}
5089
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005090static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5091 u32 ext_filter_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005093 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08005094 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
David S. Millerc78679e2012-04-01 20:27:33 -04005096 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5097 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08005099 ci.tstamp = cstamp_delta(idev->tstamp);
5100 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
Jiri Pirko1f9248e52013-12-07 19:26:53 +01005101 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
David S. Millerc78679e2012-04-01 20:27:33 -04005102 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5103 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005104 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
Ian Morris63159f22015-03-29 14:00:04 +01005105 if (!nla)
Thomas Graf04561c12006-11-14 19:53:58 -08005106 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005107 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005109 /* XXX - MC not implemented */
5110
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005111 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5112 return 0;
5113
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005114 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005115 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005116 goto nla_put_failure;
5117 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5118
5119 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005120 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005121 goto nla_put_failure;
5122 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005124 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
Ian Morris63159f22015-03-29 14:00:04 +01005125 if (!nla)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005126 goto nla_put_failure;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005127
5128 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode))
5129 goto nla_put_failure;
5130
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005131 read_lock_bh(&idev->lock);
5132 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5133 read_unlock_bh(&idev->lock);
5134
Thomas Grafb382b192010-11-16 04:33:57 +00005135 return 0;
5136
5137nla_put_failure:
5138 return -EMSGSIZE;
5139}
5140
Arad, Ronenb1974ed2015-10-19 09:23:28 -07005141static size_t inet6_get_link_af_size(const struct net_device *dev,
5142 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005143{
5144 if (!__in6_dev_get(dev))
5145 return 0;
5146
5147 return inet6_ifla6_size();
5148}
5149
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005150static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5151 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005152{
5153 struct inet6_dev *idev = __in6_dev_get(dev);
5154
5155 if (!idev)
5156 return -ENODATA;
5157
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005158 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005159 return -EMSGSIZE;
5160
5161 return 0;
5162}
5163
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005164static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5165{
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005166 struct inet6_ifaddr *ifp;
5167 struct net_device *dev = idev->dev;
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005168 bool clear_token, update_rs = false;
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005169 struct in6_addr ll_addr;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005170
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005171 ASSERT_RTNL();
5172
Ian Morris63159f22015-03-29 14:00:04 +01005173 if (!token)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005174 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005175 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5176 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005177 if (!ipv6_accept_ra(idev))
5178 return -EINVAL;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005179 if (idev->cnf.rtr_solicits == 0)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005180 return -EINVAL;
5181
5182 write_lock_bh(&idev->lock);
5183
5184 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5185 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5186
5187 write_unlock_bh(&idev->lock);
5188
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005189 clear_token = ipv6_addr_any(token);
5190 if (clear_token)
5191 goto update_lft;
5192
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005193 if (!idev->dead && (idev->if_flags & IF_READY) &&
5194 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5195 IFA_F_OPTIMISTIC)) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005196 /* If we're not ready, then normal ifup will take care
5197 * of this. Otherwise, we need to request our rs here.
5198 */
5199 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5200 update_rs = true;
5201 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005202
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005203update_lft:
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005204 write_lock_bh(&idev->lock);
Daniel Borkmannfc403832013-04-09 03:47:15 +00005205
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005206 if (update_rs) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005207 idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005208 idev->rs_interval = rfc3315_s14_backoff_init(
5209 idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005210 idev->rs_probes = 1;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005211 addrconf_mod_rs_timer(idev, idev->rs_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005212 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005213
5214 /* Well, that's kinda nasty ... */
5215 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5216 spin_lock(&ifp->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00005217 if (ifp->tokenized) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005218 ifp->valid_lft = 0;
5219 ifp->prefered_lft = 0;
5220 }
5221 spin_unlock(&ifp->lock);
5222 }
5223
5224 write_unlock_bh(&idev->lock);
Lubomir Rintelb2ed64a2014-10-27 17:39:16 +01005225 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005226 addrconf_verify_rtnl();
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005227 return 0;
5228}
5229
Daniel Borkmann11b1f822015-02-05 14:39:11 +01005230static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5231 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5232 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5233};
5234
5235static int inet6_validate_link_af(const struct net_device *dev,
5236 const struct nlattr *nla)
5237{
5238 struct nlattr *tb[IFLA_INET6_MAX + 1];
5239
5240 if (dev && !__in6_dev_get(dev))
5241 return -EAFNOSUPPORT;
5242
5243 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
5244}
5245
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005246static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5247{
5248 int err = -EINVAL;
5249 struct inet6_dev *idev = __in6_dev_get(dev);
5250 struct nlattr *tb[IFLA_INET6_MAX + 1];
5251
5252 if (!idev)
5253 return -EAFNOSUPPORT;
5254
5255 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
5256 BUG();
5257
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005258 if (tb[IFLA_INET6_TOKEN]) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005259 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005260 if (err)
5261 return err;
5262 }
5263
5264 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5265 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5266
5267 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005268 mode != IN6_ADDR_GEN_MODE_NONE &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01005269 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5270 mode != IN6_ADDR_GEN_MODE_RANDOM)
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005271 return -EINVAL;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005272
5273 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5274 !idev->cnf.stable_secret.initialized &&
5275 !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized)
5276 return -EINVAL;
5277
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005278 idev->addr_gen_mode = mode;
5279 err = 0;
5280 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005281
5282 return err;
5283}
5284
Thomas Grafb382b192010-11-16 04:33:57 +00005285static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005286 u32 portid, u32 seq, int event, unsigned int flags)
Thomas Grafb382b192010-11-16 04:33:57 +00005287{
5288 struct net_device *dev = idev->dev;
5289 struct ifinfomsg *hdr;
5290 struct nlmsghdr *nlh;
5291 void *protoinfo;
5292
Eric W. Biederman15e47302012-09-07 20:12:54 +00005293 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005294 if (!nlh)
Thomas Grafb382b192010-11-16 04:33:57 +00005295 return -EMSGSIZE;
5296
5297 hdr = nlmsg_data(nlh);
5298 hdr->ifi_family = AF_INET6;
5299 hdr->__ifi_pad = 0;
5300 hdr->ifi_type = dev->type;
5301 hdr->ifi_index = dev->ifindex;
5302 hdr->ifi_flags = dev_get_flags(dev);
5303 hdr->ifi_change = 0;
5304
David S. Millerc78679e2012-04-01 20:27:33 -04005305 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5306 (dev->addr_len &&
5307 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5308 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02005309 (dev->ifindex != dev_get_iflink(dev) &&
Andy Gospodarek03443382015-08-13 15:26:35 -04005310 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5311 nla_put_u8(skb, IFLA_OPERSTATE,
5312 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
David S. Millerc78679e2012-04-01 20:27:33 -04005313 goto nla_put_failure;
Thomas Grafb382b192010-11-16 04:33:57 +00005314 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
Ian Morris63159f22015-03-29 14:00:04 +01005315 if (!protoinfo)
Thomas Grafb382b192010-11-16 04:33:57 +00005316 goto nla_put_failure;
5317
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005318 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005319 goto nla_put_failure;
5320
Thomas Graf04561c12006-11-14 19:53:58 -08005321 nla_nest_end(skb, protoinfo);
Johannes Berg053c0952015-01-16 22:09:00 +01005322 nlmsg_end(skb, nlh);
5323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324
Thomas Graf04561c12006-11-14 19:53:58 -08005325nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005326 nlmsg_cancel(skb, nlh);
5327 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328}
5329
5330static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5331{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005332 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00005333 int h, s_h;
David S. Miller434a8a582009-11-11 18:53:00 -08005334 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 struct net_device *dev;
5336 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00005337 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338
Eric Dumazet84d26972009-11-09 12:11:28 +00005339 s_h = cb->args[0];
5340 s_idx = cb->args[1];
5341
5342 rcu_read_lock();
5343 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5344 idx = 0;
5345 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08005346 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet84d26972009-11-09 12:11:28 +00005347 if (idx < s_idx)
5348 goto cont;
5349 idev = __in6_dev_get(dev);
5350 if (!idev)
5351 goto cont;
5352 if (inet6_fill_ifinfo(skb, idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005353 NETLINK_CB(cb->skb).portid,
Eric Dumazet84d26972009-11-09 12:11:28 +00005354 cb->nlh->nlmsg_seq,
Johannes Berg053c0952015-01-16 22:09:00 +01005355 RTM_NEWLINK, NLM_F_MULTI) < 0)
Eric Dumazet84d26972009-11-09 12:11:28 +00005356 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07005357cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00005358 idx++;
5359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 }
Eric Dumazet84d26972009-11-09 12:11:28 +00005361out:
5362 rcu_read_unlock();
5363 cb->args[1] = idx;
5364 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365
5366 return skb->len;
5367}
5368
5369void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5370{
5371 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005372 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005373 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005374
Thomas Graf339bf982006-11-10 14:10:15 -08005375 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005376 if (!skb)
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005377 goto errout;
5378
5379 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005380 if (err < 0) {
5381 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5382 WARN_ON(err == -EMSGSIZE);
5383 kfree_skb(skb);
5384 goto errout;
5385 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005386 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005387 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005388errout:
5389 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005390 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391}
5392
Thomas Graf339bf982006-11-10 14:10:15 -08005393static inline size_t inet6_prefix_nlmsg_size(void)
5394{
5395 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5396 + nla_total_size(sizeof(struct in6_addr))
5397 + nla_total_size(sizeof(struct prefix_cacheinfo));
5398}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005399
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005401 struct prefix_info *pinfo, u32 portid, u32 seq,
Thomas Graf6051e2f2006-11-14 19:54:19 -08005402 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403{
Thomas Graf6051e2f2006-11-14 19:54:19 -08005404 struct prefixmsg *pmsg;
5405 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 struct prefix_cacheinfo ci;
5407
Eric W. Biederman15e47302012-09-07 20:12:54 +00005408 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005409 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005410 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08005411
5412 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07005414 pmsg->prefix_pad1 = 0;
5415 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 pmsg->prefix_ifindex = idev->dev->ifindex;
5417 pmsg->prefix_len = pinfo->prefix_len;
5418 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07005419 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 pmsg->prefix_flags = 0;
5421 if (pinfo->onlink)
5422 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5423 if (pinfo->autoconf)
5424 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5425
David S. Millerc78679e2012-04-01 20:27:33 -04005426 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5427 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 ci.preferred_time = ntohl(pinfo->prefered);
5429 ci.valid_time = ntohl(pinfo->valid);
David S. Millerc78679e2012-04-01 20:27:33 -04005430 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5431 goto nla_put_failure;
Johannes Berg053c0952015-01-16 22:09:00 +01005432 nlmsg_end(skb, nlh);
5433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434
Thomas Graf6051e2f2006-11-14 19:54:19 -08005435nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005436 nlmsg_cancel(skb, nlh);
5437 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438}
5439
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005440static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 struct prefix_info *pinfo)
5442{
5443 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005444 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005445 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
Thomas Graf339bf982006-11-10 14:10:15 -08005447 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005448 if (!skb)
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005449 goto errout;
5450
5451 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005452 if (err < 0) {
5453 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5454 WARN_ON(err == -EMSGSIZE);
5455 kfree_skb(skb);
5456 goto errout;
5457 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005458 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5459 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005460errout:
5461 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08005462 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463}
5464
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5466{
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005467 struct net *net = dev_net(ifp->idev->dev);
5468
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005469 if (event)
5470 ASSERT_RTNL();
5471
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5473
5474 switch (event) {
5475 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07005476 /*
5477 * If the address was optimistic
5478 * we inserted the route at the start of
5479 * our DAD process, so we don't need
5480 * to do it again
5481 */
Wei Wang43c792a2017-08-25 15:03:10 -07005482 if (!rcu_access_pointer(ifp->rt->rt6i_node))
Neil Horman95c385b2007-04-25 17:08:10 -07005483 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 if (ifp->idev->cnf.forwarding)
5485 addrconf_join_anycast(ifp);
Cong Wang7996c792013-05-22 05:41:06 +00005486 if (!ipv6_addr_any(&ifp->peer_addr))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005487 addrconf_prefix_route(&ifp->peer_addr, 128,
5488 ifp->idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 break;
5490 case RTM_DELADDR:
5491 if (ifp->idev->cnf.forwarding)
5492 addrconf_leave_anycast(ifp);
5493 addrconf_leave_solict(ifp->idev, &ifp->addr);
Cong Wang7996c792013-05-22 05:41:06 +00005494 if (!ipv6_addr_any(&ifp->peer_addr)) {
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005495 struct rt6_info *rt;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005496
Nicolas Dichtele7478df2014-09-03 23:59:22 +02005497 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5498 ifp->idev->dev, 0, 0);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07005499 if (rt)
5500 ip6_del_rt(rt);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005501 }
David Ahern38bd10c2016-04-21 20:56:12 -07005502 if (ifp->rt) {
5503 dst_hold(&ifp->rt->dst);
5504 ip6_del_rt(ifp->rt);
5505 }
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02005506 rt_genid_bump_ipv6(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 break;
5508 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005509 atomic_inc(&net->ipv6.dev_addr_genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510}
5511
5512static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5513{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005514 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 if (likely(ifp->idev->dead == 0))
5516 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005517 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518}
5519
5520#ifdef CONFIG_SYSCTL
5521
5522static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005523int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 void __user *buffer, size_t *lenp, loff_t *ppos)
5525{
5526 int *valp = ctl->data;
5527 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005528 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005529 struct ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 int ret;
5531
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005532 /*
5533 * ctl->data points to idev->cnf.forwarding, we should
5534 * not modify it until we get the rtnl lock.
5535 */
5536 lctl = *ctl;
5537 lctl.data = &val;
5538
5539 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08005541 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00005542 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005543 if (ret)
5544 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005545 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546}
5547
Marcelo Leitner77751422015-02-23 11:17:13 -03005548static
5549int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5550 void __user *buffer, size_t *lenp, loff_t *ppos)
5551{
5552 struct inet6_dev *idev = ctl->extra1;
5553 int min_mtu = IPV6_MIN_MTU;
5554 struct ctl_table lctl;
5555
5556 lctl = *ctl;
5557 lctl.extra1 = &min_mtu;
5558 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5559
5560 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5561}
5562
Brian Haley56d417b2009-06-01 03:07:33 -07005563static void dev_disable_change(struct inet6_dev *idev)
5564{
Cong Wang75538c22013-05-29 11:30:50 +08005565 struct netdev_notifier_info info;
5566
Brian Haley56d417b2009-06-01 03:07:33 -07005567 if (!idev || !idev->dev)
5568 return;
5569
Cong Wang75538c22013-05-29 11:30:50 +08005570 netdev_notifier_info_init(&info, idev->dev);
Brian Haley56d417b2009-06-01 03:07:33 -07005571 if (idev->cnf.disable_ipv6)
Cong Wang75538c22013-05-29 11:30:50 +08005572 addrconf_notify(NULL, NETDEV_DOWN, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005573 else
Cong Wang75538c22013-05-29 11:30:50 +08005574 addrconf_notify(NULL, NETDEV_UP, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005575}
5576
5577static void addrconf_disable_change(struct net *net, __s32 newf)
5578{
5579 struct net_device *dev;
5580 struct inet6_dev *idev;
5581
Kefeng Wang79453ab2017-01-19 16:26:21 +08005582 for_each_netdev(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07005583 idev = __in6_dev_get(dev);
5584 if (idev) {
5585 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5586 idev->cnf.disable_ipv6 = newf;
5587 if (changed)
5588 dev_disable_change(idev);
5589 }
Brian Haley56d417b2009-06-01 03:07:33 -07005590 }
Brian Haley56d417b2009-06-01 03:07:33 -07005591}
5592
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005593static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07005594{
5595 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005596 int old;
5597
5598 if (!rtnl_trylock())
5599 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07005600
5601 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005602 old = *p;
5603 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07005604
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005605 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5606 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07005607 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005608 }
Brian Haley56d417b2009-06-01 03:07:33 -07005609
5610 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07005611 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5612 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005613 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07005614 dev_disable_change((struct inet6_dev *)table->extra1);
5615
5616 rtnl_unlock();
5617 return 0;
5618}
5619
5620static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005621int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07005622 void __user *buffer, size_t *lenp, loff_t *ppos)
5623{
5624 int *valp = ctl->data;
5625 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005626 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005627 struct ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07005628 int ret;
5629
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005630 /*
5631 * ctl->data points to idev->cnf.disable_ipv6, we should
5632 * not modify it until we get the rtnl lock.
5633 */
5634 lctl = *ctl;
5635 lctl.data = &val;
5636
5637 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07005638
5639 if (write)
5640 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005641 if (ret)
5642 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07005643 return ret;
5644}
5645
stephen hemmingerc92d5492013-12-17 22:37:14 -08005646static
5647int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5648 void __user *buffer, size_t *lenp, loff_t *ppos)
5649{
5650 int *valp = ctl->data;
5651 int ret;
5652 int old, new;
5653
5654 old = *valp;
5655 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5656 new = *valp;
5657
5658 if (write && old != new) {
5659 struct net *net = ctl->extra2;
5660
5661 if (!rtnl_trylock())
5662 return restart_syscall();
5663
5664 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5665 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5666 NETCONFA_IFINDEX_DEFAULT,
5667 net->ipv6.devconf_dflt);
5668 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5669 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5670 NETCONFA_IFINDEX_ALL,
5671 net->ipv6.devconf_all);
5672 else {
5673 struct inet6_dev *idev = ctl->extra1;
5674
5675 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5676 idev->dev->ifindex,
5677 &idev->cnf);
5678 }
5679 rtnl_unlock();
5680 }
5681
5682 return ret;
5683}
5684
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005685static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5686 void __user *buffer, size_t *lenp,
5687 loff_t *ppos)
5688{
5689 int err;
5690 struct in6_addr addr;
5691 char str[IPV6_MAX_STRLEN];
5692 struct ctl_table lctl = *ctl;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005693 struct net *net = ctl->extra2;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005694 struct ipv6_stable_secret *secret = ctl->data;
5695
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005696 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5697 return -EIO;
5698
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005699 lctl.maxlen = IPV6_MAX_STRLEN;
5700 lctl.data = str;
5701
5702 if (!rtnl_trylock())
5703 return restart_syscall();
5704
5705 if (!write && !secret->initialized) {
5706 err = -EIO;
5707 goto out;
5708 }
5709
WANG Cong5449a5c2015-12-21 10:55:45 -08005710 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
5711 if (err >= sizeof(str)) {
5712 err = -EIO;
5713 goto out;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005714 }
5715
5716 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5717 if (err || !write)
5718 goto out;
5719
5720 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5721 err = -EIO;
5722 goto out;
5723 }
5724
5725 secret->initialized = true;
5726 secret->secret = addr;
5727
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005728 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5729 struct net_device *dev;
5730
5731 for_each_netdev(net, dev) {
5732 struct inet6_dev *idev = __in6_dev_get(dev);
5733
5734 if (idev) {
5735 idev->addr_gen_mode =
5736 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5737 }
5738 }
5739 } else {
5740 struct inet6_dev *idev = ctl->extra1;
5741
5742 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5743 }
5744
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005745out:
5746 rtnl_unlock();
5747
5748 return err;
5749}
stephen hemmingerc92d5492013-12-17 22:37:14 -08005750
Andy Gospodarek35103d12015-08-13 10:39:01 -04005751static
5752int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
5753 int write,
5754 void __user *buffer,
5755 size_t *lenp,
5756 loff_t *ppos)
5757{
5758 int *valp = ctl->data;
5759 int val = *valp;
5760 loff_t pos = *ppos;
5761 struct ctl_table lctl;
5762 int ret;
5763
5764 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
5765 * we should not modify it until we get the rtnl lock.
5766 */
5767 lctl = *ctl;
5768 lctl.data = &val;
5769
5770 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5771
5772 if (write)
5773 ret = addrconf_fixup_linkdown(ctl, valp, val);
5774 if (ret)
5775 *ppos = pos;
5776 return ret;
5777}
5778
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07005779static int minus_one = -1;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07005780static const int one = 1;
5781static const int two_five_five = 255;
5782
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03005783static const struct ctl_table addrconf_sysctl[] = {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005784 {
5785 .procname = "forwarding",
5786 .data = &ipv6_devconf.forwarding,
5787 .maxlen = sizeof(int),
5788 .mode = 0644,
5789 .proc_handler = addrconf_sysctl_forward,
5790 },
5791 {
5792 .procname = "hop_limit",
5793 .data = &ipv6_devconf.hop_limit,
5794 .maxlen = sizeof(int),
5795 .mode = 0644,
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07005796 .proc_handler = proc_dointvec_minmax,
5797 .extra1 = (void *)&one,
5798 .extra2 = (void *)&two_five_five,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005799 },
5800 {
5801 .procname = "mtu",
5802 .data = &ipv6_devconf.mtu6,
5803 .maxlen = sizeof(int),
5804 .mode = 0644,
5805 .proc_handler = addrconf_sysctl_mtu,
5806 },
5807 {
5808 .procname = "accept_ra",
5809 .data = &ipv6_devconf.accept_ra,
5810 .maxlen = sizeof(int),
5811 .mode = 0644,
5812 .proc_handler = proc_dointvec,
5813 },
5814 {
5815 .procname = "accept_redirects",
5816 .data = &ipv6_devconf.accept_redirects,
5817 .maxlen = sizeof(int),
5818 .mode = 0644,
5819 .proc_handler = proc_dointvec,
5820 },
5821 {
5822 .procname = "autoconf",
5823 .data = &ipv6_devconf.autoconf,
5824 .maxlen = sizeof(int),
5825 .mode = 0644,
5826 .proc_handler = proc_dointvec,
5827 },
5828 {
5829 .procname = "dad_transmits",
5830 .data = &ipv6_devconf.dad_transmits,
5831 .maxlen = sizeof(int),
5832 .mode = 0644,
5833 .proc_handler = proc_dointvec,
5834 },
5835 {
5836 .procname = "router_solicitations",
5837 .data = &ipv6_devconf.rtr_solicits,
5838 .maxlen = sizeof(int),
5839 .mode = 0644,
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07005840 .proc_handler = proc_dointvec_minmax,
5841 .extra1 = &minus_one,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005842 },
5843 {
5844 .procname = "router_solicitation_interval",
5845 .data = &ipv6_devconf.rtr_solicit_interval,
5846 .maxlen = sizeof(int),
5847 .mode = 0644,
5848 .proc_handler = proc_dointvec_jiffies,
5849 },
5850 {
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005851 .procname = "router_solicitation_max_interval",
5852 .data = &ipv6_devconf.rtr_solicit_max_interval,
5853 .maxlen = sizeof(int),
5854 .mode = 0644,
5855 .proc_handler = proc_dointvec_jiffies,
5856 },
5857 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005858 .procname = "router_solicitation_delay",
5859 .data = &ipv6_devconf.rtr_solicit_delay,
5860 .maxlen = sizeof(int),
5861 .mode = 0644,
5862 .proc_handler = proc_dointvec_jiffies,
5863 },
5864 {
5865 .procname = "force_mld_version",
5866 .data = &ipv6_devconf.force_mld_version,
5867 .maxlen = sizeof(int),
5868 .mode = 0644,
5869 .proc_handler = proc_dointvec,
5870 },
5871 {
5872 .procname = "mldv1_unsolicited_report_interval",
5873 .data =
5874 &ipv6_devconf.mldv1_unsolicited_report_interval,
5875 .maxlen = sizeof(int),
5876 .mode = 0644,
5877 .proc_handler = proc_dointvec_ms_jiffies,
5878 },
5879 {
5880 .procname = "mldv2_unsolicited_report_interval",
5881 .data =
5882 &ipv6_devconf.mldv2_unsolicited_report_interval,
5883 .maxlen = sizeof(int),
5884 .mode = 0644,
5885 .proc_handler = proc_dointvec_ms_jiffies,
5886 },
5887 {
5888 .procname = "use_tempaddr",
5889 .data = &ipv6_devconf.use_tempaddr,
5890 .maxlen = sizeof(int),
5891 .mode = 0644,
5892 .proc_handler = proc_dointvec,
5893 },
5894 {
5895 .procname = "temp_valid_lft",
5896 .data = &ipv6_devconf.temp_valid_lft,
5897 .maxlen = sizeof(int),
5898 .mode = 0644,
5899 .proc_handler = proc_dointvec,
5900 },
5901 {
5902 .procname = "temp_prefered_lft",
5903 .data = &ipv6_devconf.temp_prefered_lft,
5904 .maxlen = sizeof(int),
5905 .mode = 0644,
5906 .proc_handler = proc_dointvec,
5907 },
5908 {
5909 .procname = "regen_max_retry",
5910 .data = &ipv6_devconf.regen_max_retry,
5911 .maxlen = sizeof(int),
5912 .mode = 0644,
5913 .proc_handler = proc_dointvec,
5914 },
5915 {
5916 .procname = "max_desync_factor",
5917 .data = &ipv6_devconf.max_desync_factor,
5918 .maxlen = sizeof(int),
5919 .mode = 0644,
5920 .proc_handler = proc_dointvec,
5921 },
5922 {
5923 .procname = "max_addresses",
5924 .data = &ipv6_devconf.max_addresses,
5925 .maxlen = sizeof(int),
5926 .mode = 0644,
5927 .proc_handler = proc_dointvec,
5928 },
5929 {
5930 .procname = "accept_ra_defrtr",
5931 .data = &ipv6_devconf.accept_ra_defrtr,
5932 .maxlen = sizeof(int),
5933 .mode = 0644,
5934 .proc_handler = proc_dointvec,
5935 },
5936 {
5937 .procname = "accept_ra_min_hop_limit",
5938 .data = &ipv6_devconf.accept_ra_min_hop_limit,
5939 .maxlen = sizeof(int),
5940 .mode = 0644,
5941 .proc_handler = proc_dointvec,
5942 },
5943 {
5944 .procname = "accept_ra_pinfo",
5945 .data = &ipv6_devconf.accept_ra_pinfo,
5946 .maxlen = sizeof(int),
5947 .mode = 0644,
5948 .proc_handler = proc_dointvec,
5949 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005950#ifdef CONFIG_IPV6_ROUTER_PREF
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005951 {
5952 .procname = "accept_ra_rtr_pref",
5953 .data = &ipv6_devconf.accept_ra_rtr_pref,
5954 .maxlen = sizeof(int),
5955 .mode = 0644,
5956 .proc_handler = proc_dointvec,
5957 },
5958 {
5959 .procname = "router_probe_interval",
5960 .data = &ipv6_devconf.rtr_probe_interval,
5961 .maxlen = sizeof(int),
5962 .mode = 0644,
5963 .proc_handler = proc_dointvec_jiffies,
5964 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08005965#ifdef CONFIG_IPV6_ROUTE_INFO
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005966 {
Joel Scherpelzd860b2e2017-03-22 18:19:04 +09005967 .procname = "accept_ra_rt_info_min_plen",
5968 .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
5969 .maxlen = sizeof(int),
5970 .mode = 0644,
5971 .proc_handler = proc_dointvec,
5972 },
5973 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005974 .procname = "accept_ra_rt_info_max_plen",
5975 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
5976 .maxlen = sizeof(int),
5977 .mode = 0644,
5978 .proc_handler = proc_dointvec,
5979 },
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08005980#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005981#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005982 {
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +09005983 .procname = "accept_ra_rt_table",
5984 .data = &ipv6_devconf.accept_ra_rt_table,
5985 .maxlen = sizeof(int),
5986 .mode = 0644,
5987 .proc_handler = proc_dointvec,
5988 },
5989 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005990 .procname = "proxy_ndp",
5991 .data = &ipv6_devconf.proxy_ndp,
5992 .maxlen = sizeof(int),
5993 .mode = 0644,
5994 .proc_handler = addrconf_sysctl_proxy_ndp,
5995 },
5996 {
5997 .procname = "accept_source_route",
5998 .data = &ipv6_devconf.accept_source_route,
5999 .maxlen = sizeof(int),
6000 .mode = 0644,
6001 .proc_handler = proc_dointvec,
6002 },
Neil Horman95c385b2007-04-25 17:08:10 -07006003#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006004 {
6005 .procname = "optimistic_dad",
6006 .data = &ipv6_devconf.optimistic_dad,
6007 .maxlen = sizeof(int),
6008 .mode = 0644,
6009 .proc_handler = proc_dointvec,
6010 },
6011 {
6012 .procname = "use_optimistic",
6013 .data = &ipv6_devconf.use_optimistic,
6014 .maxlen = sizeof(int),
6015 .mode = 0644,
6016 .proc_handler = proc_dointvec,
6017 },
Neil Horman95c385b2007-04-25 17:08:10 -07006018#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006019#ifdef CONFIG_IPV6_MROUTE
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006020 {
6021 .procname = "mc_forwarding",
6022 .data = &ipv6_devconf.mc_forwarding,
6023 .maxlen = sizeof(int),
6024 .mode = 0444,
6025 .proc_handler = proc_dointvec,
6026 },
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006027#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006028 {
6029 .procname = "disable_ipv6",
6030 .data = &ipv6_devconf.disable_ipv6,
6031 .maxlen = sizeof(int),
6032 .mode = 0644,
6033 .proc_handler = addrconf_sysctl_disable,
6034 },
6035 {
6036 .procname = "accept_dad",
6037 .data = &ipv6_devconf.accept_dad,
6038 .maxlen = sizeof(int),
6039 .mode = 0644,
6040 .proc_handler = proc_dointvec,
6041 },
6042 {
6043 .procname = "force_tllao",
6044 .data = &ipv6_devconf.force_tllao,
6045 .maxlen = sizeof(int),
6046 .mode = 0644,
6047 .proc_handler = proc_dointvec
6048 },
6049 {
6050 .procname = "ndisc_notify",
6051 .data = &ipv6_devconf.ndisc_notify,
6052 .maxlen = sizeof(int),
6053 .mode = 0644,
6054 .proc_handler = proc_dointvec
6055 },
6056 {
6057 .procname = "suppress_frag_ndisc",
6058 .data = &ipv6_devconf.suppress_frag_ndisc,
6059 .maxlen = sizeof(int),
6060 .mode = 0644,
6061 .proc_handler = proc_dointvec
6062 },
6063 {
6064 .procname = "accept_ra_from_local",
6065 .data = &ipv6_devconf.accept_ra_from_local,
6066 .maxlen = sizeof(int),
6067 .mode = 0644,
6068 .proc_handler = proc_dointvec,
6069 },
6070 {
6071 .procname = "accept_ra_mtu",
6072 .data = &ipv6_devconf.accept_ra_mtu,
6073 .maxlen = sizeof(int),
6074 .mode = 0644,
6075 .proc_handler = proc_dointvec,
6076 },
6077 {
6078 .procname = "stable_secret",
6079 .data = &ipv6_devconf.stable_secret,
6080 .maxlen = IPV6_MAX_STRLEN,
6081 .mode = 0600,
6082 .proc_handler = addrconf_sysctl_stable_secret,
6083 },
6084 {
6085 .procname = "use_oif_addrs_only",
6086 .data = &ipv6_devconf.use_oif_addrs_only,
6087 .maxlen = sizeof(int),
6088 .mode = 0644,
6089 .proc_handler = proc_dointvec,
6090 },
6091 {
6092 .procname = "ignore_routes_with_linkdown",
6093 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6094 .maxlen = sizeof(int),
6095 .mode = 0644,
6096 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6097 },
6098 {
6099 .procname = "drop_unicast_in_l2_multicast",
6100 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6101 .maxlen = sizeof(int),
6102 .mode = 0644,
6103 .proc_handler = proc_dointvec,
6104 },
6105 {
6106 .procname = "drop_unsolicited_na",
6107 .data = &ipv6_devconf.drop_unsolicited_na,
6108 .maxlen = sizeof(int),
6109 .mode = 0644,
6110 .proc_handler = proc_dointvec,
6111 },
6112 {
6113 .procname = "keep_addr_on_down",
6114 .data = &ipv6_devconf.keep_addr_on_down,
6115 .maxlen = sizeof(int),
6116 .mode = 0644,
6117 .proc_handler = proc_dointvec,
David Ahernf1705ec2016-02-24 09:25:37 -08006118
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006119 },
6120 {
6121 /* sentinel */
6122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123};
6124
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08006125static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006126 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127{
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006128 int i, ifindex;
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006129 struct ctl_table *table;
Eric W. Biederman6105e292012-04-19 13:41:24 +00006130 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11006131
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006132 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6133 if (!table)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006134 goto out;
6135
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006136 for (i = 0; table[i].data; i++) {
6137 table[i].data += (char *)p - (char *)&ipv6_devconf;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006138 /* If one of these is already set, then it is not safe to
6139 * overwrite either of them: this makes proc_dointvec_minmax
6140 * usable.
6141 */
6142 if (!table[i].extra1 && !table[i].extra2) {
6143 table[i].extra1 = idev; /* embedded; no ref */
6144 table[i].extra2 = net;
6145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147
Eric W. Biederman6105e292012-04-19 13:41:24 +00006148 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006150 p->sysctl_header = register_net_sysctl(net, path, table);
6151 if (!p->sysctl_header)
Eric W. Biederman6105e292012-04-19 13:41:24 +00006152 goto free;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006153
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006154 if (!strcmp(dev_name, "all"))
6155 ifindex = NETCONFA_IFINDEX_ALL;
6156 else if (!strcmp(dev_name, "default"))
6157 ifindex = NETCONFA_IFINDEX_DEFAULT;
6158 else
6159 ifindex = idev->dev->ifindex;
6160 inet6_netconf_notify_devconf(net, NETCONFA_ALL, ifindex, p);
Pavel Emelyanov95897312008-01-10 17:41:45 -08006161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006163free:
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006164 kfree(table);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006165out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08006166 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167}
6168
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006169static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
6170{
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006171 struct ctl_table *table;
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006172
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006173 if (!p->sysctl_header)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006174 return;
6175
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006176 table = p->sysctl_header->ctl_table_arg;
6177 unregister_net_sysctl_table(p->sysctl_header);
6178 p->sysctl_header = NULL;
6179 kfree(table);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006180}
6181
WANG Conga317a2f2014-07-25 15:25:09 -07006182static int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006183{
WANG Conga317a2f2014-07-25 15:25:09 -07006184 int err;
6185
6186 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6187 return -EINVAL;
6188
6189 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6190 &ndisc_ifinfo_sysctl_change);
6191 if (err)
6192 return err;
6193 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6194 idev, &idev->cnf);
6195 if (err)
6196 neigh_sysctl_unregister(idev->nd_parms);
6197
6198 return err;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006199}
6200
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006201static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006203 __addrconf_sysctl_unregister(&idev->cnf);
6204 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205}
6206
6207
6208#endif
6209
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006210static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006211{
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006212 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006213 struct ipv6_devconf *all, *dflt;
6214
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006215 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006216 if (!all)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006217 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006218
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006219 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006220 if (!dflt)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006221 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006222
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006223 /* these will be inherited by all namespaces */
6224 dflt->autoconf = ipv6_defaults.autoconf;
6225 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006226
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006227 dflt->stable_secret.initialized = false;
6228 all->stable_secret.initialized = false;
6229
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006230 net->ipv6.devconf_all = all;
6231 net->ipv6.devconf_dflt = dflt;
6232
6233#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006234 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006235 if (err < 0)
6236 goto err_reg_all;
6237
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006238 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006239 if (err < 0)
6240 goto err_reg_dflt;
6241#endif
6242 return 0;
6243
6244#ifdef CONFIG_SYSCTL
6245err_reg_dflt:
6246 __addrconf_sysctl_unregister(all);
6247err_reg_all:
6248 kfree(dflt);
6249#endif
6250err_alloc_dflt:
6251 kfree(all);
6252err_alloc_all:
6253 return err;
6254}
6255
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006256static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006257{
6258#ifdef CONFIG_SYSCTL
6259 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
6260 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
6261#endif
zhuyj73cf0e92014-11-26 10:25:58 +08006262 kfree(net->ipv6.devconf_dflt);
6263 kfree(net->ipv6.devconf_all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006264}
6265
6266static struct pernet_operations addrconf_ops = {
6267 .init = addrconf_init_net,
6268 .exit = addrconf_exit_net,
6269};
6270
Daniel Borkmann207895f2015-01-29 12:15:03 +01006271static struct rtnl_af_ops inet6_ops __read_mostly = {
Thomas Grafb382b192010-11-16 04:33:57 +00006272 .family = AF_INET6,
6273 .fill_link_af = inet6_fill_link_af,
6274 .get_link_af_size = inet6_get_link_af_size,
Daniel Borkmann11b1f822015-02-05 14:39:11 +01006275 .validate_link_af = inet6_validate_link_af,
Daniel Borkmannf53adae2013-04-08 04:01:30 +00006276 .set_link_af = inet6_set_link_af,
Thomas Grafb382b192010-11-16 04:33:57 +00006277};
6278
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279/*
6280 * Init / cleanup code
6281 */
6282
6283int __init addrconf_init(void)
6284{
WANG Conga317a2f2014-07-25 15:25:09 -07006285 struct inet6_dev *idev;
stephen hemmingerc2e21292010-03-17 20:31:10 +00006286 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006287
Stephen Hemmingere21e8462010-03-20 16:09:01 -07006288 err = ipv6_addr_label_init();
6289 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00006290 pr_crit("%s: cannot initialize default policy table: %d\n",
6291 __func__, err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006292 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006295 err = register_pernet_subsys(&addrconf_ops);
6296 if (err < 0)
6297 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006298
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006299 addrconf_wq = create_workqueue("ipv6_addrconf");
6300 if (!addrconf_wq) {
6301 err = -ENOMEM;
6302 goto out_nowq;
6303 }
6304
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 /* The addrconf netdev notifier requires that loopback_dev
6306 * has it's ipv6 private information allocated and setup
6307 * before it can bring up and give link-local addresses
6308 * to other devices which are up.
6309 *
6310 * Unfortunately, loopback_dev is not necessarily the first
6311 * entry in the global dev_base list of net devices. In fact,
6312 * it is likely to be the very last entry on that list.
6313 * So this causes the notifier registry below to try and
6314 * give link-local addresses to all devices besides loopback_dev
6315 * first, then loopback_dev, which cases all the non-loopback_dev
6316 * devices to fail to get a link-local address.
6317 *
6318 * So, as a temporary fix, allocate the ipv6 structure for
6319 * loopback_dev first by hand.
6320 * Longer term, all of the dependencies ipv6 has upon the loopback
6321 * device and it being up should be removed.
6322 */
6323 rtnl_lock();
WANG Conga317a2f2014-07-25 15:25:09 -07006324 idev = ipv6_add_dev(init_net.loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 rtnl_unlock();
WANG Conga317a2f2014-07-25 15:25:09 -07006326 if (IS_ERR(idev)) {
6327 err = PTR_ERR(idev);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006328 goto errlo;
WANG Conga317a2f2014-07-25 15:25:09 -07006329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330
WANG Congecbd3ed2017-05-03 22:07:31 -07006331 ip6_route_init_special_entries();
6332
stephen hemmingerc2e21292010-03-17 20:31:10 +00006333 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6334 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6335
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 register_netdevice_notifier(&ipv6_dev_notf);
6337
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006338 addrconf_verify();
Thomas Grafc127ea22007-03-22 11:58:32 -07006339
stephen hemminger3678a9d2013-12-30 10:41:32 -08006340 rtnl_af_register(&inet6_ops);
Thomas Grafb382b192010-11-16 04:33:57 +00006341
Greg Rosec7ac8672011-06-10 01:27:09 +00006342 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6343 NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006344 if (err < 0)
6345 goto errout;
6346
6347 /* Only the first call to __rtnl_register can fail */
Greg Rosec7ac8672011-06-10 01:27:09 +00006348 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
6349 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
6350 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6351 inet6_dump_ifaddr, NULL);
6352 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6353 inet6_dump_ifmcaddr, NULL);
6354 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6355 inet6_dump_ifacaddr, NULL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +00006356 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
Nicolas Dichtel7a674202013-03-05 23:42:06 +00006357 inet6_netconf_dump_devconf, NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006358
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006359 ipv6_addr_label_rtnl_register();
6360
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07006362errout:
Thomas Grafb382b192010-11-16 04:33:57 +00006363 rtnl_af_unregister(&inet6_ops);
Thomas Grafc127ea22007-03-22 11:58:32 -07006364 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006365errlo:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006366 destroy_workqueue(addrconf_wq);
6367out_nowq:
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006368 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006369out_addrlabel:
6370 ipv6_addr_label_cleanup();
6371out:
Thomas Grafc127ea22007-03-22 11:58:32 -07006372 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373}
6374
Daniel Lezcano09f77092007-12-13 05:34:58 -08006375void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006377 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378 int i;
6379
6380 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006381 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006382 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383
6384 rtnl_lock();
6385
Thomas Grafb382b192010-11-16 04:33:57 +00006386 __rtnl_af_unregister(&inet6_ops);
6387
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006388 /* clean dev list */
6389 for_each_netdev(&init_net, dev) {
6390 if (__in6_dev_get(dev) == NULL)
6391 continue;
6392 addrconf_ifdown(dev, 1);
6393 }
6394 addrconf_ifdown(init_net.loopback_dev, 2);
6395
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397 * Check hash table.
6398 */
stephen hemminger5c578ae2010-03-17 20:31:11 +00006399 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00006400 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6401 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578ae2010-03-17 20:31:11 +00006402 spin_unlock_bh(&addrconf_hash_lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006403 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 rtnl_unlock();
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006405
6406 destroy_workqueue(addrconf_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407}