blob: 40c3f6a157ed59df2e271dd4815f4fff8121f2a5 [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;
1081 if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
1082 ifp->prefix_len))
1083 continue;
1084 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1085 return CLEANUP_PREFIX_RT_NOP;
1086
1087 action = CLEANUP_PREFIX_RT_EXPIRE;
1088
1089 spin_lock(&ifa->lock);
1090
1091 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1092 /*
1093 * Note: Because this address is
1094 * not permanent, lifetime <
1095 * LONG_MAX / HZ here.
1096 */
1097 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1098 *expires = ifa->tstamp + lifetime * HZ;
1099 spin_unlock(&ifa->lock);
1100 }
1101
1102 return action;
1103}
1104
1105static void
1106cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
1107{
1108 struct rt6_info *rt;
1109
1110 rt = addrconf_get_prefix_route(&ifp->addr,
1111 ifp->prefix_len,
1112 ifp->idev->dev,
1113 0, RTF_GATEWAY | RTF_DEFAULT);
1114 if (rt) {
1115 if (del_rt)
1116 ip6_del_rt(rt);
1117 else {
1118 if (!(rt->rt6i_flags & RTF_EXPIRES))
1119 rt6_set_expires(rt, expires);
1120 ip6_rt_put(rt);
1121 }
1122 }
1123}
1124
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126/* This function wants to get referenced ifp and releases it before return */
1127
1128static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1129{
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001130 int state;
Thomas Haller5b84efe2014-01-15 15:36:59 +01001131 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1132 unsigned long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001134 ASSERT_RTNL();
1135
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001136 spin_lock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001137 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -07001138 ifp->state = INET6_IFADDR_STATE_DEAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001139 spin_unlock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001140
1141 if (state == INET6_IFADDR_STATE_DEAD)
1142 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
stephen hemminger5c578ae2010-03-17 20:31:11 +00001144 spin_lock_bh(&addrconf_hash_lock);
1145 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578ae2010-03-17 20:31:11 +00001146 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Thomas Haller5b84efe2014-01-15 15:36:59 +01001148 write_lock_bh(&ifp->idev->lock);
David S. Miller5d9efa72013-10-28 20:07:50 -04001149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001151 list_del(&ifp->tmp_list);
1152 if (ifp->ifpub) {
1153 in6_ifa_put(ifp->ifpub);
1154 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
stephen hemminger372e6c82010-03-17 20:31:09 +00001156 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Thomas Haller5b84efe2014-01-15 15:36:59 +01001159 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1160 action = check_cleanup_prefix_route(ifp, &expires);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001161
Thomas Haller5b84efe2014-01-15 15:36:59 +01001162 list_del_init(&ifp->if_list);
1163 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Thomas Haller5b84efe2014-01-15 15:36:59 +01001165 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001167 addrconf_del_dad_work(ifp);
Andrey Vaginb2238562008-07-08 15:13:31 -07001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 ipv6_ifa_notify(RTM_DELADDR, ifp);
1170
Cong Wangf88c91d2013-04-14 23:18:43 +08001171 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Thomas Haller5b84efe2014-01-15 15:36:59 +01001173 if (action != CLEANUP_PREFIX_RT_NOP) {
1174 cleanup_prefix_route(ifp, expires,
1175 action == CLEANUP_PREFIX_RT_DEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177
Daniel Walterc3968a82011-04-13 21:10:57 +00001178 /* clean up prefsrc entries */
1179 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001180out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 in6_ifa_put(ifp);
1182}
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1185{
1186 struct inet6_dev *idev = ifp->idev;
1187 struct in6_addr addr, *tmpaddr;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001188 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001189 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 int tmp_plen;
1191 int ret = 0;
Neil Horman95c385b2007-04-25 17:08:10 -07001192 u32 addr_flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001193 unsigned long now = jiffies;
Jiri Bohac76506a92016-10-13 18:52:15 +02001194 long max_desync_factor;
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001195 s32 cnf_temp_preferred_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Jiri Pirko53bd6742013-12-06 09:45:22 +01001197 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (ift) {
1199 spin_lock_bh(&ift->lock);
1200 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1201 spin_unlock_bh(&ift->lock);
1202 tmpaddr = &addr;
1203 } else {
1204 tmpaddr = NULL;
1205 }
1206retry:
1207 in6_dev_hold(idev);
1208 if (idev->cnf.use_tempaddr <= 0) {
Jiri Pirko53bd6742013-12-06 09:45:22 +01001209 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001210 pr_info("%s: use_tempaddr is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 in6_dev_put(idev);
1212 ret = -1;
1213 goto out;
1214 }
1215 spin_lock_bh(&ifp->lock);
1216 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1217 idev->cnf.use_tempaddr = -1; /*XXX*/
1218 spin_unlock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01001219 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001220 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1221 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 in6_dev_put(idev);
1223 ret = -1;
1224 goto out;
1225 }
1226 in6_ifa_hold(ifp);
1227 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
Jiri Bohac9d6280d2016-10-13 18:50:02 +02001228 ipv6_try_regen_rndid(idev, tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001230 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001232 regen_advance = idev->cnf.regen_max_retry *
Ian Morris67ba4152014-08-24 21:53:10 +01001233 idev->cnf.dad_transmits *
1234 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
Jiri Bohac76506a92016-10-13 18:52:15 +02001235
1236 /* recalculate max_desync_factor each time and update
1237 * idev->desync_factor if it's larger
1238 */
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001239 cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
Jiri Bohac76506a92016-10-13 18:52:15 +02001240 max_desync_factor = min_t(__u32,
1241 idev->cnf.max_desync_factor,
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001242 cnf_temp_preferred_lft - regen_advance);
Jiri Bohac76506a92016-10-13 18:52:15 +02001243
1244 if (unlikely(idev->desync_factor > max_desync_factor)) {
1245 if (max_desync_factor > 0) {
1246 get_random_bytes(&idev->desync_factor,
1247 sizeof(idev->desync_factor));
1248 idev->desync_factor %= max_desync_factor;
1249 } else {
1250 idev->desync_factor = 0;
1251 }
1252 }
1253
1254 tmp_valid_lft = min_t(__u32,
1255 ifp->valid_lft,
1256 idev->cnf.temp_valid_lft + age);
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001257 tmp_prefered_lft = cnf_temp_preferred_lft + age -
Jiri Bohac76506a92016-10-13 18:52:15 +02001258 idev->desync_factor;
Jiri Bohac76506a92016-10-13 18:52:15 +02001259 tmp_prefered_lft = min_t(__u32, ifp->prefered_lft, tmp_prefered_lft);
1260 tmp_plen = ifp->prefix_len;
1261 tmp_tstamp = ifp->tstamp;
1262 spin_unlock_bh(&ifp->lock);
1263
Jiri Pirko53bd6742013-12-06 09:45:22 +01001264 write_unlock_bh(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -07001265
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001266 /* A temporary address is created only if this calculated Preferred
1267 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1268 * an implementation must not create a temporary address with a zero
1269 * Preferred Lifetime.
Heiner Kallweitecab6702014-03-12 22:13:19 +01001270 * Use age calculation as in addrconf_verify to avoid unnecessary
1271 * temporary addresses being generated.
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001272 */
Heiner Kallweitecab6702014-03-12 22:13:19 +01001273 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1274 if (tmp_prefered_lft <= regen_advance + age) {
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001275 in6_ifa_put(ifp);
1276 in6_dev_put(idev);
1277 ret = -1;
1278 goto out;
1279 }
1280
Neil Horman95c385b2007-04-25 17:08:10 -07001281 addr_flags = IFA_F_TEMPORARY;
1282 /* set in addrconf_prefix_rcv() */
1283 if (ifp->flags & IFA_F_OPTIMISTIC)
1284 addr_flags |= IFA_F_OPTIMISTIC;
1285
Hannes Frederic Sowa4b08a8f2013-08-16 13:02:27 +02001286 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1287 ipv6_addr_scope(&addr), addr_flags,
1288 tmp_valid_lft, tmp_prefered_lft);
1289 if (IS_ERR(ift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 in6_ifa_put(ifp);
1291 in6_dev_put(idev);
Joe Perchesf3213832012-05-15 14:11:53 +00001292 pr_info("%s: retry temporary address regeneration\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 tmpaddr = &addr;
Jiri Pirko53bd6742013-12-06 09:45:22 +01001294 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 goto retry;
1296 }
1297
1298 spin_lock_bh(&ift->lock);
1299 ift->ifpub = ifp;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001300 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 ift->tstamp = tmp_tstamp;
1302 spin_unlock_bh(&ift->lock);
1303
David S. Millercf22f9a2012-04-14 21:37:40 -04001304 addrconf_dad_start(ift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 in6_ifa_put(ift);
1306 in6_dev_put(idev);
1307out:
1308 return ret;
1309}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001312 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001314enum {
1315 IPV6_SADDR_RULE_INIT = 0,
1316 IPV6_SADDR_RULE_LOCAL,
1317 IPV6_SADDR_RULE_SCOPE,
1318 IPV6_SADDR_RULE_PREFERRED,
1319#ifdef CONFIG_IPV6_MIP6
1320 IPV6_SADDR_RULE_HOA,
1321#endif
1322 IPV6_SADDR_RULE_OIF,
1323 IPV6_SADDR_RULE_LABEL,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001324 IPV6_SADDR_RULE_PRIVACY,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001325 IPV6_SADDR_RULE_ORCHID,
1326 IPV6_SADDR_RULE_PREFIX,
Erik Kline7fd25612014-10-28 18:11:14 +09001327#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1328 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1329#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001330 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001331};
1332
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001333struct ipv6_saddr_score {
1334 int rule;
1335 int addr_type;
1336 struct inet6_ifaddr *ifa;
1337 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1338 int scopedist;
1339 int matchlen;
1340};
1341
1342struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001343 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001344 int ifindex;
1345 int scope;
1346 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001347 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001348};
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001349
Dave Jonesb6f99a22007-03-22 12:27:49 -07001350static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Ulrich Weber45bb0062010-02-25 23:28:58 +00001352 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001353 return 1;
1354 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
Erik Kline7fd25612014-10-28 18:11:14 +09001357static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
1358{
1359#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1360 return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
1361#else
1362 return false;
1363#endif
1364}
1365
Benjamin Thery3de23252008-05-28 14:51:24 +02001366static int ipv6_get_saddr_eval(struct net *net,
1367 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001368 struct ipv6_saddr_dst *dst,
1369 int i)
1370{
1371 int ret;
1372
1373 if (i <= score->rule) {
1374 switch (i) {
1375 case IPV6_SADDR_RULE_SCOPE:
1376 ret = score->scopedist;
1377 break;
1378 case IPV6_SADDR_RULE_PREFIX:
1379 ret = score->matchlen;
1380 break;
1381 default:
1382 ret = !!test_bit(i, score->scorebits);
1383 }
1384 goto out;
1385 }
1386
1387 switch (i) {
1388 case IPV6_SADDR_RULE_INIT:
1389 /* Rule 0: remember if hiscore is not ready yet */
1390 ret = !!score->ifa;
1391 break;
1392 case IPV6_SADDR_RULE_LOCAL:
1393 /* Rule 1: Prefer same address */
1394 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1395 break;
1396 case IPV6_SADDR_RULE_SCOPE:
1397 /* Rule 2: Prefer appropriate scope
1398 *
1399 * ret
1400 * ^
1401 * -1 | d 15
1402 * ---+--+-+---> scope
1403 * |
1404 * | d is scope of the destination.
1405 * B-d | \
1406 * | \ <- smaller scope is better if
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08001407 * B-15 | \ if scope is enough for destination.
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001408 * | ret = B - scope (-1 <= scope >= d <= 15).
1409 * d-C-1 | /
1410 * |/ <- greater is better
1411 * -C / if scope is not enough for destination.
1412 * /| ret = scope - C (-1 <= d < scope <= 15).
1413 *
1414 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1415 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1416 * Assume B = 0 and we get C > 29.
1417 */
1418 ret = __ipv6_addr_src_scope(score->addr_type);
1419 if (ret >= dst->scope)
1420 ret = -ret;
1421 else
1422 ret -= 128; /* 30 is enough */
1423 score->scopedist = ret;
1424 break;
1425 case IPV6_SADDR_RULE_PREFERRED:
Erik Kline7fd25612014-10-28 18:11:14 +09001426 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001427 /* Rule 3: Avoid deprecated and optimistic addresses */
Erik Kline7fd25612014-10-28 18:11:14 +09001428 u8 avoid = IFA_F_DEPRECATED;
1429
1430 if (!ipv6_use_optimistic_addr(score->ifa->idev))
1431 avoid |= IFA_F_OPTIMISTIC;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001432 ret = ipv6_saddr_preferred(score->addr_type) ||
Erik Kline7fd25612014-10-28 18:11:14 +09001433 !(score->ifa->flags & avoid);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001434 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001435 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001436#ifdef CONFIG_IPV6_MIP6
1437 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001438 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001439 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001440 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1441 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001442 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001443 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001444#endif
1445 case IPV6_SADDR_RULE_OIF:
1446 /* Rule 5: Prefer outgoing interface */
1447 ret = (!dst->ifindex ||
1448 dst->ifindex == score->ifa->idev->dev->ifindex);
1449 break;
1450 case IPV6_SADDR_RULE_LABEL:
1451 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001452 ret = ipv6_addr_label(net,
1453 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001454 score->ifa->idev->dev->ifindex) == dst->label;
1455 break;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001456 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001457 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001458 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001459 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001460 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001461 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1462 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1463 score->ifa->idev->cnf.use_tempaddr >= 2;
1464 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001465 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001466 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001467 case IPV6_SADDR_RULE_ORCHID:
1468 /* Rule 8-: Prefer ORCHID vs ORCHID or
1469 * non-ORCHID vs non-ORCHID
1470 */
1471 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1472 ipv6_addr_orchid(dst->addr));
1473 break;
1474 case IPV6_SADDR_RULE_PREFIX:
1475 /* Rule 8: Use longest matching prefix */
YOSHIFUJI Hideaki / 吉藤英明91b4b042012-09-10 18:41:07 +00001476 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1477 if (ret > score->ifa->prefix_len)
1478 ret = score->ifa->prefix_len;
1479 score->matchlen = ret;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001480 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001481#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1482 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1483 /* Optimistic addresses still have lower precedence than other
1484 * preferred addresses.
1485 */
1486 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1487 break;
1488#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001489 default:
1490 ret = 0;
1491 }
1492
1493 if (ret)
1494 __set_bit(i, score->scorebits);
1495 score->rule = i;
1496out:
1497 return ret;
1498}
1499
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001500static int __ipv6_dev_get_saddr(struct net *net,
1501 struct ipv6_saddr_dst *dst,
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001502 struct inet6_dev *idev,
1503 struct ipv6_saddr_score *scores,
1504 int hiscore_idx)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001505{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001506 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001507
1508 read_lock_bh(&idev->lock);
1509 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1510 int i;
1511
1512 /*
1513 * - Tentative Address (RFC2462 section 5.4)
1514 * - A tentative address is not considered
1515 * "assigned to an interface" in the traditional
1516 * sense, unless it is also flagged as optimistic.
1517 * - Candidate Source Address (section 4)
1518 * - In any case, anycast addresses, multicast
1519 * addresses, and the unspecified address MUST
1520 * NOT be included in a candidate set.
1521 */
1522 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1523 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1524 continue;
1525
1526 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1527
1528 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1529 score->addr_type & IPV6_ADDR_MULTICAST)) {
1530 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1531 idev->dev->name);
1532 continue;
1533 }
1534
1535 score->rule = -1;
1536 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1537
1538 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1539 int minihiscore, miniscore;
1540
1541 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1542 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1543
1544 if (minihiscore > miniscore) {
1545 if (i == IPV6_SADDR_RULE_SCOPE &&
1546 score->scopedist > 0) {
1547 /*
1548 * special case:
1549 * each remaining entry
1550 * has too small (not enough)
1551 * scope, because ifa entries
1552 * are sorted by their scope
1553 * values.
1554 */
1555 goto out;
1556 }
1557 break;
1558 } else if (minihiscore < miniscore) {
1559 if (hiscore->ifa)
1560 in6_ifa_put(hiscore->ifa);
1561
1562 in6_ifa_hold(score->ifa);
1563
1564 swap(hiscore, score);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001565 hiscore_idx = 1 - hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001566
1567 /* restore our iterator */
1568 score->ifa = hiscore->ifa;
1569
1570 break;
1571 }
1572 }
1573 }
1574out:
1575 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001576 return hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001577}
1578
David Ahernafbac6012016-06-16 16:24:26 -07001579static int ipv6_get_saddr_master(struct net *net,
1580 const struct net_device *dst_dev,
1581 const struct net_device *master,
1582 struct ipv6_saddr_dst *dst,
1583 struct ipv6_saddr_score *scores,
1584 int hiscore_idx)
1585{
1586 struct inet6_dev *idev;
1587
1588 idev = __in6_dev_get(dst_dev);
1589 if (idev)
1590 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1591 scores, hiscore_idx);
1592
1593 idev = __in6_dev_get(master);
1594 if (idev)
1595 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1596 scores, hiscore_idx);
1597
1598 return hiscore_idx;
1599}
1600
Patrick McHardyb3f644f2012-08-26 19:14:14 +02001601int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001602 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001603 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001605 struct ipv6_saddr_score scores[2], *hiscore;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001606 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001607 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001608 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001609 int dst_type;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001610 bool use_oif_addr = false;
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001611 int hiscore_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001613 dst_type = __ipv6_addr_type(daddr);
1614 dst.addr = daddr;
1615 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1616 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001617 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001618 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001619
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001620 scores[hiscore_idx].rule = -1;
1621 scores[hiscore_idx].ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001623 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001624
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001625 /* Candidate Source Address (section 4)
1626 * - multicast and link-local destination address,
1627 * the set of candidate source address MUST only
1628 * include addresses assigned to interfaces
1629 * belonging to the same link as the outgoing
1630 * interface.
1631 * (- For site-local destination addresses, the
1632 * set of candidate source addresses MUST only
1633 * include addresses assigned to interfaces
1634 * belonging to the same site as the outgoing
1635 * interface.)
Erik Kline3985e8a2015-07-22 16:38:25 +09001636 * - "It is RECOMMENDED that the candidate source addresses
1637 * be the set of unicast addresses assigned to the
1638 * interface that will be used to send to the destination
1639 * (the 'outgoing' interface)." (RFC 6724)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001640 */
1641 if (dst_dev) {
Erik Kline3985e8a2015-07-22 16:38:25 +09001642 idev = __in6_dev_get(dst_dev);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001643 if ((dst_type & IPV6_ADDR_MULTICAST) ||
Erik Kline3985e8a2015-07-22 16:38:25 +09001644 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1645 (idev && idev->cnf.use_oif_addrs_only)) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001646 use_oif_addr = true;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001647 }
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001648 }
1649
1650 if (use_oif_addr) {
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001651 if (idev)
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001652 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001653 } else {
David Ahernafbac6012016-06-16 16:24:26 -07001654 const struct net_device *master;
1655 int master_idx = 0;
1656
1657 /* if dst_dev exists and is enslaved to an L3 device, then
1658 * prefer addresses from dst_dev and then the master over
1659 * any other enslaved devices in the L3 domain.
1660 */
1661 master = l3mdev_master_dev_rcu(dst_dev);
1662 if (master) {
1663 master_idx = master->ifindex;
1664
1665 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1666 master, &dst,
1667 scores, hiscore_idx);
1668
1669 if (scores[hiscore_idx].ifa)
1670 goto out;
1671 }
1672
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001673 for_each_netdev_rcu(net, dev) {
David Ahernafbac6012016-06-16 16:24:26 -07001674 /* only consider addresses on devices in the
1675 * same L3 domain
1676 */
1677 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1678 continue;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001679 idev = __in6_dev_get(dev);
1680 if (!idev)
1681 continue;
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001682 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001683 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001684 }
David Ahernafbac6012016-06-16 16:24:26 -07001685
1686out:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001687 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001689 hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001690 if (!hiscore->ifa)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001691 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001692
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001693 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001694 in6_ifa_put(hiscore->ifa);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001695 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001697EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Amerigo Wang89657792013-06-29 21:30:49 +08001699int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001700 u32 banned_flags)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001701{
1702 struct inet6_ifaddr *ifp;
1703 int err = -EADDRNOTAVAIL;
1704
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01001705 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1706 if (ifp->scope > IFA_LINK)
1707 break;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001708 if (ifp->scope == IFA_LINK &&
1709 !(ifp->flags & banned_flags)) {
1710 *addr = ifp->addr;
1711 err = 0;
1712 break;
1713 }
1714 }
1715 return err;
1716}
1717
Neil Horman95c385b2007-04-25 17:08:10 -07001718int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001719 u32 banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
1721 struct inet6_dev *idev;
1722 int err = -EADDRNOTAVAIL;
1723
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001724 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001725 idev = __in6_dev_get(dev);
1726 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 read_lock_bh(&idev->lock);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001728 err = __ipv6_get_lladdr(idev, addr, banned_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 read_unlock_bh(&idev->lock);
1730 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001731 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return err;
1733}
1734
1735static int ipv6_count_addresses(struct inet6_dev *idev)
1736{
1737 int cnt = 0;
1738 struct inet6_ifaddr *ifp;
1739
1740 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001741 list_for_each_entry(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 cnt++;
1743 read_unlock_bh(&idev->lock);
1744 return cnt;
1745}
1746
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001747int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Florian Westphal2a7851b2013-05-17 03:56:10 +00001748 const struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Erik Klinec58da4c2015-02-04 20:01:23 +09001750 return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1751}
1752EXPORT_SYMBOL(ipv6_chk_addr);
1753
1754int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1755 const struct net_device *dev, int strict,
1756 u32 banned_flags)
1757{
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001758 struct inet6_ifaddr *ifp;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001759 unsigned int hash = inet6_addr_hash(addr);
Erik Klinec58da4c2015-02-04 20:01:23 +09001760 u32 ifp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
stephen hemminger5c578ae2010-03-17 20:31:11 +00001762 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001763 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001764 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001765 continue;
Erik Klinec58da4c2015-02-04 20:01:23 +09001766 /* Decouple optimistic from tentative for evaluation here.
1767 * Ban optimistic addresses explicitly, when required.
1768 */
1769 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1770 ? (ifp->flags&~IFA_F_TENTATIVE)
1771 : ifp->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 if (ipv6_addr_equal(&ifp->addr, addr) &&
Erik Klinec58da4c2015-02-04 20:01:23 +09001773 !(ifp_flags&banned_flags) &&
Ian Morris63159f22015-03-29 14:00:04 +01001774 (!dev || ifp->idev->dev == dev ||
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001775 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1776 rcu_read_unlock_bh();
1777 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 }
1779 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001780
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001781 rcu_read_unlock_bh();
1782 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
Erik Klinec58da4c2015-02-04 20:01:23 +09001784EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001785
David S. Miller3e81c6d2010-03-20 16:18:00 -07001786static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1787 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Eric Dumazetddbe5032012-07-18 08:11:12 +00001789 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001790 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Sasha Levinb67bfe02013-02-27 17:06:00 -08001792 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001793 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001794 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001796 if (!dev || ifp->idev->dev == dev)
David S. Miller3e81c6d2010-03-20 16:18:00 -07001797 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
1799 }
David S. Miller3e81c6d2010-03-20 16:18:00 -07001800 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801}
1802
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001803/* Compares an address/prefix_len with addresses on device @dev.
1804 * If one is found it returns true.
1805 */
1806bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1807 const unsigned int prefix_len, struct net_device *dev)
1808{
1809 struct inet6_dev *idev;
1810 struct inet6_ifaddr *ifa;
1811 bool ret = false;
1812
1813 rcu_read_lock();
1814 idev = __in6_dev_get(dev);
1815 if (idev) {
1816 read_lock_bh(&idev->lock);
1817 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1818 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1819 if (ret)
1820 break;
1821 }
1822 read_unlock_bh(&idev->lock);
1823 }
1824 rcu_read_unlock();
1825
1826 return ret;
1827}
1828EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1829
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001830int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001831{
1832 struct inet6_dev *idev;
1833 struct inet6_ifaddr *ifa;
1834 int onlink;
1835
1836 onlink = 0;
1837 rcu_read_lock();
1838 idev = __in6_dev_get(dev);
1839 if (idev) {
1840 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001841 list_for_each_entry(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001842 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1843 ifa->prefix_len);
1844 if (onlink)
1845 break;
1846 }
1847 read_unlock_bh(&idev->lock);
1848 }
1849 rcu_read_unlock();
1850 return onlink;
1851}
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001852EXPORT_SYMBOL(ipv6_chk_prefix);
1853
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001854struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001855 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
David S. Millerb79d1d52010-03-25 21:39:21 -07001857 struct inet6_ifaddr *ifp, *result = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001858 unsigned int hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
stephen hemminger5c578ae2010-03-17 20:31:11 +00001860 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001861 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001862 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001863 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001865 if (!dev || ifp->idev->dev == dev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07001867 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 in6_ifa_hold(ifp);
1869 break;
1870 }
1871 }
1872 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001873 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
David S. Millerb79d1d52010-03-25 21:39:21 -07001875 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878/* Gets referenced address, destroys ifaddr */
1879
Brian Haleycc411d02009-09-09 14:41:32 +00001880static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
Lubomir Rintel3d171f32016-01-08 13:47:23 +01001882 if (dad_failed)
1883 ifp->flags |= IFA_F_DADFAILED;
1884
Sabrina Dubrocad2c95122017-06-29 16:56:54 +02001885 if (ifp->flags&IFA_F_TEMPORARY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 struct inet6_ifaddr *ifpub;
1887 spin_lock_bh(&ifp->lock);
1888 ifpub = ifp->ifpub;
1889 if (ifpub) {
1890 in6_ifa_hold(ifpub);
1891 spin_unlock_bh(&ifp->lock);
1892 ipv6_create_tempaddr(ifpub, ifp);
1893 in6_ifa_put(ifpub);
1894 } else {
1895 spin_unlock_bh(&ifp->lock);
1896 }
1897 ipv6_del_addr(ifp);
Sabrina Dubrocad2c95122017-06-29 16:56:54 +02001898 } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
1899 spin_lock_bh(&ifp->lock);
1900 addrconf_del_dad_work(ifp);
1901 ifp->flags |= IFA_F_TENTATIVE;
1902 spin_unlock_bh(&ifp->lock);
1903 if (dad_failed)
1904 ipv6_ifa_notify(0, ifp);
1905 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001906 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
Herbert Xuf2344a12010-05-18 15:55:27 -07001911static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1912{
1913 int err = -ENOENT;
1914
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001915 spin_lock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001916 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1917 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1918 err = 0;
1919 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001920 spin_unlock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001921
1922 return err;
1923}
1924
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001925void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1926{
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001927 struct inet6_dev *idev = ifp->idev;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001928 struct net *net = dev_net(ifp->idev->dev);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001929
Ursula Braun853dc2e2010-10-24 23:06:43 +00001930 if (addrconf_dad_end(ifp)) {
1931 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07001932 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00001933 }
Herbert Xuf2344a12010-05-18 15:55:27 -07001934
Joe Perchese87cc472012-05-13 21:56:26 +00001935 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1936 ifp->idev->dev->name, &ifp->addr);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001937
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001938 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001939
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001940 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
1941 int scope = ifp->scope;
1942 u32 flags = ifp->flags;
1943 struct in6_addr new_addr;
1944 struct inet6_ifaddr *ifp2;
1945 u32 valid_lft, preferred_lft;
1946 int pfxlen = ifp->prefix_len;
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001947 int retries = ifp->stable_privacy_retry + 1;
1948
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001949 if (retries > net->ipv6.sysctl.idgen_retries) {
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001950 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
1951 ifp->idev->dev->name);
1952 goto errdad;
1953 }
1954
1955 new_addr = ifp->addr;
1956 if (ipv6_generate_stable_address(&new_addr, retries,
1957 idev))
1958 goto errdad;
1959
1960 valid_lft = ifp->valid_lft;
1961 preferred_lft = ifp->prefered_lft;
1962
1963 spin_unlock_bh(&ifp->lock);
1964
1965 if (idev->cnf.max_addresses &&
1966 ipv6_count_addresses(idev) >=
1967 idev->cnf.max_addresses)
1968 goto lock_errdad;
1969
1970 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
1971 ifp->idev->dev->name);
1972
1973 ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
1974 scope, flags, valid_lft,
1975 preferred_lft);
1976 if (IS_ERR(ifp2))
1977 goto lock_errdad;
1978
1979 spin_lock_bh(&ifp2->lock);
1980 ifp2->stable_privacy_retry = retries;
1981 ifp2->state = INET6_IFADDR_STATE_PREDAD;
1982 spin_unlock_bh(&ifp2->lock);
1983
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001984 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001985 in6_ifa_put(ifp2);
1986lock_errdad:
1987 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001988 }
1989
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001990errdad:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001991 /* transition from _POSTDAD to _ERRDAD */
1992 ifp->state = INET6_IFADDR_STATE_ERRDAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001993 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001994
1995 addrconf_mod_dad_work(ifp, 0);
Wei Yongjun751eb6b2016-09-05 16:06:31 +08001996 in6_ifa_put(ifp);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001997}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02001999/* Join to solicited addr multicast group.
2000 * caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002001void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
2003 struct in6_addr maddr;
2004
2005 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2006 return;
2007
2008 addrconf_addr_solict_mult(addr, &maddr);
2009 ipv6_dev_mc_inc(dev, &maddr);
2010}
2011
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002012/* caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002013void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
2015 struct in6_addr maddr;
2016
2017 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2018 return;
2019
2020 addrconf_addr_solict_mult(addr, &maddr);
2021 __ipv6_dev_mc_dec(idev, &maddr);
2022}
2023
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002024/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002025static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
2027 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002028
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002029 if (ifp->prefix_len >= 127) /* RFC 6164 */
Bjørn Mork2bda8a02011-07-05 23:04:13 +00002030 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2032 if (ipv6_addr_any(&addr))
2033 return;
WANG Cong013b4d92014-09-11 15:35:11 -07002034 __ipv6_dev_ac_inc(ifp->idev, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002037/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002038static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
2040 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002041
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002042 if (ifp->prefix_len >= 127) /* RFC 6164 */
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00002043 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2045 if (ipv6_addr_any(&addr))
2046 return;
2047 __ipv6_dev_ac_dec(ifp->idev, &addr);
2048}
2049
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002050static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
2051{
Alexander Aring5241c2d2015-12-14 20:55:22 +01002052 if (dev->addr_len != EUI64_ADDR_LEN)
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002053 return -1;
Alexander Aring5241c2d2015-12-14 20:55:22 +01002054 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
YOSHIFUJI Hideaki / 吉藤英明5e98a362013-01-28 10:44:29 +00002055 eui[0] ^= 2;
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002056 return 0;
2057}
2058
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002059static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2060{
2061 union fwnet_hwaddr *ha;
2062
2063 if (dev->addr_len != FWNET_ALEN)
2064 return -1;
2065
2066 ha = (union fwnet_hwaddr *)dev->dev_addr;
2067
2068 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2069 eui[0] ^= 2;
2070 return 0;
2071}
2072
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002073static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2074{
2075 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2076 if (dev->addr_len != ARCNET_ALEN)
2077 return -1;
2078 memset(eui, 0, 7);
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002079 eui[7] = *(u8 *)dev->dev_addr;
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002080 return 0;
2081}
2082
2083static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2084{
2085 if (dev->addr_len != INFINIBAND_ALEN)
2086 return -1;
2087 memcpy(eui, dev->dev_addr + 12, 8);
2088 eui[0] |= 2;
2089 return 0;
2090}
2091
stephen hemmingerc61393e2010-10-04 20:17:53 +00002092static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002093{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00002094 if (addr == 0)
2095 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002096 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2097 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2098 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2099 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2100 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2101 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2102 eui[1] = 0;
2103 eui[2] = 0x5E;
2104 eui[3] = 0xFE;
2105 memcpy(eui + 4, &addr, 4);
2106 return 0;
2107}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002108
2109static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2110{
2111 if (dev->priv_flags & IFF_ISATAP)
2112 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2113 return -1;
2114}
2115
stephen hemmingeraee80b52011-06-08 10:44:30 +00002116static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2117{
2118 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2119}
2120
Nicolas Dichtele8377352013-08-20 12:16:06 +02002121static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2122{
2123 memcpy(eui, dev->perm_addr, 3);
2124 memcpy(eui + 5, dev->perm_addr + 3, 3);
2125 eui[3] = 0xFF;
2126 eui[4] = 0xFE;
2127 eui[0] ^= 2;
2128 return 0;
2129}
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2132{
2133 switch (dev->type) {
2134 case ARPHRD_ETHER:
2135 case ARPHRD_FDDI:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002136 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002138 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002140 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002141 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002142 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002143 case ARPHRD_IPGRE:
2144 return addrconf_ifid_gre(eui, dev);
Jukka Rissanene74bccb2013-12-11 17:05:36 +02002145 case ARPHRD_6LOWPAN:
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002146 return addrconf_ifid_eui64(eui, dev);
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002147 case ARPHRD_IEEE1394:
2148 return addrconf_ifid_ieee1394(eui, dev);
Nicolas Dichtele8377352013-08-20 12:16:06 +02002149 case ARPHRD_TUNNEL6:
2150 return addrconf_ifid_ip6tnl(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 }
2152 return -1;
2153}
2154
2155static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2156{
2157 int err = -1;
2158 struct inet6_ifaddr *ifp;
2159
2160 read_lock_bh(&idev->lock);
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01002161 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2162 if (ifp->scope > IFA_LINK)
2163 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2165 memcpy(eui, ifp->addr.s6_addr+8, 8);
2166 err = 0;
2167 break;
2168 }
2169 }
2170 read_unlock_bh(&idev->lock);
2171 return err;
2172}
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002175static void ipv6_regen_rndid(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08002178 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 /*
2182 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2183 * check if generated address is not inappropriate
2184 *
2185 * - Reserved subnet anycast (RFC 2526)
2186 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002187 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 * 00-00-5E-FE-xx-xx-xx-xx
2189 * - value 0
2190 * - XXX: already assigned to an address on the device
2191 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002192 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2194 (idev->rndid[7]&0x80))
2195 goto regen;
2196 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2197 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2198 goto regen;
2199 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2200 goto regen;
2201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
2203
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002204static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002205{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002207 ipv6_regen_rndid(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +09002210u32 addrconf_rt_table(const struct net_device *dev, u32 default_table) {
2211 /* Determines into what table to put autoconf PIO/RIO/default routes
2212 * learned on this device.
2213 *
2214 * - If 0, use the same table for every device. This puts routes into
2215 * one of RT_TABLE_{PREFIX,INFO,DFLT} depending on the type of route
2216 * (but note that these three are currently all equal to
2217 * RT6_TABLE_MAIN).
2218 * - If > 0, use the specified table.
2219 * - If < 0, put routes into table dev->ifindex + (-rt_table).
2220 */
2221 struct inet6_dev *idev = in6_dev_get(dev);
2222 u32 table;
2223 int sysctl = idev->cnf.accept_ra_rt_table;
2224 if (sysctl == 0) {
2225 table = default_table;
2226 } else if (sysctl > 0) {
2227 table = (u32) sysctl;
2228 } else {
2229 table = (unsigned) dev->ifindex + (-sysctl);
2230 }
2231 in6_dev_put(idev);
2232 return table;
2233}
2234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235/*
2236 * Add prefix route.
2237 */
2238
2239static void
2240addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002241 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
Thomas Graf86872cb2006-08-22 00:01:08 -07002243 struct fib6_config cfg = {
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +09002244 .fc_table = l3mdev_fib_table(dev) ? : addrconf_rt_table(dev, RT6_TABLE_PREFIX),
Thomas Graf86872cb2006-08-22 00:01:08 -07002245 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2246 .fc_ifindex = dev->ifindex,
2247 .fc_expires = expires,
2248 .fc_dst_len = plen,
2249 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002250 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07002251 .fc_protocol = RTPROT_KERNEL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002252 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002254 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 /* Prevent useless cloning on PtP SIT.
2257 This thing is done here expecting that the whole
2258 class of non-broadcast devices need not cloning.
2259 */
Amerigo Wang07a93622012-10-29 16:23:10 +00002260#if IS_ENABLED(CONFIG_IPV6_SIT)
Thomas Graf86872cb2006-08-22 00:01:08 -07002261 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2262 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002263#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Thomas Graf86872cb2006-08-22 00:01:08 -07002265 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002268
2269static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2270 int plen,
2271 const struct net_device *dev,
2272 u32 flags, u32 noflags)
2273{
2274 struct fib6_node *fn;
2275 struct rt6_info *rt = NULL;
2276 struct fib6_table *table;
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +09002277 u32 tb_id = l3mdev_fib_table(dev) ? : addrconf_rt_table(dev, RT6_TABLE_PREFIX);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002278
David Ahernca254492015-10-12 11:47:10 -07002279 table = fib6_get_table(dev_net(dev), tb_id);
Ian Morris63159f22015-03-29 14:00:04 +01002280 if (!table)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002281 return NULL;
2282
Li RongQing5744dd92012-09-11 21:59:01 +00002283 read_lock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002284 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2285 if (!fn)
2286 goto out;
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07002287
2288 noflags |= RTF_CACHE;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002289 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002290 if (rt->dst.dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002291 continue;
2292 if ((rt->rt6i_flags & flags) != flags)
2293 continue;
Romain Kuntz85da53b2013-01-09 15:02:26 +01002294 if ((rt->rt6i_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002295 continue;
2296 dst_hold(&rt->dst);
2297 break;
2298 }
2299out:
Li RongQing5744dd92012-09-11 21:59:01 +00002300 read_unlock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002301 return rt;
2302}
2303
2304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305/* Create "default" multicast route to the interface */
2306
2307static void addrconf_add_mroute(struct net_device *dev)
2308{
Thomas Graf86872cb2006-08-22 00:01:08 -07002309 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002310 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002311 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2312 .fc_ifindex = dev->ifindex,
2313 .fc_dst_len = 8,
2314 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002315 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002316 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Thomas Graf86872cb2006-08-22 00:01:08 -07002318 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2319
2320 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321}
2322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2324{
2325 struct inet6_dev *idev;
2326
2327 ASSERT_RTNL();
2328
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002329 idev = ipv6_find_idev(dev);
2330 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00002331 return ERR_PTR(-ENOBUFS);
2332
2333 if (idev->cnf.disable_ipv6)
2334 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
2336 /* Add default multicast route */
David Ahernba46ee42016-06-13 13:44:18 -07002337 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
Li Wei4af04ab2011-12-06 21:23:45 +00002338 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 return idev;
2341}
2342
Jiri Pirko53bd6742013-12-06 09:45:22 +01002343static void manage_tempaddrs(struct inet6_dev *idev,
2344 struct inet6_ifaddr *ifp,
2345 __u32 valid_lft, __u32 prefered_lft,
2346 bool create, unsigned long now)
2347{
2348 u32 flags;
2349 struct inet6_ifaddr *ift;
2350
2351 read_lock_bh(&idev->lock);
2352 /* update all temporary addresses in the list */
2353 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2354 int age, max_valid, max_prefered;
2355
2356 if (ifp != ift->ifpub)
2357 continue;
2358
2359 /* RFC 4941 section 3.3:
2360 * If a received option will extend the lifetime of a public
2361 * address, the lifetimes of temporary addresses should
2362 * be extended, subject to the overall constraint that no
2363 * temporary addresses should ever remain "valid" or "preferred"
2364 * for a time longer than (TEMP_VALID_LIFETIME) or
2365 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2366 */
2367 age = (now - ift->cstamp) / HZ;
2368 max_valid = idev->cnf.temp_valid_lft - age;
2369 if (max_valid < 0)
2370 max_valid = 0;
2371
2372 max_prefered = idev->cnf.temp_prefered_lft -
Jiri Bohac76506a92016-10-13 18:52:15 +02002373 idev->desync_factor - age;
Jiri Pirko53bd6742013-12-06 09:45:22 +01002374 if (max_prefered < 0)
2375 max_prefered = 0;
2376
2377 if (valid_lft > max_valid)
2378 valid_lft = max_valid;
2379
2380 if (prefered_lft > max_prefered)
2381 prefered_lft = max_prefered;
2382
2383 spin_lock(&ift->lock);
2384 flags = ift->flags;
2385 ift->valid_lft = valid_lft;
2386 ift->prefered_lft = prefered_lft;
2387 ift->tstamp = now;
2388 if (prefered_lft > 0)
2389 ift->flags &= ~IFA_F_DEPRECATED;
2390
2391 spin_unlock(&ift->lock);
2392 if (!(flags&IFA_F_TENTATIVE))
2393 ipv6_ifa_notify(0, ift);
2394 }
2395
2396 if ((create || list_empty(&idev->tempaddr_list)) &&
2397 idev->cnf.use_tempaddr > 0) {
2398 /* When a new public address is created as described
2399 * in [ADDRCONF], also create a new temporary address.
2400 * Also create a temporary address if it's enabled but
2401 * no temporary address currently exists.
2402 */
2403 read_unlock_bh(&idev->lock);
2404 ipv6_create_tempaddr(ifp, NULL);
2405 } else {
2406 read_unlock_bh(&idev->lock);
2407 }
2408}
2409
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002410static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2411{
2412 return idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2413 idev->addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2414}
2415
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002416int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2417 const struct prefix_info *pinfo,
2418 struct inet6_dev *in6_dev,
2419 const struct in6_addr *addr, int addr_type,
2420 u32 addr_flags, bool sllao, bool tokenized,
2421 __u32 valid_lft, u32 prefered_lft)
Alexander Aring4f672232016-06-15 21:20:22 +02002422{
2423 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2424 int create = 0, update_lft = 0;
2425
2426 if (!ifp && valid_lft) {
2427 int max_addresses = in6_dev->cnf.max_addresses;
2428
2429#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2430 if (in6_dev->cnf.optimistic_dad &&
2431 !net->ipv6.devconf_all->forwarding && sllao)
2432 addr_flags |= IFA_F_OPTIMISTIC;
2433#endif
2434
2435 /* Do not allow to create too much of autoconfigured
2436 * addresses; this would be too easy way to crash kernel.
2437 */
2438 if (!max_addresses ||
2439 ipv6_count_addresses(in6_dev) < max_addresses)
2440 ifp = ipv6_add_addr(in6_dev, addr, NULL,
2441 pinfo->prefix_len,
2442 addr_type&IPV6_ADDR_SCOPE_MASK,
2443 addr_flags, valid_lft,
2444 prefered_lft);
2445
2446 if (IS_ERR_OR_NULL(ifp))
2447 return -1;
2448
2449 update_lft = 0;
2450 create = 1;
2451 spin_lock_bh(&ifp->lock);
2452 ifp->flags |= IFA_F_MANAGETEMPADDR;
2453 ifp->cstamp = jiffies;
2454 ifp->tokenized = tokenized;
2455 spin_unlock_bh(&ifp->lock);
2456 addrconf_dad_start(ifp);
2457 }
2458
2459 if (ifp) {
2460 u32 flags;
2461 unsigned long now;
2462 u32 stored_lft;
2463
2464 /* update lifetime (RFC2462 5.5.3 e) */
2465 spin_lock_bh(&ifp->lock);
2466 now = jiffies;
2467 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2468 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2469 else
2470 stored_lft = 0;
2471 if (!update_lft && !create && stored_lft) {
2472 const u32 minimum_lft = min_t(u32,
2473 stored_lft, MIN_VALID_LIFETIME);
2474 valid_lft = max(valid_lft, minimum_lft);
2475
2476 /* RFC4862 Section 5.5.3e:
2477 * "Note that the preferred lifetime of the
2478 * corresponding address is always reset to
2479 * the Preferred Lifetime in the received
2480 * Prefix Information option, regardless of
2481 * whether the valid lifetime is also reset or
2482 * ignored."
2483 *
2484 * So we should always update prefered_lft here.
2485 */
2486 update_lft = 1;
2487 }
2488
2489 if (update_lft) {
2490 ifp->valid_lft = valid_lft;
2491 ifp->prefered_lft = prefered_lft;
2492 ifp->tstamp = now;
2493 flags = ifp->flags;
2494 ifp->flags &= ~IFA_F_DEPRECATED;
2495 spin_unlock_bh(&ifp->lock);
2496
2497 if (!(flags&IFA_F_TENTATIVE))
2498 ipv6_ifa_notify(0, ifp);
2499 } else
2500 spin_unlock_bh(&ifp->lock);
2501
2502 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2503 create, now);
2504
2505 in6_ifa_put(ifp);
2506 addrconf_verify();
2507 }
2508
2509 return 0;
2510}
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002511EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
Alexander Aring4f672232016-06-15 21:20:22 +02002512
Neil Hormane6bff992012-01-04 10:49:15 +00002513void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
2515 struct prefix_info *pinfo;
2516 __u32 valid_lft;
2517 __u32 prefered_lft;
Alexander Aring4f672232016-06-15 21:20:22 +02002518 int addr_type, err;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002519 u32 addr_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07002521 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 if (len < sizeof(struct prefix_info)) {
dingtianhongba3542e2013-08-17 10:27:04 +08002526 ADBG("addrconf: prefix option too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 return;
2528 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002529
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 /*
2531 * Validation checks ([ADDRCONF], page 19)
2532 */
2533
2534 addr_type = ipv6_addr_type(&pinfo->prefix);
2535
2536 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2537 return;
2538
2539 valid_lft = ntohl(pinfo->valid);
2540 prefered_lft = ntohl(pinfo->prefered);
2541
2542 if (prefered_lft > valid_lft) {
Joe Perchese87cc472012-05-13 21:56:26 +00002543 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 return;
2545 }
2546
2547 in6_dev = in6_dev_get(dev);
2548
Ian Morris63159f22015-03-29 14:00:04 +01002549 if (!in6_dev) {
Joe Perchese87cc472012-05-13 21:56:26 +00002550 net_dbg_ratelimited("addrconf: device %s not configured\n",
2551 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 return;
2553 }
2554
2555 /*
2556 * Two things going on here:
2557 * 1) Add routes for on-link prefixes
2558 * 2) Configure prefixes with the auto flag set
2559 */
2560
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002561 if (pinfo->onlink) {
2562 struct rt6_info *rt;
2563 unsigned long rt_expires;
2564
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002565 /* Avoid arithmetic overflow. Really, we could
2566 * save rt_expires in seconds, likely valid_lft,
2567 * but it would require division in fib gc, that it
2568 * not good.
2569 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002570 if (HZ > USER_HZ)
2571 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2572 else
2573 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08002574
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002575 if (addrconf_finite_timeout(rt_expires))
2576 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002578 rt = addrconf_get_prefix_route(&pinfo->prefix,
2579 pinfo->prefix_len,
2580 dev,
2581 RTF_ADDRCONF | RTF_PREFIX_RT,
2582 RTF_GATEWAY | RTF_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002584 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002585 /* Autoconf prefix route */
2586 if (valid_lft == 0) {
2587 ip6_del_rt(rt);
2588 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002589 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002590 /* not infinity */
Gao feng1716a962012-04-06 00:13:10 +00002591 rt6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002592 } else {
Gao feng1716a962012-04-06 00:13:10 +00002593 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 }
2595 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002596 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002597 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2598 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002599 /* not infinity */
2600 flags |= RTF_EXPIRES;
2601 expires = jiffies_to_clock_t(rt_expires);
2602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002604 dev, expires, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00002606 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 }
2608
2609 /* Try to figure out our local address for this prefix */
2610
2611 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 struct in6_addr addr;
Alexander Aringf997c552016-06-15 21:20:23 +02002613 bool tokenized = false, dev_addr_generated = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
2615 if (pinfo->prefix_len == 64) {
2616 memcpy(&addr, &pinfo->prefix, 8);
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002617
2618 if (!ipv6_addr_any(&in6_dev->token)) {
2619 read_lock_bh(&in6_dev->lock);
2620 memcpy(addr.s6_addr + 8,
2621 in6_dev->token.s6_addr + 8, 8);
2622 read_unlock_bh(&in6_dev->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00002623 tokenized = true;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002624 } else if (is_addr_mode_generate_stable(in6_dev) &&
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002625 !ipv6_generate_stable_address(&addr, 0,
2626 in6_dev)) {
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002627 addr_flags |= IFA_F_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002628 goto ok;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002629 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2630 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
Alexander Aring4f672232016-06-15 21:20:22 +02002631 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002632 } else {
2633 dev_addr_generated = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 }
2635 goto ok;
2636 }
Joe Perchese87cc472012-05-13 21:56:26 +00002637 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2638 pinfo->prefix_len);
Alexander Aring4f672232016-06-15 21:20:22 +02002639 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641ok:
Alexander Aring4f672232016-06-15 21:20:22 +02002642 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2643 &addr, addr_type,
2644 addr_flags, sllao,
2645 tokenized, valid_lft,
2646 prefered_lft);
2647 if (err)
2648 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002649
2650 /* Ignore error case here because previous prefix add addr was
2651 * successful which will be notified.
2652 */
2653 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2654 addr_type, addr_flags, sllao,
2655 tokenized, valid_lft,
2656 prefered_lft,
2657 dev_addr_generated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 }
2659 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
Alexander Aring4f672232016-06-15 21:20:22 +02002660put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 in6_dev_put(in6_dev);
2662}
2663
2664/*
2665 * Set destination address.
2666 * Special case for SIT interfaces where we create a new "virtual"
2667 * device.
2668 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002669int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670{
2671 struct in6_ifreq ireq;
2672 struct net_device *dev;
2673 int err = -EINVAL;
2674
2675 rtnl_lock();
2676
2677 err = -EFAULT;
2678 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2679 goto err_exit;
2680
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002681 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
2683 err = -ENODEV;
Ian Morris63159f22015-03-29 14:00:04 +01002684 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 goto err_exit;
2686
Amerigo Wang07a93622012-10-29 16:23:10 +00002687#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002689 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 struct ip_tunnel_parm p;
2692
2693 err = -EADDRNOTAVAIL;
2694 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2695 goto err_exit;
2696
2697 memset(&p, 0, sizeof(p));
2698 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2699 p.iph.saddr = 0;
2700 p.iph.version = 4;
2701 p.iph.ihl = 5;
2702 p.iph.protocol = IPPROTO_IPV6;
2703 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002704 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002706 if (ops->ndo_do_ioctl) {
2707 mm_segment_t oldfs = get_fs();
2708
2709 set_fs(KERNEL_DS);
2710 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2711 set_fs(oldfs);
2712 } else
2713 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
2715 if (err == 0) {
2716 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002717 dev = __dev_get_by_name(net, p.name);
2718 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 goto err_exit;
2720 err = dev_open(dev);
2721 }
2722 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002723#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
2725err_exit:
2726 rtnl_unlock();
2727 return err;
2728}
2729
Madhu Challa93a714d2015-02-25 09:58:35 -08002730static int ipv6_mc_config(struct sock *sk, bool join,
2731 const struct in6_addr *addr, int ifindex)
2732{
2733 int ret;
2734
2735 ASSERT_RTNL();
2736
2737 lock_sock(sk);
2738 if (join)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002739 ret = ipv6_sock_mc_join(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002740 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002741 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002742 release_sock(sk);
2743
2744 return ret;
2745}
2746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747/*
2748 * Manual configuration of address on an interface
2749 */
Jiri Pirko479840f2013-12-06 09:45:21 +01002750static int inet6_addr_add(struct net *net, int ifindex,
2751 const struct in6_addr *pfx,
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002752 const struct in6_addr *peer_pfx,
Jiri Pirko479840f2013-12-06 09:45:21 +01002753 unsigned int plen, __u32 ifa_flags,
2754 __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755{
2756 struct inet6_ifaddr *ifp;
2757 struct inet6_dev *idev;
2758 struct net_device *dev;
Madhu Challa93a714d2015-02-25 09:58:35 -08002759 unsigned long timeout;
2760 clock_t expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002762 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
2764 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002765
Thomas Graf24ef0da2008-05-28 16:54:22 +02002766 if (plen > 128)
2767 return -EINVAL;
2768
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002769 /* check the lifetime */
2770 if (!valid_lft || prefered_lft > valid_lft)
2771 return -EINVAL;
2772
Jiri Pirko53bd6742013-12-06 09:45:22 +01002773 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2774 return -EINVAL;
2775
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002776 dev = __dev_get_by_index(net, ifindex);
2777 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002779
Brian Haley64e724f2010-07-20 10:34:30 +00002780 idev = addrconf_add_dev(dev);
2781 if (IS_ERR(idev))
2782 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Madhu Challa93a714d2015-02-25 09:58:35 -08002784 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2785 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2786 true, pfx, ifindex);
2787
2788 if (ret < 0)
2789 return ret;
2790 }
2791
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 scope = ipv6_addr_scope(pfx);
2793
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002794 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2795 if (addrconf_finite_timeout(timeout)) {
2796 expires = jiffies_to_clock_t(timeout * HZ);
2797 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002798 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002799 } else {
2800 expires = 0;
2801 flags = 0;
2802 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002803 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002804
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002805 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2806 if (addrconf_finite_timeout(timeout)) {
2807 if (timeout == 0)
2808 ifa_flags |= IFA_F_DEPRECATED;
2809 prefered_lft = timeout;
2810 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002811
Jiri Benc8a226b22013-08-01 10:41:28 +02002812 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2813 valid_lft, prefered_lft);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002814
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 if (!IS_ERR(ifp)) {
Thomas Haller761aac72014-01-15 15:36:58 +01002816 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2817 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2818 expires, flags);
2819 }
2820
Neil Horman95c385b2007-04-25 17:08:10 -07002821 /*
2822 * Note that section 3.1 of RFC 4429 indicates
2823 * that the Optimistic flag should not be set for
2824 * manually configured addresses
2825 */
David S. Millercf22f9a2012-04-14 21:37:40 -04002826 addrconf_dad_start(ifp);
Jiri Pirko53bd6742013-12-06 09:45:22 +01002827 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2828 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2829 true, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002831 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 return 0;
Madhu Challa93a714d2015-02-25 09:58:35 -08002833 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2834 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2835 false, pfx, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 }
2837
2838 return PTR_ERR(ifp);
2839}
2840
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002841static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2842 const struct in6_addr *pfx, unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
2844 struct inet6_ifaddr *ifp;
2845 struct inet6_dev *idev;
2846 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002847
Thomas Graf24ef0da2008-05-28 16:54:22 +02002848 if (plen > 128)
2849 return -EINVAL;
2850
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002851 dev = __dev_get_by_index(net, ifindex);
2852 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 return -ENODEV;
2854
Ian Morrise5d08d72014-11-23 21:28:43 +00002855 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01002856 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 return -ENXIO;
2858
2859 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002860 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 if (ifp->prefix_len == plen &&
2862 ipv6_addr_equal(pfx, &ifp->addr)) {
2863 in6_ifa_hold(ifp);
2864 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002865
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002866 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2867 (ifa_flags & IFA_F_MANAGETEMPADDR))
2868 manage_tempaddrs(idev, ifp, 0, 0, false,
2869 jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 ipv6_del_addr(ifp);
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002871 addrconf_verify_rtnl();
Madhu Challa93a714d2015-02-25 09:58:35 -08002872 if (ipv6_addr_is_multicast(pfx)) {
2873 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2874 false, pfx, dev->ifindex);
2875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 return 0;
2877 }
2878 }
2879 read_unlock_bh(&idev->lock);
2880 return -EADDRNOTAVAIL;
2881}
2882
2883
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002884int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885{
2886 struct in6_ifreq ireq;
2887 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002888
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002889 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2893 return -EFAULT;
2894
2895 rtnl_lock();
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002896 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002897 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2898 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 rtnl_unlock();
2900 return err;
2901}
2902
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002903int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904{
2905 struct in6_ifreq ireq;
2906 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002907
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002908 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 return -EPERM;
2910
2911 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2912 return -EFAULT;
2913
2914 rtnl_lock();
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002915 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002916 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 rtnl_unlock();
2918 return err;
2919}
2920
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002921static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2922 int plen, int scope)
2923{
2924 struct inet6_ifaddr *ifp;
2925
Jiri Benc8a226b22013-08-01 10:41:28 +02002926 ifp = ipv6_add_addr(idev, addr, NULL, plen,
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01002927 scope, IFA_F_PERMANENT,
2928 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002929 if (!IS_ERR(ifp)) {
2930 spin_lock_bh(&ifp->lock);
2931 ifp->flags &= ~IFA_F_TENTATIVE;
2932 spin_unlock_bh(&ifp->lock);
Paolo Abeni764d3be2016-11-22 16:57:40 +01002933 rt_genid_bump_ipv6(dev_net(idev->dev));
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002934 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2935 in6_ifa_put(ifp);
2936 }
2937}
2938
Amerigo Wang07a93622012-10-29 16:23:10 +00002939#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940static void sit_add_v4_addrs(struct inet6_dev *idev)
2941{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 struct in6_addr addr;
2943 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002944 struct net *net = dev_net(idev->dev);
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002945 int scope, plen;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002946 u32 pflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
2948 ASSERT_RTNL();
2949
2950 memset(&addr, 0, sizeof(struct in6_addr));
2951 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2952
2953 if (idev->dev->flags&IFF_POINTOPOINT) {
2954 addr.s6_addr32[0] = htonl(0xfe800000);
2955 scope = IFA_LINK;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002956 plen = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 } else {
2958 scope = IPV6_ADDR_COMPATv4;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002959 plen = 96;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002960 pflags |= RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 }
2962
2963 if (addr.s6_addr32[3]) {
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002964 add_addr(idev, &addr, plen, scope);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002965 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 return;
2967 }
2968
Benjamin Thery6fda7352008-03-05 10:47:47 -08002969 for_each_netdev(net, dev) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002970 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 if (in_dev && (dev->flags & IFF_UP)) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002972 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
2974 int flag = scope;
2975
2976 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
2978 addr.s6_addr32[3] = ifa->ifa_local;
2979
2980 if (ifa->ifa_scope == RT_SCOPE_LINK)
2981 continue;
2982 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2983 if (idev->dev->flags&IFF_POINTOPOINT)
2984 continue;
2985 flag |= IFA_HOST;
2986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002988 add_addr(idev, &addr, plen, flag);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002989 addrconf_prefix_route(&addr, plen, idev->dev, 0,
2990 pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 }
2992 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002995#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
2997static void init_loopback(struct net_device *dev)
2998{
2999 struct inet6_dev *idev;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303000 struct net_device *sp_dev;
3001 struct inet6_ifaddr *sp_ifa;
3002 struct rt6_info *sp_rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
3004 /* ::1 */
3005
3006 ASSERT_RTNL();
3007
Ian Morrise5d08d72014-11-23 21:28:43 +00003008 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003009 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003010 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 return;
3012 }
3013
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003014 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303015
3016 /* Add routes to other interface's IPv6 addresses */
3017 for_each_netdev(dev_net(dev), sp_dev) {
3018 if (!strcmp(sp_dev->name, dev->name))
3019 continue;
3020
3021 idev = __in6_dev_get(sp_dev);
3022 if (!idev)
3023 continue;
3024
3025 read_lock_bh(&idev->lock);
3026 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
3027
3028 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
3029 continue;
3030
Gao feng33d99112014-01-24 16:29:11 +08003031 if (sp_ifa->rt) {
3032 /* This dst has been added to garbage list when
3033 * lo device down, release this obsolete dst and
3034 * reallocate a new router for ifa.
3035 */
Nicolas Dichtela2204452016-10-12 10:10:40 +02003036 if (!atomic_read(&sp_ifa->rt->rt6i_ref)) {
Gao feng33d99112014-01-24 16:29:11 +08003037 ip6_rt_put(sp_ifa->rt);
3038 sp_ifa->rt = NULL;
3039 } else {
3040 continue;
3041 }
3042 }
Gao fenga881ae12013-06-16 11:14:30 +08003043
François-Xavier Le Bail57ec0af2013-12-02 11:28:49 +01003044 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303045
3046 /* Failure cases are ignored */
Gao feng534c8772013-06-02 22:16:21 +00003047 if (!IS_ERR(sp_rt)) {
3048 sp_ifa->rt = sp_rt;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303049 ip6_ins_rt(sp_rt);
Gao feng534c8772013-06-02 22:16:21 +00003050 }
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303051 }
3052 read_unlock_bh(&idev->lock);
3053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054}
3055
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003056void addrconf_add_linklocal(struct inet6_dev *idev,
3057 const struct in6_addr *addr, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003059 struct inet6_ifaddr *ifp;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003060 u32 addr_flags = flags | IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
Neil Horman95c385b2007-04-25 17:08:10 -07003062#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3063 if (idev->cnf.optimistic_dad &&
David Miller702beb82008-07-20 18:17:02 -07003064 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
Neil Horman95c385b2007-04-25 17:08:10 -07003065 addr_flags |= IFA_F_OPTIMISTIC;
3066#endif
3067
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01003068 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
3069 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003071 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
David S. Millercf22f9a2012-04-14 21:37:40 -04003072 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 in6_ifa_put(ifp);
3074 }
3075}
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003076EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003078static bool ipv6_reserved_interfaceid(struct in6_addr address)
3079{
3080 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3081 return true;
3082
3083 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3084 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3085 return true;
3086
3087 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3088 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3089 return true;
3090
3091 return false;
3092}
3093
3094static int ipv6_generate_stable_address(struct in6_addr *address,
3095 u8 dad_count,
3096 const struct inet6_dev *idev)
3097{
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003098 static DEFINE_SPINLOCK(lock);
3099 static __u32 digest[SHA_DIGEST_WORDS];
3100 static __u32 workspace[SHA_WORKSPACE_WORDS];
3101
3102 static union {
3103 char __data[SHA_MESSAGE_BYTES];
3104 struct {
3105 struct in6_addr secret;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003106 __be32 prefix[2];
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003107 unsigned char hwaddr[MAX_ADDR_LEN];
3108 u8 dad_count;
3109 } __packed;
3110 } data;
3111
3112 struct in6_addr secret;
3113 struct in6_addr temp;
3114 struct net *net = dev_net(idev->dev);
3115
3116 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3117
3118 if (idev->cnf.stable_secret.initialized)
3119 secret = idev->cnf.stable_secret.secret;
3120 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3121 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3122 else
3123 return -1;
3124
3125retry:
3126 spin_lock_bh(&lock);
3127
3128 sha_init(digest);
3129 memset(&data, 0, sizeof(data));
3130 memset(workspace, 0, sizeof(workspace));
3131 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003132 data.prefix[0] = address->s6_addr32[0];
3133 data.prefix[1] = address->s6_addr32[1];
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003134 data.secret = secret;
3135 data.dad_count = dad_count;
3136
3137 sha_transform(digest, data.__data, workspace);
3138
3139 temp = *address;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003140 temp.s6_addr32[2] = (__force __be32)digest[0];
3141 temp.s6_addr32[3] = (__force __be32)digest[1];
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003142
3143 spin_unlock_bh(&lock);
3144
3145 if (ipv6_reserved_interfaceid(temp)) {
3146 dad_count++;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01003147 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003148 return -1;
3149 goto retry;
3150 }
3151
3152 *address = temp;
3153 return 0;
3154}
3155
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003156static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3157{
3158 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3159
3160 if (s->initialized)
3161 return;
3162 s = &idev->cnf.stable_secret;
3163 get_random_bytes(&s->secret, sizeof(s->secret));
3164 s->initialized = true;
3165}
3166
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003167static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3168{
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003169 struct in6_addr addr;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003170
David Ahernca254492015-10-12 11:47:10 -07003171 /* no link local addresses on L3 master devices */
3172 if (netif_is_l3_master(idev->dev))
3173 return;
3174
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003175 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3176
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003177 switch (idev->addr_gen_mode) {
3178 case IN6_ADDR_GEN_MODE_RANDOM:
3179 ipv6_gen_mode_random_init(idev);
3180 /* fallthrough */
3181 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003182 if (!ipv6_generate_stable_address(&addr, 0, idev))
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003183 addrconf_add_linklocal(idev, &addr,
3184 IFA_F_STABLE_PRIVACY);
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003185 else if (prefix_route)
3186 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003187 break;
3188 case IN6_ADDR_GEN_MODE_EUI64:
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003189 /* addrconf_add_linklocal also adds a prefix_route and we
3190 * only need to care about prefix routes if ipv6_generate_eui64
3191 * couldn't generate one.
3192 */
3193 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003194 addrconf_add_linklocal(idev, &addr, 0);
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003195 else if (prefix_route)
3196 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003197 break;
3198 case IN6_ADDR_GEN_MODE_NONE:
3199 default:
3200 /* will not add any link local address */
3201 break;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003202 }
3203}
3204
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205static void addrconf_dev_config(struct net_device *dev)
3206{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003207 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208
3209 ASSERT_RTNL();
3210
Herbert Xu74235a22007-06-14 13:02:55 -07003211 if ((dev->type != ARPHRD_ETHER) &&
3212 (dev->type != ARPHRD_FDDI) &&
Herbert Xu74235a22007-06-14 13:02:55 -07003213 (dev->type != ARPHRD_ARCNET) &&
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00003214 (dev->type != ARPHRD_INFINIBAND) &&
Nicolas Dichtele8377352013-08-20 12:16:06 +02003215 (dev->type != ARPHRD_IEEE1394) &&
Jukka Rissanene74bccb2013-12-11 17:05:36 +02003216 (dev->type != ARPHRD_TUNNEL6) &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003217 (dev->type != ARPHRD_6LOWPAN) &&
3218 (dev->type != ARPHRD_NONE)) {
Herbert Xu74235a22007-06-14 13:02:55 -07003219 /* Alas, we support only Ethernet autoconfiguration. */
3220 return;
3221 }
3222
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00003224 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 return;
3226
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003227 /* this device type has no EUI support */
3228 if (dev->type == ARPHRD_NONE &&
3229 idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3230 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3231
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003232 addrconf_addr_gen(idev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233}
3234
Amerigo Wang07a93622012-10-29 16:23:10 +00003235#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236static void addrconf_sit_config(struct net_device *dev)
3237{
3238 struct inet6_dev *idev;
3239
3240 ASSERT_RTNL();
3241
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003242 /*
3243 * Configure the tunnel with one of our IPv4
3244 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 * our v4 addrs in the tunnel
3246 */
3247
Ian Morrise5d08d72014-11-23 21:28:43 +00003248 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003249 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003250 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 return;
3252 }
3253
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003254 if (dev->priv_flags & IFF_ISATAP) {
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003255 addrconf_addr_gen(idev, false);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003256 return;
3257 }
3258
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 sit_add_v4_addrs(idev);
3260
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00003261 if (dev->flags&IFF_POINTOPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003264#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
Amerigo Wang07a93622012-10-29 16:23:10 +00003266#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003267static void addrconf_gre_config(struct net_device *dev)
3268{
3269 struct inet6_dev *idev;
stephen hemmingeraee80b52011-06-08 10:44:30 +00003270
stephen hemmingeraee80b52011-06-08 10:44:30 +00003271 ASSERT_RTNL();
3272
Ian Morrise5d08d72014-11-23 21:28:43 +00003273 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003274 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003275 pr_debug("%s: add_dev failed\n", __func__);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003276 return;
3277 }
3278
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003279 addrconf_addr_gen(idev, true);
Hannes Frederic Sowad9e4ce62015-10-08 18:19:39 +02003280 if (dev->flags & IFF_POINTOPOINT)
3281 addrconf_add_mroute(dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003282}
3283#endif
3284
David Ahernf1705ec2016-02-24 09:25:37 -08003285static int fixup_permanent_addr(struct inet6_dev *idev,
3286 struct inet6_ifaddr *ifp)
3287{
David Ahern62817c32017-04-25 09:17:29 -07003288 /* rt6i_ref == 0 means the host route was removed from the
3289 * FIB, for example, if 'lo' device is taken down. In that
3290 * case regenerate the host route.
3291 */
3292 if (!ifp->rt || !atomic_read(&ifp->rt->rt6i_ref)) {
3293 struct rt6_info *rt, *prev;
David Ahernf1705ec2016-02-24 09:25:37 -08003294
3295 rt = addrconf_dst_alloc(idev, &ifp->addr, false);
3296 if (unlikely(IS_ERR(rt)))
3297 return PTR_ERR(rt);
3298
David Ahern62817c32017-04-25 09:17:29 -07003299 /* ifp->rt can be accessed outside of rtnl */
3300 spin_lock(&ifp->lock);
3301 prev = ifp->rt;
David Ahernf1705ec2016-02-24 09:25:37 -08003302 ifp->rt = rt;
David Ahern62817c32017-04-25 09:17:29 -07003303 spin_unlock(&ifp->lock);
3304
3305 ip6_rt_put(prev);
David Ahernf1705ec2016-02-24 09:25:37 -08003306 }
3307
3308 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3309 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3310 idev->dev, 0, 0);
3311 }
3312
David Ahern0f4ac292017-05-02 14:43:44 -07003313 if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3314 addrconf_dad_start(ifp);
David Ahernf1705ec2016-02-24 09:25:37 -08003315
3316 return 0;
3317}
3318
3319static void addrconf_permanent_addr(struct net_device *dev)
3320{
3321 struct inet6_ifaddr *ifp, *tmp;
3322 struct inet6_dev *idev;
3323
3324 idev = __in6_dev_get(dev);
3325 if (!idev)
3326 return;
3327
3328 write_lock_bh(&idev->lock);
3329
3330 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3331 if ((ifp->flags & IFA_F_PERMANENT) &&
3332 fixup_permanent_addr(idev, ifp) < 0) {
3333 write_unlock_bh(&idev->lock);
Eric Dumazetd87890d2017-10-30 22:47:09 -07003334 in6_ifa_hold(ifp);
David Ahernf1705ec2016-02-24 09:25:37 -08003335 ipv6_del_addr(ifp);
3336 write_lock_bh(&idev->lock);
3337
3338 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3339 idev->dev->name, &ifp->addr);
3340 }
3341 }
3342
3343 write_unlock_bh(&idev->lock);
3344}
3345
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003346static int addrconf_notify(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00003347 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
Jiri Pirko351638e2013-05-28 01:30:21 +00003349 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
David Ahern4f7f34e2016-04-07 11:10:41 -07003350 struct netdev_notifier_changeupper_info *info;
Eric Dumazet748e2d92012-08-22 21:50:59 +00003351 struct inet6_dev *idev = __in6_dev_get(dev);
WANG Cong0526ff32017-06-21 14:34:58 -07003352 struct net *net = dev_net(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003353 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07003354 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003356 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003357 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07003358 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003359 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -07003360 if (IS_ERR(idev))
3361 return notifier_from_errno(PTR_ERR(idev));
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003362 }
3363 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003364
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003365 case NETDEV_CHANGEMTU:
3366 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3367 if (dev->mtu < IPV6_MIN_MTU) {
WANG Cong0526ff32017-06-21 14:34:58 -07003368 addrconf_ifdown(dev, dev != net->loopback_dev);
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003369 break;
3370 }
3371
3372 if (idev) {
3373 rt6_mtu_change(dev, dev->mtu);
3374 idev->cnf.mtu6 = dev->mtu;
3375 break;
3376 }
3377
3378 /* allocate new idev */
3379 idev = ipv6_add_dev(dev);
3380 if (IS_ERR(idev))
3381 break;
3382
3383 /* device is still not ready */
3384 if (!(idev->if_flags & IF_READY))
3385 break;
3386
3387 run_pending = 1;
3388
3389 /* fall through */
3390
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003392 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07003393 if (dev->flags & IFF_SLAVE)
3394 break;
3395
WANG Cong3ce62a82014-09-11 15:07:16 -07003396 if (idev && idev->cnf.disable_ipv6)
3397 break;
3398
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003399 if (event == NETDEV_UP) {
David Ahern38bd10c2016-04-21 20:56:12 -07003400 /* restore routes for permanent addresses */
3401 addrconf_permanent_addr(dev);
3402
Mike Manning4bf42a22017-09-25 22:01:36 +01003403 if (!addrconf_link_ready(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003404 /* device is not ready yet. */
Joe Perchesf3213832012-05-15 14:11:53 +00003405 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003406 dev->name);
3407 break;
3408 }
Kristian Slavov99081042006-02-08 16:10:53 -08003409
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11003410 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3411 idev = ipv6_add_dev(dev);
3412
WANG Conga317a2f2014-07-25 15:25:09 -07003413 if (!IS_ERR_OR_NULL(idev)) {
Kristian Slavov99081042006-02-08 16:10:53 -08003414 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08003415 run_pending = 1;
3416 }
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003417 } else if (event == NETDEV_CHANGE) {
Mike Manning4bf42a22017-09-25 22:01:36 +01003418 if (!addrconf_link_ready(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003419 /* device is still not ready. */
3420 break;
3421 }
3422
3423 if (idev) {
Linus Lüssing1cadd392017-05-23 21:53:52 -04003424 if (idev->if_flags & IF_READY) {
3425 /* device is already configured -
3426 * but resend MLD reports, we might
3427 * have roamed and need to update
3428 * multicast snooping switches
3429 */
3430 ipv6_mc_up(idev);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003431 break;
Linus Lüssing1cadd392017-05-23 21:53:52 -04003432 }
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003433 idev->if_flags |= IF_READY;
3434 }
3435
Joe Perchesf3213832012-05-15 14:11:53 +00003436 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3437 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003438
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003439 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003440 }
3441
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003442 switch (dev->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +00003443#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 case ARPHRD_SIT:
3445 addrconf_sit_config(dev);
3446 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07003447#endif
Amerigo Wang07a93622012-10-29 16:23:10 +00003448#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003449 case ARPHRD_IPGRE:
3450 addrconf_gre_config(dev);
3451 break;
3452#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 case ARPHRD_LOOPBACK:
3454 init_loopback(dev);
3455 break;
3456
3457 default:
3458 addrconf_dev_config(dev);
3459 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003460 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003461
WANG Conga317a2f2014-07-25 15:25:09 -07003462 if (!IS_ERR_OR_NULL(idev)) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003463 if (run_pending)
3464 addrconf_dad_run(idev);
3465
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003466 /*
3467 * If the MTU changed during the interface down,
3468 * when the interface up, the changed MTU must be
3469 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003471 if (idev->cnf.mtu6 != dev->mtu &&
3472 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 rt6_mtu_change(dev, dev->mtu);
3474 idev->cnf.mtu6 = dev->mtu;
3475 }
3476 idev->tstamp = jiffies;
3477 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003478
3479 /*
3480 * If the changed mtu during down is lower than
3481 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 */
3483 if (dev->mtu < IPV6_MIN_MTU)
WANG Cong0526ff32017-06-21 14:34:58 -07003484 addrconf_ifdown(dev, dev != net->loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 }
3486 break;
3487
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 case NETDEV_DOWN:
3489 case NETDEV_UNREGISTER:
3490 /*
3491 * Remove all addresses from this interface.
3492 */
3493 addrconf_ifdown(dev, event != NETDEV_DOWN);
3494 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003495
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07003498 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003499 addrconf_sysctl_unregister(idev);
WANG Conga317a2f2014-07-25 15:25:09 -07003500 err = addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07003501 if (err)
3502 return notifier_from_errno(err);
WANG Conga317a2f2014-07-25 15:25:09 -07003503 err = snmp6_register_dev(idev);
3504 if (err) {
3505 addrconf_sysctl_unregister(idev);
3506 return notifier_from_errno(err);
3507 }
Stephen Hemminger5632c512007-04-28 21:16:39 -07003508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003510
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003511 case NETDEV_PRE_TYPE_CHANGE:
3512 case NETDEV_POST_TYPE_CHANGE:
Andrew Lunn3ef09522015-12-03 21:12:32 +01003513 if (idev)
3514 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07003515 break;
David Ahern4f7f34e2016-04-07 11:10:41 -07003516
3517 case NETDEV_CHANGEUPPER:
3518 info = ptr;
3519
3520 /* flush all routes if dev is linked to or unlinked from
3521 * an L3 master device (e.g., VRF)
3522 */
3523 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3524 addrconf_ifdown(dev, 0);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
3527 return NOTIFY_OK;
3528}
3529
3530/*
3531 * addrconf module should be notified of a device going up
3532 */
3533static struct notifier_block ipv6_dev_notf = {
3534 .notifier_call = addrconf_notify,
WANG Cong8795ee72017-05-08 10:12:13 -07003535 .priority = ADDRCONF_NOTIFY_PRIORITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536};
3537
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003538static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07003539{
3540 struct inet6_dev *idev;
3541 ASSERT_RTNL();
3542
3543 idev = __in6_dev_get(dev);
3544
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003545 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003546 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003547 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003548 ipv6_mc_unmap(idev);
3549}
3550
David Ahern70af9212016-04-08 12:01:21 -07003551static bool addr_is_local(const struct in6_addr *addr)
3552{
3553 return ipv6_addr_type(addr) &
3554 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3555}
3556
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557static int addrconf_ifdown(struct net_device *dev, int how)
3558{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003559 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003560 struct inet6_dev *idev;
David Ahernf1705ec2016-02-24 09:25:37 -08003561 struct inet6_ifaddr *ifa, *tmp;
3562 struct list_head del_list;
3563 int _keep_addr;
3564 bool keep_addr;
David S. Miller73a8bd72011-01-23 23:27:15 -08003565 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
3567 ASSERT_RTNL();
3568
David S. Miller73a8bd72011-01-23 23:27:15 -08003569 rt6_ifdown(net, dev);
3570 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
3572 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003573 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 return -ENODEV;
3575
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003576 /*
3577 * Step 1: remove reference to ipv6 device from parent device.
3578 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003580 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003582
3583 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003584 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
3586 /* Step 1.5: remove snmp6 entry */
3587 snmp6_unregister_dev(idev);
3588
3589 }
3590
David Ahernf1705ec2016-02-24 09:25:37 -08003591 /* aggregate the system setting and interface setting */
3592 _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3593 if (!_keep_addr)
3594 _keep_addr = idev->cnf.keep_addr_on_down;
3595
3596 /* combine the user config with event to determine if permanent
3597 * addresses are to be removed from address hash table
3598 */
Mike Manningbc561632016-08-12 12:02:38 +01003599 keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003600
David S. Miller73a8bd72011-01-23 23:27:15 -08003601 /* Step 2: clear hash table */
3602 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3603 struct hlist_head *h = &inet6_addr_lst[i];
David S. Miller73a8bd72011-01-23 23:27:15 -08003604
3605 spin_lock_bh(&addrconf_hash_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01003606restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08003607 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
David S. Miller73a8bd72011-01-23 23:27:15 -08003608 if (ifa->idev == idev) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003609 addrconf_del_dad_work(ifa);
David Ahernf1705ec2016-02-24 09:25:37 -08003610 /* combined flag + permanent flag decide if
3611 * address is retained on a down event
3612 */
3613 if (!keep_addr ||
David Ahern70af9212016-04-08 12:01:21 -07003614 !(ifa->flags & IFA_F_PERMANENT) ||
3615 addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003616 hlist_del_init_rcu(&ifa->addr_lst);
3617 goto restart;
3618 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003619 }
3620 }
3621 spin_unlock_bh(&addrconf_hash_lock);
3622 }
3623
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 write_lock_bh(&idev->lock);
3625
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003626 addrconf_del_rs_timer(idev);
3627
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003628 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003629 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003630 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003632 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00003633 while (!list_empty(&idev->tempaddr_list)) {
3634 ifa = list_first_entry(&idev->tempaddr_list,
3635 struct inet6_ifaddr, tmp_list);
3636 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 write_unlock_bh(&idev->lock);
3638 spin_lock_bh(&ifa->lock);
3639
3640 if (ifa->ifpub) {
3641 in6_ifa_put(ifa->ifpub);
3642 ifa->ifpub = NULL;
3643 }
3644 spin_unlock_bh(&ifa->lock);
3645 in6_ifa_put(ifa);
3646 write_lock_bh(&idev->lock);
3647 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003648
David Ahernf1705ec2016-02-24 09:25:37 -08003649 /* re-combine the user config with event to determine if permanent
3650 * addresses are to be removed from the interface list
3651 */
Mike Manningbc561632016-08-12 12:02:38 +01003652 keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003653
3654 INIT_LIST_HEAD(&del_list);
3655 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
David Ahern38bd10c2016-04-21 20:56:12 -07003656 struct rt6_info *rt = NULL;
Rabin Vincentc86872a42017-04-10 08:36:39 +02003657 bool keep;
David Ahern38bd10c2016-04-21 20:56:12 -07003658
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003659 addrconf_del_dad_work(ifa);
stephen hemminger84e8b802010-03-02 13:32:46 +00003660
Rabin Vincentc86872a42017-04-10 08:36:39 +02003661 keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3662 !addr_is_local(&ifa->addr);
3663 if (!keep)
3664 list_move(&ifa->if_list, &del_list);
3665
David S. Miller73a8bd72011-01-23 23:27:15 -08003666 write_unlock_bh(&idev->lock);
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003667 spin_lock_bh(&ifa->lock);
David Ahernf1705ec2016-02-24 09:25:37 -08003668
Rabin Vincentc86872a42017-04-10 08:36:39 +02003669 if (keep) {
David Ahernf1705ec2016-02-24 09:25:37 -08003670 /* set state to skip the notifier below */
3671 state = INET6_IFADDR_STATE_DEAD;
David Ahern0f4ac292017-05-02 14:43:44 -07003672 ifa->state = INET6_IFADDR_STATE_PREDAD;
David Ahernf1705ec2016-02-24 09:25:37 -08003673 if (!(ifa->flags & IFA_F_NODAD))
3674 ifa->flags |= IFA_F_TENTATIVE;
David Ahern38bd10c2016-04-21 20:56:12 -07003675
3676 rt = ifa->rt;
3677 ifa->rt = NULL;
David Ahernf1705ec2016-02-24 09:25:37 -08003678 } else {
3679 state = ifa->state;
3680 ifa->state = INET6_IFADDR_STATE_DEAD;
David Ahernf1705ec2016-02-24 09:25:37 -08003681 }
3682
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003683 spin_unlock_bh(&ifa->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003684
David Ahern38bd10c2016-04-21 20:56:12 -07003685 if (rt)
3686 ip6_del_rt(rt);
3687
David S. Miller73a8bd72011-01-23 23:27:15 -08003688 if (state != INET6_IFADDR_STATE_DEAD) {
3689 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Cong Wangf88c91d2013-04-14 23:18:43 +08003690 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
Mike Manningea06f712016-07-22 18:32:11 +01003691 } else {
3692 if (idev->cnf.forwarding)
3693 addrconf_leave_anycast(ifa);
3694 addrconf_leave_solict(ifa->idev, &ifa->addr);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00003695 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003696
David S. Miller73a8bd72011-01-23 23:27:15 -08003697 write_lock_bh(&idev->lock);
3698 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003699
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 write_unlock_bh(&idev->lock);
3701
David Ahernf1705ec2016-02-24 09:25:37 -08003702 /* now clean up addresses to be removed */
3703 while (!list_empty(&del_list)) {
3704 ifa = list_first_entry(&del_list,
3705 struct inet6_ifaddr, if_list);
3706 list_del(&ifa->if_list);
3707
3708 in6_ifa_put(ifa);
3709 }
3710
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003711 /* Step 5: Discard anycast and multicast list */
3712 if (how) {
3713 ipv6_ac_destroy_dev(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 ipv6_mc_destroy_dev(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003715 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 ipv6_mc_down(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003720
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003721 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003722 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003723 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 neigh_parms_release(&nd_tbl, idev->nd_parms);
3725 neigh_ifdown(&nd_tbl, dev);
3726 in6_dev_put(idev);
3727 }
3728 return 0;
3729}
3730
3731static void addrconf_rs_timer(unsigned long data)
3732{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003733 struct inet6_dev *idev = (struct inet6_dev *)data;
Cong Wangcaf92bc2013-08-31 13:44:32 +08003734 struct net_device *dev = idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003735 struct in6_addr lladdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003737 write_lock(&idev->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00003738 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 goto out;
3740
Shmulik Ladkani9ba2add2012-12-02 01:44:53 +00003741 if (!ipv6_accept_ra(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003743
3744 /* Announcement received after solicitation was sent */
3745 if (idev->if_flags & IF_RA_RCVD)
3746 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003748 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
Cong Wangcaf92bc2013-08-31 13:44:32 +08003749 write_unlock(&idev->lock);
3750 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3751 ndisc_send_rs(dev, &lladdr,
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003752 &in6addr_linklocal_allrouters);
3753 else
Cong Wangcaf92bc2013-08-31 13:44:32 +08003754 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755
Cong Wangcaf92bc2013-08-31 13:44:32 +08003756 write_lock(&idev->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003757 idev->rs_interval = rfc3315_s14_backoff_update(
3758 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003759 /* The wait after the last probe can be shorter */
3760 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3761 idev->cnf.rtr_solicits) ?
3762 idev->cnf.rtr_solicit_delay :
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003763 idev->rs_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 /*
3766 * Note: we do not support deprecated "all on-link"
3767 * assumption any longer.
3768 */
Joe Perches91df42b2012-05-15 14:11:54 +00003769 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 }
3771
3772out:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003773 write_unlock(&idev->lock);
Cong Wangcaf92bc2013-08-31 13:44:32 +08003774put:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003775 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776}
3777
3778/*
3779 * Duplicate Address Detection
3780 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003781static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3782{
3783 unsigned long rand_num;
3784 struct inet6_dev *idev = ifp->idev;
3785
Neil Horman95c385b2007-04-25 17:08:10 -07003786 if (ifp->flags & IFA_F_OPTIMISTIC)
3787 rand_num = 0;
3788 else
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003789 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
Neil Horman95c385b2007-04-25 17:08:10 -07003790
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003791 ifp->dad_probes = idev->cnf.dad_transmits;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003792 addrconf_mod_dad_work(ifp, rand_num);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003793}
3794
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003795static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796{
3797 struct inet6_dev *idev = ifp->idev;
3798 struct net_device *dev = idev->dev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003799 bool bump_id, notify = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800
3801 addrconf_join_solict(dev, &ifp->addr);
3802
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003803 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
3805 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003806 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003807 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809
3810 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09003811 idev->cnf.accept_dad < 1 ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003812 !(ifp->flags&IFA_F_TENTATIVE) ||
3813 ifp->flags & IFA_F_NODAD) {
Paolo Abeni764d3be2016-11-22 16:57:40 +01003814 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003815 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003816 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 read_unlock_bh(&idev->lock);
3818
Paolo Abeni764d3be2016-11-22 16:57:40 +01003819 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 return;
3821 }
3822
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003823 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003824 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003825 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003826 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003827 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003828 * - keep it tentative if it is a permanent address.
3829 * - otherwise, kill it.
3830 */
3831 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003832 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003833 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003834 }
Neil Horman95c385b2007-04-25 17:08:10 -07003835
3836 /*
3837 * Optimistic nodes can start receiving
3838 * Frames right away
3839 */
Erik Kline7fd25612014-10-28 18:11:14 +09003840 if (ifp->flags & IFA_F_OPTIMISTIC) {
Neil Horman95c385b2007-04-25 17:08:10 -07003841 ip6_ins_rt(ifp->rt);
Erik Kline7fd25612014-10-28 18:11:14 +09003842 if (ipv6_use_optimistic_addr(idev)) {
3843 /* Because optimistic nodes can use this address,
3844 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3845 */
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003846 notify = true;
Erik Kline7fd25612014-10-28 18:11:14 +09003847 }
3848 }
Neil Horman95c385b2007-04-25 17:08:10 -07003849
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003850 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003852 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 read_unlock_bh(&idev->lock);
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003854 if (notify)
3855 ipv6_ifa_notify(RTM_NEWADDR, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856}
3857
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003858static void addrconf_dad_start(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003860 bool begin_dad = false;
3861
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003862 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003863 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3864 ifp->state = INET6_IFADDR_STATE_PREDAD;
3865 begin_dad = true;
3866 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003867 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003868
3869 if (begin_dad)
3870 addrconf_mod_dad_work(ifp, 0);
3871}
3872
3873static void addrconf_dad_work(struct work_struct *w)
3874{
3875 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3876 struct inet6_ifaddr,
3877 dad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 struct inet6_dev *idev = ifp->idev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003879 bool bump_id, disable_ipv6 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 struct in6_addr mcaddr;
3881
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003882 enum {
3883 DAD_PROCESS,
3884 DAD_BEGIN,
3885 DAD_ABORT,
3886 } action = DAD_PROCESS;
3887
3888 rtnl_lock();
3889
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003890 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003891 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3892 action = DAD_BEGIN;
3893 ifp->state = INET6_IFADDR_STATE_DAD;
3894 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3895 action = DAD_ABORT;
3896 ifp->state = INET6_IFADDR_STATE_POSTDAD;
Mike Manning85b51b12016-08-18 14:39:40 +01003897
3898 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6 &&
3899 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
3900 struct in6_addr addr;
3901
3902 addr.s6_addr32[0] = htonl(0xfe800000);
3903 addr.s6_addr32[1] = 0;
3904
3905 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
3906 ipv6_addr_equal(&ifp->addr, &addr)) {
3907 /* DAD failed for link-local based on MAC */
3908 idev->cnf.disable_ipv6 = 1;
3909
3910 pr_info("%s: IPv6 being disabled!\n",
3911 ifp->idev->dev->name);
3912 disable_ipv6 = true;
3913 }
3914 }
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003915 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003916 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003917
3918 if (action == DAD_BEGIN) {
3919 addrconf_dad_begin(ifp);
3920 goto out;
3921 } else if (action == DAD_ABORT) {
Wei Yongjun751eb6b2016-09-05 16:06:31 +08003922 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003923 addrconf_dad_stop(ifp, 1);
Mike Manning85b51b12016-08-18 14:39:40 +01003924 if (disable_ipv6)
3925 addrconf_ifdown(idev->dev, 0);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003926 goto out;
3927 }
3928
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003929 if (!ifp->dad_probes && addrconf_dad_end(ifp))
Herbert Xuf2344a12010-05-18 15:55:27 -07003930 goto out;
3931
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003932 write_lock_bh(&idev->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003933 if (idev->dead || !(idev->if_flags & IF_READY)) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003934 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 goto out;
3936 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003937
3938 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003939 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3940 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003941 write_unlock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003942 goto out;
3943 }
3944
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003945 if (ifp->dad_probes == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 /*
3947 * DAD was successful
3948 */
3949
Paolo Abeni764d3be2016-11-22 16:57:40 +01003950 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003951 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003952 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003953 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
Paolo Abeni764d3be2016-11-22 16:57:40 +01003955 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956
3957 goto out;
3958 }
3959
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003960 ifp->dad_probes--;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003961 addrconf_mod_dad_work(ifp,
3962 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
stephen hemminger21809fa2010-02-08 19:48:52 +00003963 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003964 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
3966 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
Nicolas Dichtel304d8882015-11-27 18:17:05 +01003968 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969out:
3970 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003971 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972}
3973
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003974/* ifp->idev must be at least read locked */
3975static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3976{
3977 struct inet6_ifaddr *ifpiter;
3978 struct inet6_dev *idev = ifp->idev;
3979
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01003980 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3981 if (ifpiter->scope > IFA_LINK)
3982 break;
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003983 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3984 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3985 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
3986 IFA_F_PERMANENT)
3987 return false;
3988 }
3989 return true;
3990}
3991
Paolo Abeni764d3be2016-11-22 16:57:40 +01003992static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003994 struct net_device *dev = ifp->idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003995 struct in6_addr lladdr;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003996 bool send_rs, send_mld;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003997
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003998 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999
4000 /*
4001 * Configure the address for reception. Now it is valid.
4002 */
4003
4004 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4005
Tore Anderson026359b2011-08-28 23:47:33 +00004006 /* If added prefix is link local and we are prepared to process
4007 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 */
4009
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004010 read_lock_bh(&ifp->idev->lock);
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004011 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004012 send_rs = send_mld &&
4013 ipv6_accept_ra(ifp->idev) &&
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004014 ifp->idev->cnf.rtr_solicits != 0 &&
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004015 (dev->flags&IFF_LOOPBACK) == 0;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004016 read_unlock_bh(&ifp->idev->lock);
4017
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004018 /* While dad is in progress mld report's source address is in6_addrany.
4019 * Resend with proper ll now.
4020 */
4021 if (send_mld)
4022 ipv6_mc_dad_complete(ifp->idev);
4023
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004024 if (send_rs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 /*
4026 * If a host as already performed a random delay
4027 * [...] as part of DAD [...] there is no need
4028 * to delay again before sending the first RS
4029 */
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004030 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004031 return;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004032 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004034 write_lock_bh(&ifp->idev->lock);
4035 spin_lock(&ifp->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004036 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4037 ifp->idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004038 ifp->idev->rs_probes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 ifp->idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004040 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004041 spin_unlock(&ifp->lock);
4042 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 }
Paolo Abeni764d3be2016-11-22 16:57:40 +01004044
4045 if (bump_id)
4046 rt_genid_bump_ipv6(dev_net(dev));
Marcus Huewed385ed72017-05-23 21:53:49 -04004047
4048 /* Make sure that a new temporary address will be created
4049 * before this temporary address becomes deprecated.
4050 */
4051 if (ifp->flags & IFA_F_TEMPORARY)
4052 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053}
4054
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004055static void addrconf_dad_run(struct inet6_dev *idev)
4056{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004057 struct inet6_ifaddr *ifp;
4058
4059 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00004060 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00004061 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07004062 if (ifp->flags & IFA_F_TENTATIVE &&
4063 ifp->state == INET6_IFADDR_STATE_DAD)
4064 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00004065 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004066 }
4067 read_unlock_bh(&idev->lock);
4068}
4069
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070#ifdef CONFIG_PROC_FS
4071struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08004072 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004074 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075};
4076
Mihai Maruseac1d578302012-01-03 23:31:35 +00004077static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078{
4079 struct inet6_ifaddr *ifa = NULL;
4080 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004081 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00004082 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083
Mihai Maruseac1d578302012-01-03 23:31:35 +00004084 /* initial bucket if pos is 0 */
4085 if (pos == 0) {
4086 state->bucket = 0;
4087 state->offset = 0;
4088 }
4089
4090 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08004091 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00004092 addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004093 if (!net_eq(dev_net(ifa->idev->dev), net))
4094 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004095 /* sync with offset */
4096 if (p < state->offset) {
4097 p++;
4098 continue;
4099 }
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004100 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004101 }
4102
4103 /* prepare for next bucket */
4104 state->offset = 0;
4105 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004107 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108}
4109
stephen hemmingerc2e21292010-03-17 20:31:10 +00004110static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4111 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112{
4113 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004114 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
Sasha Levinb67bfe02013-02-27 17:06:00 -08004116 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004117 if (!net_eq(dev_net(ifa->idev->dev), net))
4118 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004119 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004120 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004121 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004122
Jeff Barnhillea143552018-09-21 00:45:27 +00004123 state->offset = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004124 while (++state->bucket < IN6_ADDR_HSIZE) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08004125 hlist_for_each_entry_rcu_bh(ifa,
stephen hemmingerc2e21292010-03-17 20:31:10 +00004126 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004127 if (!net_eq(dev_net(ifa->idev->dev), net))
4128 continue;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004129 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08004130 }
4131 }
4132
stephen hemmingerc2e21292010-03-17 20:31:10 +00004133 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134}
4135
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004137 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138{
stephen hemminger5c578ae2010-03-17 20:31:11 +00004139 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00004140 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141}
4142
4143static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4144{
4145 struct inet6_ifaddr *ifa;
4146
4147 ifa = if6_get_next(seq, v);
4148 ++*pos;
4149 return ifa;
4150}
4151
4152static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004153 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154{
stephen hemminger5c578ae2010-03-17 20:31:11 +00004155 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156}
4157
4158static int if6_seq_show(struct seq_file *seq, void *v)
4159{
4160 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Jiri Pirko971a3512013-12-10 13:56:29 +01004161 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07004162 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 ifp->idev->dev->ifindex,
4164 ifp->prefix_len,
4165 ifp->scope,
Jiri Pirko971a3512013-12-10 13:56:29 +01004166 (u8) ifp->flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 ifp->idev->dev->name);
4168 return 0;
4169}
4170
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004171static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 .start = if6_seq_start,
4173 .next = if6_seq_next,
4174 .show = if6_seq_show,
4175 .stop = if6_seq_stop,
4176};
4177
4178static int if6_seq_open(struct inode *inode, struct file *file)
4179{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004180 return seq_open_net(inode, file, &if6_seq_ops,
4181 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182}
4183
Arjan van de Ven9a321442007-02-12 00:55:35 -08004184static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 .owner = THIS_MODULE,
4186 .open = if6_seq_open,
4187 .read = seq_read,
4188 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004189 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190};
4191
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004192static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193{
Gao fengd4beaa62013-02-18 01:34:54 +00004194 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 return -ENOMEM;
4196 return 0;
4197}
4198
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004199static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08004200{
Gao fengece31ff2013-02-18 01:34:56 +00004201 remove_proc_entry("if_inet6", net->proc_net);
Daniel Lezcano3c400902008-01-10 22:42:49 -08004202}
4203
4204static struct pernet_operations if6_proc_net_ops = {
Ian Morris67ba4152014-08-24 21:53:10 +01004205 .init = if6_proc_net_init,
4206 .exit = if6_proc_net_exit,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004207};
4208
4209int __init if6_proc_init(void)
4210{
4211 return register_pernet_subsys(&if6_proc_net_ops);
4212}
4213
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214void if6_proc_exit(void)
4215{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004216 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217}
4218#endif /* CONFIG_PROC_FS */
4219
Amerigo Wang07a93622012-10-29 16:23:10 +00004220#if IS_ENABLED(CONFIG_IPV6_MIP6)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004221/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004222int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004223{
4224 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004225 struct inet6_ifaddr *ifp = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00004226 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004227
stephen hemminger5c578ae2010-03-17 20:31:11 +00004228 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08004229 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09004230 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08004231 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09004232 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004233 (ifp->flags & IFA_F_HOMEADDRESS)) {
4234 ret = 1;
4235 break;
4236 }
4237 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00004238 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004239 return ret;
4240}
4241#endif
4242
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243/*
4244 * Periodic address status verification
4245 */
4246
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004247static void addrconf_verify_rtnl(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004249 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 int i;
4252
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004253 ASSERT_RTNL();
4254
stephen hemminger5c578ae2010-03-17 20:31:11 +00004255 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004257 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004259 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004261 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262restart:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004263 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265
Yasushi Asanofad8da32013-12-31 12:04:19 +09004266 /* When setting preferred_lft to a value not zero or
4267 * infinity, while valid_lft is infinity
4268 * IFA_F_PERMANENT has a non-infinity life time.
4269 */
4270 if ((ifp->flags & IFA_F_PERMANENT) &&
4271 (ifp->prefered_lft == INFINITY_LIFE_TIME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 continue;
4273
4274 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004275 /* We try to batch several events at once. */
4276 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004278 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4279 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 spin_unlock(&ifp->lock);
4281 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 ipv6_del_addr(ifp);
4283 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004284 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4285 spin_unlock(&ifp->lock);
4286 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00004288 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 int deprecate = 0;
4290
4291 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4292 deprecate = 1;
4293 ifp->flags |= IFA_F_DEPRECATED;
4294 }
4295
Yasushi Asanofad8da32013-12-31 12:04:19 +09004296 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4297 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 next = ifp->tstamp + ifp->valid_lft * HZ;
4299
4300 spin_unlock(&ifp->lock);
4301
4302 if (deprecate) {
4303 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
4305 ipv6_ifa_notify(0, ifp);
4306 in6_ifa_put(ifp);
4307 goto restart;
4308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4310 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00004311 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4312 ifp->idev->cnf.dad_transmits *
Jiri Pirko1f9248e52013-12-07 19:26:53 +01004313 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
stephen hemminger88949cf2010-03-17 20:31:17 +00004314
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 if (age >= ifp->prefered_lft - regen_advance) {
4316 struct inet6_ifaddr *ifpub = ifp->ifpub;
4317 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4318 next = ifp->tstamp + ifp->prefered_lft * HZ;
4319 if (!ifp->regen_count && ifpub) {
4320 ifp->regen_count++;
4321 in6_ifa_hold(ifp);
4322 in6_ifa_hold(ifpub);
4323 spin_unlock(&ifp->lock);
stephen hemminger5c578ae2010-03-17 20:31:11 +00004324
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08004325 spin_lock(&ifpub->lock);
4326 ifpub->regen_count = 0;
4327 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 ipv6_create_tempaddr(ifpub, ifp);
4329 in6_ifa_put(ifpub);
4330 in6_ifa_put(ifp);
4331 goto restart;
4332 }
4333 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4334 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4335 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 } else {
4337 /* ifp->prefered_lft <= ifp->valid_lft */
4338 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4339 next = ifp->tstamp + ifp->prefered_lft * HZ;
4340 spin_unlock(&ifp->lock);
4341 }
4342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 }
4344
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004345 next_sec = round_jiffies_up(next);
4346 next_sched = next;
4347
4348 /* If rounded timeout is accurate enough, accept it. */
4349 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4350 next_sched = next_sec;
4351
4352 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4353 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4354 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4355
dingtianhongba3542e2013-08-17 10:27:04 +08004356 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4357 now, next, next_sec, next_sched);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004358 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
stephen hemminger5c578ae2010-03-17 20:31:11 +00004359 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360}
4361
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004362static void addrconf_verify_work(struct work_struct *w)
4363{
4364 rtnl_lock();
4365 addrconf_verify_rtnl();
4366 rtnl_unlock();
4367}
4368
4369static void addrconf_verify(void)
4370{
4371 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4372}
4373
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004374static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4375 struct in6_addr **peer_pfx)
Thomas Graf461d8832006-09-18 00:09:49 -07004376{
4377 struct in6_addr *pfx = NULL;
4378
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004379 *peer_pfx = NULL;
4380
Thomas Graf461d8832006-09-18 00:09:49 -07004381 if (addr)
4382 pfx = nla_data(addr);
4383
4384 if (local) {
4385 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004386 *peer_pfx = pfx;
4387 pfx = nla_data(local);
Thomas Graf461d8832006-09-18 00:09:49 -07004388 }
4389
4390 return pfx;
4391}
4392
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004393static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07004394 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4395 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4396 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
Jiri Pirko479840f2013-12-06 09:45:21 +01004397 [IFA_FLAGS] = { .len = sizeof(u32) },
Thomas Graf461d8832006-09-18 00:09:49 -07004398};
4399
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400static int
Thomas Graf661d2962013-03-21 07:45:29 +00004401inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004403 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07004404 struct ifaddrmsg *ifm;
4405 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004406 struct in6_addr *pfx, *peer_pfx;
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004407 u32 ifa_flags;
Thomas Grafb933f712006-09-18 00:10:19 -07004408 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409
Thomas Grafb933f712006-09-18 00:10:19 -07004410 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4411 if (err < 0)
4412 return err;
4413
4414 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004415 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004416 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 return -EINVAL;
4418
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004419 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4420
4421 /* We ignore other flags so far. */
4422 ifa_flags &= IFA_F_MANAGETEMPADDR;
4423
4424 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4425 ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426}
4427
Jiri Pirko479840f2013-12-06 09:45:21 +01004428static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004429 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004430{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004431 u32 flags;
4432 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004433 unsigned long timeout;
Jiri Pirko53bd6742013-12-06 09:45:22 +01004434 bool was_managetempaddr;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004435 bool had_prefixroute;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09004436
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004437 ASSERT_RTNL();
4438
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004439 if (!valid_lft || (prefered_lft > valid_lft))
4440 return -EINVAL;
4441
Jiri Pirko53bd6742013-12-06 09:45:22 +01004442 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4443 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4444 return -EINVAL;
4445
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004446 timeout = addrconf_timeout_fixup(valid_lft, HZ);
4447 if (addrconf_finite_timeout(timeout)) {
4448 expires = jiffies_to_clock_t(timeout * HZ);
4449 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004450 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004451 } else {
4452 expires = 0;
4453 flags = 0;
4454 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004455 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004456
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004457 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4458 if (addrconf_finite_timeout(timeout)) {
4459 if (timeout == 0)
4460 ifa_flags |= IFA_F_DEPRECATED;
4461 prefered_lft = timeout;
4462 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004463
4464 spin_lock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004465 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004466 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4467 !(ifp->flags & IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004468 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
Thomas Haller761aac72014-01-15 15:36:58 +01004469 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4470 IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004471 ifp->flags |= ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004472 ifp->tstamp = jiffies;
4473 ifp->valid_lft = valid_lft;
4474 ifp->prefered_lft = prefered_lft;
4475
4476 spin_unlock_bh(&ifp->lock);
4477 if (!(ifp->flags&IFA_F_TENTATIVE))
4478 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004479
Thomas Haller761aac72014-01-15 15:36:58 +01004480 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4481 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4482 expires, flags);
Thomas Haller5b84efe2014-01-15 15:36:59 +01004483 } else if (had_prefixroute) {
4484 enum cleanup_prefix_rt_t action;
4485 unsigned long rt_expires;
4486
4487 write_lock_bh(&ifp->idev->lock);
4488 action = check_cleanup_prefix_route(ifp, &rt_expires);
4489 write_unlock_bh(&ifp->idev->lock);
4490
4491 if (action != CLEANUP_PREFIX_RT_NOP) {
4492 cleanup_prefix_route(ifp, rt_expires,
4493 action == CLEANUP_PREFIX_RT_DEL);
4494 }
Thomas Haller761aac72014-01-15 15:36:58 +01004495 }
Jiri Pirko53bd6742013-12-06 09:45:22 +01004496
4497 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4498 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4499 valid_lft = prefered_lft = 0;
4500 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4501 !was_managetempaddr, jiffies);
4502 }
4503
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004504 addrconf_verify_rtnl();
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004505
4506 return 0;
4507}
4508
4509static int
Thomas Graf661d2962013-03-21 07:45:29 +00004510inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004512 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07004513 struct ifaddrmsg *ifm;
4514 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004515 struct in6_addr *pfx, *peer_pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07004516 struct inet6_ifaddr *ifa;
4517 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004518 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
Jiri Pirko479840f2013-12-06 09:45:21 +01004519 u32 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07004520 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521
Thomas Graf461d8832006-09-18 00:09:49 -07004522 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4523 if (err < 0)
4524 return err;
4525
4526 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004527 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004528 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 return -EINVAL;
4530
Thomas Graf461d8832006-09-18 00:09:49 -07004531 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004532 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07004533
4534 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004535 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07004536 preferred_lft = ci->ifa_prefered;
4537 } else {
4538 preferred_lft = INFINITY_LIFE_TIME;
4539 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004540 }
4541
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004542 dev = __dev_get_by_index(net, ifm->ifa_index);
Ian Morris63159f22015-03-29 14:00:04 +01004543 if (!dev)
Thomas Graf7198f8c2006-09-18 00:13:46 -07004544 return -ENODEV;
4545
Jiri Pirko479840f2013-12-06 09:45:21 +01004546 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4547
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004548 /* We ignore other flags so far. */
Thomas Haller761aac72014-01-15 15:36:58 +01004549 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
Madhu Challa93a714d2015-02-25 09:58:35 -08004550 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004551
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08004552 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Ian Morris63159f22015-03-29 14:00:04 +01004553 if (!ifa) {
Thomas Graf7198f8c2006-09-18 00:13:46 -07004554 /*
4555 * It would be best to check for !NLM_F_CREATE here but
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08004556 * userspace already relies on not having to provide this.
Thomas Graf7198f8c2006-09-18 00:13:46 -07004557 */
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004558 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004559 ifm->ifa_prefixlen, ifa_flags,
4560 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004561 }
4562
Thomas Graf7198f8c2006-09-18 00:13:46 -07004563 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4564 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4565 err = -EEXIST;
4566 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004567 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07004568
4569 in6_ifa_put(ifa);
4570
4571 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572}
4573
Jiri Pirko479840f2013-12-06 09:45:21 +01004574static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
Thomas Graf101bb222006-09-18 00:11:52 -07004575 u8 scope, int ifindex)
4576{
4577 struct ifaddrmsg *ifm;
4578
4579 ifm = nlmsg_data(nlh);
4580 ifm->ifa_family = AF_INET6;
4581 ifm->ifa_prefixlen = prefixlen;
4582 ifm->ifa_flags = flags;
4583 ifm->ifa_scope = scope;
4584 ifm->ifa_index = ifindex;
4585}
4586
Thomas Graf85486af2006-09-18 00:11:24 -07004587static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4588 unsigned long tstamp, u32 preferred, u32 valid)
4589{
4590 struct ifa_cacheinfo ci;
4591
Thomas Graf18a31e12010-11-17 04:12:02 +00004592 ci.cstamp = cstamp_delta(cstamp);
4593 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07004594 ci.ifa_prefered = preferred;
4595 ci.ifa_valid = valid;
4596
4597 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4598}
4599
Thomas Graf101bb222006-09-18 00:11:52 -07004600static inline int rt_scope(int ifa_scope)
4601{
4602 if (ifa_scope & IFA_HOST)
4603 return RT_SCOPE_HOST;
4604 else if (ifa_scope & IFA_LINK)
4605 return RT_SCOPE_LINK;
4606 else if (ifa_scope & IFA_SITE)
4607 return RT_SCOPE_SITE;
4608 else
4609 return RT_SCOPE_UNIVERSE;
4610}
4611
Thomas Graf0ab68032006-09-18 00:12:35 -07004612static inline int inet6_ifaddr_msgsize(void)
4613{
Thomas Graf339bf982006-11-10 14:10:15 -08004614 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004615 + nla_total_size(16) /* IFA_LOCAL */
Thomas Graf339bf982006-11-10 14:10:15 -08004616 + nla_total_size(16) /* IFA_ADDRESS */
Jiri Pirko479840f2013-12-06 09:45:21 +01004617 + nla_total_size(sizeof(struct ifa_cacheinfo))
4618 + nla_total_size(4) /* IFA_FLAGS */;
Thomas Graf0ab68032006-09-18 00:12:35 -07004619}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004620
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004622 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07004625 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626
Eric W. Biederman15e47302012-09-07 20:12:54 +00004627 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004628 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004629 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004630
Thomas Graf101bb222006-09-18 00:11:52 -07004631 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4632 ifa->idev->dev->ifindex);
4633
Yasushi Asanofad8da32013-12-31 12:04:19 +09004634 if (!((ifa->flags&IFA_F_PERMANENT) &&
4635 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
Thomas Graf85486af2006-09-18 00:11:24 -07004636 preferred = ifa->prefered_lft;
4637 valid = ifa->valid_lft;
4638 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00004640 if (preferred > tval)
4641 preferred -= tval;
4642 else
4643 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00004644 if (valid != INFINITY_LIFE_TIME) {
4645 if (valid > tval)
4646 valid -= tval;
4647 else
4648 valid = 0;
4649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 }
4651 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07004652 preferred = INFINITY_LIFE_TIME;
4653 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 }
Thomas Graf85486af2006-09-18 00:11:24 -07004655
Cong Wang7996c792013-05-22 05:41:06 +00004656 if (!ipv6_addr_any(&ifa->peer_addr)) {
Jiri Benc930345e2015-03-29 16:59:25 +02004657 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4658 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004659 goto error;
4660 } else
Jiri Benc930345e2015-03-29 16:59:25 +02004661 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004662 goto error;
4663
4664 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4665 goto error;
Thomas Graf85486af2006-09-18 00:11:24 -07004666
Jiri Pirko479840f2013-12-06 09:45:21 +01004667 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4668 goto error;
4669
Johannes Berg053c0952015-01-16 22:09:00 +01004670 nlmsg_end(skb, nlh);
4671 return 0;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004672
4673error:
4674 nlmsg_cancel(skb, nlh);
4675 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676}
4677
4678static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004679 u32 portid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004682 u8 scope = RT_SCOPE_UNIVERSE;
4683 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684
Thomas Graf101bb222006-09-18 00:11:52 -07004685 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4686 scope = RT_SCOPE_SITE;
4687
Eric W. Biederman15e47302012-09-07 20:12:54 +00004688 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004689 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004690 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004691
Thomas Graf101bb222006-09-18 00:11:52 -07004692 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004693 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004694 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004695 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4696 nlmsg_cancel(skb, nlh);
4697 return -EMSGSIZE;
4698 }
Thomas Graf85486af2006-09-18 00:11:24 -07004699
Johannes Berg053c0952015-01-16 22:09:00 +01004700 nlmsg_end(skb, nlh);
4701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702}
4703
4704static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004705 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004708 u8 scope = RT_SCOPE_UNIVERSE;
4709 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
Thomas Graf101bb222006-09-18 00:11:52 -07004711 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4712 scope = RT_SCOPE_SITE;
4713
Eric W. Biederman15e47302012-09-07 20:12:54 +00004714 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004715 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004716 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004717
Thomas Graf101bb222006-09-18 00:11:52 -07004718 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004719 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004720 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004721 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4722 nlmsg_cancel(skb, nlh);
4723 return -EMSGSIZE;
4724 }
Thomas Graf85486af2006-09-18 00:11:24 -07004725
Johannes Berg053c0952015-01-16 22:09:00 +01004726 nlmsg_end(skb, nlh);
4727 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728}
4729
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004730enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 UNICAST_ADDR,
4732 MULTICAST_ADDR,
4733 ANYCAST_ADDR,
4734};
4735
Eric Dumazet234b27c2009-11-12 04:11:50 +00004736/* called with rcu_read_lock() */
4737static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4738 struct netlink_callback *cb, enum addr_type_t type,
4739 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 struct ifmcaddr6 *ifmca;
4742 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004743 int err = 1;
4744 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745
Eric Dumazet234b27c2009-11-12 04:11:50 +00004746 read_lock_bh(&idev->lock);
4747 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00004748 case UNICAST_ADDR: {
4749 struct inet6_ifaddr *ifa;
4750
Eric Dumazet234b27c2009-11-12 04:11:50 +00004751 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00004752 list_for_each_entry(ifa, &idev->addr_list, if_list) {
David Ahernf86c55c62018-10-19 10:00:19 -07004753 if (ip_idx < s_ip_idx)
4754 goto next;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004755 err = inet6_fill_ifaddr(skb, ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004756 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004757 cb->nlh->nlmsg_seq,
4758 RTM_NEWADDR,
4759 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004760 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004761 break;
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004762 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
David Ahernf86c55c62018-10-19 10:00:19 -07004763next:
4764 ip_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004766 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00004767 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004768 case MULTICAST_ADDR:
4769 /* multicast address */
4770 for (ifmca = idev->mc_list; ifmca;
4771 ifmca = ifmca->next, ip_idx++) {
4772 if (ip_idx < s_ip_idx)
4773 continue;
4774 err = inet6_fill_ifmcaddr(skb, ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004775 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004776 cb->nlh->nlmsg_seq,
4777 RTM_GETMULTICAST,
4778 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004779 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004780 break;
4781 }
4782 break;
4783 case ANYCAST_ADDR:
4784 /* anycast address */
4785 for (ifaca = idev->ac_list; ifaca;
4786 ifaca = ifaca->aca_next, ip_idx++) {
4787 if (ip_idx < s_ip_idx)
4788 continue;
4789 err = inet6_fill_ifacaddr(skb, ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004790 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004791 cb->nlh->nlmsg_seq,
4792 RTM_GETANYCAST,
4793 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004794 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004795 break;
4796 }
4797 break;
4798 default:
4799 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004801 read_unlock_bh(&idev->lock);
4802 *p_ip_idx = ip_idx;
4803 return err;
4804}
4805
4806static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4807 enum addr_type_t type)
4808{
4809 struct net *net = sock_net(skb->sk);
4810 int h, s_h;
4811 int idx, ip_idx;
4812 int s_idx, s_ip_idx;
4813 struct net_device *dev;
4814 struct inet6_dev *idev;
4815 struct hlist_head *head;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004816
4817 s_h = cb->args[0];
4818 s_idx = idx = cb->args[1];
4819 s_ip_idx = ip_idx = cb->args[2];
4820
4821 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004822 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004823 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4824 idx = 0;
4825 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08004826 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet234b27c2009-11-12 04:11:50 +00004827 if (idx < s_idx)
4828 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07004829 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004830 s_ip_idx = 0;
4831 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004832 idev = __in6_dev_get(dev);
4833 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004834 goto cont;
4835
4836 if (in6_dump_addrs(idev, skb, cb, type,
David S. Miller7b46a642015-01-18 23:36:08 -05004837 s_ip_idx, &ip_idx) < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004838 goto done;
4839cont:
4840 idx++;
4841 }
4842 }
4843done:
4844 rcu_read_unlock();
4845 cb->args[0] = h;
4846 cb->args[1] = idx;
4847 cb->args[2] = ip_idx;
4848
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 return skb->len;
4850}
4851
4852static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4853{
4854 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004855
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 return inet6_dump_addr(skb, cb, type);
4857}
4858
4859static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4860{
4861 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004862
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 return inet6_dump_addr(skb, cb, type);
4864}
4865
4866
4867static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4868{
4869 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004870
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 return inet6_dump_addr(skb, cb, type);
4872}
4873
Thomas Graf661d2962013-03-21 07:45:29 +00004874static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004875{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004876 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004877 struct ifaddrmsg *ifm;
4878 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004879 struct in6_addr *addr = NULL, *peer;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004880 struct net_device *dev = NULL;
4881 struct inet6_ifaddr *ifa;
4882 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004883 int err;
4884
Thomas Graf1b29fc22006-09-18 00:10:50 -07004885 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4886 if (err < 0)
4887 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004888
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004889 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
Ian Morris63159f22015-03-29 14:00:04 +01004890 if (!addr) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004891 err = -EINVAL;
4892 goto errout;
4893 }
4894
4895 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004896 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004897 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004898
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004899 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4900 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004901 err = -EADDRNOTAVAIL;
4902 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004903 }
4904
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004905 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4906 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004907 err = -ENOBUFS;
4908 goto errout_ifa;
4909 }
4910
Eric W. Biederman15e47302012-09-07 20:12:54 +00004911 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004912 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004913 if (err < 0) {
4914 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4915 WARN_ON(err == -EMSGSIZE);
4916 kfree_skb(skb);
4917 goto errout_ifa;
4918 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00004919 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004920errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004921 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004922errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004923 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004924}
4925
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4927{
4928 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004929 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07004930 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931
Thomas Graf0ab68032006-09-18 00:12:35 -07004932 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01004933 if (!skb)
Thomas Graf5d620262006-08-15 00:35:02 -07004934 goto errout;
4935
4936 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004937 if (err < 0) {
4938 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4939 WARN_ON(err == -EMSGSIZE);
4940 kfree_skb(skb);
4941 goto errout;
4942 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004943 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4944 return;
Thomas Graf5d620262006-08-15 00:35:02 -07004945errout:
4946 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004947 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948}
4949
Dave Jonesb6f99a22007-03-22 12:27:49 -07004950static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 __s32 *array, int bytes)
4952{
Thomas Graf04561c12006-11-14 19:53:58 -08004953 BUG_ON(bytes < (DEVCONF_MAX * 4));
4954
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 memset(array, 0, bytes);
4956 array[DEVCONF_FORWARDING] = cnf->forwarding;
4957 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4958 array[DEVCONF_MTU6] = cnf->mtu6;
4959 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4960 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4961 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4962 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4963 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00004964 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4965 jiffies_to_msecs(cnf->rtr_solicit_interval);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004966 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
4967 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
Thomas Graf93908d12010-11-17 01:44:24 +00004968 array[DEVCONF_RTR_SOLICIT_DELAY] =
4969 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02004971 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4972 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4973 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4974 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4976 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4977 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4978 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4979 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004981 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
Hangbin Liu8013d1d2015-07-30 14:28:42 +08004982 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004983 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004984#ifdef CONFIG_IPV6_ROUTER_PREF
4985 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00004986 array[DEVCONF_RTR_PROBE_INTERVAL] =
4987 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08004988#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzd860b2e2017-03-22 18:19:04 +09004989 array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004990 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4991#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004992#endif
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +09004993 array[DEVCONF_ACCEPT_RA_RT_TABLE] = cnf->accept_ra_rt_table;
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004994 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004995 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07004996#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4997 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
Erik Kline7fd25612014-10-28 18:11:14 +09004998 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
Neil Horman95c385b2007-04-25 17:08:10 -07004999#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005000#ifdef CONFIG_IPV6_MROUTE
5001 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
5002#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09005003 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09005004 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00005005 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00005006 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02005007 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
Ben Greeard9333192014-06-25 14:44:53 -07005008 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
Harout Hedeshianc2943f12015-01-20 10:06:05 -07005009 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
Andy Gospodarek35103d12015-08-13 10:39:01 -04005010 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005011 /* we omit DEVCONF_STABLE_SECRET for now */
Erik Kline3985e8a2015-07-22 16:38:25 +09005012 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
Johannes Bergabbc3042016-02-04 13:31:19 +01005013 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
Johannes Berg7a02bf82016-02-04 13:31:20 +01005014 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
David Ahernf1705ec2016-02-24 09:25:37 -08005015 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016}
5017
Thomas Grafb382b192010-11-16 04:33:57 +00005018static inline size_t inet6_ifla6_size(void)
5019{
5020 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5021 + nla_total_size(sizeof(struct ifla_cacheinfo))
5022 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5023 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005024 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5025 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
Thomas Grafb382b192010-11-16 04:33:57 +00005026}
5027
Thomas Graf339bf982006-11-10 14:10:15 -08005028static inline size_t inet6_if_nlmsg_size(void)
5029{
5030 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5031 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5032 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5033 + nla_total_size(4) /* IFLA_MTU */
5034 + nla_total_size(4) /* IFLA_LINK */
Andy Gospodarek03443382015-08-13 15:26:35 -04005035 + nla_total_size(1) /* IFLA_OPERSTATE */
Thomas Grafb382b192010-11-16 04:33:57 +00005036 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08005037}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005038
Eric Dumazetbe281e52011-05-19 01:14:23 +00005039static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Jia Heaca05672016-09-30 11:29:03 +08005040 int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005041{
5042 int i;
Jia Heaca05672016-09-30 11:29:03 +08005043 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005044 BUG_ON(pad < 0);
5045
5046 /* Use put_unaligned() because stats may not be aligned for u64. */
Jia Heaca05672016-09-30 11:29:03 +08005047 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5048 for (i = 1; i < ICMP6_MIB_MAX; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00005049 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005050
Jia Heaca05672016-09-30 11:29:03 +08005051 memset(&stats[ICMP6_MIB_MAX], 0, pad);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005052}
5053
WANG Cong698365f2014-05-05 15:55:55 -07005054static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305055 int bytes, size_t syncpoff)
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005056{
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305057 int i, c;
5058 u64 buff[IPSTATS_MIB_MAX];
5059 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5060
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005061 BUG_ON(pad < 0);
5062
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305063 memset(buff, 0, sizeof(buff));
5064 buff[0] = IPSTATS_MIB_MAX;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005065
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305066 for_each_possible_cpu(c) {
5067 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5068 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5069 }
5070
5071 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5072 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005073}
5074
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005075static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5076 int bytes)
5077{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005078 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005079 case IFLA_INET6_STATS:
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305080 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5081 offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005082 break;
5083 case IFLA_INET6_ICMP6STATS:
Jia Heaca05672016-09-30 11:29:03 +08005084 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005085 break;
5086 }
5087}
5088
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005089static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5090 u32 ext_filter_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005092 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08005093 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094
David S. Millerc78679e2012-04-01 20:27:33 -04005095 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5096 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08005098 ci.tstamp = cstamp_delta(idev->tstamp);
5099 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
Jiri Pirko1f9248e52013-12-07 19:26:53 +01005100 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
David S. Millerc78679e2012-04-01 20:27:33 -04005101 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5102 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005103 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
Ian Morris63159f22015-03-29 14:00:04 +01005104 if (!nla)
Thomas Graf04561c12006-11-14 19:53:58 -08005105 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005106 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005108 /* XXX - MC not implemented */
5109
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005110 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5111 return 0;
5112
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005113 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005114 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005115 goto nla_put_failure;
5116 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5117
5118 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005119 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005120 goto nla_put_failure;
5121 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005123 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
Ian Morris63159f22015-03-29 14:00:04 +01005124 if (!nla)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005125 goto nla_put_failure;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005126
5127 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode))
5128 goto nla_put_failure;
5129
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005130 read_lock_bh(&idev->lock);
5131 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5132 read_unlock_bh(&idev->lock);
5133
Thomas Grafb382b192010-11-16 04:33:57 +00005134 return 0;
5135
5136nla_put_failure:
5137 return -EMSGSIZE;
5138}
5139
Arad, Ronenb1974ed2015-10-19 09:23:28 -07005140static size_t inet6_get_link_af_size(const struct net_device *dev,
5141 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005142{
5143 if (!__in6_dev_get(dev))
5144 return 0;
5145
5146 return inet6_ifla6_size();
5147}
5148
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005149static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5150 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005151{
5152 struct inet6_dev *idev = __in6_dev_get(dev);
5153
5154 if (!idev)
5155 return -ENODATA;
5156
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005157 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005158 return -EMSGSIZE;
5159
5160 return 0;
5161}
5162
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005163static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5164{
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005165 struct inet6_ifaddr *ifp;
5166 struct net_device *dev = idev->dev;
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005167 bool clear_token, update_rs = false;
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005168 struct in6_addr ll_addr;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005169
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005170 ASSERT_RTNL();
5171
Ian Morris63159f22015-03-29 14:00:04 +01005172 if (!token)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005173 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005174 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5175 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005176 if (!ipv6_accept_ra(idev))
5177 return -EINVAL;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005178 if (idev->cnf.rtr_solicits == 0)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005179 return -EINVAL;
5180
5181 write_lock_bh(&idev->lock);
5182
5183 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5184 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5185
5186 write_unlock_bh(&idev->lock);
5187
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005188 clear_token = ipv6_addr_any(token);
5189 if (clear_token)
5190 goto update_lft;
5191
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005192 if (!idev->dead && (idev->if_flags & IF_READY) &&
5193 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5194 IFA_F_OPTIMISTIC)) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005195 /* If we're not ready, then normal ifup will take care
5196 * of this. Otherwise, we need to request our rs here.
5197 */
5198 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5199 update_rs = true;
5200 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005201
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005202update_lft:
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005203 write_lock_bh(&idev->lock);
Daniel Borkmannfc403832013-04-09 03:47:15 +00005204
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005205 if (update_rs) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005206 idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005207 idev->rs_interval = rfc3315_s14_backoff_init(
5208 idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005209 idev->rs_probes = 1;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005210 addrconf_mod_rs_timer(idev, idev->rs_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005211 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005212
5213 /* Well, that's kinda nasty ... */
5214 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5215 spin_lock(&ifp->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00005216 if (ifp->tokenized) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005217 ifp->valid_lft = 0;
5218 ifp->prefered_lft = 0;
5219 }
5220 spin_unlock(&ifp->lock);
5221 }
5222
5223 write_unlock_bh(&idev->lock);
Lubomir Rintelb2ed64a2014-10-27 17:39:16 +01005224 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005225 addrconf_verify_rtnl();
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005226 return 0;
5227}
5228
Daniel Borkmann11b1f822015-02-05 14:39:11 +01005229static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5230 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5231 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5232};
5233
5234static int inet6_validate_link_af(const struct net_device *dev,
5235 const struct nlattr *nla)
5236{
5237 struct nlattr *tb[IFLA_INET6_MAX + 1];
5238
5239 if (dev && !__in6_dev_get(dev))
5240 return -EAFNOSUPPORT;
5241
5242 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
5243}
5244
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005245static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5246{
5247 int err = -EINVAL;
5248 struct inet6_dev *idev = __in6_dev_get(dev);
5249 struct nlattr *tb[IFLA_INET6_MAX + 1];
5250
5251 if (!idev)
5252 return -EAFNOSUPPORT;
5253
5254 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
5255 BUG();
5256
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005257 if (tb[IFLA_INET6_TOKEN]) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005258 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005259 if (err)
5260 return err;
5261 }
5262
5263 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5264 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5265
5266 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005267 mode != IN6_ADDR_GEN_MODE_NONE &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01005268 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5269 mode != IN6_ADDR_GEN_MODE_RANDOM)
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005270 return -EINVAL;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005271
5272 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5273 !idev->cnf.stable_secret.initialized &&
5274 !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized)
5275 return -EINVAL;
5276
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005277 idev->addr_gen_mode = mode;
5278 err = 0;
5279 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005280
5281 return err;
5282}
5283
Thomas Grafb382b192010-11-16 04:33:57 +00005284static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005285 u32 portid, u32 seq, int event, unsigned int flags)
Thomas Grafb382b192010-11-16 04:33:57 +00005286{
5287 struct net_device *dev = idev->dev;
5288 struct ifinfomsg *hdr;
5289 struct nlmsghdr *nlh;
5290 void *protoinfo;
5291
Eric W. Biederman15e47302012-09-07 20:12:54 +00005292 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005293 if (!nlh)
Thomas Grafb382b192010-11-16 04:33:57 +00005294 return -EMSGSIZE;
5295
5296 hdr = nlmsg_data(nlh);
5297 hdr->ifi_family = AF_INET6;
5298 hdr->__ifi_pad = 0;
5299 hdr->ifi_type = dev->type;
5300 hdr->ifi_index = dev->ifindex;
5301 hdr->ifi_flags = dev_get_flags(dev);
5302 hdr->ifi_change = 0;
5303
David S. Millerc78679e2012-04-01 20:27:33 -04005304 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5305 (dev->addr_len &&
5306 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5307 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02005308 (dev->ifindex != dev_get_iflink(dev) &&
Andy Gospodarek03443382015-08-13 15:26:35 -04005309 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5310 nla_put_u8(skb, IFLA_OPERSTATE,
5311 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
David S. Millerc78679e2012-04-01 20:27:33 -04005312 goto nla_put_failure;
Thomas Grafb382b192010-11-16 04:33:57 +00005313 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
Ian Morris63159f22015-03-29 14:00:04 +01005314 if (!protoinfo)
Thomas Grafb382b192010-11-16 04:33:57 +00005315 goto nla_put_failure;
5316
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005317 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005318 goto nla_put_failure;
5319
Thomas Graf04561c12006-11-14 19:53:58 -08005320 nla_nest_end(skb, protoinfo);
Johannes Berg053c0952015-01-16 22:09:00 +01005321 nlmsg_end(skb, nlh);
5322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323
Thomas Graf04561c12006-11-14 19:53:58 -08005324nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005325 nlmsg_cancel(skb, nlh);
5326 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327}
5328
5329static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5330{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005331 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00005332 int h, s_h;
David S. Miller434a8a582009-11-11 18:53:00 -08005333 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 struct net_device *dev;
5335 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00005336 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337
Eric Dumazet84d26972009-11-09 12:11:28 +00005338 s_h = cb->args[0];
5339 s_idx = cb->args[1];
5340
5341 rcu_read_lock();
5342 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5343 idx = 0;
5344 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08005345 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet84d26972009-11-09 12:11:28 +00005346 if (idx < s_idx)
5347 goto cont;
5348 idev = __in6_dev_get(dev);
5349 if (!idev)
5350 goto cont;
5351 if (inet6_fill_ifinfo(skb, idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005352 NETLINK_CB(cb->skb).portid,
Eric Dumazet84d26972009-11-09 12:11:28 +00005353 cb->nlh->nlmsg_seq,
Johannes Berg053c0952015-01-16 22:09:00 +01005354 RTM_NEWLINK, NLM_F_MULTI) < 0)
Eric Dumazet84d26972009-11-09 12:11:28 +00005355 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07005356cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00005357 idx++;
5358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 }
Eric Dumazet84d26972009-11-09 12:11:28 +00005360out:
5361 rcu_read_unlock();
5362 cb->args[1] = idx;
5363 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364
5365 return skb->len;
5366}
5367
5368void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5369{
5370 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005371 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005372 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005373
Thomas Graf339bf982006-11-10 14:10:15 -08005374 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005375 if (!skb)
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005376 goto errout;
5377
5378 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005379 if (err < 0) {
5380 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5381 WARN_ON(err == -EMSGSIZE);
5382 kfree_skb(skb);
5383 goto errout;
5384 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005385 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005386 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005387errout:
5388 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005389 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390}
5391
Thomas Graf339bf982006-11-10 14:10:15 -08005392static inline size_t inet6_prefix_nlmsg_size(void)
5393{
5394 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5395 + nla_total_size(sizeof(struct in6_addr))
5396 + nla_total_size(sizeof(struct prefix_cacheinfo));
5397}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005398
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005400 struct prefix_info *pinfo, u32 portid, u32 seq,
Thomas Graf6051e2f2006-11-14 19:54:19 -08005401 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402{
Thomas Graf6051e2f2006-11-14 19:54:19 -08005403 struct prefixmsg *pmsg;
5404 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 struct prefix_cacheinfo ci;
5406
Eric W. Biederman15e47302012-09-07 20:12:54 +00005407 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005408 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005409 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08005410
5411 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07005413 pmsg->prefix_pad1 = 0;
5414 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 pmsg->prefix_ifindex = idev->dev->ifindex;
5416 pmsg->prefix_len = pinfo->prefix_len;
5417 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07005418 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 pmsg->prefix_flags = 0;
5420 if (pinfo->onlink)
5421 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5422 if (pinfo->autoconf)
5423 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5424
David S. Millerc78679e2012-04-01 20:27:33 -04005425 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5426 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 ci.preferred_time = ntohl(pinfo->prefered);
5428 ci.valid_time = ntohl(pinfo->valid);
David S. Millerc78679e2012-04-01 20:27:33 -04005429 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5430 goto nla_put_failure;
Johannes Berg053c0952015-01-16 22:09:00 +01005431 nlmsg_end(skb, nlh);
5432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433
Thomas Graf6051e2f2006-11-14 19:54:19 -08005434nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005435 nlmsg_cancel(skb, nlh);
5436 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437}
5438
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005439static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 struct prefix_info *pinfo)
5441{
5442 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005443 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005444 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445
Thomas Graf339bf982006-11-10 14:10:15 -08005446 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005447 if (!skb)
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005448 goto errout;
5449
5450 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005451 if (err < 0) {
5452 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5453 WARN_ON(err == -EMSGSIZE);
5454 kfree_skb(skb);
5455 goto errout;
5456 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005457 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5458 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005459errout:
5460 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08005461 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462}
5463
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5465{
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005466 struct net *net = dev_net(ifp->idev->dev);
5467
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005468 if (event)
5469 ASSERT_RTNL();
5470
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5472
5473 switch (event) {
5474 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07005475 /*
5476 * If the address was optimistic
5477 * we inserted the route at the start of
5478 * our DAD process, so we don't need
5479 * to do it again
5480 */
Wei Wang43c792a2017-08-25 15:03:10 -07005481 if (!rcu_access_pointer(ifp->rt->rt6i_node))
Neil Horman95c385b2007-04-25 17:08:10 -07005482 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 if (ifp->idev->cnf.forwarding)
5484 addrconf_join_anycast(ifp);
Cong Wang7996c792013-05-22 05:41:06 +00005485 if (!ipv6_addr_any(&ifp->peer_addr))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005486 addrconf_prefix_route(&ifp->peer_addr, 128,
5487 ifp->idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 break;
5489 case RTM_DELADDR:
5490 if (ifp->idev->cnf.forwarding)
5491 addrconf_leave_anycast(ifp);
5492 addrconf_leave_solict(ifp->idev, &ifp->addr);
Cong Wang7996c792013-05-22 05:41:06 +00005493 if (!ipv6_addr_any(&ifp->peer_addr)) {
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005494 struct rt6_info *rt;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005495
Nicolas Dichtele7478df2014-09-03 23:59:22 +02005496 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5497 ifp->idev->dev, 0, 0);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07005498 if (rt)
5499 ip6_del_rt(rt);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005500 }
David Ahern38bd10c2016-04-21 20:56:12 -07005501 if (ifp->rt) {
5502 dst_hold(&ifp->rt->dst);
5503 ip6_del_rt(ifp->rt);
5504 }
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02005505 rt_genid_bump_ipv6(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 break;
5507 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005508 atomic_inc(&net->ipv6.dev_addr_genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509}
5510
5511static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5512{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005513 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 if (likely(ifp->idev->dead == 0))
5515 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005516 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517}
5518
5519#ifdef CONFIG_SYSCTL
5520
5521static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005522int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 void __user *buffer, size_t *lenp, loff_t *ppos)
5524{
5525 int *valp = ctl->data;
5526 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005527 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005528 struct ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 int ret;
5530
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005531 /*
5532 * ctl->data points to idev->cnf.forwarding, we should
5533 * not modify it until we get the rtnl lock.
5534 */
5535 lctl = *ctl;
5536 lctl.data = &val;
5537
5538 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08005540 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00005541 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005542 if (ret)
5543 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005544 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545}
5546
Marcelo Leitner77751422015-02-23 11:17:13 -03005547static
5548int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5549 void __user *buffer, size_t *lenp, loff_t *ppos)
5550{
5551 struct inet6_dev *idev = ctl->extra1;
5552 int min_mtu = IPV6_MIN_MTU;
5553 struct ctl_table lctl;
5554
5555 lctl = *ctl;
5556 lctl.extra1 = &min_mtu;
5557 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5558
5559 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5560}
5561
Brian Haley56d417b2009-06-01 03:07:33 -07005562static void dev_disable_change(struct inet6_dev *idev)
5563{
Cong Wang75538c22013-05-29 11:30:50 +08005564 struct netdev_notifier_info info;
5565
Brian Haley56d417b2009-06-01 03:07:33 -07005566 if (!idev || !idev->dev)
5567 return;
5568
Cong Wang75538c22013-05-29 11:30:50 +08005569 netdev_notifier_info_init(&info, idev->dev);
Brian Haley56d417b2009-06-01 03:07:33 -07005570 if (idev->cnf.disable_ipv6)
Cong Wang75538c22013-05-29 11:30:50 +08005571 addrconf_notify(NULL, NETDEV_DOWN, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005572 else
Cong Wang75538c22013-05-29 11:30:50 +08005573 addrconf_notify(NULL, NETDEV_UP, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005574}
5575
5576static void addrconf_disable_change(struct net *net, __s32 newf)
5577{
5578 struct net_device *dev;
5579 struct inet6_dev *idev;
5580
Kefeng Wang79453ab2017-01-19 16:26:21 +08005581 for_each_netdev(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07005582 idev = __in6_dev_get(dev);
5583 if (idev) {
5584 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5585 idev->cnf.disable_ipv6 = newf;
5586 if (changed)
5587 dev_disable_change(idev);
5588 }
Brian Haley56d417b2009-06-01 03:07:33 -07005589 }
Brian Haley56d417b2009-06-01 03:07:33 -07005590}
5591
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005592static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07005593{
5594 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005595 int old;
5596
5597 if (!rtnl_trylock())
5598 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07005599
5600 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005601 old = *p;
5602 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07005603
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005604 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5605 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07005606 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005607 }
Brian Haley56d417b2009-06-01 03:07:33 -07005608
5609 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07005610 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5611 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005612 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07005613 dev_disable_change((struct inet6_dev *)table->extra1);
5614
5615 rtnl_unlock();
5616 return 0;
5617}
5618
5619static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005620int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07005621 void __user *buffer, size_t *lenp, loff_t *ppos)
5622{
5623 int *valp = ctl->data;
5624 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005625 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005626 struct ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07005627 int ret;
5628
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005629 /*
5630 * ctl->data points to idev->cnf.disable_ipv6, we should
5631 * not modify it until we get the rtnl lock.
5632 */
5633 lctl = *ctl;
5634 lctl.data = &val;
5635
5636 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07005637
5638 if (write)
5639 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005640 if (ret)
5641 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07005642 return ret;
5643}
5644
stephen hemmingerc92d5492013-12-17 22:37:14 -08005645static
5646int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5647 void __user *buffer, size_t *lenp, loff_t *ppos)
5648{
5649 int *valp = ctl->data;
5650 int ret;
5651 int old, new;
5652
5653 old = *valp;
5654 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5655 new = *valp;
5656
5657 if (write && old != new) {
5658 struct net *net = ctl->extra2;
5659
5660 if (!rtnl_trylock())
5661 return restart_syscall();
5662
5663 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5664 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5665 NETCONFA_IFINDEX_DEFAULT,
5666 net->ipv6.devconf_dflt);
5667 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5668 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5669 NETCONFA_IFINDEX_ALL,
5670 net->ipv6.devconf_all);
5671 else {
5672 struct inet6_dev *idev = ctl->extra1;
5673
5674 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5675 idev->dev->ifindex,
5676 &idev->cnf);
5677 }
5678 rtnl_unlock();
5679 }
5680
5681 return ret;
5682}
5683
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005684static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5685 void __user *buffer, size_t *lenp,
5686 loff_t *ppos)
5687{
5688 int err;
5689 struct in6_addr addr;
5690 char str[IPV6_MAX_STRLEN];
5691 struct ctl_table lctl = *ctl;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005692 struct net *net = ctl->extra2;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005693 struct ipv6_stable_secret *secret = ctl->data;
5694
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005695 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5696 return -EIO;
5697
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005698 lctl.maxlen = IPV6_MAX_STRLEN;
5699 lctl.data = str;
5700
5701 if (!rtnl_trylock())
5702 return restart_syscall();
5703
5704 if (!write && !secret->initialized) {
5705 err = -EIO;
5706 goto out;
5707 }
5708
WANG Cong5449a5c2015-12-21 10:55:45 -08005709 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
5710 if (err >= sizeof(str)) {
5711 err = -EIO;
5712 goto out;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005713 }
5714
5715 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5716 if (err || !write)
5717 goto out;
5718
5719 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5720 err = -EIO;
5721 goto out;
5722 }
5723
5724 secret->initialized = true;
5725 secret->secret = addr;
5726
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005727 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5728 struct net_device *dev;
5729
5730 for_each_netdev(net, dev) {
5731 struct inet6_dev *idev = __in6_dev_get(dev);
5732
5733 if (idev) {
5734 idev->addr_gen_mode =
5735 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5736 }
5737 }
5738 } else {
5739 struct inet6_dev *idev = ctl->extra1;
5740
5741 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5742 }
5743
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005744out:
5745 rtnl_unlock();
5746
5747 return err;
5748}
stephen hemmingerc92d5492013-12-17 22:37:14 -08005749
Andy Gospodarek35103d12015-08-13 10:39:01 -04005750static
5751int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
5752 int write,
5753 void __user *buffer,
5754 size_t *lenp,
5755 loff_t *ppos)
5756{
5757 int *valp = ctl->data;
5758 int val = *valp;
5759 loff_t pos = *ppos;
5760 struct ctl_table lctl;
5761 int ret;
5762
5763 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
5764 * we should not modify it until we get the rtnl lock.
5765 */
5766 lctl = *ctl;
5767 lctl.data = &val;
5768
5769 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5770
5771 if (write)
5772 ret = addrconf_fixup_linkdown(ctl, valp, val);
5773 if (ret)
5774 *ppos = pos;
5775 return ret;
5776}
5777
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07005778static int minus_one = -1;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07005779static const int one = 1;
5780static const int two_five_five = 255;
5781
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03005782static const struct ctl_table addrconf_sysctl[] = {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005783 {
5784 .procname = "forwarding",
5785 .data = &ipv6_devconf.forwarding,
5786 .maxlen = sizeof(int),
5787 .mode = 0644,
5788 .proc_handler = addrconf_sysctl_forward,
5789 },
5790 {
5791 .procname = "hop_limit",
5792 .data = &ipv6_devconf.hop_limit,
5793 .maxlen = sizeof(int),
5794 .mode = 0644,
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07005795 .proc_handler = proc_dointvec_minmax,
5796 .extra1 = (void *)&one,
5797 .extra2 = (void *)&two_five_five,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005798 },
5799 {
5800 .procname = "mtu",
5801 .data = &ipv6_devconf.mtu6,
5802 .maxlen = sizeof(int),
5803 .mode = 0644,
5804 .proc_handler = addrconf_sysctl_mtu,
5805 },
5806 {
5807 .procname = "accept_ra",
5808 .data = &ipv6_devconf.accept_ra,
5809 .maxlen = sizeof(int),
5810 .mode = 0644,
5811 .proc_handler = proc_dointvec,
5812 },
5813 {
5814 .procname = "accept_redirects",
5815 .data = &ipv6_devconf.accept_redirects,
5816 .maxlen = sizeof(int),
5817 .mode = 0644,
5818 .proc_handler = proc_dointvec,
5819 },
5820 {
5821 .procname = "autoconf",
5822 .data = &ipv6_devconf.autoconf,
5823 .maxlen = sizeof(int),
5824 .mode = 0644,
5825 .proc_handler = proc_dointvec,
5826 },
5827 {
5828 .procname = "dad_transmits",
5829 .data = &ipv6_devconf.dad_transmits,
5830 .maxlen = sizeof(int),
5831 .mode = 0644,
5832 .proc_handler = proc_dointvec,
5833 },
5834 {
5835 .procname = "router_solicitations",
5836 .data = &ipv6_devconf.rtr_solicits,
5837 .maxlen = sizeof(int),
5838 .mode = 0644,
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07005839 .proc_handler = proc_dointvec_minmax,
5840 .extra1 = &minus_one,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005841 },
5842 {
5843 .procname = "router_solicitation_interval",
5844 .data = &ipv6_devconf.rtr_solicit_interval,
5845 .maxlen = sizeof(int),
5846 .mode = 0644,
5847 .proc_handler = proc_dointvec_jiffies,
5848 },
5849 {
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005850 .procname = "router_solicitation_max_interval",
5851 .data = &ipv6_devconf.rtr_solicit_max_interval,
5852 .maxlen = sizeof(int),
5853 .mode = 0644,
5854 .proc_handler = proc_dointvec_jiffies,
5855 },
5856 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005857 .procname = "router_solicitation_delay",
5858 .data = &ipv6_devconf.rtr_solicit_delay,
5859 .maxlen = sizeof(int),
5860 .mode = 0644,
5861 .proc_handler = proc_dointvec_jiffies,
5862 },
5863 {
5864 .procname = "force_mld_version",
5865 .data = &ipv6_devconf.force_mld_version,
5866 .maxlen = sizeof(int),
5867 .mode = 0644,
5868 .proc_handler = proc_dointvec,
5869 },
5870 {
5871 .procname = "mldv1_unsolicited_report_interval",
5872 .data =
5873 &ipv6_devconf.mldv1_unsolicited_report_interval,
5874 .maxlen = sizeof(int),
5875 .mode = 0644,
5876 .proc_handler = proc_dointvec_ms_jiffies,
5877 },
5878 {
5879 .procname = "mldv2_unsolicited_report_interval",
5880 .data =
5881 &ipv6_devconf.mldv2_unsolicited_report_interval,
5882 .maxlen = sizeof(int),
5883 .mode = 0644,
5884 .proc_handler = proc_dointvec_ms_jiffies,
5885 },
5886 {
5887 .procname = "use_tempaddr",
5888 .data = &ipv6_devconf.use_tempaddr,
5889 .maxlen = sizeof(int),
5890 .mode = 0644,
5891 .proc_handler = proc_dointvec,
5892 },
5893 {
5894 .procname = "temp_valid_lft",
5895 .data = &ipv6_devconf.temp_valid_lft,
5896 .maxlen = sizeof(int),
5897 .mode = 0644,
5898 .proc_handler = proc_dointvec,
5899 },
5900 {
5901 .procname = "temp_prefered_lft",
5902 .data = &ipv6_devconf.temp_prefered_lft,
5903 .maxlen = sizeof(int),
5904 .mode = 0644,
5905 .proc_handler = proc_dointvec,
5906 },
5907 {
5908 .procname = "regen_max_retry",
5909 .data = &ipv6_devconf.regen_max_retry,
5910 .maxlen = sizeof(int),
5911 .mode = 0644,
5912 .proc_handler = proc_dointvec,
5913 },
5914 {
5915 .procname = "max_desync_factor",
5916 .data = &ipv6_devconf.max_desync_factor,
5917 .maxlen = sizeof(int),
5918 .mode = 0644,
5919 .proc_handler = proc_dointvec,
5920 },
5921 {
5922 .procname = "max_addresses",
5923 .data = &ipv6_devconf.max_addresses,
5924 .maxlen = sizeof(int),
5925 .mode = 0644,
5926 .proc_handler = proc_dointvec,
5927 },
5928 {
5929 .procname = "accept_ra_defrtr",
5930 .data = &ipv6_devconf.accept_ra_defrtr,
5931 .maxlen = sizeof(int),
5932 .mode = 0644,
5933 .proc_handler = proc_dointvec,
5934 },
5935 {
5936 .procname = "accept_ra_min_hop_limit",
5937 .data = &ipv6_devconf.accept_ra_min_hop_limit,
5938 .maxlen = sizeof(int),
5939 .mode = 0644,
5940 .proc_handler = proc_dointvec,
5941 },
5942 {
5943 .procname = "accept_ra_pinfo",
5944 .data = &ipv6_devconf.accept_ra_pinfo,
5945 .maxlen = sizeof(int),
5946 .mode = 0644,
5947 .proc_handler = proc_dointvec,
5948 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005949#ifdef CONFIG_IPV6_ROUTER_PREF
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005950 {
5951 .procname = "accept_ra_rtr_pref",
5952 .data = &ipv6_devconf.accept_ra_rtr_pref,
5953 .maxlen = sizeof(int),
5954 .mode = 0644,
5955 .proc_handler = proc_dointvec,
5956 },
5957 {
5958 .procname = "router_probe_interval",
5959 .data = &ipv6_devconf.rtr_probe_interval,
5960 .maxlen = sizeof(int),
5961 .mode = 0644,
5962 .proc_handler = proc_dointvec_jiffies,
5963 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08005964#ifdef CONFIG_IPV6_ROUTE_INFO
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005965 {
Joel Scherpelzd860b2e2017-03-22 18:19:04 +09005966 .procname = "accept_ra_rt_info_min_plen",
5967 .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
5968 .maxlen = sizeof(int),
5969 .mode = 0644,
5970 .proc_handler = proc_dointvec,
5971 },
5972 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005973 .procname = "accept_ra_rt_info_max_plen",
5974 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
5975 .maxlen = sizeof(int),
5976 .mode = 0644,
5977 .proc_handler = proc_dointvec,
5978 },
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08005979#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005980#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005981 {
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +09005982 .procname = "accept_ra_rt_table",
5983 .data = &ipv6_devconf.accept_ra_rt_table,
5984 .maxlen = sizeof(int),
5985 .mode = 0644,
5986 .proc_handler = proc_dointvec,
5987 },
5988 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005989 .procname = "proxy_ndp",
5990 .data = &ipv6_devconf.proxy_ndp,
5991 .maxlen = sizeof(int),
5992 .mode = 0644,
5993 .proc_handler = addrconf_sysctl_proxy_ndp,
5994 },
5995 {
5996 .procname = "accept_source_route",
5997 .data = &ipv6_devconf.accept_source_route,
5998 .maxlen = sizeof(int),
5999 .mode = 0644,
6000 .proc_handler = proc_dointvec,
6001 },
Neil Horman95c385b2007-04-25 17:08:10 -07006002#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006003 {
6004 .procname = "optimistic_dad",
6005 .data = &ipv6_devconf.optimistic_dad,
6006 .maxlen = sizeof(int),
6007 .mode = 0644,
6008 .proc_handler = proc_dointvec,
6009 },
6010 {
6011 .procname = "use_optimistic",
6012 .data = &ipv6_devconf.use_optimistic,
6013 .maxlen = sizeof(int),
6014 .mode = 0644,
6015 .proc_handler = proc_dointvec,
6016 },
Neil Horman95c385b2007-04-25 17:08:10 -07006017#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006018#ifdef CONFIG_IPV6_MROUTE
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006019 {
6020 .procname = "mc_forwarding",
6021 .data = &ipv6_devconf.mc_forwarding,
6022 .maxlen = sizeof(int),
6023 .mode = 0444,
6024 .proc_handler = proc_dointvec,
6025 },
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006026#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006027 {
6028 .procname = "disable_ipv6",
6029 .data = &ipv6_devconf.disable_ipv6,
6030 .maxlen = sizeof(int),
6031 .mode = 0644,
6032 .proc_handler = addrconf_sysctl_disable,
6033 },
6034 {
6035 .procname = "accept_dad",
6036 .data = &ipv6_devconf.accept_dad,
6037 .maxlen = sizeof(int),
6038 .mode = 0644,
6039 .proc_handler = proc_dointvec,
6040 },
6041 {
6042 .procname = "force_tllao",
6043 .data = &ipv6_devconf.force_tllao,
6044 .maxlen = sizeof(int),
6045 .mode = 0644,
6046 .proc_handler = proc_dointvec
6047 },
6048 {
6049 .procname = "ndisc_notify",
6050 .data = &ipv6_devconf.ndisc_notify,
6051 .maxlen = sizeof(int),
6052 .mode = 0644,
6053 .proc_handler = proc_dointvec
6054 },
6055 {
6056 .procname = "suppress_frag_ndisc",
6057 .data = &ipv6_devconf.suppress_frag_ndisc,
6058 .maxlen = sizeof(int),
6059 .mode = 0644,
6060 .proc_handler = proc_dointvec
6061 },
6062 {
6063 .procname = "accept_ra_from_local",
6064 .data = &ipv6_devconf.accept_ra_from_local,
6065 .maxlen = sizeof(int),
6066 .mode = 0644,
6067 .proc_handler = proc_dointvec,
6068 },
6069 {
6070 .procname = "accept_ra_mtu",
6071 .data = &ipv6_devconf.accept_ra_mtu,
6072 .maxlen = sizeof(int),
6073 .mode = 0644,
6074 .proc_handler = proc_dointvec,
6075 },
6076 {
6077 .procname = "stable_secret",
6078 .data = &ipv6_devconf.stable_secret,
6079 .maxlen = IPV6_MAX_STRLEN,
6080 .mode = 0600,
6081 .proc_handler = addrconf_sysctl_stable_secret,
6082 },
6083 {
6084 .procname = "use_oif_addrs_only",
6085 .data = &ipv6_devconf.use_oif_addrs_only,
6086 .maxlen = sizeof(int),
6087 .mode = 0644,
6088 .proc_handler = proc_dointvec,
6089 },
6090 {
6091 .procname = "ignore_routes_with_linkdown",
6092 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6093 .maxlen = sizeof(int),
6094 .mode = 0644,
6095 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6096 },
6097 {
6098 .procname = "drop_unicast_in_l2_multicast",
6099 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6100 .maxlen = sizeof(int),
6101 .mode = 0644,
6102 .proc_handler = proc_dointvec,
6103 },
6104 {
6105 .procname = "drop_unsolicited_na",
6106 .data = &ipv6_devconf.drop_unsolicited_na,
6107 .maxlen = sizeof(int),
6108 .mode = 0644,
6109 .proc_handler = proc_dointvec,
6110 },
6111 {
6112 .procname = "keep_addr_on_down",
6113 .data = &ipv6_devconf.keep_addr_on_down,
6114 .maxlen = sizeof(int),
6115 .mode = 0644,
6116 .proc_handler = proc_dointvec,
David Ahernf1705ec2016-02-24 09:25:37 -08006117
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006118 },
6119 {
6120 /* sentinel */
6121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122};
6123
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08006124static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006125 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126{
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006127 int i, ifindex;
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006128 struct ctl_table *table;
Eric W. Biederman6105e292012-04-19 13:41:24 +00006129 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11006130
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006131 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6132 if (!table)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006133 goto out;
6134
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006135 for (i = 0; table[i].data; i++) {
6136 table[i].data += (char *)p - (char *)&ipv6_devconf;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006137 /* If one of these is already set, then it is not safe to
6138 * overwrite either of them: this makes proc_dointvec_minmax
6139 * usable.
6140 */
6141 if (!table[i].extra1 && !table[i].extra2) {
6142 table[i].extra1 = idev; /* embedded; no ref */
6143 table[i].extra2 = net;
6144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146
Eric W. Biederman6105e292012-04-19 13:41:24 +00006147 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006149 p->sysctl_header = register_net_sysctl(net, path, table);
6150 if (!p->sysctl_header)
Eric W. Biederman6105e292012-04-19 13:41:24 +00006151 goto free;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006152
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006153 if (!strcmp(dev_name, "all"))
6154 ifindex = NETCONFA_IFINDEX_ALL;
6155 else if (!strcmp(dev_name, "default"))
6156 ifindex = NETCONFA_IFINDEX_DEFAULT;
6157 else
6158 ifindex = idev->dev->ifindex;
6159 inet6_netconf_notify_devconf(net, NETCONFA_ALL, ifindex, p);
Pavel Emelyanov95897312008-01-10 17:41:45 -08006160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006162free:
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006163 kfree(table);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006164out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08006165 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166}
6167
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006168static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
6169{
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006170 struct ctl_table *table;
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006171
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006172 if (!p->sysctl_header)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006173 return;
6174
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006175 table = p->sysctl_header->ctl_table_arg;
6176 unregister_net_sysctl_table(p->sysctl_header);
6177 p->sysctl_header = NULL;
6178 kfree(table);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006179}
6180
WANG Conga317a2f2014-07-25 15:25:09 -07006181static int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006182{
WANG Conga317a2f2014-07-25 15:25:09 -07006183 int err;
6184
6185 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6186 return -EINVAL;
6187
6188 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6189 &ndisc_ifinfo_sysctl_change);
6190 if (err)
6191 return err;
6192 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6193 idev, &idev->cnf);
6194 if (err)
6195 neigh_sysctl_unregister(idev->nd_parms);
6196
6197 return err;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006198}
6199
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006200static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006202 __addrconf_sysctl_unregister(&idev->cnf);
6203 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204}
6205
6206
6207#endif
6208
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006209static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006210{
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006211 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006212 struct ipv6_devconf *all, *dflt;
6213
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006214 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006215 if (!all)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006216 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006217
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006218 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006219 if (!dflt)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006220 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006221
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006222 /* these will be inherited by all namespaces */
6223 dflt->autoconf = ipv6_defaults.autoconf;
6224 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006225
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006226 dflt->stable_secret.initialized = false;
6227 all->stable_secret.initialized = false;
6228
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006229 net->ipv6.devconf_all = all;
6230 net->ipv6.devconf_dflt = dflt;
6231
6232#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006233 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006234 if (err < 0)
6235 goto err_reg_all;
6236
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006237 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006238 if (err < 0)
6239 goto err_reg_dflt;
6240#endif
6241 return 0;
6242
6243#ifdef CONFIG_SYSCTL
6244err_reg_dflt:
6245 __addrconf_sysctl_unregister(all);
6246err_reg_all:
6247 kfree(dflt);
6248#endif
6249err_alloc_dflt:
6250 kfree(all);
6251err_alloc_all:
6252 return err;
6253}
6254
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006255static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006256{
6257#ifdef CONFIG_SYSCTL
6258 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
6259 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
6260#endif
zhuyj73cf0e92014-11-26 10:25:58 +08006261 kfree(net->ipv6.devconf_dflt);
6262 kfree(net->ipv6.devconf_all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006263}
6264
6265static struct pernet_operations addrconf_ops = {
6266 .init = addrconf_init_net,
6267 .exit = addrconf_exit_net,
6268};
6269
Daniel Borkmann207895f2015-01-29 12:15:03 +01006270static struct rtnl_af_ops inet6_ops __read_mostly = {
Thomas Grafb382b192010-11-16 04:33:57 +00006271 .family = AF_INET6,
6272 .fill_link_af = inet6_fill_link_af,
6273 .get_link_af_size = inet6_get_link_af_size,
Daniel Borkmann11b1f822015-02-05 14:39:11 +01006274 .validate_link_af = inet6_validate_link_af,
Daniel Borkmannf53adae2013-04-08 04:01:30 +00006275 .set_link_af = inet6_set_link_af,
Thomas Grafb382b192010-11-16 04:33:57 +00006276};
6277
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278/*
6279 * Init / cleanup code
6280 */
6281
6282int __init addrconf_init(void)
6283{
WANG Conga317a2f2014-07-25 15:25:09 -07006284 struct inet6_dev *idev;
stephen hemmingerc2e21292010-03-17 20:31:10 +00006285 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006286
Stephen Hemmingere21e8462010-03-20 16:09:01 -07006287 err = ipv6_addr_label_init();
6288 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00006289 pr_crit("%s: cannot initialize default policy table: %d\n",
6290 __func__, err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006291 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006294 err = register_pernet_subsys(&addrconf_ops);
6295 if (err < 0)
6296 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006297
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006298 addrconf_wq = create_workqueue("ipv6_addrconf");
6299 if (!addrconf_wq) {
6300 err = -ENOMEM;
6301 goto out_nowq;
6302 }
6303
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304 /* The addrconf netdev notifier requires that loopback_dev
6305 * has it's ipv6 private information allocated and setup
6306 * before it can bring up and give link-local addresses
6307 * to other devices which are up.
6308 *
6309 * Unfortunately, loopback_dev is not necessarily the first
6310 * entry in the global dev_base list of net devices. In fact,
6311 * it is likely to be the very last entry on that list.
6312 * So this causes the notifier registry below to try and
6313 * give link-local addresses to all devices besides loopback_dev
6314 * first, then loopback_dev, which cases all the non-loopback_dev
6315 * devices to fail to get a link-local address.
6316 *
6317 * So, as a temporary fix, allocate the ipv6 structure for
6318 * loopback_dev first by hand.
6319 * Longer term, all of the dependencies ipv6 has upon the loopback
6320 * device and it being up should be removed.
6321 */
6322 rtnl_lock();
WANG Conga317a2f2014-07-25 15:25:09 -07006323 idev = ipv6_add_dev(init_net.loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 rtnl_unlock();
WANG Conga317a2f2014-07-25 15:25:09 -07006325 if (IS_ERR(idev)) {
6326 err = PTR_ERR(idev);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006327 goto errlo;
WANG Conga317a2f2014-07-25 15:25:09 -07006328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329
WANG Congecbd3ed2017-05-03 22:07:31 -07006330 ip6_route_init_special_entries();
6331
stephen hemmingerc2e21292010-03-17 20:31:10 +00006332 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6333 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6334
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 register_netdevice_notifier(&ipv6_dev_notf);
6336
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006337 addrconf_verify();
Thomas Grafc127ea22007-03-22 11:58:32 -07006338
stephen hemminger3678a9d2013-12-30 10:41:32 -08006339 rtnl_af_register(&inet6_ops);
Thomas Grafb382b192010-11-16 04:33:57 +00006340
Greg Rosec7ac8672011-06-10 01:27:09 +00006341 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6342 NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006343 if (err < 0)
6344 goto errout;
6345
6346 /* Only the first call to __rtnl_register can fail */
Greg Rosec7ac8672011-06-10 01:27:09 +00006347 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
6348 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
6349 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6350 inet6_dump_ifaddr, NULL);
6351 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6352 inet6_dump_ifmcaddr, NULL);
6353 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6354 inet6_dump_ifacaddr, NULL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +00006355 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
Nicolas Dichtel7a674202013-03-05 23:42:06 +00006356 inet6_netconf_dump_devconf, NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006357
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006358 ipv6_addr_label_rtnl_register();
6359
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07006361errout:
Thomas Grafb382b192010-11-16 04:33:57 +00006362 rtnl_af_unregister(&inet6_ops);
Thomas Grafc127ea22007-03-22 11:58:32 -07006363 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006364errlo:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006365 destroy_workqueue(addrconf_wq);
6366out_nowq:
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006367 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006368out_addrlabel:
6369 ipv6_addr_label_cleanup();
6370out:
Thomas Grafc127ea22007-03-22 11:58:32 -07006371 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372}
6373
Daniel Lezcano09f77092007-12-13 05:34:58 -08006374void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006376 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 int i;
6378
6379 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006380 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006381 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382
6383 rtnl_lock();
6384
Thomas Grafb382b192010-11-16 04:33:57 +00006385 __rtnl_af_unregister(&inet6_ops);
6386
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006387 /* clean dev list */
6388 for_each_netdev(&init_net, dev) {
6389 if (__in6_dev_get(dev) == NULL)
6390 continue;
6391 addrconf_ifdown(dev, 1);
6392 }
6393 addrconf_ifdown(init_net.loopback_dev, 2);
6394
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396 * Check hash table.
6397 */
stephen hemminger5c578ae2010-03-17 20:31:11 +00006398 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00006399 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6400 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578ae2010-03-17 20:31:11 +00006401 spin_unlock_bh(&addrconf_hash_lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006402 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403 rtnl_unlock();
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006404
6405 destroy_workqueue(addrconf_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406}