blob: 965ca8615b5b797ba632ba3565fe02409b7fd851 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
15/*
16 * Changes:
17 *
18 * Janos Farkas : delete timer on ifdown
19 * <chexum@bankinf.banki.hu>
20 * Andi Kleen : kill double kfree on module
21 * unload.
22 * Maciej W. Rozycki : FDDI support
23 * sekiya@USAGI : Don't send too many RS
24 * packets.
25 * yoshfuji@USAGI : Fixed interval between DAD
26 * packets.
27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
28 * address validation timer.
29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
30 * support.
31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
32 * address on a same interface.
33 * YOSHIFUJI Hideaki @USAGI : ARCnet support
34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
35 * seq_file.
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -080036 * YOSHIFUJI Hideaki @USAGI : improved source address
37 * selection; consider scope,
38 * status etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
40
Joe Perchesf3213832012-05-15 14:11:53 +000041#define pr_fmt(fmt) "IPv6: " fmt
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/errno.h>
44#include <linux/types.h>
Ilpo Järvinena0bffff2009-03-21 13:36:17 -070045#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/socket.h>
47#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/net.h>
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +010049#include <linux/inet.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/in6.h>
51#include <linux/netdevice.h>
Thomas Graf18237302006-08-04 23:04:54 -070052#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/if_arp.h>
54#include <linux/if_arcnet.h>
55#include <linux/if_infiniband.h>
56#include <linux/route.h>
57#include <linux/inetdevice.h>
58#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090059#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifdef CONFIG_SYSCTL
61#include <linux/sysctl.h>
62#endif
Randy Dunlap4fc268d2006-01-11 12:17:47 -080063#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/delay.h>
65#include <linux/notifier.h>
Paulo Marques543537b2005-06-23 00:09:02 -070066#include <linux/string.h>
Eric Dumazetddbe5032012-07-18 08:11:12 +000067#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020069#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <net/sock.h>
71#include <net/snmp.h>
72
Alexander Aring5241c2d2015-12-14 20:55:22 +010073#include <net/6lowpan.h>
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +000074#include <net/firewire.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <net/ipv6.h>
76#include <net/protocol.h>
77#include <net/ndisc.h>
78#include <net/ip6_route.h>
79#include <net/addrconf.h>
80#include <net/tcp.h>
81#include <net/ip.h>
Thomas Graf5d620262006-08-15 00:35:02 -070082#include <net/netlink.h>
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -070083#include <net/pkt_sched.h>
David Ahernca254492015-10-12 11:47:10 -070084#include <net/l3mdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#include <linux/if_tunnel.h>
86#include <linux/rtnetlink.h>
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +000087#include <linux/netconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include <linux/random.h>
Stephen Hemmingere21e8462010-03-20 16:09:01 -070089#include <linux/uaccess.h>
Herbert Xu7f7d9a62007-04-24 21:54:09 -070090#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92#include <linux/proc_fs.h>
93#include <linux/seq_file.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040094#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/* Set to 3 to get tracing... */
97#define ACONF_DEBUG 2
98
99#if ACONF_DEBUG >= 3
dingtianhongba3542e2013-08-17 10:27:04 +0800100#define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#else
dingtianhongba3542e2013-08-17 10:27:04 +0800102#define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#endif
104
105#define INFINITY_LIFE_TIME 0xFFFFFFFF
Thomas Graf18a31e12010-11-17 04:12:02 +0000106
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100107#define IPV6_MAX_STRLEN \
108 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
109
Thomas Graf18a31e12010-11-17 04:12:02 +0000110static inline u32 cstamp_delta(unsigned long cstamp)
111{
112 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
113}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700115static inline s32 rfc3315_s14_backoff_init(s32 irt)
116{
117 /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
118 u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
119 do_div(tmp, 1000000);
120 return (s32)tmp;
121}
122
123static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
124{
125 /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
126 u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
127 do_div(tmp, 1000000);
128 if ((s32)tmp > mrt) {
129 /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
130 tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
131 do_div(tmp, 1000000);
132 }
133 return (s32)tmp;
134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#ifdef CONFIG_SYSCTL
WANG Conga317a2f2014-07-25 15:25:09 -0700137static int addrconf_sysctl_register(struct inet6_dev *idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800138static void addrconf_sysctl_unregister(struct inet6_dev *idev);
139#else
WANG Conga317a2f2014-07-25 15:25:09 -0700140static inline int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800141{
WANG Conga317a2f2014-07-25 15:25:09 -0700142 return 0;
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800143}
144
145static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
146{
147}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#endif
149
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200150static void ipv6_regen_rndid(struct inet6_dev *idev);
151static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900153static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static int ipv6_count_addresses(struct inet6_dev *idev);
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +0100155static int ipv6_generate_stable_address(struct in6_addr *addr,
156 u8 dad_count,
157 const struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159/*
160 * Configured unicast address hash table
161 */
stephen hemmingerc2e21292010-03-17 20:31:10 +0000162static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
stephen hemminger5c578ae2010-03-17 20:31:11 +0000163static DEFINE_SPINLOCK(addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100165static void addrconf_verify(void);
166static void addrconf_verify_rtnl(void);
167static void addrconf_verify_work(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100169static struct workqueue_struct *addrconf_wq;
170static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
173static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
174
Jiri Pirko93d9b7d2010-03-10 10:28:56 +0000175static void addrconf_type_change(struct net_device *dev,
176 unsigned long event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static int addrconf_ifdown(struct net_device *dev, int how);
178
Romain Kuntz21caa662013-01-09 21:06:03 +0000179static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
180 int plen,
181 const struct net_device *dev,
182 u32 flags, u32 noflags);
183
David S. Millercf22f9a2012-04-14 21:37:40 -0400184static void addrconf_dad_start(struct inet6_ifaddr *ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100185static void addrconf_dad_work(struct work_struct *w);
Paolo Abeni764d3be2016-11-22 16:57:40 +0100186static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900187static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static void addrconf_rs_timer(unsigned long data);
189static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
190static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
191
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900192static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct prefix_info *pinfo);
David S. Miller3e81c6d2010-03-20 16:18:00 -0700194static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
195 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Adrian Bunk888c8482008-07-22 14:21:58 -0700197static struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 .forwarding = 0,
199 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
200 .mtu6 = IPV6_MIN_MTU,
201 .accept_ra = 1,
202 .accept_redirects = 1,
203 .autoconf = 1,
204 .force_mld_version = 0,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200205 .mldv1_unsolicited_report_interval = 10 * HZ,
206 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 .dad_transmits = 1,
208 .rtr_solicits = MAX_RTR_SOLICITATIONS,
209 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700210 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Ian Morris67ba4152014-08-24 21:53:10 +0100212 .use_tempaddr = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 .temp_valid_lft = TEMP_VALID_LIFETIME,
214 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
215 .regen_max_retry = REGEN_MAX_RETRY,
216 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800218 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700219 .accept_ra_from_local = 0,
Hangbin Liu8013d1d2015-07-30 14:28:42 +0800220 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800221 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800222#ifdef CONFIG_IPV6_ROUTER_PREF
223 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800224 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800225#ifdef CONFIG_IPV6_ROUTE_INFO
226 .accept_ra_rt_info_max_plen = 0,
227#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800228#endif
Lorenzo Colitti9fcb87d2014-03-26 19:35:41 +0900229 .accept_ra_rt_table = 0,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700230 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700231 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900232 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900233 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200234 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700235 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100236 .stable_secret = {
237 .initialized = false,
Erik Kline3985e8a2015-07-22 16:38:25 +0900238 },
239 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400240 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800241 .keep_addr_on_down = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242};
243
Brian Haleyab32ea52006-09-22 14:15:41 -0700244static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 .forwarding = 0,
246 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
247 .mtu6 = IPV6_MIN_MTU,
248 .accept_ra = 1,
249 .accept_redirects = 1,
250 .autoconf = 1,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200251 .force_mld_version = 0,
252 .mldv1_unsolicited_report_interval = 10 * HZ,
253 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 .dad_transmits = 1,
255 .rtr_solicits = MAX_RTR_SOLICITATIONS,
256 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700257 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 .use_tempaddr = 0,
260 .temp_valid_lft = TEMP_VALID_LIFETIME,
261 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
262 .regen_max_retry = REGEN_MAX_RETRY,
263 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800265 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700266 .accept_ra_from_local = 0,
Hangbin Liu8013d1d2015-07-30 14:28:42 +0800267 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800268 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800269#ifdef CONFIG_IPV6_ROUTER_PREF
270 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800271 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800272#ifdef CONFIG_IPV6_ROUTE_INFO
273 .accept_ra_rt_info_max_plen = 0,
274#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800275#endif
Lorenzo Colitti9fcb87d2014-03-26 19:35:41 +0900276 .accept_ra_rt_table = 0,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700277 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700278 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900279 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900280 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200281 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700282 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100283 .stable_secret = {
284 .initialized = false,
285 },
Erik Kline3985e8a2015-07-22 16:38:25 +0900286 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400287 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800288 .keep_addr_on_down = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289};
290
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700291/* Check if a valid qdisc is available */
David S. Miller05297942008-07-08 23:01:27 -0700292static inline bool addrconf_qdisc_ok(const struct net_device *dev)
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700293{
David S. Miller05297942008-07-08 23:01:27 -0700294 return !qdisc_tx_is_noop(dev);
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700295}
296
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200297static void addrconf_del_rs_timer(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200299 if (del_timer(&idev->rs_timer))
300 __in6_dev_put(idev);
301}
302
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100303static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200304{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100305 if (cancel_delayed_work(&ifp->dad_work))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 __in6_ifa_put(ifp);
307}
308
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200309static void addrconf_mod_rs_timer(struct inet6_dev *idev,
310 unsigned long when)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200312 if (!timer_pending(&idev->rs_timer))
313 in6_dev_hold(idev);
314 mod_timer(&idev->rs_timer, jiffies + when);
315}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100317static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
318 unsigned long delay)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200319{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100320 if (!delayed_work_pending(&ifp->dad_work))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200321 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100322 mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700325static int snmp6_alloc_dev(struct inet6_dev *idev)
326{
John Stultz827da442013-10-07 15:51:58 -0700327 int i;
328
WANG Cong698365f2014-05-05 15:55:55 -0700329 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
330 if (!idev->stats.ipv6)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700331 goto err_ip;
John Stultz827da442013-10-07 15:51:58 -0700332
333 for_each_possible_cpu(i) {
334 struct ipstats_mib *addrconf_stats;
WANG Cong698365f2014-05-05 15:55:55 -0700335 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
John Stultz827da442013-10-07 15:51:58 -0700336 u64_stats_init(&addrconf_stats->syncp);
John Stultz827da442013-10-07 15:51:58 -0700337 }
338
339
Eric Dumazetbe281e52011-05-19 01:14:23 +0000340 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
341 GFP_KERNEL);
342 if (!idev->stats.icmpv6dev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700343 goto err_icmp;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000344 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
345 GFP_KERNEL);
346 if (!idev->stats.icmpv6msgdev)
David L Stevens14878f72007-09-16 16:52:35 -0700347 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700348
349 return 0;
350
David L Stevens14878f72007-09-16 16:52:35 -0700351err_icmpmsg:
Eric Dumazetbe281e52011-05-19 01:14:23 +0000352 kfree(idev->stats.icmpv6dev);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700353err_icmp:
WANG Cong698365f2014-05-05 15:55:55 -0700354 free_percpu(idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700355err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700356 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700357}
358
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000359static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 struct inet6_dev *ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700362 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 ASSERT_RTNL();
365
366 if (dev->mtu < IPV6_MIN_MTU)
WANG Conga317a2f2014-07-25 15:25:09 -0700367 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900369 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100370 if (!ndev)
WANG Conga317a2f2014-07-25 15:25:09 -0700371 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Ingo Oeser322f74a2006-03-20 23:01:47 -0800373 rwlock_init(&ndev->lock);
374 ndev->dev = dev;
stephen hemminger502a2ff2010-03-17 20:31:13 +0000375 INIT_LIST_HEAD(&ndev->addr_list);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200376 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
377 (unsigned long)ndev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900378 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100379
380 if (ndev->cnf.stable_secret.initialized)
381 ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
382 else
383 ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64;
384
Ingo Oeser322f74a2006-03-20 23:01:47 -0800385 ndev->cnf.mtu6 = dev->mtu;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800386 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
Ian Morris63159f22015-03-29 14:00:04 +0100387 if (!ndev->nd_parms) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800388 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700389 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800390 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700391 if (ndev->cnf.forwarding)
392 dev_disable_lro(dev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800393 /* We refer to the device */
394 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Ingo Oeser322f74a2006-03-20 23:01:47 -0800396 if (snmp6_alloc_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800397 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000398 "%s: cannot allocate memory for statistics; dev=%s.\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800399 __func__, dev->name);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800400 neigh_parms_release(&nd_tbl, ndev->nd_parms);
Roy Li8603e332011-09-20 15:10:16 -0400401 dev_put(dev);
402 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700403 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Ingo Oeser322f74a2006-03-20 23:01:47 -0800406 if (snmp6_register_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800407 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000408 "%s: cannot create /proc/net/dev_snmp6/%s\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800409 __func__, dev->name);
WANG Conga317a2f2014-07-25 15:25:09 -0700410 goto err_release;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200413 /* One reference from device. */
Ingo Oeser322f74a2006-03-20 23:01:47 -0800414 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900416 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
417 ndev->cnf.accept_dad = -1;
418
Amerigo Wang07a93622012-10-29 16:23:10 +0000419#if IS_ENABLED(CONFIG_IPV6_SIT)
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700420 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
Joe Perchesf3213832012-05-15 14:11:53 +0000421 pr_info("%s: Disabled Multicast RS\n", dev->name);
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700422 ndev->cnf.rtr_solicits = 0;
423 }
424#endif
425
stephen hemminger372e6c82010-03-17 20:31:09 +0000426 INIT_LIST_HEAD(&ndev->tempaddr_list);
Jiri Bohac76506a92016-10-13 18:52:15 +0200427 ndev->desync_factor = U32_MAX;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800428 if ((dev->flags&IFF_LOOPBACK) ||
429 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700430 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700431 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700432 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800433 ndev->cnf.use_tempaddr = -1;
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200434 } else
435 ipv6_regen_rndid(ndev);
David S. Miller5d9efa72013-10-28 20:07:50 -0400436
Daniel Borkmann914faa12013-04-09 03:47:14 +0000437 ndev->token = in6addr_any;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800438
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700439 if (netif_running(dev) && addrconf_qdisc_ok(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700440 ndev->if_flags |= IF_READY;
441
Ingo Oeser322f74a2006-03-20 23:01:47 -0800442 ipv6_mc_init_dev(ndev);
443 ndev->tstamp = jiffies;
WANG Conga317a2f2014-07-25 15:25:09 -0700444 err = addrconf_sysctl_register(ndev);
445 if (err) {
446 ipv6_mc_destroy_dev(ndev);
Sabrina Dubroca2a189f92015-11-04 14:47:53 +0100447 snmp6_unregister_dev(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700448 goto err_release;
449 }
David L Stevens30c4cf52007-01-04 12:31:14 -0800450 /* protected by rtnl_lock */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000451 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800452
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000453 /* Join interface-local all-node multicast group */
454 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
455
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800456 /* Join all-node multicast group */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900457 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800458
Li Weid6ddef92012-03-05 14:45:17 +0000459 /* Join all-router multicast group if forwarding is set */
Li Wei8b2aaed2012-03-07 14:58:07 +0000460 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
Li Weid6ddef92012-03-05 14:45:17 +0000461 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700464
465err_release:
466 neigh_parms_release(&nd_tbl, ndev->nd_parms);
467 ndev->dead = 1;
468 in6_dev_finish_destroy(ndev);
469 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000472static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 struct inet6_dev *idev;
475
476 ASSERT_RTNL();
477
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700478 idev = __in6_dev_get(dev);
479 if (!idev) {
480 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -0700481 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return NULL;
483 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (dev->flags&IFF_UP)
486 ipv6_mc_up(idev);
487 return idev;
488}
489
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000490static int inet6_netconf_msgsize_devconf(int type)
491{
492 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
493 + nla_total_size(4); /* NETCONFA_IFINDEX */
Zhang Shengju136ba622016-03-10 08:55:50 +0000494 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000495
Zhang Shengju136ba622016-03-10 08:55:50 +0000496 if (type == NETCONFA_ALL)
497 all = true;
498
499 if (all || type == NETCONFA_FORWARDING)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000500 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500501#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000502 if (all || type == NETCONFA_MC_FORWARDING)
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000503 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500504#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000505 if (all || type == NETCONFA_PROXY_NEIGH)
stephen hemmingerc92d5492013-12-17 22:37:14 -0800506 size += nla_total_size(4);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000507
Zhang Shengju136ba622016-03-10 08:55:50 +0000508 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
Andy Gospodarek35103d12015-08-13 10:39:01 -0400509 size += nla_total_size(4);
510
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000511 return size;
512}
513
514static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
515 struct ipv6_devconf *devconf, u32 portid,
516 u32 seq, int event, unsigned int flags,
517 int type)
518{
519 struct nlmsghdr *nlh;
520 struct netconfmsg *ncm;
Zhang Shengju136ba622016-03-10 08:55:50 +0000521 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000522
523 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
524 flags);
Ian Morris63159f22015-03-29 14:00:04 +0100525 if (!nlh)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000526 return -EMSGSIZE;
527
Zhang Shengju136ba622016-03-10 08:55:50 +0000528 if (type == NETCONFA_ALL)
529 all = true;
530
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000531 ncm = nlmsg_data(nlh);
532 ncm->ncm_family = AF_INET6;
533
534 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
535 goto nla_put_failure;
536
Zhang Shengju136ba622016-03-10 08:55:50 +0000537 if ((all || type == NETCONFA_FORWARDING) &&
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000538 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
539 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500540#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000541 if ((all || type == NETCONFA_MC_FORWARDING) &&
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000542 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
543 devconf->mc_forwarding) < 0)
544 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500545#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000546 if ((all || type == NETCONFA_PROXY_NEIGH) &&
stephen hemmingerc92d5492013-12-17 22:37:14 -0800547 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
548 goto nla_put_failure;
549
Zhang Shengju136ba622016-03-10 08:55:50 +0000550 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
Andy Gospodarek35103d12015-08-13 10:39:01 -0400551 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
552 devconf->ignore_routes_with_linkdown) < 0)
553 goto nla_put_failure;
554
Johannes Berg053c0952015-01-16 22:09:00 +0100555 nlmsg_end(skb, nlh);
556 return 0;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000557
558nla_put_failure:
559 nlmsg_cancel(skb, nlh);
560 return -EMSGSIZE;
561}
562
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000563void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
564 struct ipv6_devconf *devconf)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000565{
566 struct sk_buff *skb;
567 int err = -ENOBUFS;
568
Eric Dumazet927265b2016-07-08 05:46:04 +0200569 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100570 if (!skb)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000571 goto errout;
572
573 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
574 RTM_NEWNETCONF, 0, type);
575 if (err < 0) {
576 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
577 WARN_ON(err == -EMSGSIZE);
578 kfree_skb(skb);
579 goto errout;
580 }
Eric Dumazet927265b2016-07-08 05:46:04 +0200581 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000582 return;
583errout:
Cong Dingbd779022012-12-18 12:08:56 +0000584 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000585}
586
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000587static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
588 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
589 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
stephen hemmingerc92d5492013-12-17 22:37:14 -0800590 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
Andy Gospodarek35103d12015-08-13 10:39:01 -0400591 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000592};
593
594static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
Thomas Graf661d2962013-03-21 07:45:29 +0000595 struct nlmsghdr *nlh)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000596{
597 struct net *net = sock_net(in_skb->sk);
598 struct nlattr *tb[NETCONFA_MAX+1];
599 struct netconfmsg *ncm;
600 struct sk_buff *skb;
601 struct ipv6_devconf *devconf;
602 struct inet6_dev *in6_dev;
603 struct net_device *dev;
604 int ifindex;
605 int err;
606
607 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
608 devconf_ipv6_policy);
609 if (err < 0)
610 goto errout;
611
Anton Protopopova97eb332016-02-16 21:43:16 -0500612 err = -EINVAL;
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000613 if (!tb[NETCONFA_IFINDEX])
614 goto errout;
615
616 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
617 switch (ifindex) {
618 case NETCONFA_IFINDEX_ALL:
619 devconf = net->ipv6.devconf_all;
620 break;
621 case NETCONFA_IFINDEX_DEFAULT:
622 devconf = net->ipv6.devconf_dflt;
623 break;
624 default:
625 dev = __dev_get_by_index(net, ifindex);
Ian Morris63159f22015-03-29 14:00:04 +0100626 if (!dev)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000627 goto errout;
628 in6_dev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +0100629 if (!in6_dev)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000630 goto errout;
631 devconf = &in6_dev->cnf;
632 break;
633 }
634
635 err = -ENOBUFS;
Zhang Shengju136ba622016-03-10 08:55:50 +0000636 skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +0100637 if (!skb)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000638 goto errout;
639
640 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
641 NETLINK_CB(in_skb).portid,
642 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
Zhang Shengju136ba622016-03-10 08:55:50 +0000643 NETCONFA_ALL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000644 if (err < 0) {
645 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
646 WARN_ON(err == -EMSGSIZE);
647 kfree_skb(skb);
648 goto errout;
649 }
650 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
651errout:
652 return err;
653}
654
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000655static int inet6_netconf_dump_devconf(struct sk_buff *skb,
656 struct netlink_callback *cb)
657{
658 struct net *net = sock_net(skb->sk);
659 int h, s_h;
660 int idx, s_idx;
661 struct net_device *dev;
662 struct inet6_dev *idev;
663 struct hlist_head *head;
664
665 s_h = cb->args[0];
666 s_idx = idx = cb->args[1];
667
668 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
669 idx = 0;
670 head = &net->dev_index_head[h];
671 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000672 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
673 net->dev_base_seq;
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000674 hlist_for_each_entry_rcu(dev, head, index_hlist) {
675 if (idx < s_idx)
676 goto cont;
677 idev = __in6_dev_get(dev);
678 if (!idev)
679 goto cont;
680
681 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
682 &idev->cnf,
683 NETLINK_CB(cb->skb).portid,
684 cb->nlh->nlmsg_seq,
685 RTM_NEWNETCONF,
686 NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000687 NETCONFA_ALL) < 0) {
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000688 rcu_read_unlock();
689 goto done;
690 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000691 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000692cont:
693 idx++;
694 }
695 rcu_read_unlock();
696 }
697 if (h == NETDEV_HASHENTRIES) {
698 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
699 net->ipv6.devconf_all,
700 NETLINK_CB(cb->skb).portid,
701 cb->nlh->nlmsg_seq,
702 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000703 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000704 goto done;
705 else
706 h++;
707 }
708 if (h == NETDEV_HASHENTRIES + 1) {
709 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
710 net->ipv6.devconf_dflt,
711 NETLINK_CB(cb->skb).portid,
712 cb->nlh->nlmsg_seq,
713 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000714 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000715 goto done;
716 else
717 h++;
718 }
719done:
720 cb->args[0] = h;
721 cb->args[1] = idx;
722
723 return skb->len;
724}
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726#ifdef CONFIG_SYSCTL
727static void dev_forward_change(struct inet6_dev *idev)
728{
729 struct net_device *dev;
730 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 if (!idev)
733 return;
734 dev = idev->dev;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700735 if (idev->cnf.forwarding)
736 dev_disable_lro(dev);
Amerigo Wang1a940832012-10-28 17:43:53 +0000737 if (dev->flags & IFF_MULTICAST) {
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000738 if (idev->cnf.forwarding) {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900739 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000740 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
741 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
742 } else {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900743 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000744 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
745 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
stephen hemminger502a2ff2010-03-17 20:31:13 +0000748
749 list_for_each_entry(ifa, &idev->addr_list, if_list) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800750 if (ifa->flags&IFA_F_TENTATIVE)
751 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (idev->cnf.forwarding)
753 addrconf_join_anycast(ifa);
754 else
755 addrconf_leave_anycast(ifa);
756 }
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000757 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
758 dev->ifindex, &idev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
761
Pavel Emelyanove1869322008-01-10 17:43:50 -0800762static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 struct net_device *dev;
765 struct inet6_dev *idev;
766
Ben Hutchings4acd4942012-08-14 08:54:51 +0000767 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 idev = __in6_dev_get(dev);
769 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800770 int changed = (!idev->cnf.forwarding) ^ (!newf);
771 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (changed)
773 dev_forward_change(idev);
774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800777
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000778static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800779{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800780 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000781 int old;
782
783 if (!rtnl_trylock())
784 return restart_syscall();
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800785
786 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000787 old = *p;
788 *p = newf;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800789
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000790 if (p == &net->ipv6.devconf_dflt->forwarding) {
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000791 if ((!newf) ^ (!old))
792 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
793 NETCONFA_IFINDEX_DEFAULT,
794 net->ipv6.devconf_dflt);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000795 rtnl_unlock();
796 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +0000797 }
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000798
Pavel Emelyanove1869322008-01-10 17:43:50 -0800799 if (p == &net->ipv6.devconf_all->forwarding) {
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200800 int old_dflt = net->ipv6.devconf_dflt->forwarding;
801
Pavel Emelyanove1869322008-01-10 17:43:50 -0800802 net->ipv6.devconf_dflt->forwarding = newf;
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200803 if ((!newf) ^ (!old_dflt))
804 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
805 NETCONFA_IFINDEX_DEFAULT,
806 net->ipv6.devconf_dflt);
807
Pavel Emelyanove1869322008-01-10 17:43:50 -0800808 addrconf_forward_change(net, newf);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000809 if ((!newf) ^ (!old))
810 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
811 NETCONFA_IFINDEX_ALL,
812 net->ipv6.devconf_all);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000813 } else if ((!newf) ^ (!old))
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800814 dev_forward_change((struct inet6_dev *)table->extra1);
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700815 rtnl_unlock();
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800816
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000817 if (newf)
Daniel Lezcano7b4da532008-03-04 13:47:14 -0800818 rt6_purge_dflt_routers(net);
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000819 return 1;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800820}
Andy Gospodarek35103d12015-08-13 10:39:01 -0400821
822static void addrconf_linkdown_change(struct net *net, __s32 newf)
823{
824 struct net_device *dev;
825 struct inet6_dev *idev;
826
827 for_each_netdev(net, dev) {
828 idev = __in6_dev_get(dev);
829 if (idev) {
830 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
831
832 idev->cnf.ignore_routes_with_linkdown = newf;
833 if (changed)
834 inet6_netconf_notify_devconf(dev_net(dev),
835 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
836 dev->ifindex,
837 &idev->cnf);
838 }
839 }
840}
841
842static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
843{
844 struct net *net;
845 int old;
846
847 if (!rtnl_trylock())
848 return restart_syscall();
849
850 net = (struct net *)table->extra2;
851 old = *p;
852 *p = newf;
853
854 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
855 if ((!newf) ^ (!old))
856 inet6_netconf_notify_devconf(net,
857 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
858 NETCONFA_IFINDEX_DEFAULT,
859 net->ipv6.devconf_dflt);
860 rtnl_unlock();
861 return 0;
862 }
863
864 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
865 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
866 addrconf_linkdown_change(net, newf);
867 if ((!newf) ^ (!old))
868 inet6_netconf_notify_devconf(net,
869 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
870 NETCONFA_IFINDEX_ALL,
871 net->ipv6.devconf_all);
872 }
873 rtnl_unlock();
874
875 return 1;
876}
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878#endif
879
stephen hemminger5c578ae2010-03-17 20:31:11 +0000880/* Nobody refers to this ifaddr, destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
882{
stephen hemmingerc2e21292010-03-17 20:31:10 +0000883 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885#ifdef NET_REFCNT_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +0000886 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887#endif
888
889 in6_dev_put(ifp->idev);
890
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100891 if (cancel_delayed_work(&ifp->dad_work))
892 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
893 ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Herbert Xue9d3e082010-05-18 15:36:06 -0700895 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
Joe Perchesf3213832012-05-15 14:11:53 +0000896 pr_warn("Freeing alive inet6 address %p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return;
898 }
Amerigo Wang94e187c2012-10-29 00:13:19 +0000899 ip6_rt_put(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Lai Jiangshane5785982011-03-15 18:00:14 +0800901 kfree_rcu(ifp, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Brian Haleye55ffac2006-07-10 15:25:51 -0700904static void
905ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
906{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000907 struct list_head *p;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700908 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700909
910 /*
911 * Each device address list is sorted in order of scope -
912 * global before linklocal.
913 */
stephen hemminger502a2ff2010-03-17 20:31:13 +0000914 list_for_each(p, &idev->addr_list) {
915 struct inet6_ifaddr *ifa
916 = list_entry(p, struct inet6_ifaddr, if_list);
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700917 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700918 break;
919 }
920
David S. Millerb54c9b92010-03-25 21:25:30 -0700921 list_add_tail(&ifp->if_list, p);
Brian Haleye55ffac2006-07-10 15:25:51 -0700922}
923
Eric Dumazetddbe5032012-07-18 08:11:12 +0000924static u32 inet6_addr_hash(const struct in6_addr *addr)
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900925{
Eric Dumazetddbe5032012-07-18 08:11:12 +0000926 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900927}
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929/* On success it returns ifp with increased reference count */
930
931static struct inet6_ifaddr *
Jiri Pirko3f8f5292013-08-01 10:41:27 +0200932ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
933 const struct in6_addr *peer_addr, int pfxlen,
Jiri Benc8a226b22013-08-01 10:41:28 +0200934 int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
936 struct inet6_ifaddr *ifa = NULL;
937 struct rt6_info *rt;
stephen hemminger3a88a812010-03-17 20:31:12 +0000938 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 int err = 0;
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +0900940 int addr_type = ipv6_addr_type(addr);
941
942 if (addr_type == IPV6_ADDR_ANY ||
943 addr_type & IPV6_ADDR_MULTICAST ||
944 (!(idev->dev->flags & IFF_LOOPBACK) &&
945 addr_type & IPV6_ADDR_LOOPBACK))
946 return ERR_PTR(-EADDRNOTAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700948 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (idev->dead) {
950 err = -ENODEV; /*XXX*/
951 goto out2;
952 }
953
Brian Haley56d417b2009-06-01 03:07:33 -0700954 if (idev->cnf.disable_ipv6) {
Brian Haley9bdd8d42009-03-18 18:22:48 -0700955 err = -EACCES;
956 goto out2;
957 }
958
stephen hemminger5c578ae2010-03-17 20:31:11 +0000959 spin_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 /* Ignore adding duplicate addresses on an interface */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900962 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
dingtianhongba3542e2013-08-17 10:27:04 +0800963 ADBG("ipv6_add_addr: already assigned\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 err = -EEXIST;
965 goto out;
966 }
967
Ingo Oeser322f74a2006-03-20 23:01:47 -0800968 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Ian Morris63159f22015-03-29 14:00:04 +0100970 if (!ifa) {
dingtianhongba3542e2013-08-17 10:27:04 +0800971 ADBG("ipv6_add_addr: malloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 err = -ENOBUFS;
973 goto out;
974 }
975
David S. Miller8f031512011-12-06 16:48:14 -0500976 rt = addrconf_dst_alloc(idev, addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (IS_ERR(rt)) {
978 err = PTR_ERR(rt);
979 goto out;
980 }
981
Jiri Pirkobba24892013-12-07 19:26:57 +0100982 neigh_parms_data_state_setall(idev->nd_parms);
983
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000984 ifa->addr = *addr;
Jiri Pirko3f8f5292013-08-01 10:41:27 +0200985 if (peer_addr)
986 ifa->peer_addr = *peer_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 spin_lock_init(&ifa->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100989 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
stephen hemmingerc2e21292010-03-17 20:31:10 +0000990 INIT_HLIST_NODE(&ifa->addr_lst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 ifa->scope = scope;
992 ifa->prefix_len = pfxlen;
993 ifa->flags = flags | IFA_F_TENTATIVE;
Jiri Benc8a226b22013-08-01 10:41:28 +0200994 ifa->valid_lft = valid_lft;
995 ifa->prefered_lft = prefered_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 ifa->cstamp = ifa->tstamp = jiffies;
Daniel Borkmann617fe292013-04-09 03:47:16 +0000997 ifa->tokenized = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Keir Fraser57f5f542006-08-29 02:43:49 -0700999 ifa->rt = rt;
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 ifa->idev = idev;
1002 in6_dev_hold(idev);
1003 /* For caller */
1004 in6_ifa_hold(ifa);
1005
1006 /* Add to big hash table */
Eric Dumazetddbe5032012-07-18 08:11:12 +00001007 hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
stephen hemminger5c578ae2010-03-17 20:31:11 +00001009 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
stephen hemminger5c578ae2010-03-17 20:31:11 +00001010 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 write_lock(&idev->lock);
1013 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -07001014 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (ifa->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001017 list_add(&ifa->tmp_list, &idev->tempaddr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 in6_ifa_hold(ifa);
1019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 in6_ifa_hold(ifa);
1022 write_unlock(&idev->lock);
1023out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001024 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -07001026 if (likely(err == 0))
Cong Wangf88c91d2013-04-14 23:18:43 +08001027 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 else {
1029 kfree(ifa);
1030 ifa = ERR_PTR(err);
1031 }
1032
1033 return ifa;
1034out:
stephen hemminger5c578ae2010-03-17 20:31:11 +00001035 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 goto out2;
1037}
1038
Thomas Haller5b84efe2014-01-15 15:36:59 +01001039enum cleanup_prefix_rt_t {
1040 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1041 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1042 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1043};
1044
1045/*
1046 * Check, whether the prefix for ifp would still need a prefix route
1047 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1048 * constants.
1049 *
1050 * 1) we don't purge prefix if address was not permanent.
1051 * prefix is managed by its own lifetime.
1052 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1053 * 3) if there are no addresses, delete prefix.
1054 * 4) if there are still other permanent address(es),
1055 * corresponding prefix is still permanent.
1056 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1057 * don't purge the prefix, assume user space is managing it.
1058 * 6) otherwise, update prefix lifetime to the
1059 * longest valid lifetime among the corresponding
1060 * addresses on the device.
1061 * Note: subsequent RA will update lifetime.
1062 **/
1063static enum cleanup_prefix_rt_t
1064check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1065{
1066 struct inet6_ifaddr *ifa;
1067 struct inet6_dev *idev = ifp->idev;
1068 unsigned long lifetime;
1069 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1070
1071 *expires = jiffies;
1072
1073 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1074 if (ifa == ifp)
1075 continue;
1076 if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
1077 ifp->prefix_len))
1078 continue;
1079 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1080 return CLEANUP_PREFIX_RT_NOP;
1081
1082 action = CLEANUP_PREFIX_RT_EXPIRE;
1083
1084 spin_lock(&ifa->lock);
1085
1086 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1087 /*
1088 * Note: Because this address is
1089 * not permanent, lifetime <
1090 * LONG_MAX / HZ here.
1091 */
1092 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1093 *expires = ifa->tstamp + lifetime * HZ;
1094 spin_unlock(&ifa->lock);
1095 }
1096
1097 return action;
1098}
1099
1100static void
1101cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
1102{
1103 struct rt6_info *rt;
1104
1105 rt = addrconf_get_prefix_route(&ifp->addr,
1106 ifp->prefix_len,
1107 ifp->idev->dev,
1108 0, RTF_GATEWAY | RTF_DEFAULT);
1109 if (rt) {
1110 if (del_rt)
1111 ip6_del_rt(rt);
1112 else {
1113 if (!(rt->rt6i_flags & RTF_EXPIRES))
1114 rt6_set_expires(rt, expires);
1115 ip6_rt_put(rt);
1116 }
1117 }
1118}
1119
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121/* This function wants to get referenced ifp and releases it before return */
1122
1123static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1124{
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001125 int state;
Thomas Haller5b84efe2014-01-15 15:36:59 +01001126 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1127 unsigned long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001129 ASSERT_RTNL();
1130
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001131 spin_lock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001132 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -07001133 ifp->state = INET6_IFADDR_STATE_DEAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001134 spin_unlock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001135
1136 if (state == INET6_IFADDR_STATE_DEAD)
1137 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
stephen hemminger5c578ae2010-03-17 20:31:11 +00001139 spin_lock_bh(&addrconf_hash_lock);
1140 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578ae2010-03-17 20:31:11 +00001141 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Thomas Haller5b84efe2014-01-15 15:36:59 +01001143 write_lock_bh(&ifp->idev->lock);
David S. Miller5d9efa72013-10-28 20:07:50 -04001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001146 list_del(&ifp->tmp_list);
1147 if (ifp->ifpub) {
1148 in6_ifa_put(ifp->ifpub);
1149 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
stephen hemminger372e6c82010-03-17 20:31:09 +00001151 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Thomas Haller5b84efe2014-01-15 15:36:59 +01001154 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1155 action = check_cleanup_prefix_route(ifp, &expires);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001156
Thomas Haller5b84efe2014-01-15 15:36:59 +01001157 list_del_init(&ifp->if_list);
1158 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Thomas Haller5b84efe2014-01-15 15:36:59 +01001160 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001162 addrconf_del_dad_work(ifp);
Andrey Vaginb2238562008-07-08 15:13:31 -07001163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 ipv6_ifa_notify(RTM_DELADDR, ifp);
1165
Cong Wangf88c91d2013-04-14 23:18:43 +08001166 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Thomas Haller5b84efe2014-01-15 15:36:59 +01001168 if (action != CLEANUP_PREFIX_RT_NOP) {
1169 cleanup_prefix_route(ifp, expires,
1170 action == CLEANUP_PREFIX_RT_DEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172
Daniel Walterc3968a82011-04-13 21:10:57 +00001173 /* clean up prefsrc entries */
1174 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001175out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 in6_ifa_put(ifp);
1177}
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1180{
1181 struct inet6_dev *idev = ifp->idev;
1182 struct in6_addr addr, *tmpaddr;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001183 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001184 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 int tmp_plen;
1186 int ret = 0;
Neil Horman95c385b2007-04-25 17:08:10 -07001187 u32 addr_flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001188 unsigned long now = jiffies;
Jiri Bohac76506a92016-10-13 18:52:15 +02001189 long max_desync_factor;
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001190 s32 cnf_temp_preferred_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Jiri Pirko53bd6742013-12-06 09:45:22 +01001192 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (ift) {
1194 spin_lock_bh(&ift->lock);
1195 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1196 spin_unlock_bh(&ift->lock);
1197 tmpaddr = &addr;
1198 } else {
1199 tmpaddr = NULL;
1200 }
1201retry:
1202 in6_dev_hold(idev);
1203 if (idev->cnf.use_tempaddr <= 0) {
Jiri Pirko53bd6742013-12-06 09:45:22 +01001204 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001205 pr_info("%s: use_tempaddr is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 in6_dev_put(idev);
1207 ret = -1;
1208 goto out;
1209 }
1210 spin_lock_bh(&ifp->lock);
1211 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1212 idev->cnf.use_tempaddr = -1; /*XXX*/
1213 spin_unlock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01001214 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001215 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1216 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 in6_dev_put(idev);
1218 ret = -1;
1219 goto out;
1220 }
1221 in6_ifa_hold(ifp);
1222 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
Jiri Bohac9d6280d2016-10-13 18:50:02 +02001223 ipv6_try_regen_rndid(idev, tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001225 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001227 regen_advance = idev->cnf.regen_max_retry *
Ian Morris67ba4152014-08-24 21:53:10 +01001228 idev->cnf.dad_transmits *
1229 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
Jiri Bohac76506a92016-10-13 18:52:15 +02001230
1231 /* recalculate max_desync_factor each time and update
1232 * idev->desync_factor if it's larger
1233 */
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001234 cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
Jiri Bohac76506a92016-10-13 18:52:15 +02001235 max_desync_factor = min_t(__u32,
1236 idev->cnf.max_desync_factor,
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001237 cnf_temp_preferred_lft - regen_advance);
Jiri Bohac76506a92016-10-13 18:52:15 +02001238
1239 if (unlikely(idev->desync_factor > max_desync_factor)) {
1240 if (max_desync_factor > 0) {
1241 get_random_bytes(&idev->desync_factor,
1242 sizeof(idev->desync_factor));
1243 idev->desync_factor %= max_desync_factor;
1244 } else {
1245 idev->desync_factor = 0;
1246 }
1247 }
1248
1249 tmp_valid_lft = min_t(__u32,
1250 ifp->valid_lft,
1251 idev->cnf.temp_valid_lft + age);
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001252 tmp_prefered_lft = cnf_temp_preferred_lft + age -
Jiri Bohac76506a92016-10-13 18:52:15 +02001253 idev->desync_factor;
Jiri Bohac76506a92016-10-13 18:52:15 +02001254 tmp_prefered_lft = min_t(__u32, ifp->prefered_lft, tmp_prefered_lft);
1255 tmp_plen = ifp->prefix_len;
1256 tmp_tstamp = ifp->tstamp;
1257 spin_unlock_bh(&ifp->lock);
1258
Jiri Pirko53bd6742013-12-06 09:45:22 +01001259 write_unlock_bh(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -07001260
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001261 /* A temporary address is created only if this calculated Preferred
1262 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1263 * an implementation must not create a temporary address with a zero
1264 * Preferred Lifetime.
Heiner Kallweitecab6702014-03-12 22:13:19 +01001265 * Use age calculation as in addrconf_verify to avoid unnecessary
1266 * temporary addresses being generated.
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001267 */
Heiner Kallweitecab6702014-03-12 22:13:19 +01001268 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1269 if (tmp_prefered_lft <= regen_advance + age) {
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001270 in6_ifa_put(ifp);
1271 in6_dev_put(idev);
1272 ret = -1;
1273 goto out;
1274 }
1275
Neil Horman95c385b2007-04-25 17:08:10 -07001276 addr_flags = IFA_F_TEMPORARY;
1277 /* set in addrconf_prefix_rcv() */
1278 if (ifp->flags & IFA_F_OPTIMISTIC)
1279 addr_flags |= IFA_F_OPTIMISTIC;
1280
Hannes Frederic Sowa4b08a8f2013-08-16 13:02:27 +02001281 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1282 ipv6_addr_scope(&addr), addr_flags,
1283 tmp_valid_lft, tmp_prefered_lft);
1284 if (IS_ERR(ift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 in6_ifa_put(ifp);
1286 in6_dev_put(idev);
Joe Perchesf3213832012-05-15 14:11:53 +00001287 pr_info("%s: retry temporary address regeneration\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 tmpaddr = &addr;
Jiri Pirko53bd6742013-12-06 09:45:22 +01001289 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 goto retry;
1291 }
1292
1293 spin_lock_bh(&ift->lock);
1294 ift->ifpub = ifp;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001295 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 ift->tstamp = tmp_tstamp;
1297 spin_unlock_bh(&ift->lock);
1298
David S. Millercf22f9a2012-04-14 21:37:40 -04001299 addrconf_dad_start(ift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 in6_ifa_put(ift);
1301 in6_dev_put(idev);
1302out:
1303 return ret;
1304}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001307 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001309enum {
1310 IPV6_SADDR_RULE_INIT = 0,
1311 IPV6_SADDR_RULE_LOCAL,
1312 IPV6_SADDR_RULE_SCOPE,
1313 IPV6_SADDR_RULE_PREFERRED,
1314#ifdef CONFIG_IPV6_MIP6
1315 IPV6_SADDR_RULE_HOA,
1316#endif
1317 IPV6_SADDR_RULE_OIF,
1318 IPV6_SADDR_RULE_LABEL,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001319 IPV6_SADDR_RULE_PRIVACY,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001320 IPV6_SADDR_RULE_ORCHID,
1321 IPV6_SADDR_RULE_PREFIX,
Erik Kline7fd25612014-10-28 18:11:14 +09001322#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1323 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1324#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001325 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001326};
1327
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001328struct ipv6_saddr_score {
1329 int rule;
1330 int addr_type;
1331 struct inet6_ifaddr *ifa;
1332 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1333 int scopedist;
1334 int matchlen;
1335};
1336
1337struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001338 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001339 int ifindex;
1340 int scope;
1341 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001342 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001343};
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001344
Dave Jonesb6f99a22007-03-22 12:27:49 -07001345static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Ulrich Weber45bb0062010-02-25 23:28:58 +00001347 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001348 return 1;
1349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
Erik Kline7fd25612014-10-28 18:11:14 +09001352static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
1353{
1354#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1355 return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
1356#else
1357 return false;
1358#endif
1359}
1360
Benjamin Thery3de23252008-05-28 14:51:24 +02001361static int ipv6_get_saddr_eval(struct net *net,
1362 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001363 struct ipv6_saddr_dst *dst,
1364 int i)
1365{
1366 int ret;
1367
1368 if (i <= score->rule) {
1369 switch (i) {
1370 case IPV6_SADDR_RULE_SCOPE:
1371 ret = score->scopedist;
1372 break;
1373 case IPV6_SADDR_RULE_PREFIX:
1374 ret = score->matchlen;
1375 break;
1376 default:
1377 ret = !!test_bit(i, score->scorebits);
1378 }
1379 goto out;
1380 }
1381
1382 switch (i) {
1383 case IPV6_SADDR_RULE_INIT:
1384 /* Rule 0: remember if hiscore is not ready yet */
1385 ret = !!score->ifa;
1386 break;
1387 case IPV6_SADDR_RULE_LOCAL:
1388 /* Rule 1: Prefer same address */
1389 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1390 break;
1391 case IPV6_SADDR_RULE_SCOPE:
1392 /* Rule 2: Prefer appropriate scope
1393 *
1394 * ret
1395 * ^
1396 * -1 | d 15
1397 * ---+--+-+---> scope
1398 * |
1399 * | d is scope of the destination.
1400 * B-d | \
1401 * | \ <- smaller scope is better if
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08001402 * B-15 | \ if scope is enough for destination.
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001403 * | ret = B - scope (-1 <= scope >= d <= 15).
1404 * d-C-1 | /
1405 * |/ <- greater is better
1406 * -C / if scope is not enough for destination.
1407 * /| ret = scope - C (-1 <= d < scope <= 15).
1408 *
1409 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1410 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1411 * Assume B = 0 and we get C > 29.
1412 */
1413 ret = __ipv6_addr_src_scope(score->addr_type);
1414 if (ret >= dst->scope)
1415 ret = -ret;
1416 else
1417 ret -= 128; /* 30 is enough */
1418 score->scopedist = ret;
1419 break;
1420 case IPV6_SADDR_RULE_PREFERRED:
Erik Kline7fd25612014-10-28 18:11:14 +09001421 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001422 /* Rule 3: Avoid deprecated and optimistic addresses */
Erik Kline7fd25612014-10-28 18:11:14 +09001423 u8 avoid = IFA_F_DEPRECATED;
1424
1425 if (!ipv6_use_optimistic_addr(score->ifa->idev))
1426 avoid |= IFA_F_OPTIMISTIC;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001427 ret = ipv6_saddr_preferred(score->addr_type) ||
Erik Kline7fd25612014-10-28 18:11:14 +09001428 !(score->ifa->flags & avoid);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001429 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001430 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001431#ifdef CONFIG_IPV6_MIP6
1432 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001433 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001434 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001435 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1436 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001437 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001438 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001439#endif
1440 case IPV6_SADDR_RULE_OIF:
1441 /* Rule 5: Prefer outgoing interface */
1442 ret = (!dst->ifindex ||
1443 dst->ifindex == score->ifa->idev->dev->ifindex);
1444 break;
1445 case IPV6_SADDR_RULE_LABEL:
1446 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001447 ret = ipv6_addr_label(net,
1448 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001449 score->ifa->idev->dev->ifindex) == dst->label;
1450 break;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001451 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001452 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001453 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001454 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001455 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001456 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1457 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1458 score->ifa->idev->cnf.use_tempaddr >= 2;
1459 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001460 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001461 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001462 case IPV6_SADDR_RULE_ORCHID:
1463 /* Rule 8-: Prefer ORCHID vs ORCHID or
1464 * non-ORCHID vs non-ORCHID
1465 */
1466 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1467 ipv6_addr_orchid(dst->addr));
1468 break;
1469 case IPV6_SADDR_RULE_PREFIX:
1470 /* Rule 8: Use longest matching prefix */
YOSHIFUJI Hideaki / 吉藤英明91b4b042012-09-10 18:41:07 +00001471 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1472 if (ret > score->ifa->prefix_len)
1473 ret = score->ifa->prefix_len;
1474 score->matchlen = ret;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001475 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001476#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1477 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1478 /* Optimistic addresses still have lower precedence than other
1479 * preferred addresses.
1480 */
1481 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1482 break;
1483#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001484 default:
1485 ret = 0;
1486 }
1487
1488 if (ret)
1489 __set_bit(i, score->scorebits);
1490 score->rule = i;
1491out:
1492 return ret;
1493}
1494
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001495static int __ipv6_dev_get_saddr(struct net *net,
1496 struct ipv6_saddr_dst *dst,
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001497 struct inet6_dev *idev,
1498 struct ipv6_saddr_score *scores,
1499 int hiscore_idx)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001500{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001501 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001502
1503 read_lock_bh(&idev->lock);
1504 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1505 int i;
1506
1507 /*
1508 * - Tentative Address (RFC2462 section 5.4)
1509 * - A tentative address is not considered
1510 * "assigned to an interface" in the traditional
1511 * sense, unless it is also flagged as optimistic.
1512 * - Candidate Source Address (section 4)
1513 * - In any case, anycast addresses, multicast
1514 * addresses, and the unspecified address MUST
1515 * NOT be included in a candidate set.
1516 */
1517 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1518 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1519 continue;
1520
1521 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1522
1523 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1524 score->addr_type & IPV6_ADDR_MULTICAST)) {
1525 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1526 idev->dev->name);
1527 continue;
1528 }
1529
1530 score->rule = -1;
1531 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1532
1533 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1534 int minihiscore, miniscore;
1535
1536 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1537 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1538
1539 if (minihiscore > miniscore) {
1540 if (i == IPV6_SADDR_RULE_SCOPE &&
1541 score->scopedist > 0) {
1542 /*
1543 * special case:
1544 * each remaining entry
1545 * has too small (not enough)
1546 * scope, because ifa entries
1547 * are sorted by their scope
1548 * values.
1549 */
1550 goto out;
1551 }
1552 break;
1553 } else if (minihiscore < miniscore) {
1554 if (hiscore->ifa)
1555 in6_ifa_put(hiscore->ifa);
1556
1557 in6_ifa_hold(score->ifa);
1558
1559 swap(hiscore, score);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001560 hiscore_idx = 1 - hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001561
1562 /* restore our iterator */
1563 score->ifa = hiscore->ifa;
1564
1565 break;
1566 }
1567 }
1568 }
1569out:
1570 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001571 return hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001572}
1573
David Ahernafbac6012016-06-16 16:24:26 -07001574static int ipv6_get_saddr_master(struct net *net,
1575 const struct net_device *dst_dev,
1576 const struct net_device *master,
1577 struct ipv6_saddr_dst *dst,
1578 struct ipv6_saddr_score *scores,
1579 int hiscore_idx)
1580{
1581 struct inet6_dev *idev;
1582
1583 idev = __in6_dev_get(dst_dev);
1584 if (idev)
1585 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1586 scores, hiscore_idx);
1587
1588 idev = __in6_dev_get(master);
1589 if (idev)
1590 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1591 scores, hiscore_idx);
1592
1593 return hiscore_idx;
1594}
1595
Patrick McHardyb3f644f2012-08-26 19:14:14 +02001596int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001597 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001598 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001600 struct ipv6_saddr_score scores[2], *hiscore;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001601 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001602 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001603 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001604 int dst_type;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001605 bool use_oif_addr = false;
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001606 int hiscore_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001608 dst_type = __ipv6_addr_type(daddr);
1609 dst.addr = daddr;
1610 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1611 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001612 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001613 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001614
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001615 scores[hiscore_idx].rule = -1;
1616 scores[hiscore_idx].ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001618 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001619
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001620 /* Candidate Source Address (section 4)
1621 * - multicast and link-local destination address,
1622 * the set of candidate source address MUST only
1623 * include addresses assigned to interfaces
1624 * belonging to the same link as the outgoing
1625 * interface.
1626 * (- For site-local destination addresses, the
1627 * set of candidate source addresses MUST only
1628 * include addresses assigned to interfaces
1629 * belonging to the same site as the outgoing
1630 * interface.)
Erik Kline3985e8a2015-07-22 16:38:25 +09001631 * - "It is RECOMMENDED that the candidate source addresses
1632 * be the set of unicast addresses assigned to the
1633 * interface that will be used to send to the destination
1634 * (the 'outgoing' interface)." (RFC 6724)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001635 */
1636 if (dst_dev) {
Erik Kline3985e8a2015-07-22 16:38:25 +09001637 idev = __in6_dev_get(dst_dev);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001638 if ((dst_type & IPV6_ADDR_MULTICAST) ||
Erik Kline3985e8a2015-07-22 16:38:25 +09001639 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1640 (idev && idev->cnf.use_oif_addrs_only)) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001641 use_oif_addr = true;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001642 }
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001643 }
1644
1645 if (use_oif_addr) {
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001646 if (idev)
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001647 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001648 } else {
David Ahernafbac6012016-06-16 16:24:26 -07001649 const struct net_device *master;
1650 int master_idx = 0;
1651
1652 /* if dst_dev exists and is enslaved to an L3 device, then
1653 * prefer addresses from dst_dev and then the master over
1654 * any other enslaved devices in the L3 domain.
1655 */
1656 master = l3mdev_master_dev_rcu(dst_dev);
1657 if (master) {
1658 master_idx = master->ifindex;
1659
1660 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1661 master, &dst,
1662 scores, hiscore_idx);
1663
1664 if (scores[hiscore_idx].ifa)
1665 goto out;
1666 }
1667
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001668 for_each_netdev_rcu(net, dev) {
David Ahernafbac6012016-06-16 16:24:26 -07001669 /* only consider addresses on devices in the
1670 * same L3 domain
1671 */
1672 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1673 continue;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001674 idev = __in6_dev_get(dev);
1675 if (!idev)
1676 continue;
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001677 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001678 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001679 }
David Ahernafbac6012016-06-16 16:24:26 -07001680
1681out:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001682 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001684 hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001685 if (!hiscore->ifa)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001686 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001687
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001688 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001689 in6_ifa_put(hiscore->ifa);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001690 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001692EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Amerigo Wang89657792013-06-29 21:30:49 +08001694int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001695 u32 banned_flags)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001696{
1697 struct inet6_ifaddr *ifp;
1698 int err = -EADDRNOTAVAIL;
1699
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01001700 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1701 if (ifp->scope > IFA_LINK)
1702 break;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001703 if (ifp->scope == IFA_LINK &&
1704 !(ifp->flags & banned_flags)) {
1705 *addr = ifp->addr;
1706 err = 0;
1707 break;
1708 }
1709 }
1710 return err;
1711}
1712
Neil Horman95c385b2007-04-25 17:08:10 -07001713int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001714 u32 banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
1716 struct inet6_dev *idev;
1717 int err = -EADDRNOTAVAIL;
1718
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001719 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001720 idev = __in6_dev_get(dev);
1721 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 read_lock_bh(&idev->lock);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001723 err = __ipv6_get_lladdr(idev, addr, banned_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 read_unlock_bh(&idev->lock);
1725 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001726 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return err;
1728}
1729
1730static int ipv6_count_addresses(struct inet6_dev *idev)
1731{
1732 int cnt = 0;
1733 struct inet6_ifaddr *ifp;
1734
1735 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001736 list_for_each_entry(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 cnt++;
1738 read_unlock_bh(&idev->lock);
1739 return cnt;
1740}
1741
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001742int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Florian Westphal2a7851b2013-05-17 03:56:10 +00001743 const struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
Erik Klinec58da4c2015-02-04 20:01:23 +09001745 return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1746}
1747EXPORT_SYMBOL(ipv6_chk_addr);
1748
1749int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1750 const struct net_device *dev, int strict,
1751 u32 banned_flags)
1752{
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001753 struct inet6_ifaddr *ifp;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001754 unsigned int hash = inet6_addr_hash(addr);
Erik Klinec58da4c2015-02-04 20:01:23 +09001755 u32 ifp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
stephen hemminger5c578ae2010-03-17 20:31:11 +00001757 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001758 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001759 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001760 continue;
Erik Klinec58da4c2015-02-04 20:01:23 +09001761 /* Decouple optimistic from tentative for evaluation here.
1762 * Ban optimistic addresses explicitly, when required.
1763 */
1764 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1765 ? (ifp->flags&~IFA_F_TENTATIVE)
1766 : ifp->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (ipv6_addr_equal(&ifp->addr, addr) &&
Erik Klinec58da4c2015-02-04 20:01:23 +09001768 !(ifp_flags&banned_flags) &&
Ian Morris63159f22015-03-29 14:00:04 +01001769 (!dev || ifp->idev->dev == dev ||
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001770 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1771 rcu_read_unlock_bh();
1772 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001775
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001776 rcu_read_unlock_bh();
1777 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
Erik Klinec58da4c2015-02-04 20:01:23 +09001779EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001780
David S. Miller3e81c6d2010-03-20 16:18:00 -07001781static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1782 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Eric Dumazetddbe5032012-07-18 08:11:12 +00001784 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001785 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Sasha Levinb67bfe02013-02-27 17:06:00 -08001787 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001788 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001789 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001791 if (!dev || ifp->idev->dev == dev)
David S. Miller3e81c6d2010-03-20 16:18:00 -07001792 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794 }
David S. Miller3e81c6d2010-03-20 16:18:00 -07001795 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
1797
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001798/* Compares an address/prefix_len with addresses on device @dev.
1799 * If one is found it returns true.
1800 */
1801bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1802 const unsigned int prefix_len, struct net_device *dev)
1803{
1804 struct inet6_dev *idev;
1805 struct inet6_ifaddr *ifa;
1806 bool ret = false;
1807
1808 rcu_read_lock();
1809 idev = __in6_dev_get(dev);
1810 if (idev) {
1811 read_lock_bh(&idev->lock);
1812 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1813 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1814 if (ret)
1815 break;
1816 }
1817 read_unlock_bh(&idev->lock);
1818 }
1819 rcu_read_unlock();
1820
1821 return ret;
1822}
1823EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1824
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001825int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001826{
1827 struct inet6_dev *idev;
1828 struct inet6_ifaddr *ifa;
1829 int onlink;
1830
1831 onlink = 0;
1832 rcu_read_lock();
1833 idev = __in6_dev_get(dev);
1834 if (idev) {
1835 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001836 list_for_each_entry(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001837 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1838 ifa->prefix_len);
1839 if (onlink)
1840 break;
1841 }
1842 read_unlock_bh(&idev->lock);
1843 }
1844 rcu_read_unlock();
1845 return onlink;
1846}
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001847EXPORT_SYMBOL(ipv6_chk_prefix);
1848
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001849struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001850 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
David S. Millerb79d1d52010-03-25 21:39:21 -07001852 struct inet6_ifaddr *ifp, *result = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001853 unsigned int hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
stephen hemminger5c578ae2010-03-17 20:31:11 +00001855 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001856 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001857 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001858 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001860 if (!dev || ifp->idev->dev == dev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07001862 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 in6_ifa_hold(ifp);
1864 break;
1865 }
1866 }
1867 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001868 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
David S. Millerb79d1d52010-03-25 21:39:21 -07001870 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871}
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873/* Gets referenced address, destroys ifaddr */
1874
Brian Haleycc411d02009-09-09 14:41:32 +00001875static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Lubomir Rintel3d171f32016-01-08 13:47:23 +01001877 if (dad_failed)
1878 ifp->flags |= IFA_F_DADFAILED;
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 if (ifp->flags&IFA_F_PERMANENT) {
1881 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001882 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 ifp->flags |= IFA_F_TENTATIVE;
1884 spin_unlock_bh(&ifp->lock);
Herbert Xue2577a02010-03-13 12:23:29 -08001885 if (dad_failed)
1886 ipv6_ifa_notify(0, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 } else if (ifp->flags&IFA_F_TEMPORARY) {
1889 struct inet6_ifaddr *ifpub;
1890 spin_lock_bh(&ifp->lock);
1891 ifpub = ifp->ifpub;
1892 if (ifpub) {
1893 in6_ifa_hold(ifpub);
1894 spin_unlock_bh(&ifp->lock);
1895 ipv6_create_tempaddr(ifpub, ifp);
1896 in6_ifa_put(ifpub);
1897 } else {
1898 spin_unlock_bh(&ifp->lock);
1899 }
1900 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001901 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904}
1905
Herbert Xuf2344a12010-05-18 15:55:27 -07001906static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1907{
1908 int err = -ENOENT;
1909
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001910 spin_lock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001911 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1912 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1913 err = 0;
1914 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001915 spin_unlock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001916
1917 return err;
1918}
1919
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001920void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1921{
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001922 struct inet6_dev *idev = ifp->idev;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001923 struct net *net = dev_net(ifp->idev->dev);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001924
Ursula Braun853dc2e2010-10-24 23:06:43 +00001925 if (addrconf_dad_end(ifp)) {
1926 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07001927 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00001928 }
Herbert Xuf2344a12010-05-18 15:55:27 -07001929
Joe Perchese87cc472012-05-13 21:56:26 +00001930 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1931 ifp->idev->dev->name, &ifp->addr);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001932
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001933 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001934
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001935 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
1936 int scope = ifp->scope;
1937 u32 flags = ifp->flags;
1938 struct in6_addr new_addr;
1939 struct inet6_ifaddr *ifp2;
1940 u32 valid_lft, preferred_lft;
1941 int pfxlen = ifp->prefix_len;
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001942 int retries = ifp->stable_privacy_retry + 1;
1943
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001944 if (retries > net->ipv6.sysctl.idgen_retries) {
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001945 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
1946 ifp->idev->dev->name);
1947 goto errdad;
1948 }
1949
1950 new_addr = ifp->addr;
1951 if (ipv6_generate_stable_address(&new_addr, retries,
1952 idev))
1953 goto errdad;
1954
1955 valid_lft = ifp->valid_lft;
1956 preferred_lft = ifp->prefered_lft;
1957
1958 spin_unlock_bh(&ifp->lock);
1959
1960 if (idev->cnf.max_addresses &&
1961 ipv6_count_addresses(idev) >=
1962 idev->cnf.max_addresses)
1963 goto lock_errdad;
1964
1965 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
1966 ifp->idev->dev->name);
1967
1968 ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
1969 scope, flags, valid_lft,
1970 preferred_lft);
1971 if (IS_ERR(ifp2))
1972 goto lock_errdad;
1973
1974 spin_lock_bh(&ifp2->lock);
1975 ifp2->stable_privacy_retry = retries;
1976 ifp2->state = INET6_IFADDR_STATE_PREDAD;
1977 spin_unlock_bh(&ifp2->lock);
1978
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001979 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001980 in6_ifa_put(ifp2);
1981lock_errdad:
1982 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001983 }
1984
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001985errdad:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001986 /* transition from _POSTDAD to _ERRDAD */
1987 ifp->state = INET6_IFADDR_STATE_ERRDAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001988 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001989
1990 addrconf_mod_dad_work(ifp, 0);
Wei Yongjun751eb6b2016-09-05 16:06:31 +08001991 in6_ifa_put(ifp);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001992}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02001994/* Join to solicited addr multicast group.
1995 * caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001996void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 struct in6_addr maddr;
1999
2000 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2001 return;
2002
2003 addrconf_addr_solict_mult(addr, &maddr);
2004 ipv6_dev_mc_inc(dev, &maddr);
2005}
2006
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002007/* caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002008void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
2010 struct in6_addr maddr;
2011
2012 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2013 return;
2014
2015 addrconf_addr_solict_mult(addr, &maddr);
2016 __ipv6_dev_mc_dec(idev, &maddr);
2017}
2018
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002019/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002020static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
2022 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002023
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002024 if (ifp->prefix_len >= 127) /* RFC 6164 */
Bjørn Mork2bda8a02011-07-05 23:04:13 +00002025 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2027 if (ipv6_addr_any(&addr))
2028 return;
WANG Cong013b4d92014-09-11 15:35:11 -07002029 __ipv6_dev_ac_inc(ifp->idev, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002032/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002033static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
2035 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002036
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002037 if (ifp->prefix_len >= 127) /* RFC 6164 */
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00002038 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2040 if (ipv6_addr_any(&addr))
2041 return;
2042 __ipv6_dev_ac_dec(ifp->idev, &addr);
2043}
2044
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002045static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
2046{
Alexander Aring5241c2d2015-12-14 20:55:22 +01002047 if (dev->addr_len != EUI64_ADDR_LEN)
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002048 return -1;
Alexander Aring5241c2d2015-12-14 20:55:22 +01002049 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
YOSHIFUJI Hideaki / 吉藤英明5e98a362013-01-28 10:44:29 +00002050 eui[0] ^= 2;
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002051 return 0;
2052}
2053
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002054static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2055{
2056 union fwnet_hwaddr *ha;
2057
2058 if (dev->addr_len != FWNET_ALEN)
2059 return -1;
2060
2061 ha = (union fwnet_hwaddr *)dev->dev_addr;
2062
2063 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2064 eui[0] ^= 2;
2065 return 0;
2066}
2067
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002068static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2069{
2070 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2071 if (dev->addr_len != ARCNET_ALEN)
2072 return -1;
2073 memset(eui, 0, 7);
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002074 eui[7] = *(u8 *)dev->dev_addr;
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002075 return 0;
2076}
2077
2078static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2079{
2080 if (dev->addr_len != INFINIBAND_ALEN)
2081 return -1;
2082 memcpy(eui, dev->dev_addr + 12, 8);
2083 eui[0] |= 2;
2084 return 0;
2085}
2086
stephen hemmingerc61393e2010-10-04 20:17:53 +00002087static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002088{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00002089 if (addr == 0)
2090 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002091 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2092 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2093 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2094 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2095 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2096 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2097 eui[1] = 0;
2098 eui[2] = 0x5E;
2099 eui[3] = 0xFE;
2100 memcpy(eui + 4, &addr, 4);
2101 return 0;
2102}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002103
2104static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2105{
2106 if (dev->priv_flags & IFF_ISATAP)
2107 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2108 return -1;
2109}
2110
stephen hemmingeraee80b52011-06-08 10:44:30 +00002111static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2112{
2113 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2114}
2115
Nicolas Dichtele8377352013-08-20 12:16:06 +02002116static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2117{
2118 memcpy(eui, dev->perm_addr, 3);
2119 memcpy(eui + 5, dev->perm_addr + 3, 3);
2120 eui[3] = 0xFF;
2121 eui[4] = 0xFE;
2122 eui[0] ^= 2;
2123 return 0;
2124}
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2127{
2128 switch (dev->type) {
2129 case ARPHRD_ETHER:
2130 case ARPHRD_FDDI:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002131 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002133 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002135 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002136 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002137 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002138 case ARPHRD_IPGRE:
2139 return addrconf_ifid_gre(eui, dev);
Jukka Rissanene74bccb2013-12-11 17:05:36 +02002140 case ARPHRD_6LOWPAN:
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002141 return addrconf_ifid_eui64(eui, dev);
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002142 case ARPHRD_IEEE1394:
2143 return addrconf_ifid_ieee1394(eui, dev);
Nicolas Dichtele8377352013-08-20 12:16:06 +02002144 case ARPHRD_TUNNEL6:
2145 return addrconf_ifid_ip6tnl(eui, dev);
Harout Hedeshian4ec051032015-06-24 15:16:34 -06002146 case ARPHRD_RAWIP: {
2147 struct in6_addr lladdr;
2148
2149 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
2150 get_random_bytes(eui, 8);
2151 else
2152 memcpy(eui, lladdr.s6_addr + 8, 8);
2153
Subash Abhinov Kasiviswanathan54948002010-09-15 13:08:00 -06002154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
Harout Hedeshian4ec051032015-06-24 15:16:34 -06002156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 return -1;
2158}
2159
2160static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2161{
2162 int err = -1;
2163 struct inet6_ifaddr *ifp;
2164
2165 read_lock_bh(&idev->lock);
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01002166 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2167 if (ifp->scope > IFA_LINK)
2168 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2170 memcpy(eui, ifp->addr.s6_addr+8, 8);
2171 err = 0;
2172 break;
2173 }
2174 }
2175 read_unlock_bh(&idev->lock);
2176 return err;
2177}
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002180static void ipv6_regen_rndid(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08002183 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186 /*
2187 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2188 * check if generated address is not inappropriate
2189 *
2190 * - Reserved subnet anycast (RFC 2526)
2191 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002192 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 * 00-00-5E-FE-xx-xx-xx-xx
2194 * - value 0
2195 * - XXX: already assigned to an address on the device
2196 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002197 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2199 (idev->rndid[7]&0x80))
2200 goto regen;
2201 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2202 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2203 goto regen;
2204 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2205 goto regen;
2206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207}
2208
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002209static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002210{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002212 ipv6_regen_rndid(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Lorenzo Colitti9fcb87d2014-03-26 19:35:41 +09002215u32 addrconf_rt_table(const struct net_device *dev, u32 default_table) {
2216 /* Determines into what table to put autoconf PIO/RIO/default routes
2217 * learned on this device.
2218 *
2219 * - If 0, use the same table for every device. This puts routes into
2220 * one of RT_TABLE_{PREFIX,INFO,DFLT} depending on the type of route
2221 * (but note that these three are currently all equal to
2222 * RT6_TABLE_MAIN).
2223 * - If > 0, use the specified table.
2224 * - If < 0, put routes into table dev->ifindex + (-rt_table).
2225 */
2226 struct inet6_dev *idev = in6_dev_get(dev);
2227 u32 table;
2228 int sysctl = idev->cnf.accept_ra_rt_table;
2229 if (sysctl == 0) {
2230 table = default_table;
2231 } else if (sysctl > 0) {
2232 table = (u32) sysctl;
2233 } else {
2234 table = (unsigned) dev->ifindex + (-sysctl);
2235 }
2236 in6_dev_put(idev);
2237 return table;
2238}
2239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240/*
2241 * Add prefix route.
2242 */
2243
2244static void
2245addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002246 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
Thomas Graf86872cb2006-08-22 00:01:08 -07002248 struct fib6_config cfg = {
Lorenzo Colitti9fcb87d2014-03-26 19:35:41 +09002249 .fc_table = l3mdev_fib_table(dev) ? : addrconf_rt_table(dev, RT6_TABLE_PREFIX),
Thomas Graf86872cb2006-08-22 00:01:08 -07002250 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2251 .fc_ifindex = dev->ifindex,
2252 .fc_expires = expires,
2253 .fc_dst_len = plen,
2254 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002255 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07002256 .fc_protocol = RTPROT_KERNEL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002257 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002259 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 /* Prevent useless cloning on PtP SIT.
2262 This thing is done here expecting that the whole
2263 class of non-broadcast devices need not cloning.
2264 */
Amerigo Wang07a93622012-10-29 16:23:10 +00002265#if IS_ENABLED(CONFIG_IPV6_SIT)
Thomas Graf86872cb2006-08-22 00:01:08 -07002266 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2267 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002268#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Thomas Graf86872cb2006-08-22 00:01:08 -07002270 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271}
2272
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002273
2274static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2275 int plen,
2276 const struct net_device *dev,
2277 u32 flags, u32 noflags)
2278{
2279 struct fib6_node *fn;
2280 struct rt6_info *rt = NULL;
2281 struct fib6_table *table;
Lorenzo Colitti9fcb87d2014-03-26 19:35:41 +09002282 u32 tb_id = l3mdev_fib_table(dev) ? : addrconf_rt_table(dev, RT6_TABLE_PREFIX);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002283
David Ahernca254492015-10-12 11:47:10 -07002284 table = fib6_get_table(dev_net(dev), tb_id);
Ian Morris63159f22015-03-29 14:00:04 +01002285 if (!table)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002286 return NULL;
2287
Li RongQing5744dd92012-09-11 21:59:01 +00002288 read_lock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002289 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2290 if (!fn)
2291 goto out;
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07002292
2293 noflags |= RTF_CACHE;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002294 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002295 if (rt->dst.dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002296 continue;
2297 if ((rt->rt6i_flags & flags) != flags)
2298 continue;
Romain Kuntz85da53b2013-01-09 15:02:26 +01002299 if ((rt->rt6i_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002300 continue;
2301 dst_hold(&rt->dst);
2302 break;
2303 }
2304out:
Li RongQing5744dd92012-09-11 21:59:01 +00002305 read_unlock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002306 return rt;
2307}
2308
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310/* Create "default" multicast route to the interface */
2311
2312static void addrconf_add_mroute(struct net_device *dev)
2313{
Thomas Graf86872cb2006-08-22 00:01:08 -07002314 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002315 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002316 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2317 .fc_ifindex = dev->ifindex,
2318 .fc_dst_len = 8,
2319 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002320 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002321 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Thomas Graf86872cb2006-08-22 00:01:08 -07002323 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2324
2325 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326}
2327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2329{
2330 struct inet6_dev *idev;
2331
2332 ASSERT_RTNL();
2333
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002334 idev = ipv6_find_idev(dev);
2335 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00002336 return ERR_PTR(-ENOBUFS);
2337
2338 if (idev->cnf.disable_ipv6)
2339 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
2341 /* Add default multicast route */
David Ahernba46ee42016-06-13 13:44:18 -07002342 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
Li Wei4af04ab2011-12-06 21:23:45 +00002343 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 return idev;
2346}
2347
Jiri Pirko53bd6742013-12-06 09:45:22 +01002348static void manage_tempaddrs(struct inet6_dev *idev,
2349 struct inet6_ifaddr *ifp,
2350 __u32 valid_lft, __u32 prefered_lft,
2351 bool create, unsigned long now)
2352{
2353 u32 flags;
2354 struct inet6_ifaddr *ift;
2355
2356 read_lock_bh(&idev->lock);
2357 /* update all temporary addresses in the list */
2358 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2359 int age, max_valid, max_prefered;
2360
2361 if (ifp != ift->ifpub)
2362 continue;
2363
2364 /* RFC 4941 section 3.3:
2365 * If a received option will extend the lifetime of a public
2366 * address, the lifetimes of temporary addresses should
2367 * be extended, subject to the overall constraint that no
2368 * temporary addresses should ever remain "valid" or "preferred"
2369 * for a time longer than (TEMP_VALID_LIFETIME) or
2370 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2371 */
2372 age = (now - ift->cstamp) / HZ;
2373 max_valid = idev->cnf.temp_valid_lft - age;
2374 if (max_valid < 0)
2375 max_valid = 0;
2376
2377 max_prefered = idev->cnf.temp_prefered_lft -
Jiri Bohac76506a92016-10-13 18:52:15 +02002378 idev->desync_factor - age;
Jiri Pirko53bd6742013-12-06 09:45:22 +01002379 if (max_prefered < 0)
2380 max_prefered = 0;
2381
2382 if (valid_lft > max_valid)
2383 valid_lft = max_valid;
2384
2385 if (prefered_lft > max_prefered)
2386 prefered_lft = max_prefered;
2387
2388 spin_lock(&ift->lock);
2389 flags = ift->flags;
2390 ift->valid_lft = valid_lft;
2391 ift->prefered_lft = prefered_lft;
2392 ift->tstamp = now;
2393 if (prefered_lft > 0)
2394 ift->flags &= ~IFA_F_DEPRECATED;
2395
2396 spin_unlock(&ift->lock);
2397 if (!(flags&IFA_F_TENTATIVE))
2398 ipv6_ifa_notify(0, ift);
2399 }
2400
2401 if ((create || list_empty(&idev->tempaddr_list)) &&
2402 idev->cnf.use_tempaddr > 0) {
2403 /* When a new public address is created as described
2404 * in [ADDRCONF], also create a new temporary address.
2405 * Also create a temporary address if it's enabled but
2406 * no temporary address currently exists.
2407 */
2408 read_unlock_bh(&idev->lock);
2409 ipv6_create_tempaddr(ifp, NULL);
2410 } else {
2411 read_unlock_bh(&idev->lock);
2412 }
2413}
2414
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002415static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2416{
2417 return idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2418 idev->addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2419}
2420
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002421int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2422 const struct prefix_info *pinfo,
2423 struct inet6_dev *in6_dev,
2424 const struct in6_addr *addr, int addr_type,
2425 u32 addr_flags, bool sllao, bool tokenized,
2426 __u32 valid_lft, u32 prefered_lft)
Alexander Aring4f672232016-06-15 21:20:22 +02002427{
2428 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2429 int create = 0, update_lft = 0;
2430
2431 if (!ifp && valid_lft) {
2432 int max_addresses = in6_dev->cnf.max_addresses;
2433
2434#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2435 if (in6_dev->cnf.optimistic_dad &&
2436 !net->ipv6.devconf_all->forwarding && sllao)
2437 addr_flags |= IFA_F_OPTIMISTIC;
2438#endif
2439
2440 /* Do not allow to create too much of autoconfigured
2441 * addresses; this would be too easy way to crash kernel.
2442 */
2443 if (!max_addresses ||
2444 ipv6_count_addresses(in6_dev) < max_addresses)
2445 ifp = ipv6_add_addr(in6_dev, addr, NULL,
2446 pinfo->prefix_len,
2447 addr_type&IPV6_ADDR_SCOPE_MASK,
2448 addr_flags, valid_lft,
2449 prefered_lft);
2450
2451 if (IS_ERR_OR_NULL(ifp))
2452 return -1;
2453
2454 update_lft = 0;
2455 create = 1;
2456 spin_lock_bh(&ifp->lock);
2457 ifp->flags |= IFA_F_MANAGETEMPADDR;
2458 ifp->cstamp = jiffies;
2459 ifp->tokenized = tokenized;
2460 spin_unlock_bh(&ifp->lock);
2461 addrconf_dad_start(ifp);
2462 }
2463
2464 if (ifp) {
2465 u32 flags;
2466 unsigned long now;
2467 u32 stored_lft;
2468
2469 /* update lifetime (RFC2462 5.5.3 e) */
2470 spin_lock_bh(&ifp->lock);
2471 now = jiffies;
2472 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2473 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2474 else
2475 stored_lft = 0;
2476 if (!update_lft && !create && stored_lft) {
2477 const u32 minimum_lft = min_t(u32,
2478 stored_lft, MIN_VALID_LIFETIME);
2479 valid_lft = max(valid_lft, minimum_lft);
2480
2481 /* RFC4862 Section 5.5.3e:
2482 * "Note that the preferred lifetime of the
2483 * corresponding address is always reset to
2484 * the Preferred Lifetime in the received
2485 * Prefix Information option, regardless of
2486 * whether the valid lifetime is also reset or
2487 * ignored."
2488 *
2489 * So we should always update prefered_lft here.
2490 */
2491 update_lft = 1;
2492 }
2493
2494 if (update_lft) {
2495 ifp->valid_lft = valid_lft;
2496 ifp->prefered_lft = prefered_lft;
2497 ifp->tstamp = now;
2498 flags = ifp->flags;
2499 ifp->flags &= ~IFA_F_DEPRECATED;
2500 spin_unlock_bh(&ifp->lock);
2501
2502 if (!(flags&IFA_F_TENTATIVE))
2503 ipv6_ifa_notify(0, ifp);
2504 } else
2505 spin_unlock_bh(&ifp->lock);
2506
2507 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2508 create, now);
2509
2510 in6_ifa_put(ifp);
2511 addrconf_verify();
2512 }
2513
2514 return 0;
2515}
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002516EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
Alexander Aring4f672232016-06-15 21:20:22 +02002517
Neil Hormane6bff992012-01-04 10:49:15 +00002518void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
2520 struct prefix_info *pinfo;
2521 __u32 valid_lft;
2522 __u32 prefered_lft;
Alexander Aring4f672232016-06-15 21:20:22 +02002523 int addr_type, err;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002524 u32 addr_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07002526 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
2528 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002529
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 if (len < sizeof(struct prefix_info)) {
dingtianhongba3542e2013-08-17 10:27:04 +08002531 ADBG("addrconf: prefix option too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 return;
2533 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 /*
2536 * Validation checks ([ADDRCONF], page 19)
2537 */
2538
2539 addr_type = ipv6_addr_type(&pinfo->prefix);
2540
2541 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2542 return;
2543
2544 valid_lft = ntohl(pinfo->valid);
2545 prefered_lft = ntohl(pinfo->prefered);
2546
2547 if (prefered_lft > valid_lft) {
Joe Perchese87cc472012-05-13 21:56:26 +00002548 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 return;
2550 }
2551
2552 in6_dev = in6_dev_get(dev);
2553
Ian Morris63159f22015-03-29 14:00:04 +01002554 if (!in6_dev) {
Joe Perchese87cc472012-05-13 21:56:26 +00002555 net_dbg_ratelimited("addrconf: device %s not configured\n",
2556 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 return;
2558 }
2559
2560 /*
2561 * Two things going on here:
2562 * 1) Add routes for on-link prefixes
2563 * 2) Configure prefixes with the auto flag set
2564 */
2565
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002566 if (pinfo->onlink) {
2567 struct rt6_info *rt;
2568 unsigned long rt_expires;
2569
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002570 /* Avoid arithmetic overflow. Really, we could
2571 * save rt_expires in seconds, likely valid_lft,
2572 * but it would require division in fib gc, that it
2573 * not good.
2574 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002575 if (HZ > USER_HZ)
2576 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2577 else
2578 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08002579
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002580 if (addrconf_finite_timeout(rt_expires))
2581 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002583 rt = addrconf_get_prefix_route(&pinfo->prefix,
2584 pinfo->prefix_len,
2585 dev,
2586 RTF_ADDRCONF | RTF_PREFIX_RT,
2587 RTF_GATEWAY | RTF_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002589 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002590 /* Autoconf prefix route */
2591 if (valid_lft == 0) {
2592 ip6_del_rt(rt);
2593 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002594 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002595 /* not infinity */
Gao feng1716a962012-04-06 00:13:10 +00002596 rt6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002597 } else {
Gao feng1716a962012-04-06 00:13:10 +00002598 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 }
2600 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002601 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002602 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2603 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002604 /* not infinity */
2605 flags |= RTF_EXPIRES;
2606 expires = jiffies_to_clock_t(rt_expires);
2607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002609 dev, expires, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00002611 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 }
2613
2614 /* Try to figure out our local address for this prefix */
2615
2616 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 struct in6_addr addr;
Alexander Aringf997c552016-06-15 21:20:23 +02002618 bool tokenized = false, dev_addr_generated = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
2620 if (pinfo->prefix_len == 64) {
2621 memcpy(&addr, &pinfo->prefix, 8);
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002622
2623 if (!ipv6_addr_any(&in6_dev->token)) {
2624 read_lock_bh(&in6_dev->lock);
2625 memcpy(addr.s6_addr + 8,
2626 in6_dev->token.s6_addr + 8, 8);
2627 read_unlock_bh(&in6_dev->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00002628 tokenized = true;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002629 } else if (is_addr_mode_generate_stable(in6_dev) &&
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002630 !ipv6_generate_stable_address(&addr, 0,
2631 in6_dev)) {
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002632 addr_flags |= IFA_F_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002633 goto ok;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002634 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2635 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
Alexander Aring4f672232016-06-15 21:20:22 +02002636 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002637 } else {
2638 dev_addr_generated = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 }
2640 goto ok;
2641 }
Joe Perchese87cc472012-05-13 21:56:26 +00002642 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2643 pinfo->prefix_len);
Alexander Aring4f672232016-06-15 21:20:22 +02002644 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
2646ok:
Alexander Aring4f672232016-06-15 21:20:22 +02002647 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2648 &addr, addr_type,
2649 addr_flags, sllao,
2650 tokenized, valid_lft,
2651 prefered_lft);
2652 if (err)
2653 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002654
2655 /* Ignore error case here because previous prefix add addr was
2656 * successful which will be notified.
2657 */
2658 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2659 addr_type, addr_flags, sllao,
2660 tokenized, valid_lft,
2661 prefered_lft,
2662 dev_addr_generated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 }
2664 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
Alexander Aring4f672232016-06-15 21:20:22 +02002665put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 in6_dev_put(in6_dev);
2667}
2668
2669/*
2670 * Set destination address.
2671 * Special case for SIT interfaces where we create a new "virtual"
2672 * device.
2673 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002674int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
2676 struct in6_ifreq ireq;
2677 struct net_device *dev;
2678 int err = -EINVAL;
2679
2680 rtnl_lock();
2681
2682 err = -EFAULT;
2683 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2684 goto err_exit;
2685
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002686 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
2688 err = -ENODEV;
Ian Morris63159f22015-03-29 14:00:04 +01002689 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 goto err_exit;
2691
Amerigo Wang07a93622012-10-29 16:23:10 +00002692#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002694 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 struct ip_tunnel_parm p;
2697
2698 err = -EADDRNOTAVAIL;
2699 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2700 goto err_exit;
2701
2702 memset(&p, 0, sizeof(p));
2703 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2704 p.iph.saddr = 0;
2705 p.iph.version = 4;
2706 p.iph.ihl = 5;
2707 p.iph.protocol = IPPROTO_IPV6;
2708 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002709 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002711 if (ops->ndo_do_ioctl) {
2712 mm_segment_t oldfs = get_fs();
2713
2714 set_fs(KERNEL_DS);
2715 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2716 set_fs(oldfs);
2717 } else
2718 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
2720 if (err == 0) {
2721 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002722 dev = __dev_get_by_name(net, p.name);
2723 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 goto err_exit;
2725 err = dev_open(dev);
2726 }
2727 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002728#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
2730err_exit:
2731 rtnl_unlock();
2732 return err;
2733}
2734
Madhu Challa93a714d2015-02-25 09:58:35 -08002735static int ipv6_mc_config(struct sock *sk, bool join,
2736 const struct in6_addr *addr, int ifindex)
2737{
2738 int ret;
2739
2740 ASSERT_RTNL();
2741
2742 lock_sock(sk);
2743 if (join)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002744 ret = ipv6_sock_mc_join(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002745 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002746 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002747 release_sock(sk);
2748
2749 return ret;
2750}
2751
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752/*
2753 * Manual configuration of address on an interface
2754 */
Jiri Pirko479840f2013-12-06 09:45:21 +01002755static int inet6_addr_add(struct net *net, int ifindex,
2756 const struct in6_addr *pfx,
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002757 const struct in6_addr *peer_pfx,
Jiri Pirko479840f2013-12-06 09:45:21 +01002758 unsigned int plen, __u32 ifa_flags,
2759 __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
2761 struct inet6_ifaddr *ifp;
2762 struct inet6_dev *idev;
2763 struct net_device *dev;
Madhu Challa93a714d2015-02-25 09:58:35 -08002764 unsigned long timeout;
2765 clock_t expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002767 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
2769 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002770
Thomas Graf24ef0da2008-05-28 16:54:22 +02002771 if (plen > 128)
2772 return -EINVAL;
2773
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002774 /* check the lifetime */
2775 if (!valid_lft || prefered_lft > valid_lft)
2776 return -EINVAL;
2777
Jiri Pirko53bd6742013-12-06 09:45:22 +01002778 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2779 return -EINVAL;
2780
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002781 dev = __dev_get_by_index(net, ifindex);
2782 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002784
Brian Haley64e724f2010-07-20 10:34:30 +00002785 idev = addrconf_add_dev(dev);
2786 if (IS_ERR(idev))
2787 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
Madhu Challa93a714d2015-02-25 09:58:35 -08002789 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2790 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2791 true, pfx, ifindex);
2792
2793 if (ret < 0)
2794 return ret;
2795 }
2796
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 scope = ipv6_addr_scope(pfx);
2798
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002799 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2800 if (addrconf_finite_timeout(timeout)) {
2801 expires = jiffies_to_clock_t(timeout * HZ);
2802 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002803 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002804 } else {
2805 expires = 0;
2806 flags = 0;
2807 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002808 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002809
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002810 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2811 if (addrconf_finite_timeout(timeout)) {
2812 if (timeout == 0)
2813 ifa_flags |= IFA_F_DEPRECATED;
2814 prefered_lft = timeout;
2815 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002816
Jiri Benc8a226b22013-08-01 10:41:28 +02002817 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2818 valid_lft, prefered_lft);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002819
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 if (!IS_ERR(ifp)) {
Thomas Haller761aac72014-01-15 15:36:58 +01002821 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2822 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2823 expires, flags);
2824 }
2825
Neil Horman95c385b2007-04-25 17:08:10 -07002826 /*
2827 * Note that section 3.1 of RFC 4429 indicates
2828 * that the Optimistic flag should not be set for
2829 * manually configured addresses
2830 */
David S. Millercf22f9a2012-04-14 21:37:40 -04002831 addrconf_dad_start(ifp);
Jiri Pirko53bd6742013-12-06 09:45:22 +01002832 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2833 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2834 true, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002836 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 return 0;
Madhu Challa93a714d2015-02-25 09:58:35 -08002838 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2839 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2840 false, pfx, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 }
2842
2843 return PTR_ERR(ifp);
2844}
2845
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002846static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2847 const struct in6_addr *pfx, unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
2849 struct inet6_ifaddr *ifp;
2850 struct inet6_dev *idev;
2851 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002852
Thomas Graf24ef0da2008-05-28 16:54:22 +02002853 if (plen > 128)
2854 return -EINVAL;
2855
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002856 dev = __dev_get_by_index(net, ifindex);
2857 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 return -ENODEV;
2859
Ian Morrise5d08d72014-11-23 21:28:43 +00002860 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01002861 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 return -ENXIO;
2863
2864 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002865 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 if (ifp->prefix_len == plen &&
2867 ipv6_addr_equal(pfx, &ifp->addr)) {
2868 in6_ifa_hold(ifp);
2869 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002870
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002871 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2872 (ifa_flags & IFA_F_MANAGETEMPADDR))
2873 manage_tempaddrs(idev, ifp, 0, 0, false,
2874 jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 ipv6_del_addr(ifp);
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002876 addrconf_verify_rtnl();
Madhu Challa93a714d2015-02-25 09:58:35 -08002877 if (ipv6_addr_is_multicast(pfx)) {
2878 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2879 false, pfx, dev->ifindex);
2880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 return 0;
2882 }
2883 }
2884 read_unlock_bh(&idev->lock);
2885 return -EADDRNOTAVAIL;
2886}
2887
2888
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002889int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
2891 struct in6_ifreq ireq;
2892 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002893
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002894 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002896
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2898 return -EFAULT;
2899
2900 rtnl_lock();
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002901 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002902 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2903 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 rtnl_unlock();
2905 return err;
2906}
2907
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002908int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909{
2910 struct in6_ifreq ireq;
2911 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002912
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002913 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 return -EPERM;
2915
2916 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2917 return -EFAULT;
2918
2919 rtnl_lock();
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002920 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002921 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 rtnl_unlock();
2923 return err;
2924}
2925
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002926static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2927 int plen, int scope)
2928{
2929 struct inet6_ifaddr *ifp;
2930
Jiri Benc8a226b22013-08-01 10:41:28 +02002931 ifp = ipv6_add_addr(idev, addr, NULL, plen,
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01002932 scope, IFA_F_PERMANENT,
2933 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002934 if (!IS_ERR(ifp)) {
2935 spin_lock_bh(&ifp->lock);
2936 ifp->flags &= ~IFA_F_TENTATIVE;
2937 spin_unlock_bh(&ifp->lock);
Paolo Abeni764d3be2016-11-22 16:57:40 +01002938 rt_genid_bump_ipv6(dev_net(idev->dev));
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002939 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2940 in6_ifa_put(ifp);
2941 }
2942}
2943
Amerigo Wang07a93622012-10-29 16:23:10 +00002944#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945static void sit_add_v4_addrs(struct inet6_dev *idev)
2946{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 struct in6_addr addr;
2948 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002949 struct net *net = dev_net(idev->dev);
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002950 int scope, plen;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002951 u32 pflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
2953 ASSERT_RTNL();
2954
2955 memset(&addr, 0, sizeof(struct in6_addr));
2956 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2957
2958 if (idev->dev->flags&IFF_POINTOPOINT) {
2959 addr.s6_addr32[0] = htonl(0xfe800000);
2960 scope = IFA_LINK;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002961 plen = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 } else {
2963 scope = IPV6_ADDR_COMPATv4;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002964 plen = 96;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002965 pflags |= RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 }
2967
2968 if (addr.s6_addr32[3]) {
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002969 add_addr(idev, &addr, plen, scope);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002970 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 return;
2972 }
2973
Benjamin Thery6fda7352008-03-05 10:47:47 -08002974 for_each_netdev(net, dev) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002975 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 if (in_dev && (dev->flags & IFF_UP)) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002977 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
2979 int flag = scope;
2980
2981 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
2983 addr.s6_addr32[3] = ifa->ifa_local;
2984
2985 if (ifa->ifa_scope == RT_SCOPE_LINK)
2986 continue;
2987 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2988 if (idev->dev->flags&IFF_POINTOPOINT)
2989 continue;
2990 flag |= IFA_HOST;
2991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002993 add_addr(idev, &addr, plen, flag);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002994 addrconf_prefix_route(&addr, plen, idev->dev, 0,
2995 pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 }
2997 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003000#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
3002static void init_loopback(struct net_device *dev)
3003{
3004 struct inet6_dev *idev;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303005 struct net_device *sp_dev;
3006 struct inet6_ifaddr *sp_ifa;
3007 struct rt6_info *sp_rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
3009 /* ::1 */
3010
3011 ASSERT_RTNL();
3012
Ian Morrise5d08d72014-11-23 21:28:43 +00003013 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003014 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003015 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 return;
3017 }
3018
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003019 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303020
3021 /* Add routes to other interface's IPv6 addresses */
3022 for_each_netdev(dev_net(dev), sp_dev) {
3023 if (!strcmp(sp_dev->name, dev->name))
3024 continue;
3025
3026 idev = __in6_dev_get(sp_dev);
3027 if (!idev)
3028 continue;
3029
3030 read_lock_bh(&idev->lock);
3031 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
3032
3033 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
3034 continue;
3035
Gao feng33d99112014-01-24 16:29:11 +08003036 if (sp_ifa->rt) {
3037 /* This dst has been added to garbage list when
3038 * lo device down, release this obsolete dst and
3039 * reallocate a new router for ifa.
3040 */
Nicolas Dichtela2204452016-10-12 10:10:40 +02003041 if (!atomic_read(&sp_ifa->rt->rt6i_ref)) {
Gao feng33d99112014-01-24 16:29:11 +08003042 ip6_rt_put(sp_ifa->rt);
3043 sp_ifa->rt = NULL;
3044 } else {
3045 continue;
3046 }
3047 }
Gao fenga881ae12013-06-16 11:14:30 +08003048
François-Xavier Le Bail57ec0af2013-12-02 11:28:49 +01003049 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303050
3051 /* Failure cases are ignored */
Gao feng534c8772013-06-02 22:16:21 +00003052 if (!IS_ERR(sp_rt)) {
3053 sp_ifa->rt = sp_rt;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303054 ip6_ins_rt(sp_rt);
Gao feng534c8772013-06-02 22:16:21 +00003055 }
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303056 }
3057 read_unlock_bh(&idev->lock);
3058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059}
3060
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003061void addrconf_add_linklocal(struct inet6_dev *idev,
3062 const struct in6_addr *addr, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003064 struct inet6_ifaddr *ifp;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003065 u32 addr_flags = flags | IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
Neil Horman95c385b2007-04-25 17:08:10 -07003067#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3068 if (idev->cnf.optimistic_dad &&
David Miller702beb82008-07-20 18:17:02 -07003069 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
Neil Horman95c385b2007-04-25 17:08:10 -07003070 addr_flags |= IFA_F_OPTIMISTIC;
3071#endif
3072
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01003073 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
3074 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003076 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
David S. Millercf22f9a2012-04-14 21:37:40 -04003077 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 in6_ifa_put(ifp);
3079 }
3080}
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003081EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003083static bool ipv6_reserved_interfaceid(struct in6_addr address)
3084{
3085 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3086 return true;
3087
3088 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3089 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3090 return true;
3091
3092 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3093 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3094 return true;
3095
3096 return false;
3097}
3098
3099static int ipv6_generate_stable_address(struct in6_addr *address,
3100 u8 dad_count,
3101 const struct inet6_dev *idev)
3102{
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003103 static DEFINE_SPINLOCK(lock);
3104 static __u32 digest[SHA_DIGEST_WORDS];
3105 static __u32 workspace[SHA_WORKSPACE_WORDS];
3106
3107 static union {
3108 char __data[SHA_MESSAGE_BYTES];
3109 struct {
3110 struct in6_addr secret;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003111 __be32 prefix[2];
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003112 unsigned char hwaddr[MAX_ADDR_LEN];
3113 u8 dad_count;
3114 } __packed;
3115 } data;
3116
3117 struct in6_addr secret;
3118 struct in6_addr temp;
3119 struct net *net = dev_net(idev->dev);
3120
3121 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3122
3123 if (idev->cnf.stable_secret.initialized)
3124 secret = idev->cnf.stable_secret.secret;
3125 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3126 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3127 else
3128 return -1;
3129
3130retry:
3131 spin_lock_bh(&lock);
3132
3133 sha_init(digest);
3134 memset(&data, 0, sizeof(data));
3135 memset(workspace, 0, sizeof(workspace));
3136 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003137 data.prefix[0] = address->s6_addr32[0];
3138 data.prefix[1] = address->s6_addr32[1];
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003139 data.secret = secret;
3140 data.dad_count = dad_count;
3141
3142 sha_transform(digest, data.__data, workspace);
3143
3144 temp = *address;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003145 temp.s6_addr32[2] = (__force __be32)digest[0];
3146 temp.s6_addr32[3] = (__force __be32)digest[1];
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003147
3148 spin_unlock_bh(&lock);
3149
3150 if (ipv6_reserved_interfaceid(temp)) {
3151 dad_count++;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01003152 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003153 return -1;
3154 goto retry;
3155 }
3156
3157 *address = temp;
3158 return 0;
3159}
3160
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003161static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3162{
3163 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3164
3165 if (s->initialized)
3166 return;
3167 s = &idev->cnf.stable_secret;
3168 get_random_bytes(&s->secret, sizeof(s->secret));
3169 s->initialized = true;
3170}
3171
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003172static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3173{
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003174 struct in6_addr addr;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003175
David Ahernca254492015-10-12 11:47:10 -07003176 /* no link local addresses on L3 master devices */
3177 if (netif_is_l3_master(idev->dev))
3178 return;
3179
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003180 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3181
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003182 switch (idev->addr_gen_mode) {
3183 case IN6_ADDR_GEN_MODE_RANDOM:
3184 ipv6_gen_mode_random_init(idev);
3185 /* fallthrough */
3186 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003187 if (!ipv6_generate_stable_address(&addr, 0, idev))
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003188 addrconf_add_linklocal(idev, &addr,
3189 IFA_F_STABLE_PRIVACY);
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003190 else if (prefix_route)
3191 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003192 break;
3193 case IN6_ADDR_GEN_MODE_EUI64:
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003194 /* addrconf_add_linklocal also adds a prefix_route and we
3195 * only need to care about prefix routes if ipv6_generate_eui64
3196 * couldn't generate one.
3197 */
3198 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003199 addrconf_add_linklocal(idev, &addr, 0);
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003200 else if (prefix_route)
3201 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003202 break;
3203 case IN6_ADDR_GEN_MODE_NONE:
3204 default:
3205 /* will not add any link local address */
3206 break;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003207 }
3208}
3209
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210static void addrconf_dev_config(struct net_device *dev)
3211{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003212 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
3214 ASSERT_RTNL();
3215
Herbert Xu74235a22007-06-14 13:02:55 -07003216 if ((dev->type != ARPHRD_ETHER) &&
3217 (dev->type != ARPHRD_FDDI) &&
Herbert Xu74235a22007-06-14 13:02:55 -07003218 (dev->type != ARPHRD_ARCNET) &&
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00003219 (dev->type != ARPHRD_INFINIBAND) &&
Nicolas Dichtele8377352013-08-20 12:16:06 +02003220 (dev->type != ARPHRD_IEEE1394) &&
Jukka Rissanene74bccb2013-12-11 17:05:36 +02003221 (dev->type != ARPHRD_TUNNEL6) &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003222 (dev->type != ARPHRD_6LOWPAN) &&
Subash Abhinov Kasiviswanathan54948002010-09-15 13:08:00 -06003223 (dev->type != ARPHRD_NONE) &&
3224 (dev->type != ARPHRD_RAWIP)) {
Herbert Xu74235a22007-06-14 13:02:55 -07003225 /* Alas, we support only Ethernet autoconfiguration. */
3226 return;
3227 }
3228
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00003230 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 return;
3232
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003233 /* this device type has no EUI support */
3234 if (dev->type == ARPHRD_NONE &&
3235 idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3236 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3237
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003238 addrconf_addr_gen(idev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239}
3240
Amerigo Wang07a93622012-10-29 16:23:10 +00003241#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242static void addrconf_sit_config(struct net_device *dev)
3243{
3244 struct inet6_dev *idev;
3245
3246 ASSERT_RTNL();
3247
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003248 /*
3249 * Configure the tunnel with one of our IPv4
3250 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 * our v4 addrs in the tunnel
3252 */
3253
Ian Morrise5d08d72014-11-23 21:28:43 +00003254 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003255 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003256 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 return;
3258 }
3259
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003260 if (dev->priv_flags & IFF_ISATAP) {
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003261 addrconf_addr_gen(idev, false);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003262 return;
3263 }
3264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 sit_add_v4_addrs(idev);
3266
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00003267 if (dev->flags&IFF_POINTOPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003270#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Amerigo Wang07a93622012-10-29 16:23:10 +00003272#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003273static void addrconf_gre_config(struct net_device *dev)
3274{
3275 struct inet6_dev *idev;
stephen hemmingeraee80b52011-06-08 10:44:30 +00003276
stephen hemmingeraee80b52011-06-08 10:44:30 +00003277 ASSERT_RTNL();
3278
Ian Morrise5d08d72014-11-23 21:28:43 +00003279 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003280 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003281 pr_debug("%s: add_dev failed\n", __func__);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003282 return;
3283 }
3284
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003285 addrconf_addr_gen(idev, true);
Hannes Frederic Sowad9e4ce62015-10-08 18:19:39 +02003286 if (dev->flags & IFF_POINTOPOINT)
3287 addrconf_add_mroute(dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003288}
3289#endif
3290
David Ahernf1705ec2016-02-24 09:25:37 -08003291static int fixup_permanent_addr(struct inet6_dev *idev,
3292 struct inet6_ifaddr *ifp)
3293{
3294 if (!ifp->rt) {
3295 struct rt6_info *rt;
3296
3297 rt = addrconf_dst_alloc(idev, &ifp->addr, false);
3298 if (unlikely(IS_ERR(rt)))
3299 return PTR_ERR(rt);
3300
3301 ifp->rt = rt;
3302 }
3303
3304 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3305 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3306 idev->dev, 0, 0);
3307 }
3308
3309 addrconf_dad_start(ifp);
3310
3311 return 0;
3312}
3313
3314static void addrconf_permanent_addr(struct net_device *dev)
3315{
3316 struct inet6_ifaddr *ifp, *tmp;
3317 struct inet6_dev *idev;
3318
3319 idev = __in6_dev_get(dev);
3320 if (!idev)
3321 return;
3322
3323 write_lock_bh(&idev->lock);
3324
3325 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3326 if ((ifp->flags & IFA_F_PERMANENT) &&
3327 fixup_permanent_addr(idev, ifp) < 0) {
3328 write_unlock_bh(&idev->lock);
3329 ipv6_del_addr(ifp);
3330 write_lock_bh(&idev->lock);
3331
3332 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3333 idev->dev->name, &ifp->addr);
3334 }
3335 }
3336
3337 write_unlock_bh(&idev->lock);
3338}
3339
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003340static int addrconf_notify(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00003341 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342{
Jiri Pirko351638e2013-05-28 01:30:21 +00003343 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
David Ahern4f7f34e2016-04-07 11:10:41 -07003344 struct netdev_notifier_changeupper_info *info;
Eric Dumazet748e2d92012-08-22 21:50:59 +00003345 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003346 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07003347 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003349 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003350 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07003351 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003352 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -07003353 if (IS_ERR(idev))
3354 return notifier_from_errno(PTR_ERR(idev));
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003355 }
3356 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003357
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003358 case NETDEV_CHANGEMTU:
3359 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3360 if (dev->mtu < IPV6_MIN_MTU) {
3361 addrconf_ifdown(dev, 1);
3362 break;
3363 }
3364
3365 if (idev) {
3366 rt6_mtu_change(dev, dev->mtu);
3367 idev->cnf.mtu6 = dev->mtu;
3368 break;
3369 }
3370
3371 /* allocate new idev */
3372 idev = ipv6_add_dev(dev);
3373 if (IS_ERR(idev))
3374 break;
3375
3376 /* device is still not ready */
3377 if (!(idev->if_flags & IF_READY))
3378 break;
3379
3380 run_pending = 1;
3381
3382 /* fall through */
3383
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003385 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07003386 if (dev->flags & IFF_SLAVE)
3387 break;
3388
WANG Cong3ce62a82014-09-11 15:07:16 -07003389 if (idev && idev->cnf.disable_ipv6)
3390 break;
3391
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003392 if (event == NETDEV_UP) {
David Ahern38bd10c2016-04-21 20:56:12 -07003393 /* restore routes for permanent addresses */
3394 addrconf_permanent_addr(dev);
3395
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07003396 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003397 /* device is not ready yet. */
Joe Perchesf3213832012-05-15 14:11:53 +00003398 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003399 dev->name);
3400 break;
3401 }
Kristian Slavov99081042006-02-08 16:10:53 -08003402
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11003403 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3404 idev = ipv6_add_dev(dev);
3405
WANG Conga317a2f2014-07-25 15:25:09 -07003406 if (!IS_ERR_OR_NULL(idev)) {
Kristian Slavov99081042006-02-08 16:10:53 -08003407 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08003408 run_pending = 1;
3409 }
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003410 } else if (event == NETDEV_CHANGE) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07003411 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003412 /* device is still not ready. */
3413 break;
3414 }
3415
3416 if (idev) {
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003417 if (idev->if_flags & IF_READY)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003418 /* device is already configured. */
3419 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003420 idev->if_flags |= IF_READY;
3421 }
3422
Joe Perchesf3213832012-05-15 14:11:53 +00003423 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3424 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003425
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003426 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003427 }
3428
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003429 switch (dev->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +00003430#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 case ARPHRD_SIT:
3432 addrconf_sit_config(dev);
3433 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07003434#endif
Amerigo Wang07a93622012-10-29 16:23:10 +00003435#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003436 case ARPHRD_IPGRE:
3437 addrconf_gre_config(dev);
3438 break;
3439#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 case ARPHRD_LOOPBACK:
3441 init_loopback(dev);
3442 break;
3443
3444 default:
3445 addrconf_dev_config(dev);
3446 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003447 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003448
WANG Conga317a2f2014-07-25 15:25:09 -07003449 if (!IS_ERR_OR_NULL(idev)) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003450 if (run_pending)
3451 addrconf_dad_run(idev);
3452
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003453 /*
3454 * If the MTU changed during the interface down,
3455 * when the interface up, the changed MTU must be
3456 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003458 if (idev->cnf.mtu6 != dev->mtu &&
3459 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 rt6_mtu_change(dev, dev->mtu);
3461 idev->cnf.mtu6 = dev->mtu;
3462 }
3463 idev->tstamp = jiffies;
3464 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003465
3466 /*
3467 * If the changed mtu during down is lower than
3468 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 */
3470 if (dev->mtu < IPV6_MIN_MTU)
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003471 addrconf_ifdown(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 }
3473 break;
3474
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 case NETDEV_DOWN:
3476 case NETDEV_UNREGISTER:
3477 /*
3478 * Remove all addresses from this interface.
3479 */
3480 addrconf_ifdown(dev, event != NETDEV_DOWN);
3481 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003482
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07003485 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003486 addrconf_sysctl_unregister(idev);
WANG Conga317a2f2014-07-25 15:25:09 -07003487 err = addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07003488 if (err)
3489 return notifier_from_errno(err);
WANG Conga317a2f2014-07-25 15:25:09 -07003490 err = snmp6_register_dev(idev);
3491 if (err) {
3492 addrconf_sysctl_unregister(idev);
3493 return notifier_from_errno(err);
3494 }
Stephen Hemminger5632c512007-04-28 21:16:39 -07003495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003497
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003498 case NETDEV_PRE_TYPE_CHANGE:
3499 case NETDEV_POST_TYPE_CHANGE:
Andrew Lunn3ef09522015-12-03 21:12:32 +01003500 if (idev)
3501 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07003502 break;
David Ahern4f7f34e2016-04-07 11:10:41 -07003503
3504 case NETDEV_CHANGEUPPER:
3505 info = ptr;
3506
3507 /* flush all routes if dev is linked to or unlinked from
3508 * an L3 master device (e.g., VRF)
3509 */
3510 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3511 addrconf_ifdown(dev, 0);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
3514 return NOTIFY_OK;
3515}
3516
3517/*
3518 * addrconf module should be notified of a device going up
3519 */
3520static struct notifier_block ipv6_dev_notf = {
3521 .notifier_call = addrconf_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522};
3523
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003524static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07003525{
3526 struct inet6_dev *idev;
3527 ASSERT_RTNL();
3528
3529 idev = __in6_dev_get(dev);
3530
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003531 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003532 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003533 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003534 ipv6_mc_unmap(idev);
3535}
3536
David Ahern70af9212016-04-08 12:01:21 -07003537static bool addr_is_local(const struct in6_addr *addr)
3538{
3539 return ipv6_addr_type(addr) &
3540 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3541}
3542
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543static int addrconf_ifdown(struct net_device *dev, int how)
3544{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003545 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003546 struct inet6_dev *idev;
David Ahernf1705ec2016-02-24 09:25:37 -08003547 struct inet6_ifaddr *ifa, *tmp;
3548 struct list_head del_list;
3549 int _keep_addr;
3550 bool keep_addr;
David S. Miller73a8bd72011-01-23 23:27:15 -08003551 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
3553 ASSERT_RTNL();
3554
David S. Miller73a8bd72011-01-23 23:27:15 -08003555 rt6_ifdown(net, dev);
3556 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
3558 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003559 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 return -ENODEV;
3561
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003562 /*
3563 * Step 1: remove reference to ipv6 device from parent device.
3564 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003566 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003568
3569 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003570 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
3572 /* Step 1.5: remove snmp6 entry */
3573 snmp6_unregister_dev(idev);
3574
3575 }
3576
David Ahernf1705ec2016-02-24 09:25:37 -08003577 /* aggregate the system setting and interface setting */
3578 _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3579 if (!_keep_addr)
3580 _keep_addr = idev->cnf.keep_addr_on_down;
3581
3582 /* combine the user config with event to determine if permanent
3583 * addresses are to be removed from address hash table
3584 */
Mike Manningbc561632016-08-12 12:02:38 +01003585 keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003586
David S. Miller73a8bd72011-01-23 23:27:15 -08003587 /* Step 2: clear hash table */
3588 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3589 struct hlist_head *h = &inet6_addr_lst[i];
David S. Miller73a8bd72011-01-23 23:27:15 -08003590
3591 spin_lock_bh(&addrconf_hash_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01003592restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08003593 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
David S. Miller73a8bd72011-01-23 23:27:15 -08003594 if (ifa->idev == idev) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003595 addrconf_del_dad_work(ifa);
David Ahernf1705ec2016-02-24 09:25:37 -08003596 /* combined flag + permanent flag decide if
3597 * address is retained on a down event
3598 */
3599 if (!keep_addr ||
David Ahern70af9212016-04-08 12:01:21 -07003600 !(ifa->flags & IFA_F_PERMANENT) ||
3601 addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003602 hlist_del_init_rcu(&ifa->addr_lst);
3603 goto restart;
3604 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003605 }
3606 }
3607 spin_unlock_bh(&addrconf_hash_lock);
3608 }
3609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 write_lock_bh(&idev->lock);
3611
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003612 addrconf_del_rs_timer(idev);
3613
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003614 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003615 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003616 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003618 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00003619 while (!list_empty(&idev->tempaddr_list)) {
3620 ifa = list_first_entry(&idev->tempaddr_list,
3621 struct inet6_ifaddr, tmp_list);
3622 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 write_unlock_bh(&idev->lock);
3624 spin_lock_bh(&ifa->lock);
3625
3626 if (ifa->ifpub) {
3627 in6_ifa_put(ifa->ifpub);
3628 ifa->ifpub = NULL;
3629 }
3630 spin_unlock_bh(&ifa->lock);
3631 in6_ifa_put(ifa);
3632 write_lock_bh(&idev->lock);
3633 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003634
David Ahernf1705ec2016-02-24 09:25:37 -08003635 /* re-combine the user config with event to determine if permanent
3636 * addresses are to be removed from the interface list
3637 */
Mike Manningbc561632016-08-12 12:02:38 +01003638 keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003639
3640 INIT_LIST_HEAD(&del_list);
3641 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
David Ahern38bd10c2016-04-21 20:56:12 -07003642 struct rt6_info *rt = NULL;
3643
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003644 addrconf_del_dad_work(ifa);
stephen hemminger84e8b802010-03-02 13:32:46 +00003645
David S. Miller73a8bd72011-01-23 23:27:15 -08003646 write_unlock_bh(&idev->lock);
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003647 spin_lock_bh(&ifa->lock);
David Ahernf1705ec2016-02-24 09:25:37 -08003648
David Ahern70af9212016-04-08 12:01:21 -07003649 if (keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3650 !addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003651 /* set state to skip the notifier below */
3652 state = INET6_IFADDR_STATE_DEAD;
3653 ifa->state = 0;
3654 if (!(ifa->flags & IFA_F_NODAD))
3655 ifa->flags |= IFA_F_TENTATIVE;
David Ahern38bd10c2016-04-21 20:56:12 -07003656
3657 rt = ifa->rt;
3658 ifa->rt = NULL;
David Ahernf1705ec2016-02-24 09:25:37 -08003659 } else {
3660 state = ifa->state;
3661 ifa->state = INET6_IFADDR_STATE_DEAD;
3662
Wei Yongjunc8822192016-07-28 16:19:03 +00003663 list_move(&ifa->if_list, &del_list);
David Ahernf1705ec2016-02-24 09:25:37 -08003664 }
3665
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003666 spin_unlock_bh(&ifa->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003667
David Ahern38bd10c2016-04-21 20:56:12 -07003668 if (rt)
3669 ip6_del_rt(rt);
3670
David S. Miller73a8bd72011-01-23 23:27:15 -08003671 if (state != INET6_IFADDR_STATE_DEAD) {
3672 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Cong Wangf88c91d2013-04-14 23:18:43 +08003673 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
Mike Manningea06f712016-07-22 18:32:11 +01003674 } else {
3675 if (idev->cnf.forwarding)
3676 addrconf_leave_anycast(ifa);
3677 addrconf_leave_solict(ifa->idev, &ifa->addr);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00003678 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003679
David S. Miller73a8bd72011-01-23 23:27:15 -08003680 write_lock_bh(&idev->lock);
3681 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003682
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 write_unlock_bh(&idev->lock);
3684
David Ahernf1705ec2016-02-24 09:25:37 -08003685 /* now clean up addresses to be removed */
3686 while (!list_empty(&del_list)) {
3687 ifa = list_first_entry(&del_list,
3688 struct inet6_ifaddr, if_list);
3689 list_del(&ifa->if_list);
3690
3691 in6_ifa_put(ifa);
3692 }
3693
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003694 /* Step 5: Discard anycast and multicast list */
3695 if (how) {
3696 ipv6_ac_destroy_dev(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 ipv6_mc_destroy_dev(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003698 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 ipv6_mc_down(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003703
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003704 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003705 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003706 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 neigh_parms_release(&nd_tbl, idev->nd_parms);
3708 neigh_ifdown(&nd_tbl, dev);
3709 in6_dev_put(idev);
3710 }
3711 return 0;
3712}
3713
3714static void addrconf_rs_timer(unsigned long data)
3715{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003716 struct inet6_dev *idev = (struct inet6_dev *)data;
Cong Wangcaf92bc2013-08-31 13:44:32 +08003717 struct net_device *dev = idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003718 struct in6_addr lladdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003720 write_lock(&idev->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00003721 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 goto out;
3723
Shmulik Ladkani9ba2add2012-12-02 01:44:53 +00003724 if (!ipv6_accept_ra(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003726
3727 /* Announcement received after solicitation was sent */
3728 if (idev->if_flags & IF_RA_RCVD)
3729 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003731 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
Cong Wangcaf92bc2013-08-31 13:44:32 +08003732 write_unlock(&idev->lock);
3733 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3734 ndisc_send_rs(dev, &lladdr,
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003735 &in6addr_linklocal_allrouters);
3736 else
Cong Wangcaf92bc2013-08-31 13:44:32 +08003737 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
Cong Wangcaf92bc2013-08-31 13:44:32 +08003739 write_lock(&idev->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003740 idev->rs_interval = rfc3315_s14_backoff_update(
3741 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003742 /* The wait after the last probe can be shorter */
3743 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3744 idev->cnf.rtr_solicits) ?
3745 idev->cnf.rtr_solicit_delay :
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003746 idev->rs_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 /*
3749 * Note: we do not support deprecated "all on-link"
3750 * assumption any longer.
3751 */
Joe Perches91df42b2012-05-15 14:11:54 +00003752 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 }
3754
3755out:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003756 write_unlock(&idev->lock);
Cong Wangcaf92bc2013-08-31 13:44:32 +08003757put:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003758 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759}
3760
3761/*
3762 * Duplicate Address Detection
3763 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003764static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3765{
3766 unsigned long rand_num;
3767 struct inet6_dev *idev = ifp->idev;
3768
Neil Horman95c385b2007-04-25 17:08:10 -07003769 if (ifp->flags & IFA_F_OPTIMISTIC)
3770 rand_num = 0;
3771 else
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003772 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
Neil Horman95c385b2007-04-25 17:08:10 -07003773
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003774 ifp->dad_probes = idev->cnf.dad_transmits;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003775 addrconf_mod_dad_work(ifp, rand_num);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003776}
3777
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003778static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779{
3780 struct inet6_dev *idev = ifp->idev;
3781 struct net_device *dev = idev->dev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003782 bool bump_id, notify = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783
3784 addrconf_join_solict(dev, &ifp->addr);
3785
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003786 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787
3788 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003789 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003790 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
3793 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09003794 idev->cnf.accept_dad < 1 ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003795 !(ifp->flags&IFA_F_TENTATIVE) ||
3796 ifp->flags & IFA_F_NODAD) {
Paolo Abeni764d3be2016-11-22 16:57:40 +01003797 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003798 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003799 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 read_unlock_bh(&idev->lock);
3801
Paolo Abeni764d3be2016-11-22 16:57:40 +01003802 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 return;
3804 }
3805
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003806 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003807 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003808 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003809 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003810 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003811 * - keep it tentative if it is a permanent address.
3812 * - otherwise, kill it.
3813 */
3814 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003815 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003816 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003817 }
Neil Horman95c385b2007-04-25 17:08:10 -07003818
3819 /*
3820 * Optimistic nodes can start receiving
3821 * Frames right away
3822 */
Erik Kline7fd25612014-10-28 18:11:14 +09003823 if (ifp->flags & IFA_F_OPTIMISTIC) {
Neil Horman95c385b2007-04-25 17:08:10 -07003824 ip6_ins_rt(ifp->rt);
Erik Kline7fd25612014-10-28 18:11:14 +09003825 if (ipv6_use_optimistic_addr(idev)) {
3826 /* Because optimistic nodes can use this address,
3827 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3828 */
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003829 notify = true;
Erik Kline7fd25612014-10-28 18:11:14 +09003830 }
3831 }
Neil Horman95c385b2007-04-25 17:08:10 -07003832
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003833 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003835 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 read_unlock_bh(&idev->lock);
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003837 if (notify)
3838 ipv6_ifa_notify(RTM_NEWADDR, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839}
3840
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003841static void addrconf_dad_start(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003843 bool begin_dad = false;
3844
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003845 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003846 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3847 ifp->state = INET6_IFADDR_STATE_PREDAD;
3848 begin_dad = true;
3849 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003850 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003851
3852 if (begin_dad)
3853 addrconf_mod_dad_work(ifp, 0);
3854}
3855
3856static void addrconf_dad_work(struct work_struct *w)
3857{
3858 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3859 struct inet6_ifaddr,
3860 dad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 struct inet6_dev *idev = ifp->idev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003862 bool bump_id, disable_ipv6 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 struct in6_addr mcaddr;
3864
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003865 enum {
3866 DAD_PROCESS,
3867 DAD_BEGIN,
3868 DAD_ABORT,
3869 } action = DAD_PROCESS;
3870
3871 rtnl_lock();
3872
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003873 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003874 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3875 action = DAD_BEGIN;
3876 ifp->state = INET6_IFADDR_STATE_DAD;
3877 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3878 action = DAD_ABORT;
3879 ifp->state = INET6_IFADDR_STATE_POSTDAD;
Mike Manning85b51b12016-08-18 14:39:40 +01003880
3881 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6 &&
3882 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
3883 struct in6_addr addr;
3884
3885 addr.s6_addr32[0] = htonl(0xfe800000);
3886 addr.s6_addr32[1] = 0;
3887
3888 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
3889 ipv6_addr_equal(&ifp->addr, &addr)) {
3890 /* DAD failed for link-local based on MAC */
3891 idev->cnf.disable_ipv6 = 1;
3892
3893 pr_info("%s: IPv6 being disabled!\n",
3894 ifp->idev->dev->name);
3895 disable_ipv6 = true;
3896 }
3897 }
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003898 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003899 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003900
3901 if (action == DAD_BEGIN) {
3902 addrconf_dad_begin(ifp);
3903 goto out;
3904 } else if (action == DAD_ABORT) {
Wei Yongjun751eb6b2016-09-05 16:06:31 +08003905 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003906 addrconf_dad_stop(ifp, 1);
Mike Manning85b51b12016-08-18 14:39:40 +01003907 if (disable_ipv6)
3908 addrconf_ifdown(idev->dev, 0);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003909 goto out;
3910 }
3911
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003912 if (!ifp->dad_probes && addrconf_dad_end(ifp))
Herbert Xuf2344a12010-05-18 15:55:27 -07003913 goto out;
3914
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003915 write_lock_bh(&idev->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003916 if (idev->dead || !(idev->if_flags & IF_READY)) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003917 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 goto out;
3919 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003920
3921 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003922 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3923 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003924 write_unlock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003925 goto out;
3926 }
3927
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003928 if (ifp->dad_probes == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 /*
3930 * DAD was successful
3931 */
3932
Paolo Abeni764d3be2016-11-22 16:57:40 +01003933 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003934 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003935 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003936 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
Paolo Abeni764d3be2016-11-22 16:57:40 +01003938 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
3940 goto out;
3941 }
3942
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003943 ifp->dad_probes--;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003944 addrconf_mod_dad_work(ifp,
3945 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
stephen hemminger21809fa2010-02-08 19:48:52 +00003946 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003947 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948
3949 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
Nicolas Dichtel304d8882015-11-27 18:17:05 +01003951 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952out:
3953 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003954 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955}
3956
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003957/* ifp->idev must be at least read locked */
3958static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3959{
3960 struct inet6_ifaddr *ifpiter;
3961 struct inet6_dev *idev = ifp->idev;
3962
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01003963 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3964 if (ifpiter->scope > IFA_LINK)
3965 break;
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003966 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3967 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3968 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
3969 IFA_F_PERMANENT)
3970 return false;
3971 }
3972 return true;
3973}
3974
Paolo Abeni764d3be2016-11-22 16:57:40 +01003975static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003977 struct net_device *dev = ifp->idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003978 struct in6_addr lladdr;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003979 bool send_rs, send_mld;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003980
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003981 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
3983 /*
3984 * Configure the address for reception. Now it is valid.
3985 */
3986
3987 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3988
Tore Anderson026359b2011-08-28 23:47:33 +00003989 /* If added prefix is link local and we are prepared to process
3990 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 */
3992
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003993 read_lock_bh(&ifp->idev->lock);
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003994 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003995 send_rs = send_mld &&
3996 ipv6_accept_ra(ifp->idev) &&
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003997 ifp->idev->cnf.rtr_solicits != 0 &&
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003998 (dev->flags&IFF_LOOPBACK) == 0;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003999 read_unlock_bh(&ifp->idev->lock);
4000
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004001 /* While dad is in progress mld report's source address is in6_addrany.
4002 * Resend with proper ll now.
4003 */
4004 if (send_mld)
4005 ipv6_mc_dad_complete(ifp->idev);
4006
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004007 if (send_rs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 /*
4009 * If a host as already performed a random delay
4010 * [...] as part of DAD [...] there is no need
4011 * to delay again before sending the first RS
4012 */
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004013 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004014 return;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004015 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004017 write_lock_bh(&ifp->idev->lock);
4018 spin_lock(&ifp->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004019 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4020 ifp->idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004021 ifp->idev->rs_probes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 ifp->idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004023 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004024 spin_unlock(&ifp->lock);
4025 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 }
Paolo Abeni764d3be2016-11-22 16:57:40 +01004027
4028 if (bump_id)
4029 rt_genid_bump_ipv6(dev_net(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030}
4031
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004032static void addrconf_dad_run(struct inet6_dev *idev)
4033{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004034 struct inet6_ifaddr *ifp;
4035
4036 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00004037 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00004038 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07004039 if (ifp->flags & IFA_F_TENTATIVE &&
4040 ifp->state == INET6_IFADDR_STATE_DAD)
4041 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00004042 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004043 }
4044 read_unlock_bh(&idev->lock);
4045}
4046
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047#ifdef CONFIG_PROC_FS
4048struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08004049 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004051 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052};
4053
Mihai Maruseac1d578302012-01-03 23:31:35 +00004054static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055{
4056 struct inet6_ifaddr *ifa = NULL;
4057 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004058 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00004059 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
Mihai Maruseac1d578302012-01-03 23:31:35 +00004061 /* initial bucket if pos is 0 */
4062 if (pos == 0) {
4063 state->bucket = 0;
4064 state->offset = 0;
4065 }
4066
4067 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08004068 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00004069 addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004070 if (!net_eq(dev_net(ifa->idev->dev), net))
4071 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004072 /* sync with offset */
4073 if (p < state->offset) {
4074 p++;
4075 continue;
4076 }
4077 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004078 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004079 }
4080
4081 /* prepare for next bucket */
4082 state->offset = 0;
4083 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004085 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086}
4087
stephen hemmingerc2e21292010-03-17 20:31:10 +00004088static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4089 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090{
4091 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004092 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
Sasha Levinb67bfe02013-02-27 17:06:00 -08004094 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004095 if (!net_eq(dev_net(ifa->idev->dev), net))
4096 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004097 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004098 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004099 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004100
4101 while (++state->bucket < IN6_ADDR_HSIZE) {
Mihai Maruseac1d578302012-01-03 23:31:35 +00004102 state->offset = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004103 hlist_for_each_entry_rcu_bh(ifa,
stephen hemmingerc2e21292010-03-17 20:31:10 +00004104 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004105 if (!net_eq(dev_net(ifa->idev->dev), net))
4106 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004107 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004108 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08004109 }
4110 }
4111
stephen hemmingerc2e21292010-03-17 20:31:10 +00004112 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113}
4114
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004116 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117{
stephen hemminger5c578ae2010-03-17 20:31:11 +00004118 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00004119 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120}
4121
4122static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4123{
4124 struct inet6_ifaddr *ifa;
4125
4126 ifa = if6_get_next(seq, v);
4127 ++*pos;
4128 return ifa;
4129}
4130
4131static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004132 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133{
stephen hemminger5c578ae2010-03-17 20:31:11 +00004134 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135}
4136
4137static int if6_seq_show(struct seq_file *seq, void *v)
4138{
4139 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Jiri Pirko971a3512013-12-10 13:56:29 +01004140 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07004141 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 ifp->idev->dev->ifindex,
4143 ifp->prefix_len,
4144 ifp->scope,
Jiri Pirko971a3512013-12-10 13:56:29 +01004145 (u8) ifp->flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 ifp->idev->dev->name);
4147 return 0;
4148}
4149
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004150static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 .start = if6_seq_start,
4152 .next = if6_seq_next,
4153 .show = if6_seq_show,
4154 .stop = if6_seq_stop,
4155};
4156
4157static int if6_seq_open(struct inode *inode, struct file *file)
4158{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004159 return seq_open_net(inode, file, &if6_seq_ops,
4160 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161}
4162
Arjan van de Ven9a321442007-02-12 00:55:35 -08004163static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 .owner = THIS_MODULE,
4165 .open = if6_seq_open,
4166 .read = seq_read,
4167 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004168 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169};
4170
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004171static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172{
Gao fengd4beaa62013-02-18 01:34:54 +00004173 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 return -ENOMEM;
4175 return 0;
4176}
4177
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004178static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08004179{
Gao fengece31ff2013-02-18 01:34:56 +00004180 remove_proc_entry("if_inet6", net->proc_net);
Daniel Lezcano3c400902008-01-10 22:42:49 -08004181}
4182
4183static struct pernet_operations if6_proc_net_ops = {
Ian Morris67ba4152014-08-24 21:53:10 +01004184 .init = if6_proc_net_init,
4185 .exit = if6_proc_net_exit,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004186};
4187
4188int __init if6_proc_init(void)
4189{
4190 return register_pernet_subsys(&if6_proc_net_ops);
4191}
4192
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193void if6_proc_exit(void)
4194{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004195 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196}
4197#endif /* CONFIG_PROC_FS */
4198
Amerigo Wang07a93622012-10-29 16:23:10 +00004199#if IS_ENABLED(CONFIG_IPV6_MIP6)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004200/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004201int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004202{
4203 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004204 struct inet6_ifaddr *ifp = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00004205 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004206
stephen hemminger5c578ae2010-03-17 20:31:11 +00004207 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08004208 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09004209 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08004210 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09004211 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004212 (ifp->flags & IFA_F_HOMEADDRESS)) {
4213 ret = 1;
4214 break;
4215 }
4216 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00004217 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004218 return ret;
4219}
4220#endif
4221
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222/*
4223 * Periodic address status verification
4224 */
4225
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004226static void addrconf_verify_rtnl(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004228 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 int i;
4231
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004232 ASSERT_RTNL();
4233
stephen hemminger5c578ae2010-03-17 20:31:11 +00004234 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004236 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004238 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004240 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241restart:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004242 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244
Yasushi Asanofad8da32013-12-31 12:04:19 +09004245 /* When setting preferred_lft to a value not zero or
4246 * infinity, while valid_lft is infinity
4247 * IFA_F_PERMANENT has a non-infinity life time.
4248 */
4249 if ((ifp->flags & IFA_F_PERMANENT) &&
4250 (ifp->prefered_lft == INFINITY_LIFE_TIME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 continue;
4252
4253 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004254 /* We try to batch several events at once. */
4255 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004257 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4258 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 spin_unlock(&ifp->lock);
4260 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 ipv6_del_addr(ifp);
4262 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004263 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4264 spin_unlock(&ifp->lock);
4265 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00004267 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 int deprecate = 0;
4269
4270 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4271 deprecate = 1;
4272 ifp->flags |= IFA_F_DEPRECATED;
4273 }
4274
Yasushi Asanofad8da32013-12-31 12:04:19 +09004275 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4276 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 next = ifp->tstamp + ifp->valid_lft * HZ;
4278
4279 spin_unlock(&ifp->lock);
4280
4281 if (deprecate) {
4282 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283
4284 ipv6_ifa_notify(0, ifp);
4285 in6_ifa_put(ifp);
4286 goto restart;
4287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4289 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00004290 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4291 ifp->idev->cnf.dad_transmits *
Jiri Pirko1f9248e52013-12-07 19:26:53 +01004292 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
stephen hemminger88949cf2010-03-17 20:31:17 +00004293
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 if (age >= ifp->prefered_lft - regen_advance) {
4295 struct inet6_ifaddr *ifpub = ifp->ifpub;
4296 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4297 next = ifp->tstamp + ifp->prefered_lft * HZ;
4298 if (!ifp->regen_count && ifpub) {
4299 ifp->regen_count++;
4300 in6_ifa_hold(ifp);
4301 in6_ifa_hold(ifpub);
4302 spin_unlock(&ifp->lock);
stephen hemminger5c578ae2010-03-17 20:31:11 +00004303
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08004304 spin_lock(&ifpub->lock);
4305 ifpub->regen_count = 0;
4306 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 ipv6_create_tempaddr(ifpub, ifp);
4308 in6_ifa_put(ifpub);
4309 in6_ifa_put(ifp);
4310 goto restart;
4311 }
4312 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4313 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4314 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 } else {
4316 /* ifp->prefered_lft <= ifp->valid_lft */
4317 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4318 next = ifp->tstamp + ifp->prefered_lft * HZ;
4319 spin_unlock(&ifp->lock);
4320 }
4321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 }
4323
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004324 next_sec = round_jiffies_up(next);
4325 next_sched = next;
4326
4327 /* If rounded timeout is accurate enough, accept it. */
4328 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4329 next_sched = next_sec;
4330
4331 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4332 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4333 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4334
dingtianhongba3542e2013-08-17 10:27:04 +08004335 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4336 now, next, next_sec, next_sched);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004337 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
stephen hemminger5c578ae2010-03-17 20:31:11 +00004338 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339}
4340
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004341static void addrconf_verify_work(struct work_struct *w)
4342{
4343 rtnl_lock();
4344 addrconf_verify_rtnl();
4345 rtnl_unlock();
4346}
4347
4348static void addrconf_verify(void)
4349{
4350 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4351}
4352
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004353static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4354 struct in6_addr **peer_pfx)
Thomas Graf461d8832006-09-18 00:09:49 -07004355{
4356 struct in6_addr *pfx = NULL;
4357
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004358 *peer_pfx = NULL;
4359
Thomas Graf461d8832006-09-18 00:09:49 -07004360 if (addr)
4361 pfx = nla_data(addr);
4362
4363 if (local) {
4364 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004365 *peer_pfx = pfx;
4366 pfx = nla_data(local);
Thomas Graf461d8832006-09-18 00:09:49 -07004367 }
4368
4369 return pfx;
4370}
4371
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004372static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07004373 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4374 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4375 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
Jiri Pirko479840f2013-12-06 09:45:21 +01004376 [IFA_FLAGS] = { .len = sizeof(u32) },
Thomas Graf461d8832006-09-18 00:09:49 -07004377};
4378
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379static int
Thomas Graf661d2962013-03-21 07:45:29 +00004380inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004382 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07004383 struct ifaddrmsg *ifm;
4384 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004385 struct in6_addr *pfx, *peer_pfx;
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004386 u32 ifa_flags;
Thomas Grafb933f712006-09-18 00:10:19 -07004387 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
Thomas Grafb933f712006-09-18 00:10:19 -07004389 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4390 if (err < 0)
4391 return err;
4392
4393 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004394 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004395 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 return -EINVAL;
4397
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004398 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4399
4400 /* We ignore other flags so far. */
4401 ifa_flags &= IFA_F_MANAGETEMPADDR;
4402
4403 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4404 ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405}
4406
Jiri Pirko479840f2013-12-06 09:45:21 +01004407static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004408 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004409{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004410 u32 flags;
4411 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004412 unsigned long timeout;
Jiri Pirko53bd6742013-12-06 09:45:22 +01004413 bool was_managetempaddr;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004414 bool had_prefixroute;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09004415
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004416 ASSERT_RTNL();
4417
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004418 if (!valid_lft || (prefered_lft > valid_lft))
4419 return -EINVAL;
4420
Jiri Pirko53bd6742013-12-06 09:45:22 +01004421 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4422 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4423 return -EINVAL;
4424
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004425 timeout = addrconf_timeout_fixup(valid_lft, HZ);
4426 if (addrconf_finite_timeout(timeout)) {
4427 expires = jiffies_to_clock_t(timeout * HZ);
4428 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004429 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004430 } else {
4431 expires = 0;
4432 flags = 0;
4433 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004434 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004435
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004436 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4437 if (addrconf_finite_timeout(timeout)) {
4438 if (timeout == 0)
4439 ifa_flags |= IFA_F_DEPRECATED;
4440 prefered_lft = timeout;
4441 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004442
4443 spin_lock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004444 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004445 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4446 !(ifp->flags & IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004447 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
Thomas Haller761aac72014-01-15 15:36:58 +01004448 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4449 IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004450 ifp->flags |= ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004451 ifp->tstamp = jiffies;
4452 ifp->valid_lft = valid_lft;
4453 ifp->prefered_lft = prefered_lft;
4454
4455 spin_unlock_bh(&ifp->lock);
4456 if (!(ifp->flags&IFA_F_TENTATIVE))
4457 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004458
Thomas Haller761aac72014-01-15 15:36:58 +01004459 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4460 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4461 expires, flags);
Thomas Haller5b84efe2014-01-15 15:36:59 +01004462 } else if (had_prefixroute) {
4463 enum cleanup_prefix_rt_t action;
4464 unsigned long rt_expires;
4465
4466 write_lock_bh(&ifp->idev->lock);
4467 action = check_cleanup_prefix_route(ifp, &rt_expires);
4468 write_unlock_bh(&ifp->idev->lock);
4469
4470 if (action != CLEANUP_PREFIX_RT_NOP) {
4471 cleanup_prefix_route(ifp, rt_expires,
4472 action == CLEANUP_PREFIX_RT_DEL);
4473 }
Thomas Haller761aac72014-01-15 15:36:58 +01004474 }
Jiri Pirko53bd6742013-12-06 09:45:22 +01004475
4476 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4477 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4478 valid_lft = prefered_lft = 0;
4479 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4480 !was_managetempaddr, jiffies);
4481 }
4482
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004483 addrconf_verify_rtnl();
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004484
4485 return 0;
4486}
4487
4488static int
Thomas Graf661d2962013-03-21 07:45:29 +00004489inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004491 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07004492 struct ifaddrmsg *ifm;
4493 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004494 struct in6_addr *pfx, *peer_pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07004495 struct inet6_ifaddr *ifa;
4496 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004497 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
Jiri Pirko479840f2013-12-06 09:45:21 +01004498 u32 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07004499 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
Thomas Graf461d8832006-09-18 00:09:49 -07004501 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4502 if (err < 0)
4503 return err;
4504
4505 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004506 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004507 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 return -EINVAL;
4509
Thomas Graf461d8832006-09-18 00:09:49 -07004510 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004511 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07004512
4513 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004514 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07004515 preferred_lft = ci->ifa_prefered;
4516 } else {
4517 preferred_lft = INFINITY_LIFE_TIME;
4518 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004519 }
4520
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004521 dev = __dev_get_by_index(net, ifm->ifa_index);
Ian Morris63159f22015-03-29 14:00:04 +01004522 if (!dev)
Thomas Graf7198f8c2006-09-18 00:13:46 -07004523 return -ENODEV;
4524
Jiri Pirko479840f2013-12-06 09:45:21 +01004525 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4526
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004527 /* We ignore other flags so far. */
Thomas Haller761aac72014-01-15 15:36:58 +01004528 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
Madhu Challa93a714d2015-02-25 09:58:35 -08004529 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004530
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08004531 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Ian Morris63159f22015-03-29 14:00:04 +01004532 if (!ifa) {
Thomas Graf7198f8c2006-09-18 00:13:46 -07004533 /*
4534 * It would be best to check for !NLM_F_CREATE here but
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08004535 * userspace already relies on not having to provide this.
Thomas Graf7198f8c2006-09-18 00:13:46 -07004536 */
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004537 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004538 ifm->ifa_prefixlen, ifa_flags,
4539 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004540 }
4541
Thomas Graf7198f8c2006-09-18 00:13:46 -07004542 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4543 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4544 err = -EEXIST;
4545 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004546 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07004547
4548 in6_ifa_put(ifa);
4549
4550 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551}
4552
Jiri Pirko479840f2013-12-06 09:45:21 +01004553static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
Thomas Graf101bb222006-09-18 00:11:52 -07004554 u8 scope, int ifindex)
4555{
4556 struct ifaddrmsg *ifm;
4557
4558 ifm = nlmsg_data(nlh);
4559 ifm->ifa_family = AF_INET6;
4560 ifm->ifa_prefixlen = prefixlen;
4561 ifm->ifa_flags = flags;
4562 ifm->ifa_scope = scope;
4563 ifm->ifa_index = ifindex;
4564}
4565
Thomas Graf85486af2006-09-18 00:11:24 -07004566static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4567 unsigned long tstamp, u32 preferred, u32 valid)
4568{
4569 struct ifa_cacheinfo ci;
4570
Thomas Graf18a31e12010-11-17 04:12:02 +00004571 ci.cstamp = cstamp_delta(cstamp);
4572 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07004573 ci.ifa_prefered = preferred;
4574 ci.ifa_valid = valid;
4575
4576 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4577}
4578
Thomas Graf101bb222006-09-18 00:11:52 -07004579static inline int rt_scope(int ifa_scope)
4580{
4581 if (ifa_scope & IFA_HOST)
4582 return RT_SCOPE_HOST;
4583 else if (ifa_scope & IFA_LINK)
4584 return RT_SCOPE_LINK;
4585 else if (ifa_scope & IFA_SITE)
4586 return RT_SCOPE_SITE;
4587 else
4588 return RT_SCOPE_UNIVERSE;
4589}
4590
Thomas Graf0ab68032006-09-18 00:12:35 -07004591static inline int inet6_ifaddr_msgsize(void)
4592{
Thomas Graf339bf982006-11-10 14:10:15 -08004593 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004594 + nla_total_size(16) /* IFA_LOCAL */
Thomas Graf339bf982006-11-10 14:10:15 -08004595 + nla_total_size(16) /* IFA_ADDRESS */
Jiri Pirko479840f2013-12-06 09:45:21 +01004596 + nla_total_size(sizeof(struct ifa_cacheinfo))
4597 + nla_total_size(4) /* IFA_FLAGS */;
Thomas Graf0ab68032006-09-18 00:12:35 -07004598}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004599
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004601 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07004604 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
Eric W. Biederman15e47302012-09-07 20:12:54 +00004606 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004607 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004608 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004609
Thomas Graf101bb222006-09-18 00:11:52 -07004610 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4611 ifa->idev->dev->ifindex);
4612
Yasushi Asanofad8da32013-12-31 12:04:19 +09004613 if (!((ifa->flags&IFA_F_PERMANENT) &&
4614 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
Thomas Graf85486af2006-09-18 00:11:24 -07004615 preferred = ifa->prefered_lft;
4616 valid = ifa->valid_lft;
4617 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00004619 if (preferred > tval)
4620 preferred -= tval;
4621 else
4622 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00004623 if (valid != INFINITY_LIFE_TIME) {
4624 if (valid > tval)
4625 valid -= tval;
4626 else
4627 valid = 0;
4628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 }
4630 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07004631 preferred = INFINITY_LIFE_TIME;
4632 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 }
Thomas Graf85486af2006-09-18 00:11:24 -07004634
Cong Wang7996c792013-05-22 05:41:06 +00004635 if (!ipv6_addr_any(&ifa->peer_addr)) {
Jiri Benc930345e2015-03-29 16:59:25 +02004636 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4637 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004638 goto error;
4639 } else
Jiri Benc930345e2015-03-29 16:59:25 +02004640 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004641 goto error;
4642
4643 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4644 goto error;
Thomas Graf85486af2006-09-18 00:11:24 -07004645
Jiri Pirko479840f2013-12-06 09:45:21 +01004646 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4647 goto error;
4648
Johannes Berg053c0952015-01-16 22:09:00 +01004649 nlmsg_end(skb, nlh);
4650 return 0;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004651
4652error:
4653 nlmsg_cancel(skb, nlh);
4654 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655}
4656
4657static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004658 u32 portid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004661 u8 scope = RT_SCOPE_UNIVERSE;
4662 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
Thomas Graf101bb222006-09-18 00:11:52 -07004664 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4665 scope = RT_SCOPE_SITE;
4666
Eric W. Biederman15e47302012-09-07 20:12:54 +00004667 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004668 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004669 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004670
Thomas Graf101bb222006-09-18 00:11:52 -07004671 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004672 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004673 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004674 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4675 nlmsg_cancel(skb, nlh);
4676 return -EMSGSIZE;
4677 }
Thomas Graf85486af2006-09-18 00:11:24 -07004678
Johannes Berg053c0952015-01-16 22:09:00 +01004679 nlmsg_end(skb, nlh);
4680 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681}
4682
4683static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004684 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004687 u8 scope = RT_SCOPE_UNIVERSE;
4688 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689
Thomas Graf101bb222006-09-18 00:11:52 -07004690 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4691 scope = RT_SCOPE_SITE;
4692
Eric W. Biederman15e47302012-09-07 20:12:54 +00004693 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004694 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004695 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004696
Thomas Graf101bb222006-09-18 00:11:52 -07004697 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004698 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004699 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004700 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4701 nlmsg_cancel(skb, nlh);
4702 return -EMSGSIZE;
4703 }
Thomas Graf85486af2006-09-18 00:11:24 -07004704
Johannes Berg053c0952015-01-16 22:09:00 +01004705 nlmsg_end(skb, nlh);
4706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707}
4708
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004709enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 UNICAST_ADDR,
4711 MULTICAST_ADDR,
4712 ANYCAST_ADDR,
4713};
4714
Eric Dumazet234b27c2009-11-12 04:11:50 +00004715/* called with rcu_read_lock() */
4716static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4717 struct netlink_callback *cb, enum addr_type_t type,
4718 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 struct ifmcaddr6 *ifmca;
4721 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004722 int err = 1;
4723 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724
Eric Dumazet234b27c2009-11-12 04:11:50 +00004725 read_lock_bh(&idev->lock);
4726 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00004727 case UNICAST_ADDR: {
4728 struct inet6_ifaddr *ifa;
4729
Eric Dumazet234b27c2009-11-12 04:11:50 +00004730 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00004731 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4732 if (++ip_idx < s_ip_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004733 continue;
4734 err = inet6_fill_ifaddr(skb, ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004735 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004736 cb->nlh->nlmsg_seq,
4737 RTM_NEWADDR,
4738 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004739 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004740 break;
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004741 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004743 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00004744 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004745 case MULTICAST_ADDR:
4746 /* multicast address */
4747 for (ifmca = idev->mc_list; ifmca;
4748 ifmca = ifmca->next, ip_idx++) {
4749 if (ip_idx < s_ip_idx)
4750 continue;
4751 err = inet6_fill_ifmcaddr(skb, ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004752 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004753 cb->nlh->nlmsg_seq,
4754 RTM_GETMULTICAST,
4755 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004756 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004757 break;
4758 }
4759 break;
4760 case ANYCAST_ADDR:
4761 /* anycast address */
4762 for (ifaca = idev->ac_list; ifaca;
4763 ifaca = ifaca->aca_next, ip_idx++) {
4764 if (ip_idx < s_ip_idx)
4765 continue;
4766 err = inet6_fill_ifacaddr(skb, ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004767 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004768 cb->nlh->nlmsg_seq,
4769 RTM_GETANYCAST,
4770 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004771 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004772 break;
4773 }
4774 break;
4775 default:
4776 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004778 read_unlock_bh(&idev->lock);
4779 *p_ip_idx = ip_idx;
4780 return err;
4781}
4782
4783static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4784 enum addr_type_t type)
4785{
4786 struct net *net = sock_net(skb->sk);
4787 int h, s_h;
4788 int idx, ip_idx;
4789 int s_idx, s_ip_idx;
4790 struct net_device *dev;
4791 struct inet6_dev *idev;
4792 struct hlist_head *head;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004793
4794 s_h = cb->args[0];
4795 s_idx = idx = cb->args[1];
4796 s_ip_idx = ip_idx = cb->args[2];
4797
4798 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004799 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004800 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4801 idx = 0;
4802 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08004803 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet234b27c2009-11-12 04:11:50 +00004804 if (idx < s_idx)
4805 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07004806 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004807 s_ip_idx = 0;
4808 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004809 idev = __in6_dev_get(dev);
4810 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004811 goto cont;
4812
4813 if (in6_dump_addrs(idev, skb, cb, type,
David S. Miller7b46a642015-01-18 23:36:08 -05004814 s_ip_idx, &ip_idx) < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004815 goto done;
4816cont:
4817 idx++;
4818 }
4819 }
4820done:
4821 rcu_read_unlock();
4822 cb->args[0] = h;
4823 cb->args[1] = idx;
4824 cb->args[2] = ip_idx;
4825
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 return skb->len;
4827}
4828
4829static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4830{
4831 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004832
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 return inet6_dump_addr(skb, cb, type);
4834}
4835
4836static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4837{
4838 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004839
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 return inet6_dump_addr(skb, cb, type);
4841}
4842
4843
4844static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4845{
4846 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004847
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 return inet6_dump_addr(skb, cb, type);
4849}
4850
Thomas Graf661d2962013-03-21 07:45:29 +00004851static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004852{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004853 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004854 struct ifaddrmsg *ifm;
4855 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004856 struct in6_addr *addr = NULL, *peer;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004857 struct net_device *dev = NULL;
4858 struct inet6_ifaddr *ifa;
4859 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004860 int err;
4861
Thomas Graf1b29fc22006-09-18 00:10:50 -07004862 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4863 if (err < 0)
4864 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004865
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004866 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
Ian Morris63159f22015-03-29 14:00:04 +01004867 if (!addr) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004868 err = -EINVAL;
4869 goto errout;
4870 }
4871
4872 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004873 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004874 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004875
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004876 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4877 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004878 err = -EADDRNOTAVAIL;
4879 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004880 }
4881
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004882 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4883 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004884 err = -ENOBUFS;
4885 goto errout_ifa;
4886 }
4887
Eric W. Biederman15e47302012-09-07 20:12:54 +00004888 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004889 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004890 if (err < 0) {
4891 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4892 WARN_ON(err == -EMSGSIZE);
4893 kfree_skb(skb);
4894 goto errout_ifa;
4895 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00004896 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004897errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004898 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004899errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004900 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004901}
4902
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4904{
4905 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004906 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07004907 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908
Thomas Graf0ab68032006-09-18 00:12:35 -07004909 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01004910 if (!skb)
Thomas Graf5d620262006-08-15 00:35:02 -07004911 goto errout;
4912
4913 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004914 if (err < 0) {
4915 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4916 WARN_ON(err == -EMSGSIZE);
4917 kfree_skb(skb);
4918 goto errout;
4919 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004920 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4921 return;
Thomas Graf5d620262006-08-15 00:35:02 -07004922errout:
4923 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004924 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925}
4926
Dave Jonesb6f99a22007-03-22 12:27:49 -07004927static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 __s32 *array, int bytes)
4929{
Thomas Graf04561c12006-11-14 19:53:58 -08004930 BUG_ON(bytes < (DEVCONF_MAX * 4));
4931
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 memset(array, 0, bytes);
4933 array[DEVCONF_FORWARDING] = cnf->forwarding;
4934 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4935 array[DEVCONF_MTU6] = cnf->mtu6;
4936 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4937 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4938 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4939 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4940 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00004941 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4942 jiffies_to_msecs(cnf->rtr_solicit_interval);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004943 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
4944 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
Thomas Graf93908d12010-11-17 01:44:24 +00004945 array[DEVCONF_RTR_SOLICIT_DELAY] =
4946 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02004948 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4949 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4950 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4951 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4953 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4954 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4955 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4956 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004958 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
Hangbin Liu8013d1d2015-07-30 14:28:42 +08004959 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004960 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004961#ifdef CONFIG_IPV6_ROUTER_PREF
4962 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00004963 array[DEVCONF_RTR_PROBE_INTERVAL] =
4964 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08004965#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004966 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4967#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004968#endif
Lorenzo Colitti9fcb87d2014-03-26 19:35:41 +09004969 array[DEVCONF_ACCEPT_RA_RT_TABLE] = cnf->accept_ra_rt_table;
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004970 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004971 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07004972#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4973 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
Erik Kline7fd25612014-10-28 18:11:14 +09004974 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
Neil Horman95c385b2007-04-25 17:08:10 -07004975#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004976#ifdef CONFIG_IPV6_MROUTE
4977 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4978#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09004979 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09004980 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00004981 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00004982 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02004983 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
Ben Greeard9333192014-06-25 14:44:53 -07004984 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
Harout Hedeshianc2943f12015-01-20 10:06:05 -07004985 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
Andy Gospodarek35103d12015-08-13 10:39:01 -04004986 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01004987 /* we omit DEVCONF_STABLE_SECRET for now */
Erik Kline3985e8a2015-07-22 16:38:25 +09004988 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
Johannes Bergabbc3042016-02-04 13:31:19 +01004989 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
Johannes Berg7a02bf82016-02-04 13:31:20 +01004990 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
David Ahernf1705ec2016-02-24 09:25:37 -08004991 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992}
4993
Thomas Grafb382b192010-11-16 04:33:57 +00004994static inline size_t inet6_ifla6_size(void)
4995{
4996 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4997 + nla_total_size(sizeof(struct ifla_cacheinfo))
4998 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4999 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005000 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5001 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
Thomas Grafb382b192010-11-16 04:33:57 +00005002}
5003
Thomas Graf339bf982006-11-10 14:10:15 -08005004static inline size_t inet6_if_nlmsg_size(void)
5005{
5006 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5007 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5008 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5009 + nla_total_size(4) /* IFLA_MTU */
5010 + nla_total_size(4) /* IFLA_LINK */
Andy Gospodarek03443382015-08-13 15:26:35 -04005011 + nla_total_size(1) /* IFLA_OPERSTATE */
Thomas Grafb382b192010-11-16 04:33:57 +00005012 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08005013}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005014
Eric Dumazetbe281e52011-05-19 01:14:23 +00005015static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Jia Heaca05672016-09-30 11:29:03 +08005016 int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005017{
5018 int i;
Jia Heaca05672016-09-30 11:29:03 +08005019 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005020 BUG_ON(pad < 0);
5021
5022 /* Use put_unaligned() because stats may not be aligned for u64. */
Jia Heaca05672016-09-30 11:29:03 +08005023 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5024 for (i = 1; i < ICMP6_MIB_MAX; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00005025 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005026
Jia Heaca05672016-09-30 11:29:03 +08005027 memset(&stats[ICMP6_MIB_MAX], 0, pad);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005028}
5029
WANG Cong698365f2014-05-05 15:55:55 -07005030static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305031 int bytes, size_t syncpoff)
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005032{
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305033 int i, c;
5034 u64 buff[IPSTATS_MIB_MAX];
5035 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5036
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005037 BUG_ON(pad < 0);
5038
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305039 memset(buff, 0, sizeof(buff));
5040 buff[0] = IPSTATS_MIB_MAX;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005041
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305042 for_each_possible_cpu(c) {
5043 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5044 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5045 }
5046
5047 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5048 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005049}
5050
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005051static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5052 int bytes)
5053{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005054 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005055 case IFLA_INET6_STATS:
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305056 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5057 offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005058 break;
5059 case IFLA_INET6_ICMP6STATS:
Jia Heaca05672016-09-30 11:29:03 +08005060 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005061 break;
5062 }
5063}
5064
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005065static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5066 u32 ext_filter_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005068 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08005069 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070
David S. Millerc78679e2012-04-01 20:27:33 -04005071 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5072 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08005074 ci.tstamp = cstamp_delta(idev->tstamp);
5075 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
Jiri Pirko1f9248e52013-12-07 19:26:53 +01005076 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
David S. Millerc78679e2012-04-01 20:27:33 -04005077 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5078 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005079 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
Ian Morris63159f22015-03-29 14:00:04 +01005080 if (!nla)
Thomas Graf04561c12006-11-14 19:53:58 -08005081 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005082 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005084 /* XXX - MC not implemented */
5085
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005086 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5087 return 0;
5088
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005089 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005090 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005091 goto nla_put_failure;
5092 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5093
5094 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005095 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005096 goto nla_put_failure;
5097 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005099 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
Ian Morris63159f22015-03-29 14:00:04 +01005100 if (!nla)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005101 goto nla_put_failure;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005102
5103 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode))
5104 goto nla_put_failure;
5105
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005106 read_lock_bh(&idev->lock);
5107 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5108 read_unlock_bh(&idev->lock);
5109
Thomas Grafb382b192010-11-16 04:33:57 +00005110 return 0;
5111
5112nla_put_failure:
5113 return -EMSGSIZE;
5114}
5115
Arad, Ronenb1974ed2015-10-19 09:23:28 -07005116static size_t inet6_get_link_af_size(const struct net_device *dev,
5117 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005118{
5119 if (!__in6_dev_get(dev))
5120 return 0;
5121
5122 return inet6_ifla6_size();
5123}
5124
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005125static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5126 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005127{
5128 struct inet6_dev *idev = __in6_dev_get(dev);
5129
5130 if (!idev)
5131 return -ENODATA;
5132
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005133 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005134 return -EMSGSIZE;
5135
5136 return 0;
5137}
5138
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005139static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5140{
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005141 struct inet6_ifaddr *ifp;
5142 struct net_device *dev = idev->dev;
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005143 bool clear_token, update_rs = false;
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005144 struct in6_addr ll_addr;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005145
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005146 ASSERT_RTNL();
5147
Ian Morris63159f22015-03-29 14:00:04 +01005148 if (!token)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005149 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005150 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5151 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005152 if (!ipv6_accept_ra(idev))
5153 return -EINVAL;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005154 if (idev->cnf.rtr_solicits == 0)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005155 return -EINVAL;
5156
5157 write_lock_bh(&idev->lock);
5158
5159 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5160 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5161
5162 write_unlock_bh(&idev->lock);
5163
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005164 clear_token = ipv6_addr_any(token);
5165 if (clear_token)
5166 goto update_lft;
5167
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005168 if (!idev->dead && (idev->if_flags & IF_READY) &&
5169 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5170 IFA_F_OPTIMISTIC)) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005171 /* If we're not ready, then normal ifup will take care
5172 * of this. Otherwise, we need to request our rs here.
5173 */
5174 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5175 update_rs = true;
5176 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005177
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005178update_lft:
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005179 write_lock_bh(&idev->lock);
Daniel Borkmannfc403832013-04-09 03:47:15 +00005180
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005181 if (update_rs) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005182 idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005183 idev->rs_interval = rfc3315_s14_backoff_init(
5184 idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005185 idev->rs_probes = 1;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005186 addrconf_mod_rs_timer(idev, idev->rs_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005187 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005188
5189 /* Well, that's kinda nasty ... */
5190 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5191 spin_lock(&ifp->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00005192 if (ifp->tokenized) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005193 ifp->valid_lft = 0;
5194 ifp->prefered_lft = 0;
5195 }
5196 spin_unlock(&ifp->lock);
5197 }
5198
5199 write_unlock_bh(&idev->lock);
Lubomir Rintelb2ed64a2014-10-27 17:39:16 +01005200 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005201 addrconf_verify_rtnl();
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005202 return 0;
5203}
5204
Daniel Borkmann11b1f822015-02-05 14:39:11 +01005205static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5206 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5207 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5208};
5209
5210static int inet6_validate_link_af(const struct net_device *dev,
5211 const struct nlattr *nla)
5212{
5213 struct nlattr *tb[IFLA_INET6_MAX + 1];
5214
5215 if (dev && !__in6_dev_get(dev))
5216 return -EAFNOSUPPORT;
5217
5218 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
5219}
5220
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005221static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5222{
5223 int err = -EINVAL;
5224 struct inet6_dev *idev = __in6_dev_get(dev);
5225 struct nlattr *tb[IFLA_INET6_MAX + 1];
5226
5227 if (!idev)
5228 return -EAFNOSUPPORT;
5229
5230 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
5231 BUG();
5232
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005233 if (tb[IFLA_INET6_TOKEN]) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005234 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005235 if (err)
5236 return err;
5237 }
5238
5239 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5240 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5241
5242 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005243 mode != IN6_ADDR_GEN_MODE_NONE &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01005244 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5245 mode != IN6_ADDR_GEN_MODE_RANDOM)
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005246 return -EINVAL;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005247
5248 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5249 !idev->cnf.stable_secret.initialized &&
5250 !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized)
5251 return -EINVAL;
5252
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005253 idev->addr_gen_mode = mode;
5254 err = 0;
5255 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005256
5257 return err;
5258}
5259
Thomas Grafb382b192010-11-16 04:33:57 +00005260static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005261 u32 portid, u32 seq, int event, unsigned int flags)
Thomas Grafb382b192010-11-16 04:33:57 +00005262{
5263 struct net_device *dev = idev->dev;
5264 struct ifinfomsg *hdr;
5265 struct nlmsghdr *nlh;
5266 void *protoinfo;
5267
Eric W. Biederman15e47302012-09-07 20:12:54 +00005268 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005269 if (!nlh)
Thomas Grafb382b192010-11-16 04:33:57 +00005270 return -EMSGSIZE;
5271
5272 hdr = nlmsg_data(nlh);
5273 hdr->ifi_family = AF_INET6;
5274 hdr->__ifi_pad = 0;
5275 hdr->ifi_type = dev->type;
5276 hdr->ifi_index = dev->ifindex;
5277 hdr->ifi_flags = dev_get_flags(dev);
5278 hdr->ifi_change = 0;
5279
David S. Millerc78679e2012-04-01 20:27:33 -04005280 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5281 (dev->addr_len &&
5282 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5283 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02005284 (dev->ifindex != dev_get_iflink(dev) &&
Andy Gospodarek03443382015-08-13 15:26:35 -04005285 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5286 nla_put_u8(skb, IFLA_OPERSTATE,
5287 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
David S. Millerc78679e2012-04-01 20:27:33 -04005288 goto nla_put_failure;
Thomas Grafb382b192010-11-16 04:33:57 +00005289 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
Ian Morris63159f22015-03-29 14:00:04 +01005290 if (!protoinfo)
Thomas Grafb382b192010-11-16 04:33:57 +00005291 goto nla_put_failure;
5292
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005293 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005294 goto nla_put_failure;
5295
Thomas Graf04561c12006-11-14 19:53:58 -08005296 nla_nest_end(skb, protoinfo);
Johannes Berg053c0952015-01-16 22:09:00 +01005297 nlmsg_end(skb, nlh);
5298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299
Thomas Graf04561c12006-11-14 19:53:58 -08005300nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005301 nlmsg_cancel(skb, nlh);
5302 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303}
5304
5305static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5306{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005307 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00005308 int h, s_h;
David S. Miller434a8a582009-11-11 18:53:00 -08005309 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 struct net_device *dev;
5311 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00005312 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313
Eric Dumazet84d26972009-11-09 12:11:28 +00005314 s_h = cb->args[0];
5315 s_idx = cb->args[1];
5316
5317 rcu_read_lock();
5318 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5319 idx = 0;
5320 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08005321 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet84d26972009-11-09 12:11:28 +00005322 if (idx < s_idx)
5323 goto cont;
5324 idev = __in6_dev_get(dev);
5325 if (!idev)
5326 goto cont;
5327 if (inet6_fill_ifinfo(skb, idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005328 NETLINK_CB(cb->skb).portid,
Eric Dumazet84d26972009-11-09 12:11:28 +00005329 cb->nlh->nlmsg_seq,
Johannes Berg053c0952015-01-16 22:09:00 +01005330 RTM_NEWLINK, NLM_F_MULTI) < 0)
Eric Dumazet84d26972009-11-09 12:11:28 +00005331 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07005332cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00005333 idx++;
5334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 }
Eric Dumazet84d26972009-11-09 12:11:28 +00005336out:
5337 rcu_read_unlock();
5338 cb->args[1] = idx;
5339 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340
5341 return skb->len;
5342}
5343
5344void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5345{
5346 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005347 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005348 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005349
Thomas Graf339bf982006-11-10 14:10:15 -08005350 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005351 if (!skb)
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005352 goto errout;
5353
5354 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005355 if (err < 0) {
5356 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5357 WARN_ON(err == -EMSGSIZE);
5358 kfree_skb(skb);
5359 goto errout;
5360 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005361 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005362 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005363errout:
5364 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005365 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366}
5367
Thomas Graf339bf982006-11-10 14:10:15 -08005368static inline size_t inet6_prefix_nlmsg_size(void)
5369{
5370 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5371 + nla_total_size(sizeof(struct in6_addr))
5372 + nla_total_size(sizeof(struct prefix_cacheinfo));
5373}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005374
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005376 struct prefix_info *pinfo, u32 portid, u32 seq,
Thomas Graf6051e2f2006-11-14 19:54:19 -08005377 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378{
Thomas Graf6051e2f2006-11-14 19:54:19 -08005379 struct prefixmsg *pmsg;
5380 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 struct prefix_cacheinfo ci;
5382
Eric W. Biederman15e47302012-09-07 20:12:54 +00005383 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005384 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005385 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08005386
5387 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07005389 pmsg->prefix_pad1 = 0;
5390 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 pmsg->prefix_ifindex = idev->dev->ifindex;
5392 pmsg->prefix_len = pinfo->prefix_len;
5393 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07005394 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 pmsg->prefix_flags = 0;
5396 if (pinfo->onlink)
5397 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5398 if (pinfo->autoconf)
5399 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5400
David S. Millerc78679e2012-04-01 20:27:33 -04005401 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5402 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 ci.preferred_time = ntohl(pinfo->prefered);
5404 ci.valid_time = ntohl(pinfo->valid);
David S. Millerc78679e2012-04-01 20:27:33 -04005405 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5406 goto nla_put_failure;
Johannes Berg053c0952015-01-16 22:09:00 +01005407 nlmsg_end(skb, nlh);
5408 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409
Thomas Graf6051e2f2006-11-14 19:54:19 -08005410nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005411 nlmsg_cancel(skb, nlh);
5412 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413}
5414
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005415static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 struct prefix_info *pinfo)
5417{
5418 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005419 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005420 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421
Thomas Graf339bf982006-11-10 14:10:15 -08005422 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005423 if (!skb)
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005424 goto errout;
5425
5426 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005427 if (err < 0) {
5428 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5429 WARN_ON(err == -EMSGSIZE);
5430 kfree_skb(skb);
5431 goto errout;
5432 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005433 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5434 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005435errout:
5436 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08005437 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438}
5439
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5441{
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005442 struct net *net = dev_net(ifp->idev->dev);
5443
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005444 if (event)
5445 ASSERT_RTNL();
5446
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5448
5449 switch (event) {
5450 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07005451 /*
5452 * If the address was optimistic
5453 * we inserted the route at the start of
5454 * our DAD process, so we don't need
5455 * to do it again
5456 */
5457 if (!(ifp->rt->rt6i_node))
5458 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 if (ifp->idev->cnf.forwarding)
5460 addrconf_join_anycast(ifp);
Cong Wang7996c792013-05-22 05:41:06 +00005461 if (!ipv6_addr_any(&ifp->peer_addr))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005462 addrconf_prefix_route(&ifp->peer_addr, 128,
5463 ifp->idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 break;
5465 case RTM_DELADDR:
5466 if (ifp->idev->cnf.forwarding)
5467 addrconf_leave_anycast(ifp);
5468 addrconf_leave_solict(ifp->idev, &ifp->addr);
Cong Wang7996c792013-05-22 05:41:06 +00005469 if (!ipv6_addr_any(&ifp->peer_addr)) {
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005470 struct rt6_info *rt;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005471
Nicolas Dichtele7478df2014-09-03 23:59:22 +02005472 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5473 ifp->idev->dev, 0, 0);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07005474 if (rt)
5475 ip6_del_rt(rt);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005476 }
David Ahern38bd10c2016-04-21 20:56:12 -07005477 if (ifp->rt) {
5478 dst_hold(&ifp->rt->dst);
5479 ip6_del_rt(ifp->rt);
5480 }
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02005481 rt_genid_bump_ipv6(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 break;
5483 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005484 atomic_inc(&net->ipv6.dev_addr_genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485}
5486
5487static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5488{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005489 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 if (likely(ifp->idev->dead == 0))
5491 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005492 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493}
5494
5495#ifdef CONFIG_SYSCTL
5496
5497static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005498int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 void __user *buffer, size_t *lenp, loff_t *ppos)
5500{
5501 int *valp = ctl->data;
5502 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005503 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005504 struct ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 int ret;
5506
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005507 /*
5508 * ctl->data points to idev->cnf.forwarding, we should
5509 * not modify it until we get the rtnl lock.
5510 */
5511 lctl = *ctl;
5512 lctl.data = &val;
5513
5514 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08005516 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00005517 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005518 if (ret)
5519 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005520 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521}
5522
Marcelo Leitner77751422015-02-23 11:17:13 -03005523static
5524int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5525 void __user *buffer, size_t *lenp, loff_t *ppos)
5526{
5527 struct inet6_dev *idev = ctl->extra1;
5528 int min_mtu = IPV6_MIN_MTU;
5529 struct ctl_table lctl;
5530
5531 lctl = *ctl;
5532 lctl.extra1 = &min_mtu;
5533 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5534
5535 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5536}
5537
Brian Haley56d417b2009-06-01 03:07:33 -07005538static void dev_disable_change(struct inet6_dev *idev)
5539{
Cong Wang75538c22013-05-29 11:30:50 +08005540 struct netdev_notifier_info info;
5541
Brian Haley56d417b2009-06-01 03:07:33 -07005542 if (!idev || !idev->dev)
5543 return;
5544
Cong Wang75538c22013-05-29 11:30:50 +08005545 netdev_notifier_info_init(&info, idev->dev);
Brian Haley56d417b2009-06-01 03:07:33 -07005546 if (idev->cnf.disable_ipv6)
Cong Wang75538c22013-05-29 11:30:50 +08005547 addrconf_notify(NULL, NETDEV_DOWN, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005548 else
Cong Wang75538c22013-05-29 11:30:50 +08005549 addrconf_notify(NULL, NETDEV_UP, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005550}
5551
5552static void addrconf_disable_change(struct net *net, __s32 newf)
5553{
5554 struct net_device *dev;
5555 struct inet6_dev *idev;
5556
Kefeng Wang79453ab2017-01-19 16:26:21 +08005557 for_each_netdev(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07005558 idev = __in6_dev_get(dev);
5559 if (idev) {
5560 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5561 idev->cnf.disable_ipv6 = newf;
5562 if (changed)
5563 dev_disable_change(idev);
5564 }
Brian Haley56d417b2009-06-01 03:07:33 -07005565 }
Brian Haley56d417b2009-06-01 03:07:33 -07005566}
5567
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005568static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07005569{
5570 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005571 int old;
5572
5573 if (!rtnl_trylock())
5574 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07005575
5576 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005577 old = *p;
5578 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07005579
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005580 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5581 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07005582 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005583 }
Brian Haley56d417b2009-06-01 03:07:33 -07005584
5585 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07005586 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5587 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005588 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07005589 dev_disable_change((struct inet6_dev *)table->extra1);
5590
5591 rtnl_unlock();
5592 return 0;
5593}
5594
5595static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005596int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07005597 void __user *buffer, size_t *lenp, loff_t *ppos)
5598{
5599 int *valp = ctl->data;
5600 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005601 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005602 struct ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07005603 int ret;
5604
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005605 /*
5606 * ctl->data points to idev->cnf.disable_ipv6, we should
5607 * not modify it until we get the rtnl lock.
5608 */
5609 lctl = *ctl;
5610 lctl.data = &val;
5611
5612 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07005613
5614 if (write)
5615 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005616 if (ret)
5617 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07005618 return ret;
5619}
5620
stephen hemmingerc92d5492013-12-17 22:37:14 -08005621static
5622int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5623 void __user *buffer, size_t *lenp, loff_t *ppos)
5624{
5625 int *valp = ctl->data;
5626 int ret;
5627 int old, new;
5628
5629 old = *valp;
5630 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5631 new = *valp;
5632
5633 if (write && old != new) {
5634 struct net *net = ctl->extra2;
5635
5636 if (!rtnl_trylock())
5637 return restart_syscall();
5638
5639 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5640 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5641 NETCONFA_IFINDEX_DEFAULT,
5642 net->ipv6.devconf_dflt);
5643 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5644 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5645 NETCONFA_IFINDEX_ALL,
5646 net->ipv6.devconf_all);
5647 else {
5648 struct inet6_dev *idev = ctl->extra1;
5649
5650 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5651 idev->dev->ifindex,
5652 &idev->cnf);
5653 }
5654 rtnl_unlock();
5655 }
5656
5657 return ret;
5658}
5659
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005660static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5661 void __user *buffer, size_t *lenp,
5662 loff_t *ppos)
5663{
5664 int err;
5665 struct in6_addr addr;
5666 char str[IPV6_MAX_STRLEN];
5667 struct ctl_table lctl = *ctl;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005668 struct net *net = ctl->extra2;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005669 struct ipv6_stable_secret *secret = ctl->data;
5670
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005671 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5672 return -EIO;
5673
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005674 lctl.maxlen = IPV6_MAX_STRLEN;
5675 lctl.data = str;
5676
5677 if (!rtnl_trylock())
5678 return restart_syscall();
5679
5680 if (!write && !secret->initialized) {
5681 err = -EIO;
5682 goto out;
5683 }
5684
WANG Cong5449a5c2015-12-21 10:55:45 -08005685 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
5686 if (err >= sizeof(str)) {
5687 err = -EIO;
5688 goto out;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005689 }
5690
5691 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5692 if (err || !write)
5693 goto out;
5694
5695 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5696 err = -EIO;
5697 goto out;
5698 }
5699
5700 secret->initialized = true;
5701 secret->secret = addr;
5702
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005703 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5704 struct net_device *dev;
5705
5706 for_each_netdev(net, dev) {
5707 struct inet6_dev *idev = __in6_dev_get(dev);
5708
5709 if (idev) {
5710 idev->addr_gen_mode =
5711 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5712 }
5713 }
5714 } else {
5715 struct inet6_dev *idev = ctl->extra1;
5716
5717 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5718 }
5719
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005720out:
5721 rtnl_unlock();
5722
5723 return err;
5724}
stephen hemmingerc92d5492013-12-17 22:37:14 -08005725
Andy Gospodarek35103d12015-08-13 10:39:01 -04005726static
5727int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
5728 int write,
5729 void __user *buffer,
5730 size_t *lenp,
5731 loff_t *ppos)
5732{
5733 int *valp = ctl->data;
5734 int val = *valp;
5735 loff_t pos = *ppos;
5736 struct ctl_table lctl;
5737 int ret;
5738
5739 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
5740 * we should not modify it until we get the rtnl lock.
5741 */
5742 lctl = *ctl;
5743 lctl.data = &val;
5744
5745 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5746
5747 if (write)
5748 ret = addrconf_fixup_linkdown(ctl, valp, val);
5749 if (ret)
5750 *ppos = pos;
5751 return ret;
5752}
5753
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07005754static int minus_one = -1;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07005755static const int one = 1;
5756static const int two_five_five = 255;
5757
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03005758static const struct ctl_table addrconf_sysctl[] = {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005759 {
5760 .procname = "forwarding",
5761 .data = &ipv6_devconf.forwarding,
5762 .maxlen = sizeof(int),
5763 .mode = 0644,
5764 .proc_handler = addrconf_sysctl_forward,
5765 },
5766 {
5767 .procname = "hop_limit",
5768 .data = &ipv6_devconf.hop_limit,
5769 .maxlen = sizeof(int),
5770 .mode = 0644,
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07005771 .proc_handler = proc_dointvec_minmax,
5772 .extra1 = (void *)&one,
5773 .extra2 = (void *)&two_five_five,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005774 },
5775 {
5776 .procname = "mtu",
5777 .data = &ipv6_devconf.mtu6,
5778 .maxlen = sizeof(int),
5779 .mode = 0644,
5780 .proc_handler = addrconf_sysctl_mtu,
5781 },
5782 {
5783 .procname = "accept_ra",
5784 .data = &ipv6_devconf.accept_ra,
5785 .maxlen = sizeof(int),
5786 .mode = 0644,
5787 .proc_handler = proc_dointvec,
5788 },
5789 {
5790 .procname = "accept_redirects",
5791 .data = &ipv6_devconf.accept_redirects,
5792 .maxlen = sizeof(int),
5793 .mode = 0644,
5794 .proc_handler = proc_dointvec,
5795 },
5796 {
5797 .procname = "autoconf",
5798 .data = &ipv6_devconf.autoconf,
5799 .maxlen = sizeof(int),
5800 .mode = 0644,
5801 .proc_handler = proc_dointvec,
5802 },
5803 {
5804 .procname = "dad_transmits",
5805 .data = &ipv6_devconf.dad_transmits,
5806 .maxlen = sizeof(int),
5807 .mode = 0644,
5808 .proc_handler = proc_dointvec,
5809 },
5810 {
5811 .procname = "router_solicitations",
5812 .data = &ipv6_devconf.rtr_solicits,
5813 .maxlen = sizeof(int),
5814 .mode = 0644,
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07005815 .proc_handler = proc_dointvec_minmax,
5816 .extra1 = &minus_one,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005817 },
5818 {
5819 .procname = "router_solicitation_interval",
5820 .data = &ipv6_devconf.rtr_solicit_interval,
5821 .maxlen = sizeof(int),
5822 .mode = 0644,
5823 .proc_handler = proc_dointvec_jiffies,
5824 },
5825 {
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005826 .procname = "router_solicitation_max_interval",
5827 .data = &ipv6_devconf.rtr_solicit_max_interval,
5828 .maxlen = sizeof(int),
5829 .mode = 0644,
5830 .proc_handler = proc_dointvec_jiffies,
5831 },
5832 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005833 .procname = "router_solicitation_delay",
5834 .data = &ipv6_devconf.rtr_solicit_delay,
5835 .maxlen = sizeof(int),
5836 .mode = 0644,
5837 .proc_handler = proc_dointvec_jiffies,
5838 },
5839 {
5840 .procname = "force_mld_version",
5841 .data = &ipv6_devconf.force_mld_version,
5842 .maxlen = sizeof(int),
5843 .mode = 0644,
5844 .proc_handler = proc_dointvec,
5845 },
5846 {
5847 .procname = "mldv1_unsolicited_report_interval",
5848 .data =
5849 &ipv6_devconf.mldv1_unsolicited_report_interval,
5850 .maxlen = sizeof(int),
5851 .mode = 0644,
5852 .proc_handler = proc_dointvec_ms_jiffies,
5853 },
5854 {
5855 .procname = "mldv2_unsolicited_report_interval",
5856 .data =
5857 &ipv6_devconf.mldv2_unsolicited_report_interval,
5858 .maxlen = sizeof(int),
5859 .mode = 0644,
5860 .proc_handler = proc_dointvec_ms_jiffies,
5861 },
5862 {
5863 .procname = "use_tempaddr",
5864 .data = &ipv6_devconf.use_tempaddr,
5865 .maxlen = sizeof(int),
5866 .mode = 0644,
5867 .proc_handler = proc_dointvec,
5868 },
5869 {
5870 .procname = "temp_valid_lft",
5871 .data = &ipv6_devconf.temp_valid_lft,
5872 .maxlen = sizeof(int),
5873 .mode = 0644,
5874 .proc_handler = proc_dointvec,
5875 },
5876 {
5877 .procname = "temp_prefered_lft",
5878 .data = &ipv6_devconf.temp_prefered_lft,
5879 .maxlen = sizeof(int),
5880 .mode = 0644,
5881 .proc_handler = proc_dointvec,
5882 },
5883 {
5884 .procname = "regen_max_retry",
5885 .data = &ipv6_devconf.regen_max_retry,
5886 .maxlen = sizeof(int),
5887 .mode = 0644,
5888 .proc_handler = proc_dointvec,
5889 },
5890 {
5891 .procname = "max_desync_factor",
5892 .data = &ipv6_devconf.max_desync_factor,
5893 .maxlen = sizeof(int),
5894 .mode = 0644,
5895 .proc_handler = proc_dointvec,
5896 },
5897 {
5898 .procname = "max_addresses",
5899 .data = &ipv6_devconf.max_addresses,
5900 .maxlen = sizeof(int),
5901 .mode = 0644,
5902 .proc_handler = proc_dointvec,
5903 },
5904 {
5905 .procname = "accept_ra_defrtr",
5906 .data = &ipv6_devconf.accept_ra_defrtr,
5907 .maxlen = sizeof(int),
5908 .mode = 0644,
5909 .proc_handler = proc_dointvec,
5910 },
5911 {
5912 .procname = "accept_ra_min_hop_limit",
5913 .data = &ipv6_devconf.accept_ra_min_hop_limit,
5914 .maxlen = sizeof(int),
5915 .mode = 0644,
5916 .proc_handler = proc_dointvec,
5917 },
5918 {
5919 .procname = "accept_ra_pinfo",
5920 .data = &ipv6_devconf.accept_ra_pinfo,
5921 .maxlen = sizeof(int),
5922 .mode = 0644,
5923 .proc_handler = proc_dointvec,
5924 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005925#ifdef CONFIG_IPV6_ROUTER_PREF
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005926 {
5927 .procname = "accept_ra_rtr_pref",
5928 .data = &ipv6_devconf.accept_ra_rtr_pref,
5929 .maxlen = sizeof(int),
5930 .mode = 0644,
5931 .proc_handler = proc_dointvec,
5932 },
5933 {
5934 .procname = "router_probe_interval",
5935 .data = &ipv6_devconf.rtr_probe_interval,
5936 .maxlen = sizeof(int),
5937 .mode = 0644,
5938 .proc_handler = proc_dointvec_jiffies,
5939 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08005940#ifdef CONFIG_IPV6_ROUTE_INFO
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005941 {
5942 .procname = "accept_ra_rt_info_max_plen",
5943 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
5944 .maxlen = sizeof(int),
5945 .mode = 0644,
5946 .proc_handler = proc_dointvec,
5947 },
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08005948#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005949#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005950 {
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +09005951 .procname = "accept_ra_rt_table",
5952 .data = &ipv6_devconf.accept_ra_rt_table,
5953 .maxlen = sizeof(int),
5954 .mode = 0644,
5955 .proc_handler = proc_dointvec,
5956 },
5957 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005958 .procname = "proxy_ndp",
5959 .data = &ipv6_devconf.proxy_ndp,
5960 .maxlen = sizeof(int),
5961 .mode = 0644,
5962 .proc_handler = addrconf_sysctl_proxy_ndp,
5963 },
5964 {
5965 .procname = "accept_source_route",
5966 .data = &ipv6_devconf.accept_source_route,
5967 .maxlen = sizeof(int),
5968 .mode = 0644,
5969 .proc_handler = proc_dointvec,
5970 },
Lorenzo Colitti9fcb87d2014-03-26 19:35:41 +09005971 {
5972 .procname = "accept_ra_rt_table",
5973 .data = &ipv6_devconf.accept_ra_rt_table,
5974 .maxlen = sizeof(int),
5975 .mode = 0644,
5976 .proc_handler = proc_dointvec,
5977 },
Neil Horman95c385b2007-04-25 17:08:10 -07005978#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005979 {
5980 .procname = "optimistic_dad",
5981 .data = &ipv6_devconf.optimistic_dad,
5982 .maxlen = sizeof(int),
5983 .mode = 0644,
5984 .proc_handler = proc_dointvec,
5985 },
5986 {
5987 .procname = "use_optimistic",
5988 .data = &ipv6_devconf.use_optimistic,
5989 .maxlen = sizeof(int),
5990 .mode = 0644,
5991 .proc_handler = proc_dointvec,
5992 },
Neil Horman95c385b2007-04-25 17:08:10 -07005993#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005994#ifdef CONFIG_IPV6_MROUTE
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005995 {
5996 .procname = "mc_forwarding",
5997 .data = &ipv6_devconf.mc_forwarding,
5998 .maxlen = sizeof(int),
5999 .mode = 0444,
6000 .proc_handler = proc_dointvec,
6001 },
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006002#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006003 {
6004 .procname = "disable_ipv6",
6005 .data = &ipv6_devconf.disable_ipv6,
6006 .maxlen = sizeof(int),
6007 .mode = 0644,
6008 .proc_handler = addrconf_sysctl_disable,
6009 },
6010 {
6011 .procname = "accept_dad",
6012 .data = &ipv6_devconf.accept_dad,
6013 .maxlen = sizeof(int),
6014 .mode = 0644,
6015 .proc_handler = proc_dointvec,
6016 },
6017 {
6018 .procname = "force_tllao",
6019 .data = &ipv6_devconf.force_tllao,
6020 .maxlen = sizeof(int),
6021 .mode = 0644,
6022 .proc_handler = proc_dointvec
6023 },
6024 {
6025 .procname = "ndisc_notify",
6026 .data = &ipv6_devconf.ndisc_notify,
6027 .maxlen = sizeof(int),
6028 .mode = 0644,
6029 .proc_handler = proc_dointvec
6030 },
6031 {
6032 .procname = "suppress_frag_ndisc",
6033 .data = &ipv6_devconf.suppress_frag_ndisc,
6034 .maxlen = sizeof(int),
6035 .mode = 0644,
6036 .proc_handler = proc_dointvec
6037 },
6038 {
6039 .procname = "accept_ra_from_local",
6040 .data = &ipv6_devconf.accept_ra_from_local,
6041 .maxlen = sizeof(int),
6042 .mode = 0644,
6043 .proc_handler = proc_dointvec,
6044 },
6045 {
6046 .procname = "accept_ra_mtu",
6047 .data = &ipv6_devconf.accept_ra_mtu,
6048 .maxlen = sizeof(int),
6049 .mode = 0644,
6050 .proc_handler = proc_dointvec,
6051 },
6052 {
6053 .procname = "stable_secret",
6054 .data = &ipv6_devconf.stable_secret,
6055 .maxlen = IPV6_MAX_STRLEN,
6056 .mode = 0600,
6057 .proc_handler = addrconf_sysctl_stable_secret,
6058 },
6059 {
6060 .procname = "use_oif_addrs_only",
6061 .data = &ipv6_devconf.use_oif_addrs_only,
6062 .maxlen = sizeof(int),
6063 .mode = 0644,
6064 .proc_handler = proc_dointvec,
6065 },
6066 {
6067 .procname = "ignore_routes_with_linkdown",
6068 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6069 .maxlen = sizeof(int),
6070 .mode = 0644,
6071 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6072 },
6073 {
6074 .procname = "drop_unicast_in_l2_multicast",
6075 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6076 .maxlen = sizeof(int),
6077 .mode = 0644,
6078 .proc_handler = proc_dointvec,
6079 },
6080 {
6081 .procname = "drop_unsolicited_na",
6082 .data = &ipv6_devconf.drop_unsolicited_na,
6083 .maxlen = sizeof(int),
6084 .mode = 0644,
6085 .proc_handler = proc_dointvec,
6086 },
6087 {
6088 .procname = "keep_addr_on_down",
6089 .data = &ipv6_devconf.keep_addr_on_down,
6090 .maxlen = sizeof(int),
6091 .mode = 0644,
6092 .proc_handler = proc_dointvec,
David Ahernf1705ec2016-02-24 09:25:37 -08006093
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006094 },
6095 {
6096 /* sentinel */
6097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098};
6099
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08006100static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006101 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102{
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006103 int i, ifindex;
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006104 struct ctl_table *table;
Eric W. Biederman6105e292012-04-19 13:41:24 +00006105 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11006106
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006107 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6108 if (!table)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006109 goto out;
6110
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006111 for (i = 0; table[i].data; i++) {
6112 table[i].data += (char *)p - (char *)&ipv6_devconf;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006113 /* If one of these is already set, then it is not safe to
6114 * overwrite either of them: this makes proc_dointvec_minmax
6115 * usable.
6116 */
6117 if (!table[i].extra1 && !table[i].extra2) {
6118 table[i].extra1 = idev; /* embedded; no ref */
6119 table[i].extra2 = net;
6120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122
Eric W. Biederman6105e292012-04-19 13:41:24 +00006123 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006125 p->sysctl_header = register_net_sysctl(net, path, table);
6126 if (!p->sysctl_header)
Eric W. Biederman6105e292012-04-19 13:41:24 +00006127 goto free;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006128
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006129 if (!strcmp(dev_name, "all"))
6130 ifindex = NETCONFA_IFINDEX_ALL;
6131 else if (!strcmp(dev_name, "default"))
6132 ifindex = NETCONFA_IFINDEX_DEFAULT;
6133 else
6134 ifindex = idev->dev->ifindex;
6135 inet6_netconf_notify_devconf(net, NETCONFA_ALL, ifindex, p);
Pavel Emelyanov95897312008-01-10 17:41:45 -08006136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006138free:
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006139 kfree(table);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006140out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08006141 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142}
6143
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006144static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
6145{
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006146 struct ctl_table *table;
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006147
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006148 if (!p->sysctl_header)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006149 return;
6150
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006151 table = p->sysctl_header->ctl_table_arg;
6152 unregister_net_sysctl_table(p->sysctl_header);
6153 p->sysctl_header = NULL;
6154 kfree(table);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006155}
6156
WANG Conga317a2f2014-07-25 15:25:09 -07006157static int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006158{
WANG Conga317a2f2014-07-25 15:25:09 -07006159 int err;
6160
6161 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6162 return -EINVAL;
6163
6164 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6165 &ndisc_ifinfo_sysctl_change);
6166 if (err)
6167 return err;
6168 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6169 idev, &idev->cnf);
6170 if (err)
6171 neigh_sysctl_unregister(idev->nd_parms);
6172
6173 return err;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006174}
6175
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006176static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006178 __addrconf_sysctl_unregister(&idev->cnf);
6179 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180}
6181
6182
6183#endif
6184
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006185static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006186{
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006187 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006188 struct ipv6_devconf *all, *dflt;
6189
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006190 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006191 if (!all)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006192 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006193
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006194 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006195 if (!dflt)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006196 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006197
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006198 /* these will be inherited by all namespaces */
6199 dflt->autoconf = ipv6_defaults.autoconf;
6200 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006201
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006202 dflt->stable_secret.initialized = false;
6203 all->stable_secret.initialized = false;
6204
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006205 net->ipv6.devconf_all = all;
6206 net->ipv6.devconf_dflt = dflt;
6207
6208#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006209 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006210 if (err < 0)
6211 goto err_reg_all;
6212
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006213 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006214 if (err < 0)
6215 goto err_reg_dflt;
6216#endif
6217 return 0;
6218
6219#ifdef CONFIG_SYSCTL
6220err_reg_dflt:
6221 __addrconf_sysctl_unregister(all);
6222err_reg_all:
6223 kfree(dflt);
6224#endif
6225err_alloc_dflt:
6226 kfree(all);
6227err_alloc_all:
6228 return err;
6229}
6230
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006231static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006232{
6233#ifdef CONFIG_SYSCTL
6234 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
6235 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
6236#endif
zhuyj73cf0e92014-11-26 10:25:58 +08006237 kfree(net->ipv6.devconf_dflt);
6238 kfree(net->ipv6.devconf_all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006239}
6240
6241static struct pernet_operations addrconf_ops = {
6242 .init = addrconf_init_net,
6243 .exit = addrconf_exit_net,
6244};
6245
Daniel Borkmann207895f2015-01-29 12:15:03 +01006246static struct rtnl_af_ops inet6_ops __read_mostly = {
Thomas Grafb382b192010-11-16 04:33:57 +00006247 .family = AF_INET6,
6248 .fill_link_af = inet6_fill_link_af,
6249 .get_link_af_size = inet6_get_link_af_size,
Daniel Borkmann11b1f822015-02-05 14:39:11 +01006250 .validate_link_af = inet6_validate_link_af,
Daniel Borkmannf53adae2013-04-08 04:01:30 +00006251 .set_link_af = inet6_set_link_af,
Thomas Grafb382b192010-11-16 04:33:57 +00006252};
6253
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254/*
6255 * Init / cleanup code
6256 */
6257
6258int __init addrconf_init(void)
6259{
WANG Conga317a2f2014-07-25 15:25:09 -07006260 struct inet6_dev *idev;
stephen hemmingerc2e21292010-03-17 20:31:10 +00006261 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006262
Stephen Hemmingere21e8462010-03-20 16:09:01 -07006263 err = ipv6_addr_label_init();
6264 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00006265 pr_crit("%s: cannot initialize default policy table: %d\n",
6266 __func__, err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006267 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006270 err = register_pernet_subsys(&addrconf_ops);
6271 if (err < 0)
6272 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006273
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006274 addrconf_wq = create_workqueue("ipv6_addrconf");
6275 if (!addrconf_wq) {
6276 err = -ENOMEM;
6277 goto out_nowq;
6278 }
6279
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 /* The addrconf netdev notifier requires that loopback_dev
6281 * has it's ipv6 private information allocated and setup
6282 * before it can bring up and give link-local addresses
6283 * to other devices which are up.
6284 *
6285 * Unfortunately, loopback_dev is not necessarily the first
6286 * entry in the global dev_base list of net devices. In fact,
6287 * it is likely to be the very last entry on that list.
6288 * So this causes the notifier registry below to try and
6289 * give link-local addresses to all devices besides loopback_dev
6290 * first, then loopback_dev, which cases all the non-loopback_dev
6291 * devices to fail to get a link-local address.
6292 *
6293 * So, as a temporary fix, allocate the ipv6 structure for
6294 * loopback_dev first by hand.
6295 * Longer term, all of the dependencies ipv6 has upon the loopback
6296 * device and it being up should be removed.
6297 */
6298 rtnl_lock();
WANG Conga317a2f2014-07-25 15:25:09 -07006299 idev = ipv6_add_dev(init_net.loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 rtnl_unlock();
WANG Conga317a2f2014-07-25 15:25:09 -07006301 if (IS_ERR(idev)) {
6302 err = PTR_ERR(idev);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006303 goto errlo;
WANG Conga317a2f2014-07-25 15:25:09 -07006304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305
stephen hemmingerc2e21292010-03-17 20:31:10 +00006306 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6307 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6308
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 register_netdevice_notifier(&ipv6_dev_notf);
6310
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006311 addrconf_verify();
Thomas Grafc127ea22007-03-22 11:58:32 -07006312
stephen hemminger3678a9d2013-12-30 10:41:32 -08006313 rtnl_af_register(&inet6_ops);
Thomas Grafb382b192010-11-16 04:33:57 +00006314
Greg Rosec7ac8672011-06-10 01:27:09 +00006315 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6316 NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006317 if (err < 0)
6318 goto errout;
6319
6320 /* Only the first call to __rtnl_register can fail */
Greg Rosec7ac8672011-06-10 01:27:09 +00006321 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
6322 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
6323 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6324 inet6_dump_ifaddr, NULL);
6325 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6326 inet6_dump_ifmcaddr, NULL);
6327 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6328 inet6_dump_ifacaddr, NULL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +00006329 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
Nicolas Dichtel7a674202013-03-05 23:42:06 +00006330 inet6_netconf_dump_devconf, NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006331
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006332 ipv6_addr_label_rtnl_register();
6333
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07006335errout:
Thomas Grafb382b192010-11-16 04:33:57 +00006336 rtnl_af_unregister(&inet6_ops);
Thomas Grafc127ea22007-03-22 11:58:32 -07006337 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006338errlo:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006339 destroy_workqueue(addrconf_wq);
6340out_nowq:
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006341 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006342out_addrlabel:
6343 ipv6_addr_label_cleanup();
6344out:
Thomas Grafc127ea22007-03-22 11:58:32 -07006345 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346}
6347
Daniel Lezcano09f77092007-12-13 05:34:58 -08006348void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006350 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 int i;
6352
6353 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006354 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006355 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356
6357 rtnl_lock();
6358
Thomas Grafb382b192010-11-16 04:33:57 +00006359 __rtnl_af_unregister(&inet6_ops);
6360
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006361 /* clean dev list */
6362 for_each_netdev(&init_net, dev) {
6363 if (__in6_dev_get(dev) == NULL)
6364 continue;
6365 addrconf_ifdown(dev, 1);
6366 }
6367 addrconf_ifdown(init_net.loopback_dev, 2);
6368
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370 * Check hash table.
6371 */
stephen hemminger5c578ae2010-03-17 20:31:11 +00006372 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00006373 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6374 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578ae2010-03-17 20:31:11 +00006375 spin_unlock_bh(&addrconf_hash_lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006376 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 rtnl_unlock();
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006378
6379 destroy_workqueue(addrconf_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380}