blob: cbd9343751a2538e8373d3696cd8ac2a2c8ddeca [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
Sorin Dumitrueb7e0572012-08-30 02:01:45 +0000150static 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 -0700152static void ipv6_regen_rndid(unsigned long data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900154static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static int ipv6_count_addresses(struct inet6_dev *idev);
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +0100156static int ipv6_generate_stable_address(struct in6_addr *addr,
157 u8 dad_count,
158 const struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160/*
161 * Configured unicast address hash table
162 */
stephen hemmingerc2e21292010-03-17 20:31:10 +0000163static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
stephen hemminger5c578aed2010-03-17 20:31:11 +0000164static DEFINE_SPINLOCK(addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100166static void addrconf_verify(void);
167static void addrconf_verify_rtnl(void);
168static void addrconf_verify_work(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100170static struct workqueue_struct *addrconf_wq;
171static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
174static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
175
Jiri Pirko93d9b7d2010-03-10 10:28:56 +0000176static void addrconf_type_change(struct net_device *dev,
177 unsigned long event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178static int addrconf_ifdown(struct net_device *dev, int how);
179
Romain Kuntz21caa662013-01-09 21:06:03 +0000180static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
181 int plen,
182 const struct net_device *dev,
183 u32 flags, u32 noflags);
184
David S. Millercf22f9a2012-04-14 21:37:40 -0400185static void addrconf_dad_start(struct inet6_ifaddr *ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100186static void addrconf_dad_work(struct work_struct *w);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900188static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static void addrconf_rs_timer(unsigned long data);
190static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
191static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
192
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900193static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct prefix_info *pinfo);
David S. Miller3e81c6d2010-03-20 16:18:00 -0700195static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
196 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Adrian Bunk888c8482008-07-22 14:21:58 -0700198static struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 .forwarding = 0,
200 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
201 .mtu6 = IPV6_MIN_MTU,
202 .accept_ra = 1,
203 .accept_redirects = 1,
204 .autoconf = 1,
205 .force_mld_version = 0,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200206 .mldv1_unsolicited_report_interval = 10 * HZ,
207 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 .dad_transmits = 1,
209 .rtr_solicits = MAX_RTR_SOLICITATIONS,
210 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700211 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Ian Morris67ba4152014-08-24 21:53:10 +0100213 .use_tempaddr = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 .temp_valid_lft = TEMP_VALID_LIFETIME,
215 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
216 .regen_max_retry = REGEN_MAX_RETRY,
217 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800219 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700220 .accept_ra_from_local = 0,
Hangbin Liu8013d1d72015-07-30 14:28:42 +0800221 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800222 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800223#ifdef CONFIG_IPV6_ROUTER_PREF
224 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800225 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800226#ifdef CONFIG_IPV6_ROUTE_INFO
227 .accept_ra_rt_info_max_plen = 0,
228#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800229#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700230 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700231 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900232 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900233 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200234 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700235 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100236 .stable_secret = {
237 .initialized = false,
Erik Kline3985e8a2015-07-22 16:38:25 +0900238 },
239 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400240 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800241 .keep_addr_on_down = 0,
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 Liu8013d1d72015-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 Hideaki52e163562006-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
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700276 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700277 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900278 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900279 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200280 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700281 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100282 .stable_secret = {
283 .initialized = false,
284 },
Erik Kline3985e8a2015-07-22 16:38:25 +0900285 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400286 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800287 .keep_addr_on_down = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288};
289
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700290/* Check if a valid qdisc is available */
David S. Miller05297942008-07-08 23:01:27 -0700291static inline bool addrconf_qdisc_ok(const struct net_device *dev)
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700292{
David S. Miller05297942008-07-08 23:01:27 -0700293 return !qdisc_tx_is_noop(dev);
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700294}
295
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200296static void addrconf_del_rs_timer(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200298 if (del_timer(&idev->rs_timer))
299 __in6_dev_put(idev);
300}
301
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100302static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200303{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100304 if (cancel_delayed_work(&ifp->dad_work))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 __in6_ifa_put(ifp);
306}
307
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200308static void addrconf_mod_rs_timer(struct inet6_dev *idev,
309 unsigned long when)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200311 if (!timer_pending(&idev->rs_timer))
312 in6_dev_hold(idev);
313 mod_timer(&idev->rs_timer, jiffies + when);
314}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100316static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
317 unsigned long delay)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200318{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100319 if (!delayed_work_pending(&ifp->dad_work))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200320 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100321 mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700324static int snmp6_alloc_dev(struct inet6_dev *idev)
325{
John Stultz827da442013-10-07 15:51:58 -0700326 int i;
327
WANG Cong698365f2014-05-05 15:55:55 -0700328 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
329 if (!idev->stats.ipv6)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700330 goto err_ip;
John Stultz827da442013-10-07 15:51:58 -0700331
332 for_each_possible_cpu(i) {
333 struct ipstats_mib *addrconf_stats;
WANG Cong698365f2014-05-05 15:55:55 -0700334 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
John Stultz827da442013-10-07 15:51:58 -0700335 u64_stats_init(&addrconf_stats->syncp);
John Stultz827da442013-10-07 15:51:58 -0700336 }
337
338
Eric Dumazetbe281e52011-05-19 01:14:23 +0000339 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
340 GFP_KERNEL);
341 if (!idev->stats.icmpv6dev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700342 goto err_icmp;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000343 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
344 GFP_KERNEL);
345 if (!idev->stats.icmpv6msgdev)
David L Stevens14878f72007-09-16 16:52:35 -0700346 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700347
348 return 0;
349
David L Stevens14878f72007-09-16 16:52:35 -0700350err_icmpmsg:
Eric Dumazetbe281e52011-05-19 01:14:23 +0000351 kfree(idev->stats.icmpv6dev);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700352err_icmp:
WANG Cong698365f2014-05-05 15:55:55 -0700353 free_percpu(idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700354err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700355 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700356}
357
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000358static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 struct inet6_dev *ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700361 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 ASSERT_RTNL();
364
365 if (dev->mtu < IPV6_MIN_MTU)
WANG Conga317a2f2014-07-25 15:25:09 -0700366 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900368 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100369 if (!ndev)
WANG Conga317a2f2014-07-25 15:25:09 -0700370 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Ingo Oeser322f74a2006-03-20 23:01:47 -0800372 rwlock_init(&ndev->lock);
373 ndev->dev = dev;
stephen hemminger502a2ff2010-03-17 20:31:13 +0000374 INIT_LIST_HEAD(&ndev->addr_list);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200375 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
376 (unsigned long)ndev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900377 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100378
379 if (ndev->cnf.stable_secret.initialized)
380 ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
381 else
382 ndev->addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64;
383
Ingo Oeser322f74a2006-03-20 23:01:47 -0800384 ndev->cnf.mtu6 = dev->mtu;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800385 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
Ian Morris63159f22015-03-29 14:00:04 +0100386 if (!ndev->nd_parms) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800387 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700388 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800389 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700390 if (ndev->cnf.forwarding)
391 dev_disable_lro(dev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800392 /* We refer to the device */
393 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Ingo Oeser322f74a2006-03-20 23:01:47 -0800395 if (snmp6_alloc_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800396 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000397 "%s: cannot allocate memory for statistics; dev=%s.\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800398 __func__, dev->name);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800399 neigh_parms_release(&nd_tbl, ndev->nd_parms);
Roy Li8603e332011-09-20 15:10:16 -0400400 dev_put(dev);
401 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700402 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Ingo Oeser322f74a2006-03-20 23:01:47 -0800405 if (snmp6_register_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800406 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000407 "%s: cannot create /proc/net/dev_snmp6/%s\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800408 __func__, dev->name);
WANG Conga317a2f2014-07-25 15:25:09 -0700409 goto err_release;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Ingo Oeser322f74a2006-03-20 23:01:47 -0800412 /* One reference from device. We must do this before
413 * we invoke __ipv6_regen_rndid().
414 */
415 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900417 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
418 ndev->cnf.accept_dad = -1;
419
Amerigo Wang07a93622012-10-29 16:23:10 +0000420#if IS_ENABLED(CONFIG_IPV6_SIT)
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700421 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
Joe Perchesf3213832012-05-15 14:11:53 +0000422 pr_info("%s: Disabled Multicast RS\n", dev->name);
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700423 ndev->cnf.rtr_solicits = 0;
424 }
425#endif
426
stephen hemminger372e6c82010-03-17 20:31:09 +0000427 INIT_LIST_HEAD(&ndev->tempaddr_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800428 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800429 if ((dev->flags&IFF_LOOPBACK) ||
430 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700431 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700432 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700433 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800434 ndev->cnf.use_tempaddr = -1;
435 } else {
436 in6_dev_hold(ndev);
437 ipv6_regen_rndid((unsigned long) ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
David S. Miller5d9efa72013-10-28 20:07:50 -0400439
Daniel Borkmann914faa12013-04-09 03:47:14 +0000440 ndev->token = in6addr_any;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800441
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700442 if (netif_running(dev) && addrconf_qdisc_ok(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700443 ndev->if_flags |= IF_READY;
444
Ingo Oeser322f74a2006-03-20 23:01:47 -0800445 ipv6_mc_init_dev(ndev);
446 ndev->tstamp = jiffies;
WANG Conga317a2f2014-07-25 15:25:09 -0700447 err = addrconf_sysctl_register(ndev);
448 if (err) {
449 ipv6_mc_destroy_dev(ndev);
450 del_timer(&ndev->regen_timer);
Sabrina Dubroca2a189f9e2015-11-04 14:47:53 +0100451 snmp6_unregister_dev(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700452 goto err_release;
453 }
David L Stevens30c4cf52007-01-04 12:31:14 -0800454 /* protected by rtnl_lock */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000455 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800456
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000457 /* Join interface-local all-node multicast group */
458 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
459
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800460 /* Join all-node multicast group */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900461 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800462
Li Weid6ddef92012-03-05 14:45:17 +0000463 /* Join all-router multicast group if forwarding is set */
Li Wei8b2aaed2012-03-07 14:58:07 +0000464 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
Li Weid6ddef92012-03-05 14:45:17 +0000465 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700468
469err_release:
470 neigh_parms_release(&nd_tbl, ndev->nd_parms);
471 ndev->dead = 1;
472 in6_dev_finish_destroy(ndev);
473 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000476static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 struct inet6_dev *idev;
479
480 ASSERT_RTNL();
481
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700482 idev = __in6_dev_get(dev);
483 if (!idev) {
484 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -0700485 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return NULL;
487 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (dev->flags&IFF_UP)
490 ipv6_mc_up(idev);
491 return idev;
492}
493
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000494static int inet6_netconf_msgsize_devconf(int type)
495{
496 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
497 + nla_total_size(4); /* NETCONFA_IFINDEX */
Zhang Shengju136ba622016-03-10 08:55:50 +0000498 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000499
Zhang Shengju136ba622016-03-10 08:55:50 +0000500 if (type == NETCONFA_ALL)
501 all = true;
502
503 if (all || type == NETCONFA_FORWARDING)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000504 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500505#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000506 if (all || type == NETCONFA_MC_FORWARDING)
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000507 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500508#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000509 if (all || type == NETCONFA_PROXY_NEIGH)
stephen hemmingerc92d5492013-12-17 22:37:14 -0800510 size += nla_total_size(4);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000511
Zhang Shengju136ba622016-03-10 08:55:50 +0000512 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
Andy Gospodarek35103d12015-08-13 10:39:01 -0400513 size += nla_total_size(4);
514
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000515 return size;
516}
517
518static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
519 struct ipv6_devconf *devconf, u32 portid,
520 u32 seq, int event, unsigned int flags,
521 int type)
522{
523 struct nlmsghdr *nlh;
524 struct netconfmsg *ncm;
Zhang Shengju136ba622016-03-10 08:55:50 +0000525 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000526
527 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
528 flags);
Ian Morris63159f22015-03-29 14:00:04 +0100529 if (!nlh)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000530 return -EMSGSIZE;
531
Zhang Shengju136ba622016-03-10 08:55:50 +0000532 if (type == NETCONFA_ALL)
533 all = true;
534
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000535 ncm = nlmsg_data(nlh);
536 ncm->ncm_family = AF_INET6;
537
538 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
539 goto nla_put_failure;
540
Zhang Shengju136ba622016-03-10 08:55:50 +0000541 if ((all || type == NETCONFA_FORWARDING) &&
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000542 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
543 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500544#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000545 if ((all || type == NETCONFA_MC_FORWARDING) &&
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000546 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
547 devconf->mc_forwarding) < 0)
548 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500549#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000550 if ((all || type == NETCONFA_PROXY_NEIGH) &&
stephen hemmingerc92d5492013-12-17 22:37:14 -0800551 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
552 goto nla_put_failure;
553
Zhang Shengju136ba622016-03-10 08:55:50 +0000554 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
Andy Gospodarek35103d12015-08-13 10:39:01 -0400555 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
556 devconf->ignore_routes_with_linkdown) < 0)
557 goto nla_put_failure;
558
Johannes Berg053c0952015-01-16 22:09:00 +0100559 nlmsg_end(skb, nlh);
560 return 0;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000561
562nla_put_failure:
563 nlmsg_cancel(skb, nlh);
564 return -EMSGSIZE;
565}
566
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000567void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
568 struct ipv6_devconf *devconf)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000569{
570 struct sk_buff *skb;
571 int err = -ENOBUFS;
572
Eric Dumazet927265b2016-07-08 05:46:04 +0200573 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100574 if (!skb)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000575 goto errout;
576
577 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
578 RTM_NEWNETCONF, 0, type);
579 if (err < 0) {
580 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
581 WARN_ON(err == -EMSGSIZE);
582 kfree_skb(skb);
583 goto errout;
584 }
Eric Dumazet927265b2016-07-08 05:46:04 +0200585 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000586 return;
587errout:
Cong Dingbd779022012-12-18 12:08:56 +0000588 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000589}
590
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000591static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
592 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
593 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
stephen hemmingerc92d5492013-12-17 22:37:14 -0800594 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
Andy Gospodarek35103d12015-08-13 10:39:01 -0400595 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000596};
597
598static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
Thomas Graf661d2962013-03-21 07:45:29 +0000599 struct nlmsghdr *nlh)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000600{
601 struct net *net = sock_net(in_skb->sk);
602 struct nlattr *tb[NETCONFA_MAX+1];
603 struct netconfmsg *ncm;
604 struct sk_buff *skb;
605 struct ipv6_devconf *devconf;
606 struct inet6_dev *in6_dev;
607 struct net_device *dev;
608 int ifindex;
609 int err;
610
611 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
612 devconf_ipv6_policy);
613 if (err < 0)
614 goto errout;
615
Anton Protopopova97eb332016-02-16 21:43:16 -0500616 err = -EINVAL;
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000617 if (!tb[NETCONFA_IFINDEX])
618 goto errout;
619
620 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
621 switch (ifindex) {
622 case NETCONFA_IFINDEX_ALL:
623 devconf = net->ipv6.devconf_all;
624 break;
625 case NETCONFA_IFINDEX_DEFAULT:
626 devconf = net->ipv6.devconf_dflt;
627 break;
628 default:
629 dev = __dev_get_by_index(net, ifindex);
Ian Morris63159f22015-03-29 14:00:04 +0100630 if (!dev)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000631 goto errout;
632 in6_dev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +0100633 if (!in6_dev)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000634 goto errout;
635 devconf = &in6_dev->cnf;
636 break;
637 }
638
639 err = -ENOBUFS;
Zhang Shengju136ba622016-03-10 08:55:50 +0000640 skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +0100641 if (!skb)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000642 goto errout;
643
644 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
645 NETLINK_CB(in_skb).portid,
646 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
Zhang Shengju136ba622016-03-10 08:55:50 +0000647 NETCONFA_ALL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000648 if (err < 0) {
649 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
650 WARN_ON(err == -EMSGSIZE);
651 kfree_skb(skb);
652 goto errout;
653 }
654 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
655errout:
656 return err;
657}
658
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000659static int inet6_netconf_dump_devconf(struct sk_buff *skb,
660 struct netlink_callback *cb)
661{
662 struct net *net = sock_net(skb->sk);
663 int h, s_h;
664 int idx, s_idx;
665 struct net_device *dev;
666 struct inet6_dev *idev;
667 struct hlist_head *head;
668
669 s_h = cb->args[0];
670 s_idx = idx = cb->args[1];
671
672 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
673 idx = 0;
674 head = &net->dev_index_head[h];
675 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000676 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
677 net->dev_base_seq;
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000678 hlist_for_each_entry_rcu(dev, head, index_hlist) {
679 if (idx < s_idx)
680 goto cont;
681 idev = __in6_dev_get(dev);
682 if (!idev)
683 goto cont;
684
685 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
686 &idev->cnf,
687 NETLINK_CB(cb->skb).portid,
688 cb->nlh->nlmsg_seq,
689 RTM_NEWNETCONF,
690 NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000691 NETCONFA_ALL) < 0) {
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000692 rcu_read_unlock();
693 goto done;
694 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000695 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000696cont:
697 idx++;
698 }
699 rcu_read_unlock();
700 }
701 if (h == NETDEV_HASHENTRIES) {
702 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
703 net->ipv6.devconf_all,
704 NETLINK_CB(cb->skb).portid,
705 cb->nlh->nlmsg_seq,
706 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000707 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000708 goto done;
709 else
710 h++;
711 }
712 if (h == NETDEV_HASHENTRIES + 1) {
713 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
714 net->ipv6.devconf_dflt,
715 NETLINK_CB(cb->skb).portid,
716 cb->nlh->nlmsg_seq,
717 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000718 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000719 goto done;
720 else
721 h++;
722 }
723done:
724 cb->args[0] = h;
725 cb->args[1] = idx;
726
727 return skb->len;
728}
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730#ifdef CONFIG_SYSCTL
731static void dev_forward_change(struct inet6_dev *idev)
732{
733 struct net_device *dev;
734 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 if (!idev)
737 return;
738 dev = idev->dev;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700739 if (idev->cnf.forwarding)
740 dev_disable_lro(dev);
Amerigo Wang1a940832012-10-28 17:43:53 +0000741 if (dev->flags & IFF_MULTICAST) {
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000742 if (idev->cnf.forwarding) {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900743 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000744 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
745 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
746 } else {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900747 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000748 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
749 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
stephen hemminger502a2ff2010-03-17 20:31:13 +0000752
753 list_for_each_entry(ifa, &idev->addr_list, if_list) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800754 if (ifa->flags&IFA_F_TENTATIVE)
755 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (idev->cnf.forwarding)
757 addrconf_join_anycast(ifa);
758 else
759 addrconf_leave_anycast(ifa);
760 }
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000761 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
762 dev->ifindex, &idev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
765
Pavel Emelyanove1869322008-01-10 17:43:50 -0800766static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 struct net_device *dev;
769 struct inet6_dev *idev;
770
Ben Hutchings4acd4942012-08-14 08:54:51 +0000771 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 idev = __in6_dev_get(dev);
773 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800774 int changed = (!idev->cnf.forwarding) ^ (!newf);
775 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (changed)
777 dev_forward_change(idev);
778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800781
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000782static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800783{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800784 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000785 int old;
786
787 if (!rtnl_trylock())
788 return restart_syscall();
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800789
790 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000791 old = *p;
792 *p = newf;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800793
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000794 if (p == &net->ipv6.devconf_dflt->forwarding) {
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000795 if ((!newf) ^ (!old))
796 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
797 NETCONFA_IFINDEX_DEFAULT,
798 net->ipv6.devconf_dflt);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000799 rtnl_unlock();
800 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +0000801 }
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000802
Pavel Emelyanove1869322008-01-10 17:43:50 -0800803 if (p == &net->ipv6.devconf_all->forwarding) {
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200804 int old_dflt = net->ipv6.devconf_dflt->forwarding;
805
Pavel Emelyanove1869322008-01-10 17:43:50 -0800806 net->ipv6.devconf_dflt->forwarding = newf;
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200807 if ((!newf) ^ (!old_dflt))
808 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
809 NETCONFA_IFINDEX_DEFAULT,
810 net->ipv6.devconf_dflt);
811
Pavel Emelyanove1869322008-01-10 17:43:50 -0800812 addrconf_forward_change(net, newf);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000813 if ((!newf) ^ (!old))
814 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
815 NETCONFA_IFINDEX_ALL,
816 net->ipv6.devconf_all);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000817 } else if ((!newf) ^ (!old))
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800818 dev_forward_change((struct inet6_dev *)table->extra1);
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700819 rtnl_unlock();
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800820
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000821 if (newf)
Daniel Lezcano7b4da532008-03-04 13:47:14 -0800822 rt6_purge_dflt_routers(net);
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000823 return 1;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800824}
Andy Gospodarek35103d12015-08-13 10:39:01 -0400825
826static void addrconf_linkdown_change(struct net *net, __s32 newf)
827{
828 struct net_device *dev;
829 struct inet6_dev *idev;
830
831 for_each_netdev(net, dev) {
832 idev = __in6_dev_get(dev);
833 if (idev) {
834 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
835
836 idev->cnf.ignore_routes_with_linkdown = newf;
837 if (changed)
838 inet6_netconf_notify_devconf(dev_net(dev),
839 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
840 dev->ifindex,
841 &idev->cnf);
842 }
843 }
844}
845
846static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
847{
848 struct net *net;
849 int old;
850
851 if (!rtnl_trylock())
852 return restart_syscall();
853
854 net = (struct net *)table->extra2;
855 old = *p;
856 *p = newf;
857
858 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
859 if ((!newf) ^ (!old))
860 inet6_netconf_notify_devconf(net,
861 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
862 NETCONFA_IFINDEX_DEFAULT,
863 net->ipv6.devconf_dflt);
864 rtnl_unlock();
865 return 0;
866 }
867
868 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
869 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
870 addrconf_linkdown_change(net, newf);
871 if ((!newf) ^ (!old))
872 inet6_netconf_notify_devconf(net,
873 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
874 NETCONFA_IFINDEX_ALL,
875 net->ipv6.devconf_all);
876 }
877 rtnl_unlock();
878
879 return 1;
880}
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882#endif
883
stephen hemminger5c578aed2010-03-17 20:31:11 +0000884/* Nobody refers to this ifaddr, destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
886{
stephen hemmingerc2e21292010-03-17 20:31:10 +0000887 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889#ifdef NET_REFCNT_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +0000890 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891#endif
892
893 in6_dev_put(ifp->idev);
894
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100895 if (cancel_delayed_work(&ifp->dad_work))
896 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
897 ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Herbert Xue9d3e082010-05-18 15:36:06 -0700899 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
Joe Perchesf3213832012-05-15 14:11:53 +0000900 pr_warn("Freeing alive inet6 address %p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return;
902 }
Amerigo Wang94e187c2012-10-29 00:13:19 +0000903 ip6_rt_put(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Lai Jiangshane5785982011-03-15 18:00:14 +0800905 kfree_rcu(ifp, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
Brian Haleye55ffac2006-07-10 15:25:51 -0700908static void
909ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
910{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000911 struct list_head *p;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700912 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700913
914 /*
915 * Each device address list is sorted in order of scope -
916 * global before linklocal.
917 */
stephen hemminger502a2ff2010-03-17 20:31:13 +0000918 list_for_each(p, &idev->addr_list) {
919 struct inet6_ifaddr *ifa
920 = list_entry(p, struct inet6_ifaddr, if_list);
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700921 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700922 break;
923 }
924
David S. Millerb54c9b92010-03-25 21:25:30 -0700925 list_add_tail(&ifp->if_list, p);
Brian Haleye55ffac2006-07-10 15:25:51 -0700926}
927
Eric Dumazetddbe5032012-07-18 08:11:12 +0000928static u32 inet6_addr_hash(const struct in6_addr *addr)
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900929{
Eric Dumazetddbe5032012-07-18 08:11:12 +0000930 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900931}
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933/* On success it returns ifp with increased reference count */
934
935static struct inet6_ifaddr *
Jiri Pirko3f8f5292013-08-01 10:41:27 +0200936ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
937 const struct in6_addr *peer_addr, int pfxlen,
Jiri Benc8a226b22013-08-01 10:41:28 +0200938 int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
940 struct inet6_ifaddr *ifa = NULL;
941 struct rt6_info *rt;
stephen hemminger3a88a812010-03-17 20:31:12 +0000942 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 int err = 0;
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +0900944 int addr_type = ipv6_addr_type(addr);
945
946 if (addr_type == IPV6_ADDR_ANY ||
947 addr_type & IPV6_ADDR_MULTICAST ||
948 (!(idev->dev->flags & IFF_LOOPBACK) &&
949 addr_type & IPV6_ADDR_LOOPBACK))
950 return ERR_PTR(-EADDRNOTAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700952 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (idev->dead) {
954 err = -ENODEV; /*XXX*/
955 goto out2;
956 }
957
Brian Haley56d417b2009-06-01 03:07:33 -0700958 if (idev->cnf.disable_ipv6) {
Brian Haley9bdd8d42009-03-18 18:22:48 -0700959 err = -EACCES;
960 goto out2;
961 }
962
stephen hemminger5c578aed2010-03-17 20:31:11 +0000963 spin_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 /* Ignore adding duplicate addresses on an interface */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900966 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
dingtianhongba3542e2013-08-17 10:27:04 +0800967 ADBG("ipv6_add_addr: already assigned\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 err = -EEXIST;
969 goto out;
970 }
971
Ingo Oeser322f74a2006-03-20 23:01:47 -0800972 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Ian Morris63159f22015-03-29 14:00:04 +0100974 if (!ifa) {
dingtianhongba3542e2013-08-17 10:27:04 +0800975 ADBG("ipv6_add_addr: malloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 err = -ENOBUFS;
977 goto out;
978 }
979
David S. Miller8f031512011-12-06 16:48:14 -0500980 rt = addrconf_dst_alloc(idev, addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (IS_ERR(rt)) {
982 err = PTR_ERR(rt);
983 goto out;
984 }
985
Jiri Pirkobba24892013-12-07 19:26:57 +0100986 neigh_parms_data_state_setall(idev->nd_parms);
987
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000988 ifa->addr = *addr;
Jiri Pirko3f8f5292013-08-01 10:41:27 +0200989 if (peer_addr)
990 ifa->peer_addr = *peer_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 spin_lock_init(&ifa->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100993 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
stephen hemmingerc2e21292010-03-17 20:31:10 +0000994 INIT_HLIST_NODE(&ifa->addr_lst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 ifa->scope = scope;
996 ifa->prefix_len = pfxlen;
997 ifa->flags = flags | IFA_F_TENTATIVE;
Jiri Benc8a226b22013-08-01 10:41:28 +0200998 ifa->valid_lft = valid_lft;
999 ifa->prefered_lft = prefered_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 ifa->cstamp = ifa->tstamp = jiffies;
Daniel Borkmann617fe292013-04-09 03:47:16 +00001001 ifa->tokenized = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Keir Fraser57f5f542006-08-29 02:43:49 -07001003 ifa->rt = rt;
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 ifa->idev = idev;
1006 in6_dev_hold(idev);
1007 /* For caller */
1008 in6_ifa_hold(ifa);
1009
1010 /* Add to big hash table */
Eric Dumazetddbe5032012-07-18 08:11:12 +00001011 hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
stephen hemminger5c578aed2010-03-17 20:31:11 +00001013 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
stephen hemminger5c578aed2010-03-17 20:31:11 +00001014 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 write_lock(&idev->lock);
1017 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -07001018 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (ifa->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001021 list_add(&ifa->tmp_list, &idev->tempaddr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 in6_ifa_hold(ifa);
1023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 in6_ifa_hold(ifa);
1026 write_unlock(&idev->lock);
1027out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001028 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -07001030 if (likely(err == 0))
Cong Wangf88c91d2013-04-14 23:18:43 +08001031 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 else {
1033 kfree(ifa);
1034 ifa = ERR_PTR(err);
1035 }
1036
1037 return ifa;
1038out:
stephen hemminger5c578aed2010-03-17 20:31:11 +00001039 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 goto out2;
1041}
1042
Thomas Haller5b84efe2014-01-15 15:36:59 +01001043enum cleanup_prefix_rt_t {
1044 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1045 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1046 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1047};
1048
1049/*
1050 * Check, whether the prefix for ifp would still need a prefix route
1051 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1052 * constants.
1053 *
1054 * 1) we don't purge prefix if address was not permanent.
1055 * prefix is managed by its own lifetime.
1056 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1057 * 3) if there are no addresses, delete prefix.
1058 * 4) if there are still other permanent address(es),
1059 * corresponding prefix is still permanent.
1060 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1061 * don't purge the prefix, assume user space is managing it.
1062 * 6) otherwise, update prefix lifetime to the
1063 * longest valid lifetime among the corresponding
1064 * addresses on the device.
1065 * Note: subsequent RA will update lifetime.
1066 **/
1067static enum cleanup_prefix_rt_t
1068check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1069{
1070 struct inet6_ifaddr *ifa;
1071 struct inet6_dev *idev = ifp->idev;
1072 unsigned long lifetime;
1073 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1074
1075 *expires = jiffies;
1076
1077 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1078 if (ifa == ifp)
1079 continue;
1080 if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
1081 ifp->prefix_len))
1082 continue;
1083 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1084 return CLEANUP_PREFIX_RT_NOP;
1085
1086 action = CLEANUP_PREFIX_RT_EXPIRE;
1087
1088 spin_lock(&ifa->lock);
1089
1090 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1091 /*
1092 * Note: Because this address is
1093 * not permanent, lifetime <
1094 * LONG_MAX / HZ here.
1095 */
1096 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1097 *expires = ifa->tstamp + lifetime * HZ;
1098 spin_unlock(&ifa->lock);
1099 }
1100
1101 return action;
1102}
1103
1104static void
1105cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
1106{
1107 struct rt6_info *rt;
1108
1109 rt = addrconf_get_prefix_route(&ifp->addr,
1110 ifp->prefix_len,
1111 ifp->idev->dev,
1112 0, RTF_GATEWAY | RTF_DEFAULT);
1113 if (rt) {
1114 if (del_rt)
1115 ip6_del_rt(rt);
1116 else {
1117 if (!(rt->rt6i_flags & RTF_EXPIRES))
1118 rt6_set_expires(rt, expires);
1119 ip6_rt_put(rt);
1120 }
1121 }
1122}
1123
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125/* This function wants to get referenced ifp and releases it before return */
1126
1127static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1128{
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001129 int state;
Thomas Haller5b84efe2014-01-15 15:36:59 +01001130 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1131 unsigned long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001133 ASSERT_RTNL();
1134
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001135 spin_lock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001136 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -07001137 ifp->state = INET6_IFADDR_STATE_DEAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001138 spin_unlock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001139
1140 if (state == INET6_IFADDR_STATE_DEAD)
1141 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
stephen hemminger5c578aed2010-03-17 20:31:11 +00001143 spin_lock_bh(&addrconf_hash_lock);
1144 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578aed2010-03-17 20:31:11 +00001145 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Thomas Haller5b84efe2014-01-15 15:36:59 +01001147 write_lock_bh(&ifp->idev->lock);
David S. Miller5d9efa72013-10-28 20:07:50 -04001148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001150 list_del(&ifp->tmp_list);
1151 if (ifp->ifpub) {
1152 in6_ifa_put(ifp->ifpub);
1153 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
stephen hemminger372e6c82010-03-17 20:31:09 +00001155 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Thomas Haller5b84efe2014-01-15 15:36:59 +01001158 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1159 action = check_cleanup_prefix_route(ifp, &expires);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001160
Thomas Haller5b84efe2014-01-15 15:36:59 +01001161 list_del_init(&ifp->if_list);
1162 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Thomas Haller5b84efe2014-01-15 15:36:59 +01001164 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001166 addrconf_del_dad_work(ifp);
Andrey Vaginb2238562008-07-08 15:13:31 -07001167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 ipv6_ifa_notify(RTM_DELADDR, ifp);
1169
Cong Wangf88c91d2013-04-14 23:18:43 +08001170 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Thomas Haller5b84efe2014-01-15 15:36:59 +01001172 if (action != CLEANUP_PREFIX_RT_NOP) {
1173 cleanup_prefix_route(ifp, expires,
1174 action == CLEANUP_PREFIX_RT_DEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176
Daniel Walterc3968a82011-04-13 21:10:57 +00001177 /* clean up prefsrc entries */
1178 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001179out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 in6_ifa_put(ifp);
1181}
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1184{
1185 struct inet6_dev *idev = ifp->idev;
1186 struct in6_addr addr, *tmpaddr;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001187 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001188 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 int tmp_plen;
1190 int ret = 0;
Neil Horman95c385b2007-04-25 17:08:10 -07001191 u32 addr_flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001192 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Jiri Pirko53bd6742013-12-06 09:45:22 +01001194 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (ift) {
1196 spin_lock_bh(&ift->lock);
1197 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1198 spin_unlock_bh(&ift->lock);
1199 tmpaddr = &addr;
1200 } else {
1201 tmpaddr = NULL;
1202 }
1203retry:
1204 in6_dev_hold(idev);
1205 if (idev->cnf.use_tempaddr <= 0) {
Jiri Pirko53bd6742013-12-06 09:45:22 +01001206 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001207 pr_info("%s: use_tempaddr is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 in6_dev_put(idev);
1209 ret = -1;
1210 goto out;
1211 }
1212 spin_lock_bh(&ifp->lock);
1213 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1214 idev->cnf.use_tempaddr = -1; /*XXX*/
1215 spin_unlock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01001216 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001217 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1218 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 in6_dev_put(idev);
1220 ret = -1;
1221 goto out;
1222 }
1223 in6_ifa_hold(ifp);
1224 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001225 __ipv6_try_regen_rndid(idev, tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001227 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 tmp_valid_lft = min_t(__u32,
1229 ifp->valid_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +00001230 idev->cnf.temp_valid_lft + age);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001231 tmp_prefered_lft = min_t(__u32,
1232 ifp->prefered_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +00001233 idev->cnf.temp_prefered_lft + age -
Ben Hutchings784e2712010-06-26 11:42:55 +00001234 idev->cnf.max_desync_factor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 tmp_plen = ifp->prefix_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 tmp_tstamp = ifp->tstamp;
1237 spin_unlock_bh(&ifp->lock);
1238
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001239 regen_advance = idev->cnf.regen_max_retry *
Ian Morris67ba4152014-08-24 21:53:10 +01001240 idev->cnf.dad_transmits *
1241 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
Jiri Pirko53bd6742013-12-06 09:45:22 +01001242 write_unlock_bh(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -07001243
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001244 /* A temporary address is created only if this calculated Preferred
1245 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1246 * an implementation must not create a temporary address with a zero
1247 * Preferred Lifetime.
Heiner Kallweitecab6702014-03-12 22:13:19 +01001248 * Use age calculation as in addrconf_verify to avoid unnecessary
1249 * temporary addresses being generated.
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001250 */
Heiner Kallweitecab6702014-03-12 22:13:19 +01001251 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1252 if (tmp_prefered_lft <= regen_advance + age) {
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001253 in6_ifa_put(ifp);
1254 in6_dev_put(idev);
1255 ret = -1;
1256 goto out;
1257 }
1258
Neil Horman95c385b2007-04-25 17:08:10 -07001259 addr_flags = IFA_F_TEMPORARY;
1260 /* set in addrconf_prefix_rcv() */
1261 if (ifp->flags & IFA_F_OPTIMISTIC)
1262 addr_flags |= IFA_F_OPTIMISTIC;
1263
Hannes Frederic Sowa4b08a8f2013-08-16 13:02:27 +02001264 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1265 ipv6_addr_scope(&addr), addr_flags,
1266 tmp_valid_lft, tmp_prefered_lft);
1267 if (IS_ERR(ift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 in6_ifa_put(ifp);
1269 in6_dev_put(idev);
Joe Perchesf3213832012-05-15 14:11:53 +00001270 pr_info("%s: retry temporary address regeneration\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 tmpaddr = &addr;
Jiri Pirko53bd6742013-12-06 09:45:22 +01001272 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 goto retry;
1274 }
1275
1276 spin_lock_bh(&ift->lock);
1277 ift->ifpub = ifp;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001278 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 ift->tstamp = tmp_tstamp;
1280 spin_unlock_bh(&ift->lock);
1281
David S. Millercf22f9a2012-04-14 21:37:40 -04001282 addrconf_dad_start(ift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 in6_ifa_put(ift);
1284 in6_dev_put(idev);
1285out:
1286 return ret;
1287}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289/*
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001290 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001292enum {
1293 IPV6_SADDR_RULE_INIT = 0,
1294 IPV6_SADDR_RULE_LOCAL,
1295 IPV6_SADDR_RULE_SCOPE,
1296 IPV6_SADDR_RULE_PREFERRED,
1297#ifdef CONFIG_IPV6_MIP6
1298 IPV6_SADDR_RULE_HOA,
1299#endif
1300 IPV6_SADDR_RULE_OIF,
1301 IPV6_SADDR_RULE_LABEL,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001302 IPV6_SADDR_RULE_PRIVACY,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001303 IPV6_SADDR_RULE_ORCHID,
1304 IPV6_SADDR_RULE_PREFIX,
Erik Kline7fd25612014-10-28 18:11:14 +09001305#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1306 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1307#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001308 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001309};
1310
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001311struct ipv6_saddr_score {
1312 int rule;
1313 int addr_type;
1314 struct inet6_ifaddr *ifa;
1315 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1316 int scopedist;
1317 int matchlen;
1318};
1319
1320struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001321 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001322 int ifindex;
1323 int scope;
1324 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001325 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001326};
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001327
Dave Jonesb6f99a22007-03-22 12:27:49 -07001328static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Ulrich Weber45bb0062010-02-25 23:28:58 +00001330 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001331 return 1;
1332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
Erik Kline7fd25612014-10-28 18:11:14 +09001335static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
1336{
1337#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1338 return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
1339#else
1340 return false;
1341#endif
1342}
1343
Benjamin Thery3de23252008-05-28 14:51:24 +02001344static int ipv6_get_saddr_eval(struct net *net,
1345 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001346 struct ipv6_saddr_dst *dst,
1347 int i)
1348{
1349 int ret;
1350
1351 if (i <= score->rule) {
1352 switch (i) {
1353 case IPV6_SADDR_RULE_SCOPE:
1354 ret = score->scopedist;
1355 break;
1356 case IPV6_SADDR_RULE_PREFIX:
1357 ret = score->matchlen;
1358 break;
1359 default:
1360 ret = !!test_bit(i, score->scorebits);
1361 }
1362 goto out;
1363 }
1364
1365 switch (i) {
1366 case IPV6_SADDR_RULE_INIT:
1367 /* Rule 0: remember if hiscore is not ready yet */
1368 ret = !!score->ifa;
1369 break;
1370 case IPV6_SADDR_RULE_LOCAL:
1371 /* Rule 1: Prefer same address */
1372 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1373 break;
1374 case IPV6_SADDR_RULE_SCOPE:
1375 /* Rule 2: Prefer appropriate scope
1376 *
1377 * ret
1378 * ^
1379 * -1 | d 15
1380 * ---+--+-+---> scope
1381 * |
1382 * | d is scope of the destination.
1383 * B-d | \
1384 * | \ <- smaller scope is better if
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08001385 * B-15 | \ if scope is enough for destination.
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001386 * | ret = B - scope (-1 <= scope >= d <= 15).
1387 * d-C-1 | /
1388 * |/ <- greater is better
1389 * -C / if scope is not enough for destination.
1390 * /| ret = scope - C (-1 <= d < scope <= 15).
1391 *
1392 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1393 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1394 * Assume B = 0 and we get C > 29.
1395 */
1396 ret = __ipv6_addr_src_scope(score->addr_type);
1397 if (ret >= dst->scope)
1398 ret = -ret;
1399 else
1400 ret -= 128; /* 30 is enough */
1401 score->scopedist = ret;
1402 break;
1403 case IPV6_SADDR_RULE_PREFERRED:
Erik Kline7fd25612014-10-28 18:11:14 +09001404 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001405 /* Rule 3: Avoid deprecated and optimistic addresses */
Erik Kline7fd25612014-10-28 18:11:14 +09001406 u8 avoid = IFA_F_DEPRECATED;
1407
1408 if (!ipv6_use_optimistic_addr(score->ifa->idev))
1409 avoid |= IFA_F_OPTIMISTIC;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001410 ret = ipv6_saddr_preferred(score->addr_type) ||
Erik Kline7fd25612014-10-28 18:11:14 +09001411 !(score->ifa->flags & avoid);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001412 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001413 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001414#ifdef CONFIG_IPV6_MIP6
1415 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001416 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001417 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001418 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1419 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001420 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001421 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001422#endif
1423 case IPV6_SADDR_RULE_OIF:
1424 /* Rule 5: Prefer outgoing interface */
1425 ret = (!dst->ifindex ||
1426 dst->ifindex == score->ifa->idev->dev->ifindex);
1427 break;
1428 case IPV6_SADDR_RULE_LABEL:
1429 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001430 ret = ipv6_addr_label(net,
1431 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001432 score->ifa->idev->dev->ifindex) == dst->label;
1433 break;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001434 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001435 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001436 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001437 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001438 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001439 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1440 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1441 score->ifa->idev->cnf.use_tempaddr >= 2;
1442 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001443 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001444 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001445 case IPV6_SADDR_RULE_ORCHID:
1446 /* Rule 8-: Prefer ORCHID vs ORCHID or
1447 * non-ORCHID vs non-ORCHID
1448 */
1449 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1450 ipv6_addr_orchid(dst->addr));
1451 break;
1452 case IPV6_SADDR_RULE_PREFIX:
1453 /* Rule 8: Use longest matching prefix */
YOSHIFUJI Hideaki / 吉藤英明91b4b042012-09-10 18:41:07 +00001454 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1455 if (ret > score->ifa->prefix_len)
1456 ret = score->ifa->prefix_len;
1457 score->matchlen = ret;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001458 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001459#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1460 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1461 /* Optimistic addresses still have lower precedence than other
1462 * preferred addresses.
1463 */
1464 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1465 break;
1466#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001467 default:
1468 ret = 0;
1469 }
1470
1471 if (ret)
1472 __set_bit(i, score->scorebits);
1473 score->rule = i;
1474out:
1475 return ret;
1476}
1477
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001478static int __ipv6_dev_get_saddr(struct net *net,
1479 struct ipv6_saddr_dst *dst,
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001480 struct inet6_dev *idev,
1481 struct ipv6_saddr_score *scores,
1482 int hiscore_idx)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001483{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001484 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001485
1486 read_lock_bh(&idev->lock);
1487 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1488 int i;
1489
1490 /*
1491 * - Tentative Address (RFC2462 section 5.4)
1492 * - A tentative address is not considered
1493 * "assigned to an interface" in the traditional
1494 * sense, unless it is also flagged as optimistic.
1495 * - Candidate Source Address (section 4)
1496 * - In any case, anycast addresses, multicast
1497 * addresses, and the unspecified address MUST
1498 * NOT be included in a candidate set.
1499 */
1500 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1501 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1502 continue;
1503
1504 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1505
1506 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1507 score->addr_type & IPV6_ADDR_MULTICAST)) {
1508 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1509 idev->dev->name);
1510 continue;
1511 }
1512
1513 score->rule = -1;
1514 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1515
1516 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1517 int minihiscore, miniscore;
1518
1519 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1520 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1521
1522 if (minihiscore > miniscore) {
1523 if (i == IPV6_SADDR_RULE_SCOPE &&
1524 score->scopedist > 0) {
1525 /*
1526 * special case:
1527 * each remaining entry
1528 * has too small (not enough)
1529 * scope, because ifa entries
1530 * are sorted by their scope
1531 * values.
1532 */
1533 goto out;
1534 }
1535 break;
1536 } else if (minihiscore < miniscore) {
1537 if (hiscore->ifa)
1538 in6_ifa_put(hiscore->ifa);
1539
1540 in6_ifa_hold(score->ifa);
1541
1542 swap(hiscore, score);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001543 hiscore_idx = 1 - hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001544
1545 /* restore our iterator */
1546 score->ifa = hiscore->ifa;
1547
1548 break;
1549 }
1550 }
1551 }
1552out:
1553 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001554 return hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001555}
1556
David Ahernafbac6012016-06-16 16:24:26 -07001557static int ipv6_get_saddr_master(struct net *net,
1558 const struct net_device *dst_dev,
1559 const struct net_device *master,
1560 struct ipv6_saddr_dst *dst,
1561 struct ipv6_saddr_score *scores,
1562 int hiscore_idx)
1563{
1564 struct inet6_dev *idev;
1565
1566 idev = __in6_dev_get(dst_dev);
1567 if (idev)
1568 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1569 scores, hiscore_idx);
1570
1571 idev = __in6_dev_get(master);
1572 if (idev)
1573 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1574 scores, hiscore_idx);
1575
1576 return hiscore_idx;
1577}
1578
Patrick McHardyb3f644f2012-08-26 19:14:14 +02001579int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001580 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001581 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001583 struct ipv6_saddr_score scores[2], *hiscore;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001584 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001585 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001586 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001587 int dst_type;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001588 bool use_oif_addr = false;
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001589 int hiscore_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001591 dst_type = __ipv6_addr_type(daddr);
1592 dst.addr = daddr;
1593 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1594 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001595 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001596 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001597
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001598 scores[hiscore_idx].rule = -1;
1599 scores[hiscore_idx].ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001601 rcu_read_lock();
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001602
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001603 /* Candidate Source Address (section 4)
1604 * - multicast and link-local destination address,
1605 * the set of candidate source address MUST only
1606 * include addresses assigned to interfaces
1607 * belonging to the same link as the outgoing
1608 * interface.
1609 * (- For site-local destination addresses, the
1610 * set of candidate source addresses MUST only
1611 * include addresses assigned to interfaces
1612 * belonging to the same site as the outgoing
1613 * interface.)
Erik Kline3985e8a2015-07-22 16:38:25 +09001614 * - "It is RECOMMENDED that the candidate source addresses
1615 * be the set of unicast addresses assigned to the
1616 * interface that will be used to send to the destination
1617 * (the 'outgoing' interface)." (RFC 6724)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001618 */
1619 if (dst_dev) {
Erik Kline3985e8a2015-07-22 16:38:25 +09001620 idev = __in6_dev_get(dst_dev);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001621 if ((dst_type & IPV6_ADDR_MULTICAST) ||
Erik Kline3985e8a2015-07-22 16:38:25 +09001622 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1623 (idev && idev->cnf.use_oif_addrs_only)) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001624 use_oif_addr = true;
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001625 }
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001626 }
1627
1628 if (use_oif_addr) {
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001629 if (idev)
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001630 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001631 } else {
David Ahernafbac6012016-06-16 16:24:26 -07001632 const struct net_device *master;
1633 int master_idx = 0;
1634
1635 /* if dst_dev exists and is enslaved to an L3 device, then
1636 * prefer addresses from dst_dev and then the master over
1637 * any other enslaved devices in the L3 domain.
1638 */
1639 master = l3mdev_master_dev_rcu(dst_dev);
1640 if (master) {
1641 master_idx = master->ifindex;
1642
1643 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1644 master, &dst,
1645 scores, hiscore_idx);
1646
1647 if (scores[hiscore_idx].ifa)
1648 goto out;
1649 }
1650
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001651 for_each_netdev_rcu(net, dev) {
David Ahernafbac6012016-06-16 16:24:26 -07001652 /* only consider addresses on devices in the
1653 * same L3 domain
1654 */
1655 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1656 continue;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001657 idev = __in6_dev_get(dev);
1658 if (!idev)
1659 continue;
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001660 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001661 }
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001662 }
David Ahernafbac6012016-06-16 16:24:26 -07001663
1664out:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001665 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001667 hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001668 if (!hiscore->ifa)
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001669 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001670
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001671 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001672 in6_ifa_put(hiscore->ifa);
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001675EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Amerigo Wang89657792013-06-29 21:30:49 +08001677int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001678 u32 banned_flags)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001679{
1680 struct inet6_ifaddr *ifp;
1681 int err = -EADDRNOTAVAIL;
1682
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01001683 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1684 if (ifp->scope > IFA_LINK)
1685 break;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001686 if (ifp->scope == IFA_LINK &&
1687 !(ifp->flags & banned_flags)) {
1688 *addr = ifp->addr;
1689 err = 0;
1690 break;
1691 }
1692 }
1693 return err;
1694}
1695
Neil Horman95c385b2007-04-25 17:08:10 -07001696int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001697 u32 banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 struct inet6_dev *idev;
1700 int err = -EADDRNOTAVAIL;
1701
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001702 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001703 idev = __in6_dev_get(dev);
1704 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 read_lock_bh(&idev->lock);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001706 err = __ipv6_get_lladdr(idev, addr, banned_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 read_unlock_bh(&idev->lock);
1708 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001709 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 return err;
1711}
1712
1713static int ipv6_count_addresses(struct inet6_dev *idev)
1714{
1715 int cnt = 0;
1716 struct inet6_ifaddr *ifp;
1717
1718 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001719 list_for_each_entry(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 cnt++;
1721 read_unlock_bh(&idev->lock);
1722 return cnt;
1723}
1724
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001725int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Florian Westphal2a7851b2013-05-17 03:56:10 +00001726 const struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
Erik Klinec58da4c2015-02-04 20:01:23 +09001728 return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1729}
1730EXPORT_SYMBOL(ipv6_chk_addr);
1731
1732int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1733 const struct net_device *dev, int strict,
1734 u32 banned_flags)
1735{
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001736 struct inet6_ifaddr *ifp;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001737 unsigned int hash = inet6_addr_hash(addr);
Erik Klinec58da4c2015-02-04 20:01:23 +09001738 u32 ifp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
stephen hemminger5c578aed2010-03-17 20:31:11 +00001740 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001741 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001742 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001743 continue;
Erik Klinec58da4c2015-02-04 20:01:23 +09001744 /* Decouple optimistic from tentative for evaluation here.
1745 * Ban optimistic addresses explicitly, when required.
1746 */
1747 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1748 ? (ifp->flags&~IFA_F_TENTATIVE)
1749 : ifp->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (ipv6_addr_equal(&ifp->addr, addr) &&
Erik Klinec58da4c2015-02-04 20:01:23 +09001751 !(ifp_flags&banned_flags) &&
Ian Morris63159f22015-03-29 14:00:04 +01001752 (!dev || ifp->idev->dev == dev ||
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001753 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1754 rcu_read_unlock_bh();
1755 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
1757 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00001758
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001759 rcu_read_unlock_bh();
1760 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
Erik Klinec58da4c2015-02-04 20:01:23 +09001762EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001763
David S. Miller3e81c6d2010-03-20 16:18:00 -07001764static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1765 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Eric Dumazetddbe5032012-07-18 08:11:12 +00001767 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001768 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Sasha Levinb67bfe02013-02-27 17:06:00 -08001770 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001771 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001772 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001774 if (!dev || ifp->idev->dev == dev)
David S. Miller3e81c6d2010-03-20 16:18:00 -07001775 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
1777 }
David S. Miller3e81c6d2010-03-20 16:18:00 -07001778 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001781/* Compares an address/prefix_len with addresses on device @dev.
1782 * If one is found it returns true.
1783 */
1784bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1785 const unsigned int prefix_len, struct net_device *dev)
1786{
1787 struct inet6_dev *idev;
1788 struct inet6_ifaddr *ifa;
1789 bool ret = false;
1790
1791 rcu_read_lock();
1792 idev = __in6_dev_get(dev);
1793 if (idev) {
1794 read_lock_bh(&idev->lock);
1795 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1796 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1797 if (ret)
1798 break;
1799 }
1800 read_unlock_bh(&idev->lock);
1801 }
1802 rcu_read_unlock();
1803
1804 return ret;
1805}
1806EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1807
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001808int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001809{
1810 struct inet6_dev *idev;
1811 struct inet6_ifaddr *ifa;
1812 int onlink;
1813
1814 onlink = 0;
1815 rcu_read_lock();
1816 idev = __in6_dev_get(dev);
1817 if (idev) {
1818 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001819 list_for_each_entry(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001820 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1821 ifa->prefix_len);
1822 if (onlink)
1823 break;
1824 }
1825 read_unlock_bh(&idev->lock);
1826 }
1827 rcu_read_unlock();
1828 return onlink;
1829}
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001830EXPORT_SYMBOL(ipv6_chk_prefix);
1831
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001832struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001833 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
David S. Millerb79d1d52010-03-25 21:39:21 -07001835 struct inet6_ifaddr *ifp, *result = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001836 unsigned int hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
stephen hemminger5c578aed2010-03-17 20:31:11 +00001838 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001839 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001840 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001841 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001843 if (!dev || ifp->idev->dev == dev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07001845 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 in6_ifa_hold(ifp);
1847 break;
1848 }
1849 }
1850 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00001851 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
David S. Millerb79d1d52010-03-25 21:39:21 -07001853 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856/* Gets referenced address, destroys ifaddr */
1857
Brian Haleycc411d02009-09-09 14:41:32 +00001858static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
Lubomir Rintel3d171f32016-01-08 13:47:23 +01001860 if (dad_failed)
1861 ifp->flags |= IFA_F_DADFAILED;
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (ifp->flags&IFA_F_PERMANENT) {
1864 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001865 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 ifp->flags |= IFA_F_TENTATIVE;
1867 spin_unlock_bh(&ifp->lock);
Herbert Xue2577a02010-03-13 12:23:29 -08001868 if (dad_failed)
1869 ipv6_ifa_notify(0, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 } else if (ifp->flags&IFA_F_TEMPORARY) {
1872 struct inet6_ifaddr *ifpub;
1873 spin_lock_bh(&ifp->lock);
1874 ifpub = ifp->ifpub;
1875 if (ifpub) {
1876 in6_ifa_hold(ifpub);
1877 spin_unlock_bh(&ifp->lock);
1878 ipv6_create_tempaddr(ifpub, ifp);
1879 in6_ifa_put(ifpub);
1880 } else {
1881 spin_unlock_bh(&ifp->lock);
1882 }
1883 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001884 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
1888
Herbert Xuf2344a12010-05-18 15:55:27 -07001889static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1890{
1891 int err = -ENOENT;
1892
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001893 spin_lock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001894 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1895 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1896 err = 0;
1897 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001898 spin_unlock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001899
1900 return err;
1901}
1902
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001903void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1904{
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001905 struct inet6_dev *idev = ifp->idev;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001906 struct net *net = dev_net(ifp->idev->dev);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001907
Ursula Braun853dc2e2010-10-24 23:06:43 +00001908 if (addrconf_dad_end(ifp)) {
1909 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07001910 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00001911 }
Herbert Xuf2344a12010-05-18 15:55:27 -07001912
Joe Perchese87cc472012-05-13 21:56:26 +00001913 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1914 ifp->idev->dev->name, &ifp->addr);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001915
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001916 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001917
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001918 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
1919 int scope = ifp->scope;
1920 u32 flags = ifp->flags;
1921 struct in6_addr new_addr;
1922 struct inet6_ifaddr *ifp2;
1923 u32 valid_lft, preferred_lft;
1924 int pfxlen = ifp->prefix_len;
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001925 int retries = ifp->stable_privacy_retry + 1;
1926
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001927 if (retries > net->ipv6.sysctl.idgen_retries) {
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001928 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
1929 ifp->idev->dev->name);
1930 goto errdad;
1931 }
1932
1933 new_addr = ifp->addr;
1934 if (ipv6_generate_stable_address(&new_addr, retries,
1935 idev))
1936 goto errdad;
1937
1938 valid_lft = ifp->valid_lft;
1939 preferred_lft = ifp->prefered_lft;
1940
1941 spin_unlock_bh(&ifp->lock);
1942
1943 if (idev->cnf.max_addresses &&
1944 ipv6_count_addresses(idev) >=
1945 idev->cnf.max_addresses)
1946 goto lock_errdad;
1947
1948 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
1949 ifp->idev->dev->name);
1950
1951 ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
1952 scope, flags, valid_lft,
1953 preferred_lft);
1954 if (IS_ERR(ifp2))
1955 goto lock_errdad;
1956
1957 spin_lock_bh(&ifp2->lock);
1958 ifp2->stable_privacy_retry = retries;
1959 ifp2->state = INET6_IFADDR_STATE_PREDAD;
1960 spin_unlock_bh(&ifp2->lock);
1961
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001962 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001963 in6_ifa_put(ifp2);
1964lock_errdad:
1965 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001966 }
1967
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001968errdad:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001969 /* transition from _POSTDAD to _ERRDAD */
1970 ifp->state = INET6_IFADDR_STATE_ERRDAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001971 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001972
1973 addrconf_mod_dad_work(ifp, 0);
Wei Yongjun751eb6b2016-09-05 16:06:31 +08001974 in6_ifa_put(ifp);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001975}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02001977/* Join to solicited addr multicast group.
1978 * caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001979void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
1981 struct in6_addr maddr;
1982
1983 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1984 return;
1985
1986 addrconf_addr_solict_mult(addr, &maddr);
1987 ipv6_dev_mc_inc(dev, &maddr);
1988}
1989
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02001990/* caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001991void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992{
1993 struct in6_addr maddr;
1994
1995 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1996 return;
1997
1998 addrconf_addr_solict_mult(addr, &maddr);
1999 __ipv6_dev_mc_dec(idev, &maddr);
2000}
2001
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002002/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002003static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
2005 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002006
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002007 if (ifp->prefix_len >= 127) /* RFC 6164 */
Bjørn Mork2bda8a02011-07-05 23:04:13 +00002008 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2010 if (ipv6_addr_any(&addr))
2011 return;
WANG Cong013b4d92014-09-11 15:35:11 -07002012 __ipv6_dev_ac_inc(ifp->idev, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013}
2014
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002015/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002016static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
2018 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002019
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002020 if (ifp->prefix_len >= 127) /* RFC 6164 */
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00002021 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2023 if (ipv6_addr_any(&addr))
2024 return;
2025 __ipv6_dev_ac_dec(ifp->idev, &addr);
2026}
2027
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002028static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
2029{
Alexander Aring5241c2d2015-12-14 20:55:22 +01002030 if (dev->addr_len != EUI64_ADDR_LEN)
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002031 return -1;
Alexander Aring5241c2d2015-12-14 20:55:22 +01002032 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
YOSHIFUJI Hideaki / 吉藤英明5e98a362013-01-28 10:44:29 +00002033 eui[0] ^= 2;
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002034 return 0;
2035}
2036
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002037static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2038{
2039 union fwnet_hwaddr *ha;
2040
2041 if (dev->addr_len != FWNET_ALEN)
2042 return -1;
2043
2044 ha = (union fwnet_hwaddr *)dev->dev_addr;
2045
2046 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2047 eui[0] ^= 2;
2048 return 0;
2049}
2050
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002051static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2052{
2053 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2054 if (dev->addr_len != ARCNET_ALEN)
2055 return -1;
2056 memset(eui, 0, 7);
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002057 eui[7] = *(u8 *)dev->dev_addr;
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002058 return 0;
2059}
2060
2061static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2062{
2063 if (dev->addr_len != INFINIBAND_ALEN)
2064 return -1;
2065 memcpy(eui, dev->dev_addr + 12, 8);
2066 eui[0] |= 2;
2067 return 0;
2068}
2069
stephen hemmingerc61393e2010-10-04 20:17:53 +00002070static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002071{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00002072 if (addr == 0)
2073 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002074 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2075 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2076 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2077 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2078 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2079 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2080 eui[1] = 0;
2081 eui[2] = 0x5E;
2082 eui[3] = 0xFE;
2083 memcpy(eui + 4, &addr, 4);
2084 return 0;
2085}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002086
2087static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2088{
2089 if (dev->priv_flags & IFF_ISATAP)
2090 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2091 return -1;
2092}
2093
stephen hemmingeraee80b52011-06-08 10:44:30 +00002094static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2095{
2096 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2097}
2098
Nicolas Dichtele8377352013-08-20 12:16:06 +02002099static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2100{
2101 memcpy(eui, dev->perm_addr, 3);
2102 memcpy(eui + 5, dev->perm_addr + 3, 3);
2103 eui[3] = 0xFF;
2104 eui[4] = 0xFE;
2105 eui[0] ^= 2;
2106 return 0;
2107}
2108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2110{
2111 switch (dev->type) {
2112 case ARPHRD_ETHER:
2113 case ARPHRD_FDDI:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002114 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002116 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002118 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002119 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002120 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002121 case ARPHRD_IPGRE:
2122 return addrconf_ifid_gre(eui, dev);
Jukka Rissanene74bccb82013-12-11 17:05:36 +02002123 case ARPHRD_6LOWPAN:
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002124 return addrconf_ifid_eui64(eui, dev);
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002125 case ARPHRD_IEEE1394:
2126 return addrconf_ifid_ieee1394(eui, dev);
Nicolas Dichtele8377352013-08-20 12:16:06 +02002127 case ARPHRD_TUNNEL6:
2128 return addrconf_ifid_ip6tnl(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 }
2130 return -1;
2131}
2132
2133static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2134{
2135 int err = -1;
2136 struct inet6_ifaddr *ifp;
2137
2138 read_lock_bh(&idev->lock);
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01002139 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2140 if (ifp->scope > IFA_LINK)
2141 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2143 memcpy(eui, ifp->addr.s6_addr+8, 8);
2144 err = 0;
2145 break;
2146 }
2147 }
2148 read_unlock_bh(&idev->lock);
2149 return err;
2150}
2151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00002153static void __ipv6_regen_rndid(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08002156 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
2159 /*
2160 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2161 * check if generated address is not inappropriate
2162 *
2163 * - Reserved subnet anycast (RFC 2526)
2164 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002165 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 * 00-00-5E-FE-xx-xx-xx-xx
2167 * - value 0
2168 * - XXX: already assigned to an address on the device
2169 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002170 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2172 (idev->rndid[7]&0x80))
2173 goto regen;
2174 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2175 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2176 goto regen;
2177 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2178 goto regen;
2179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
2182static void ipv6_regen_rndid(unsigned long data)
2183{
2184 struct inet6_dev *idev = (struct inet6_dev *) data;
2185 unsigned long expires;
2186
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002187 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 write_lock_bh(&idev->lock);
2189
2190 if (idev->dead)
2191 goto out;
2192
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00002193 __ipv6_regen_rndid(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 expires = jiffies +
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002196 idev->cnf.temp_prefered_lft * HZ -
Jiri Pirko1f9248e2013-12-07 19:26:53 +01002197 idev->cnf.regen_max_retry * idev->cnf.dad_transmits *
2198 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) -
Ben Hutchings784e2712010-06-26 11:42:55 +00002199 idev->cnf.max_desync_factor * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if (time_before(expires, jiffies)) {
Joe Perchesf3213832012-05-15 14:11:53 +00002201 pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
2202 __func__, idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 goto out;
2204 }
2205
2206 if (!mod_timer(&idev->regen_timer, expires))
2207 in6_dev_hold(idev);
2208
2209out:
2210 write_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002211 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 in6_dev_put(idev);
2213}
2214
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00002215static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002216{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00002218 __ipv6_regen_rndid(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
2221/*
2222 * Add prefix route.
2223 */
2224
2225static void
2226addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002227 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
Thomas Graf86872cb2006-08-22 00:01:08 -07002229 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002230 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
Thomas Graf86872cb2006-08-22 00:01:08 -07002231 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2232 .fc_ifindex = dev->ifindex,
2233 .fc_expires = expires,
2234 .fc_dst_len = plen,
2235 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002236 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07002237 .fc_protocol = RTPROT_KERNEL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002238 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002240 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242 /* Prevent useless cloning on PtP SIT.
2243 This thing is done here expecting that the whole
2244 class of non-broadcast devices need not cloning.
2245 */
Amerigo Wang07a93622012-10-29 16:23:10 +00002246#if IS_ENABLED(CONFIG_IPV6_SIT)
Thomas Graf86872cb2006-08-22 00:01:08 -07002247 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2248 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002249#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Thomas Graf86872cb2006-08-22 00:01:08 -07002251 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002254
2255static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2256 int plen,
2257 const struct net_device *dev,
2258 u32 flags, u32 noflags)
2259{
2260 struct fib6_node *fn;
2261 struct rt6_info *rt = NULL;
2262 struct fib6_table *table;
David Ahernca254492015-10-12 11:47:10 -07002263 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002264
David Ahernca254492015-10-12 11:47:10 -07002265 table = fib6_get_table(dev_net(dev), tb_id);
Ian Morris63159f22015-03-29 14:00:04 +01002266 if (!table)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002267 return NULL;
2268
Li RongQing5744dd92012-09-11 21:59:01 +00002269 read_lock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002270 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2271 if (!fn)
2272 goto out;
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07002273
2274 noflags |= RTF_CACHE;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002275 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002276 if (rt->dst.dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002277 continue;
2278 if ((rt->rt6i_flags & flags) != flags)
2279 continue;
Romain Kuntz85da53b2013-01-09 15:02:26 +01002280 if ((rt->rt6i_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002281 continue;
2282 dst_hold(&rt->dst);
2283 break;
2284 }
2285out:
Li RongQing5744dd92012-09-11 21:59:01 +00002286 read_unlock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002287 return rt;
2288}
2289
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291/* Create "default" multicast route to the interface */
2292
2293static void addrconf_add_mroute(struct net_device *dev)
2294{
Thomas Graf86872cb2006-08-22 00:01:08 -07002295 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002296 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002297 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2298 .fc_ifindex = dev->ifindex,
2299 .fc_dst_len = 8,
2300 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002301 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002302 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Thomas Graf86872cb2006-08-22 00:01:08 -07002304 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2305
2306 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307}
2308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2310{
2311 struct inet6_dev *idev;
2312
2313 ASSERT_RTNL();
2314
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002315 idev = ipv6_find_idev(dev);
2316 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00002317 return ERR_PTR(-ENOBUFS);
2318
2319 if (idev->cnf.disable_ipv6)
2320 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 /* Add default multicast route */
David Ahernba46ee42016-06-13 13:44:18 -07002323 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
Li Wei4af04ab2011-12-06 21:23:45 +00002324 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 return idev;
2327}
2328
Jiri Pirko53bd6742013-12-06 09:45:22 +01002329static void manage_tempaddrs(struct inet6_dev *idev,
2330 struct inet6_ifaddr *ifp,
2331 __u32 valid_lft, __u32 prefered_lft,
2332 bool create, unsigned long now)
2333{
2334 u32 flags;
2335 struct inet6_ifaddr *ift;
2336
2337 read_lock_bh(&idev->lock);
2338 /* update all temporary addresses in the list */
2339 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2340 int age, max_valid, max_prefered;
2341
2342 if (ifp != ift->ifpub)
2343 continue;
2344
2345 /* RFC 4941 section 3.3:
2346 * If a received option will extend the lifetime of a public
2347 * address, the lifetimes of temporary addresses should
2348 * be extended, subject to the overall constraint that no
2349 * temporary addresses should ever remain "valid" or "preferred"
2350 * for a time longer than (TEMP_VALID_LIFETIME) or
2351 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2352 */
2353 age = (now - ift->cstamp) / HZ;
2354 max_valid = idev->cnf.temp_valid_lft - age;
2355 if (max_valid < 0)
2356 max_valid = 0;
2357
2358 max_prefered = idev->cnf.temp_prefered_lft -
2359 idev->cnf.max_desync_factor - age;
2360 if (max_prefered < 0)
2361 max_prefered = 0;
2362
2363 if (valid_lft > max_valid)
2364 valid_lft = max_valid;
2365
2366 if (prefered_lft > max_prefered)
2367 prefered_lft = max_prefered;
2368
2369 spin_lock(&ift->lock);
2370 flags = ift->flags;
2371 ift->valid_lft = valid_lft;
2372 ift->prefered_lft = prefered_lft;
2373 ift->tstamp = now;
2374 if (prefered_lft > 0)
2375 ift->flags &= ~IFA_F_DEPRECATED;
2376
2377 spin_unlock(&ift->lock);
2378 if (!(flags&IFA_F_TENTATIVE))
2379 ipv6_ifa_notify(0, ift);
2380 }
2381
2382 if ((create || list_empty(&idev->tempaddr_list)) &&
2383 idev->cnf.use_tempaddr > 0) {
2384 /* When a new public address is created as described
2385 * in [ADDRCONF], also create a new temporary address.
2386 * Also create a temporary address if it's enabled but
2387 * no temporary address currently exists.
2388 */
2389 read_unlock_bh(&idev->lock);
2390 ipv6_create_tempaddr(ifp, NULL);
2391 } else {
2392 read_unlock_bh(&idev->lock);
2393 }
2394}
2395
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002396static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2397{
2398 return idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2399 idev->addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
2400}
2401
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002402int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2403 const struct prefix_info *pinfo,
2404 struct inet6_dev *in6_dev,
2405 const struct in6_addr *addr, int addr_type,
2406 u32 addr_flags, bool sllao, bool tokenized,
2407 __u32 valid_lft, u32 prefered_lft)
Alexander Aring4f672232016-06-15 21:20:22 +02002408{
2409 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2410 int create = 0, update_lft = 0;
2411
2412 if (!ifp && valid_lft) {
2413 int max_addresses = in6_dev->cnf.max_addresses;
2414
2415#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2416 if (in6_dev->cnf.optimistic_dad &&
2417 !net->ipv6.devconf_all->forwarding && sllao)
2418 addr_flags |= IFA_F_OPTIMISTIC;
2419#endif
2420
2421 /* Do not allow to create too much of autoconfigured
2422 * addresses; this would be too easy way to crash kernel.
2423 */
2424 if (!max_addresses ||
2425 ipv6_count_addresses(in6_dev) < max_addresses)
2426 ifp = ipv6_add_addr(in6_dev, addr, NULL,
2427 pinfo->prefix_len,
2428 addr_type&IPV6_ADDR_SCOPE_MASK,
2429 addr_flags, valid_lft,
2430 prefered_lft);
2431
2432 if (IS_ERR_OR_NULL(ifp))
2433 return -1;
2434
2435 update_lft = 0;
2436 create = 1;
2437 spin_lock_bh(&ifp->lock);
2438 ifp->flags |= IFA_F_MANAGETEMPADDR;
2439 ifp->cstamp = jiffies;
2440 ifp->tokenized = tokenized;
2441 spin_unlock_bh(&ifp->lock);
2442 addrconf_dad_start(ifp);
2443 }
2444
2445 if (ifp) {
2446 u32 flags;
2447 unsigned long now;
2448 u32 stored_lft;
2449
2450 /* update lifetime (RFC2462 5.5.3 e) */
2451 spin_lock_bh(&ifp->lock);
2452 now = jiffies;
2453 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2454 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2455 else
2456 stored_lft = 0;
2457 if (!update_lft && !create && stored_lft) {
2458 const u32 minimum_lft = min_t(u32,
2459 stored_lft, MIN_VALID_LIFETIME);
2460 valid_lft = max(valid_lft, minimum_lft);
2461
2462 /* RFC4862 Section 5.5.3e:
2463 * "Note that the preferred lifetime of the
2464 * corresponding address is always reset to
2465 * the Preferred Lifetime in the received
2466 * Prefix Information option, regardless of
2467 * whether the valid lifetime is also reset or
2468 * ignored."
2469 *
2470 * So we should always update prefered_lft here.
2471 */
2472 update_lft = 1;
2473 }
2474
2475 if (update_lft) {
2476 ifp->valid_lft = valid_lft;
2477 ifp->prefered_lft = prefered_lft;
2478 ifp->tstamp = now;
2479 flags = ifp->flags;
2480 ifp->flags &= ~IFA_F_DEPRECATED;
2481 spin_unlock_bh(&ifp->lock);
2482
2483 if (!(flags&IFA_F_TENTATIVE))
2484 ipv6_ifa_notify(0, ifp);
2485 } else
2486 spin_unlock_bh(&ifp->lock);
2487
2488 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2489 create, now);
2490
2491 in6_ifa_put(ifp);
2492 addrconf_verify();
2493 }
2494
2495 return 0;
2496}
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002497EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
Alexander Aring4f672232016-06-15 21:20:22 +02002498
Neil Hormane6bff992012-01-04 10:49:15 +00002499void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500{
2501 struct prefix_info *pinfo;
2502 __u32 valid_lft;
2503 __u32 prefered_lft;
Alexander Aring4f672232016-06-15 21:20:22 +02002504 int addr_type, err;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002505 u32 addr_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07002507 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
2509 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 if (len < sizeof(struct prefix_info)) {
dingtianhongba3542e2013-08-17 10:27:04 +08002512 ADBG("addrconf: prefix option too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 return;
2514 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 /*
2517 * Validation checks ([ADDRCONF], page 19)
2518 */
2519
2520 addr_type = ipv6_addr_type(&pinfo->prefix);
2521
2522 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2523 return;
2524
2525 valid_lft = ntohl(pinfo->valid);
2526 prefered_lft = ntohl(pinfo->prefered);
2527
2528 if (prefered_lft > valid_lft) {
Joe Perchese87cc472012-05-13 21:56:26 +00002529 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return;
2531 }
2532
2533 in6_dev = in6_dev_get(dev);
2534
Ian Morris63159f22015-03-29 14:00:04 +01002535 if (!in6_dev) {
Joe Perchese87cc472012-05-13 21:56:26 +00002536 net_dbg_ratelimited("addrconf: device %s not configured\n",
2537 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return;
2539 }
2540
2541 /*
2542 * Two things going on here:
2543 * 1) Add routes for on-link prefixes
2544 * 2) Configure prefixes with the auto flag set
2545 */
2546
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002547 if (pinfo->onlink) {
2548 struct rt6_info *rt;
2549 unsigned long rt_expires;
2550
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002551 /* Avoid arithmetic overflow. Really, we could
2552 * save rt_expires in seconds, likely valid_lft,
2553 * but it would require division in fib gc, that it
2554 * not good.
2555 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002556 if (HZ > USER_HZ)
2557 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2558 else
2559 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08002560
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002561 if (addrconf_finite_timeout(rt_expires))
2562 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002564 rt = addrconf_get_prefix_route(&pinfo->prefix,
2565 pinfo->prefix_len,
2566 dev,
2567 RTF_ADDRCONF | RTF_PREFIX_RT,
2568 RTF_GATEWAY | RTF_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002570 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002571 /* Autoconf prefix route */
2572 if (valid_lft == 0) {
2573 ip6_del_rt(rt);
2574 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002575 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002576 /* not infinity */
Gao feng1716a962012-04-06 00:13:10 +00002577 rt6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002578 } else {
Gao feng1716a962012-04-06 00:13:10 +00002579 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 }
2581 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002582 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002583 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2584 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002585 /* not infinity */
2586 flags |= RTF_EXPIRES;
2587 expires = jiffies_to_clock_t(rt_expires);
2588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002590 dev, expires, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00002592 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
2594
2595 /* Try to figure out our local address for this prefix */
2596
2597 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 struct in6_addr addr;
Alexander Aringf997c552016-06-15 21:20:23 +02002599 bool tokenized = false, dev_addr_generated = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
2601 if (pinfo->prefix_len == 64) {
2602 memcpy(&addr, &pinfo->prefix, 8);
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002603
2604 if (!ipv6_addr_any(&in6_dev->token)) {
2605 read_lock_bh(&in6_dev->lock);
2606 memcpy(addr.s6_addr + 8,
2607 in6_dev->token.s6_addr + 8, 8);
2608 read_unlock_bh(&in6_dev->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00002609 tokenized = true;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002610 } else if (is_addr_mode_generate_stable(in6_dev) &&
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01002611 !ipv6_generate_stable_address(&addr, 0,
2612 in6_dev)) {
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002613 addr_flags |= IFA_F_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01002614 goto ok;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002615 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2616 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
Alexander Aring4f672232016-06-15 21:20:22 +02002617 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002618 } else {
2619 dev_addr_generated = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 }
2621 goto ok;
2622 }
Joe Perchese87cc472012-05-13 21:56:26 +00002623 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2624 pinfo->prefix_len);
Alexander Aring4f672232016-06-15 21:20:22 +02002625 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
2627ok:
Alexander Aring4f672232016-06-15 21:20:22 +02002628 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2629 &addr, addr_type,
2630 addr_flags, sllao,
2631 tokenized, valid_lft,
2632 prefered_lft);
2633 if (err)
2634 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002635
2636 /* Ignore error case here because previous prefix add addr was
2637 * successful which will be notified.
2638 */
2639 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2640 addr_type, addr_flags, sllao,
2641 tokenized, valid_lft,
2642 prefered_lft,
2643 dev_addr_generated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 }
2645 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
Alexander Aring4f672232016-06-15 21:20:22 +02002646put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 in6_dev_put(in6_dev);
2648}
2649
2650/*
2651 * Set destination address.
2652 * Special case for SIT interfaces where we create a new "virtual"
2653 * device.
2654 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002655int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
2657 struct in6_ifreq ireq;
2658 struct net_device *dev;
2659 int err = -EINVAL;
2660
2661 rtnl_lock();
2662
2663 err = -EFAULT;
2664 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2665 goto err_exit;
2666
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002667 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
2669 err = -ENODEV;
Ian Morris63159f22015-03-29 14:00:04 +01002670 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 goto err_exit;
2672
Amerigo Wang07a93622012-10-29 16:23:10 +00002673#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002675 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 struct ip_tunnel_parm p;
2678
2679 err = -EADDRNOTAVAIL;
2680 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2681 goto err_exit;
2682
2683 memset(&p, 0, sizeof(p));
2684 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2685 p.iph.saddr = 0;
2686 p.iph.version = 4;
2687 p.iph.ihl = 5;
2688 p.iph.protocol = IPPROTO_IPV6;
2689 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002690 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002692 if (ops->ndo_do_ioctl) {
2693 mm_segment_t oldfs = get_fs();
2694
2695 set_fs(KERNEL_DS);
2696 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2697 set_fs(oldfs);
2698 } else
2699 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
2701 if (err == 0) {
2702 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002703 dev = __dev_get_by_name(net, p.name);
2704 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 goto err_exit;
2706 err = dev_open(dev);
2707 }
2708 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002709#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
2711err_exit:
2712 rtnl_unlock();
2713 return err;
2714}
2715
Madhu Challa93a714d2015-02-25 09:58:35 -08002716static int ipv6_mc_config(struct sock *sk, bool join,
2717 const struct in6_addr *addr, int ifindex)
2718{
2719 int ret;
2720
2721 ASSERT_RTNL();
2722
2723 lock_sock(sk);
2724 if (join)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002725 ret = ipv6_sock_mc_join(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002726 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002727 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002728 release_sock(sk);
2729
2730 return ret;
2731}
2732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733/*
2734 * Manual configuration of address on an interface
2735 */
Jiri Pirko479840f2013-12-06 09:45:21 +01002736static int inet6_addr_add(struct net *net, int ifindex,
2737 const struct in6_addr *pfx,
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002738 const struct in6_addr *peer_pfx,
Jiri Pirko479840f2013-12-06 09:45:21 +01002739 unsigned int plen, __u32 ifa_flags,
2740 __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741{
2742 struct inet6_ifaddr *ifp;
2743 struct inet6_dev *idev;
2744 struct net_device *dev;
Madhu Challa93a714d2015-02-25 09:58:35 -08002745 unsigned long timeout;
2746 clock_t expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002748 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
2750 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002751
Thomas Graf24ef0da2008-05-28 16:54:22 +02002752 if (plen > 128)
2753 return -EINVAL;
2754
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09002755 /* check the lifetime */
2756 if (!valid_lft || prefered_lft > valid_lft)
2757 return -EINVAL;
2758
Jiri Pirko53bd6742013-12-06 09:45:22 +01002759 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2760 return -EINVAL;
2761
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002762 dev = __dev_get_by_index(net, ifindex);
2763 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002765
Brian Haley64e724f2010-07-20 10:34:30 +00002766 idev = addrconf_add_dev(dev);
2767 if (IS_ERR(idev))
2768 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
Madhu Challa93a714d2015-02-25 09:58:35 -08002770 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2771 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2772 true, pfx, ifindex);
2773
2774 if (ret < 0)
2775 return ret;
2776 }
2777
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 scope = ipv6_addr_scope(pfx);
2779
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002780 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2781 if (addrconf_finite_timeout(timeout)) {
2782 expires = jiffies_to_clock_t(timeout * HZ);
2783 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002784 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002785 } else {
2786 expires = 0;
2787 flags = 0;
2788 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002789 }
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09002790
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002791 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2792 if (addrconf_finite_timeout(timeout)) {
2793 if (timeout == 0)
2794 ifa_flags |= IFA_F_DEPRECATED;
2795 prefered_lft = timeout;
2796 }
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09002797
Jiri Benc8a226b22013-08-01 10:41:28 +02002798 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2799 valid_lft, prefered_lft);
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09002800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 if (!IS_ERR(ifp)) {
Thomas Haller761aac72014-01-15 15:36:58 +01002802 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2803 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2804 expires, flags);
2805 }
2806
Neil Horman95c385b2007-04-25 17:08:10 -07002807 /*
2808 * Note that section 3.1 of RFC 4429 indicates
2809 * that the Optimistic flag should not be set for
2810 * manually configured addresses
2811 */
David S. Millercf22f9a2012-04-14 21:37:40 -04002812 addrconf_dad_start(ifp);
Jiri Pirko53bd6742013-12-06 09:45:22 +01002813 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2814 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2815 true, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002817 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 return 0;
Madhu Challa93a714d2015-02-25 09:58:35 -08002819 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2820 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2821 false, pfx, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 }
2823
2824 return PTR_ERR(ifp);
2825}
2826
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002827static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2828 const struct in6_addr *pfx, unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
2830 struct inet6_ifaddr *ifp;
2831 struct inet6_dev *idev;
2832 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002833
Thomas Graf24ef0da2008-05-28 16:54:22 +02002834 if (plen > 128)
2835 return -EINVAL;
2836
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002837 dev = __dev_get_by_index(net, ifindex);
2838 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 return -ENODEV;
2840
Ian Morrise5d08d72014-11-23 21:28:43 +00002841 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01002842 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 return -ENXIO;
2844
2845 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002846 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 if (ifp->prefix_len == plen &&
2848 ipv6_addr_equal(pfx, &ifp->addr)) {
2849 in6_ifa_hold(ifp);
2850 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002851
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002852 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2853 (ifa_flags & IFA_F_MANAGETEMPADDR))
2854 manage_tempaddrs(idev, ifp, 0, 0, false,
2855 jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 ipv6_del_addr(ifp);
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002857 addrconf_verify_rtnl();
Madhu Challa93a714d2015-02-25 09:58:35 -08002858 if (ipv6_addr_is_multicast(pfx)) {
2859 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2860 false, pfx, dev->ifindex);
2861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 return 0;
2863 }
2864 }
2865 read_unlock_bh(&idev->lock);
2866 return -EADDRNOTAVAIL;
2867}
2868
2869
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002870int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871{
2872 struct in6_ifreq ireq;
2873 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002874
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002875 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002877
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2879 return -EFAULT;
2880
2881 rtnl_lock();
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002882 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002883 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2884 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 rtnl_unlock();
2886 return err;
2887}
2888
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002889int addrconf_del_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;
2896
2897 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2898 return -EFAULT;
2899
2900 rtnl_lock();
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002901 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002902 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 rtnl_unlock();
2904 return err;
2905}
2906
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002907static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2908 int plen, int scope)
2909{
2910 struct inet6_ifaddr *ifp;
2911
Jiri Benc8a226b22013-08-01 10:41:28 +02002912 ifp = ipv6_add_addr(idev, addr, NULL, plen,
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01002913 scope, IFA_F_PERMANENT,
2914 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002915 if (!IS_ERR(ifp)) {
2916 spin_lock_bh(&ifp->lock);
2917 ifp->flags &= ~IFA_F_TENTATIVE;
2918 spin_unlock_bh(&ifp->lock);
2919 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2920 in6_ifa_put(ifp);
2921 }
2922}
2923
Amerigo Wang07a93622012-10-29 16:23:10 +00002924#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925static void sit_add_v4_addrs(struct inet6_dev *idev)
2926{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 struct in6_addr addr;
2928 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002929 struct net *net = dev_net(idev->dev);
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002930 int scope, plen;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002931 u32 pflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
2933 ASSERT_RTNL();
2934
2935 memset(&addr, 0, sizeof(struct in6_addr));
2936 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2937
2938 if (idev->dev->flags&IFF_POINTOPOINT) {
2939 addr.s6_addr32[0] = htonl(0xfe800000);
2940 scope = IFA_LINK;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002941 plen = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 } else {
2943 scope = IPV6_ADDR_COMPATv4;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002944 plen = 96;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002945 pflags |= RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 }
2947
2948 if (addr.s6_addr32[3]) {
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002949 add_addr(idev, &addr, plen, scope);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002950 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 return;
2952 }
2953
Benjamin Thery6fda7352008-03-05 10:47:47 -08002954 for_each_netdev(net, dev) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002955 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 if (in_dev && (dev->flags & IFF_UP)) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002957 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
2959 int flag = scope;
2960
2961 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
2963 addr.s6_addr32[3] = ifa->ifa_local;
2964
2965 if (ifa->ifa_scope == RT_SCOPE_LINK)
2966 continue;
2967 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2968 if (idev->dev->flags&IFF_POINTOPOINT)
2969 continue;
2970 flag |= IFA_HOST;
2971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002973 add_addr(idev, &addr, plen, flag);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002974 addrconf_prefix_route(&addr, plen, idev->dev, 0,
2975 pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 }
2977 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002980#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
2982static void init_loopback(struct net_device *dev)
2983{
2984 struct inet6_dev *idev;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05302985 struct net_device *sp_dev;
2986 struct inet6_ifaddr *sp_ifa;
2987 struct rt6_info *sp_rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
2989 /* ::1 */
2990
2991 ASSERT_RTNL();
2992
Ian Morrise5d08d72014-11-23 21:28:43 +00002993 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01002994 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00002995 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 return;
2997 }
2998
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002999 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303000
3001 /* Add routes to other interface's IPv6 addresses */
3002 for_each_netdev(dev_net(dev), sp_dev) {
3003 if (!strcmp(sp_dev->name, dev->name))
3004 continue;
3005
3006 idev = __in6_dev_get(sp_dev);
3007 if (!idev)
3008 continue;
3009
3010 read_lock_bh(&idev->lock);
3011 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
3012
3013 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
3014 continue;
3015
Gao feng33d99112014-01-24 16:29:11 +08003016 if (sp_ifa->rt) {
3017 /* This dst has been added to garbage list when
3018 * lo device down, release this obsolete dst and
3019 * reallocate a new router for ifa.
3020 */
3021 if (sp_ifa->rt->dst.obsolete > 0) {
3022 ip6_rt_put(sp_ifa->rt);
3023 sp_ifa->rt = NULL;
3024 } else {
3025 continue;
3026 }
3027 }
Gao fenga881ae12013-06-16 11:14:30 +08003028
François-Xavier Le Bail57ec0af2013-12-02 11:28:49 +01003029 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303030
3031 /* Failure cases are ignored */
Gao feng534c8772013-06-02 22:16:21 +00003032 if (!IS_ERR(sp_rt)) {
3033 sp_ifa->rt = sp_rt;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303034 ip6_ins_rt(sp_rt);
Gao feng534c8772013-06-02 22:16:21 +00003035 }
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303036 }
3037 read_unlock_bh(&idev->lock);
3038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039}
3040
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003041void addrconf_add_linklocal(struct inet6_dev *idev,
3042 const struct in6_addr *addr, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003044 struct inet6_ifaddr *ifp;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003045 u32 addr_flags = flags | IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Neil Horman95c385b2007-04-25 17:08:10 -07003047#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3048 if (idev->cnf.optimistic_dad &&
David Miller702beb82008-07-20 18:17:02 -07003049 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
Neil Horman95c385b2007-04-25 17:08:10 -07003050 addr_flags |= IFA_F_OPTIMISTIC;
3051#endif
3052
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01003053 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
3054 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003056 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
David S. Millercf22f9a2012-04-14 21:37:40 -04003057 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 in6_ifa_put(ifp);
3059 }
3060}
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003061EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003063static bool ipv6_reserved_interfaceid(struct in6_addr address)
3064{
3065 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3066 return true;
3067
3068 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3069 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3070 return true;
3071
3072 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3073 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3074 return true;
3075
3076 return false;
3077}
3078
3079static int ipv6_generate_stable_address(struct in6_addr *address,
3080 u8 dad_count,
3081 const struct inet6_dev *idev)
3082{
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003083 static DEFINE_SPINLOCK(lock);
3084 static __u32 digest[SHA_DIGEST_WORDS];
3085 static __u32 workspace[SHA_WORKSPACE_WORDS];
3086
3087 static union {
3088 char __data[SHA_MESSAGE_BYTES];
3089 struct {
3090 struct in6_addr secret;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003091 __be32 prefix[2];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003092 unsigned char hwaddr[MAX_ADDR_LEN];
3093 u8 dad_count;
3094 } __packed;
3095 } data;
3096
3097 struct in6_addr secret;
3098 struct in6_addr temp;
3099 struct net *net = dev_net(idev->dev);
3100
3101 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3102
3103 if (idev->cnf.stable_secret.initialized)
3104 secret = idev->cnf.stable_secret.secret;
3105 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3106 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3107 else
3108 return -1;
3109
3110retry:
3111 spin_lock_bh(&lock);
3112
3113 sha_init(digest);
3114 memset(&data, 0, sizeof(data));
3115 memset(workspace, 0, sizeof(workspace));
3116 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003117 data.prefix[0] = address->s6_addr32[0];
3118 data.prefix[1] = address->s6_addr32[1];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003119 data.secret = secret;
3120 data.dad_count = dad_count;
3121
3122 sha_transform(digest, data.__data, workspace);
3123
3124 temp = *address;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003125 temp.s6_addr32[2] = (__force __be32)digest[0];
3126 temp.s6_addr32[3] = (__force __be32)digest[1];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003127
3128 spin_unlock_bh(&lock);
3129
3130 if (ipv6_reserved_interfaceid(temp)) {
3131 dad_count++;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01003132 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003133 return -1;
3134 goto retry;
3135 }
3136
3137 *address = temp;
3138 return 0;
3139}
3140
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003141static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3142{
3143 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3144
3145 if (s->initialized)
3146 return;
3147 s = &idev->cnf.stable_secret;
3148 get_random_bytes(&s->secret, sizeof(s->secret));
3149 s->initialized = true;
3150}
3151
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003152static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3153{
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003154 struct in6_addr addr;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003155
David Ahernca254492015-10-12 11:47:10 -07003156 /* no link local addresses on L3 master devices */
3157 if (netif_is_l3_master(idev->dev))
3158 return;
3159
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003160 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3161
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003162 switch (idev->addr_gen_mode) {
3163 case IN6_ADDR_GEN_MODE_RANDOM:
3164 ipv6_gen_mode_random_init(idev);
3165 /* fallthrough */
3166 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003167 if (!ipv6_generate_stable_address(&addr, 0, idev))
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003168 addrconf_add_linklocal(idev, &addr,
3169 IFA_F_STABLE_PRIVACY);
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003170 else if (prefix_route)
3171 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003172 break;
3173 case IN6_ADDR_GEN_MODE_EUI64:
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003174 /* addrconf_add_linklocal also adds a prefix_route and we
3175 * only need to care about prefix routes if ipv6_generate_eui64
3176 * couldn't generate one.
3177 */
3178 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003179 addrconf_add_linklocal(idev, &addr, 0);
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003180 else if (prefix_route)
3181 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003182 break;
3183 case IN6_ADDR_GEN_MODE_NONE:
3184 default:
3185 /* will not add any link local address */
3186 break;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003187 }
3188}
3189
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190static void addrconf_dev_config(struct net_device *dev)
3191{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003192 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
3194 ASSERT_RTNL();
3195
Herbert Xu74235a22007-06-14 13:02:55 -07003196 if ((dev->type != ARPHRD_ETHER) &&
3197 (dev->type != ARPHRD_FDDI) &&
Herbert Xu74235a22007-06-14 13:02:55 -07003198 (dev->type != ARPHRD_ARCNET) &&
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00003199 (dev->type != ARPHRD_INFINIBAND) &&
Nicolas Dichtele8377352013-08-20 12:16:06 +02003200 (dev->type != ARPHRD_IEEE1394) &&
Jukka Rissanene74bccb82013-12-11 17:05:36 +02003201 (dev->type != ARPHRD_TUNNEL6) &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003202 (dev->type != ARPHRD_6LOWPAN) &&
3203 (dev->type != ARPHRD_NONE)) {
Herbert Xu74235a22007-06-14 13:02:55 -07003204 /* Alas, we support only Ethernet autoconfiguration. */
3205 return;
3206 }
3207
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00003209 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 return;
3211
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003212 /* this device type has no EUI support */
3213 if (dev->type == ARPHRD_NONE &&
3214 idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3215 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
3216
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003217 addrconf_addr_gen(idev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218}
3219
Amerigo Wang07a93622012-10-29 16:23:10 +00003220#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221static void addrconf_sit_config(struct net_device *dev)
3222{
3223 struct inet6_dev *idev;
3224
3225 ASSERT_RTNL();
3226
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003227 /*
3228 * Configure the tunnel with one of our IPv4
3229 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 * our v4 addrs in the tunnel
3231 */
3232
Ian Morrise5d08d72014-11-23 21:28:43 +00003233 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003234 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003235 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 return;
3237 }
3238
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003239 if (dev->priv_flags & IFF_ISATAP) {
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003240 addrconf_addr_gen(idev, false);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003241 return;
3242 }
3243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 sit_add_v4_addrs(idev);
3245
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00003246 if (dev->flags&IFF_POINTOPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003249#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
Amerigo Wang07a93622012-10-29 16:23:10 +00003251#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003252static void addrconf_gre_config(struct net_device *dev)
3253{
3254 struct inet6_dev *idev;
stephen hemmingeraee80b52011-06-08 10:44:30 +00003255
stephen hemmingeraee80b52011-06-08 10:44:30 +00003256 ASSERT_RTNL();
3257
Ian Morrise5d08d72014-11-23 21:28:43 +00003258 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003259 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003260 pr_debug("%s: add_dev failed\n", __func__);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003261 return;
3262 }
3263
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003264 addrconf_addr_gen(idev, true);
Hannes Frederic Sowad9e4ce62015-10-08 18:19:39 +02003265 if (dev->flags & IFF_POINTOPOINT)
3266 addrconf_add_mroute(dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003267}
3268#endif
3269
David Ahernf1705ec2016-02-24 09:25:37 -08003270static int fixup_permanent_addr(struct inet6_dev *idev,
3271 struct inet6_ifaddr *ifp)
3272{
3273 if (!ifp->rt) {
3274 struct rt6_info *rt;
3275
3276 rt = addrconf_dst_alloc(idev, &ifp->addr, false);
3277 if (unlikely(IS_ERR(rt)))
3278 return PTR_ERR(rt);
3279
3280 ifp->rt = rt;
3281 }
3282
3283 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3284 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3285 idev->dev, 0, 0);
3286 }
3287
3288 addrconf_dad_start(ifp);
3289
3290 return 0;
3291}
3292
3293static void addrconf_permanent_addr(struct net_device *dev)
3294{
3295 struct inet6_ifaddr *ifp, *tmp;
3296 struct inet6_dev *idev;
3297
3298 idev = __in6_dev_get(dev);
3299 if (!idev)
3300 return;
3301
3302 write_lock_bh(&idev->lock);
3303
3304 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3305 if ((ifp->flags & IFA_F_PERMANENT) &&
3306 fixup_permanent_addr(idev, ifp) < 0) {
3307 write_unlock_bh(&idev->lock);
3308 ipv6_del_addr(ifp);
3309 write_lock_bh(&idev->lock);
3310
3311 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3312 idev->dev->name, &ifp->addr);
3313 }
3314 }
3315
3316 write_unlock_bh(&idev->lock);
3317}
3318
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003319static int addrconf_notify(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00003320 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321{
Jiri Pirko351638e2013-05-28 01:30:21 +00003322 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
David Ahern4f7f34e2016-04-07 11:10:41 -07003323 struct netdev_notifier_changeupper_info *info;
Eric Dumazet748e2d92012-08-22 21:50:59 +00003324 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003325 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07003326 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003328 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003329 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07003330 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003331 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -07003332 if (IS_ERR(idev))
3333 return notifier_from_errno(PTR_ERR(idev));
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003334 }
3335 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003336
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003337 case NETDEV_CHANGEMTU:
3338 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3339 if (dev->mtu < IPV6_MIN_MTU) {
3340 addrconf_ifdown(dev, 1);
3341 break;
3342 }
3343
3344 if (idev) {
3345 rt6_mtu_change(dev, dev->mtu);
3346 idev->cnf.mtu6 = dev->mtu;
3347 break;
3348 }
3349
3350 /* allocate new idev */
3351 idev = ipv6_add_dev(dev);
3352 if (IS_ERR(idev))
3353 break;
3354
3355 /* device is still not ready */
3356 if (!(idev->if_flags & IF_READY))
3357 break;
3358
3359 run_pending = 1;
3360
3361 /* fall through */
3362
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003364 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07003365 if (dev->flags & IFF_SLAVE)
3366 break;
3367
WANG Cong3ce62a82014-09-11 15:07:16 -07003368 if (idev && idev->cnf.disable_ipv6)
3369 break;
3370
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003371 if (event == NETDEV_UP) {
David Ahern38bd10c2016-04-21 20:56:12 -07003372 /* restore routes for permanent addresses */
3373 addrconf_permanent_addr(dev);
3374
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07003375 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003376 /* device is not ready yet. */
Joe Perchesf3213832012-05-15 14:11:53 +00003377 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003378 dev->name);
3379 break;
3380 }
Kristian Slavov99081042006-02-08 16:10:53 -08003381
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11003382 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3383 idev = ipv6_add_dev(dev);
3384
WANG Conga317a2f2014-07-25 15:25:09 -07003385 if (!IS_ERR_OR_NULL(idev)) {
Kristian Slavov99081042006-02-08 16:10:53 -08003386 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08003387 run_pending = 1;
3388 }
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003389 } else if (event == NETDEV_CHANGE) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07003390 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003391 /* device is still not ready. */
3392 break;
3393 }
3394
3395 if (idev) {
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003396 if (idev->if_flags & IF_READY)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003397 /* device is already configured. */
3398 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003399 idev->if_flags |= IF_READY;
3400 }
3401
Joe Perchesf3213832012-05-15 14:11:53 +00003402 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3403 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003404
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003405 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003406 }
3407
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003408 switch (dev->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +00003409#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 case ARPHRD_SIT:
3411 addrconf_sit_config(dev);
3412 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07003413#endif
Amerigo Wang07a93622012-10-29 16:23:10 +00003414#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003415 case ARPHRD_IPGRE:
3416 addrconf_gre_config(dev);
3417 break;
3418#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 case ARPHRD_LOOPBACK:
3420 init_loopback(dev);
3421 break;
3422
3423 default:
3424 addrconf_dev_config(dev);
3425 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003426 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003427
WANG Conga317a2f2014-07-25 15:25:09 -07003428 if (!IS_ERR_OR_NULL(idev)) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003429 if (run_pending)
3430 addrconf_dad_run(idev);
3431
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003432 /*
3433 * If the MTU changed during the interface down,
3434 * when the interface up, the changed MTU must be
3435 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003437 if (idev->cnf.mtu6 != dev->mtu &&
3438 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 rt6_mtu_change(dev, dev->mtu);
3440 idev->cnf.mtu6 = dev->mtu;
3441 }
3442 idev->tstamp = jiffies;
3443 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003444
3445 /*
3446 * If the changed mtu during down is lower than
3447 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 */
3449 if (dev->mtu < IPV6_MIN_MTU)
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003450 addrconf_ifdown(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 }
3452 break;
3453
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 case NETDEV_DOWN:
3455 case NETDEV_UNREGISTER:
3456 /*
3457 * Remove all addresses from this interface.
3458 */
3459 addrconf_ifdown(dev, event != NETDEV_DOWN);
3460 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003461
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07003464 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003465 addrconf_sysctl_unregister(idev);
WANG Conga317a2f2014-07-25 15:25:09 -07003466 err = addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07003467 if (err)
3468 return notifier_from_errno(err);
WANG Conga317a2f2014-07-25 15:25:09 -07003469 err = snmp6_register_dev(idev);
3470 if (err) {
3471 addrconf_sysctl_unregister(idev);
3472 return notifier_from_errno(err);
3473 }
Stephen Hemminger5632c512007-04-28 21:16:39 -07003474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003476
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003477 case NETDEV_PRE_TYPE_CHANGE:
3478 case NETDEV_POST_TYPE_CHANGE:
Andrew Lunn3ef09522015-12-03 21:12:32 +01003479 if (idev)
3480 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07003481 break;
David Ahern4f7f34e2016-04-07 11:10:41 -07003482
3483 case NETDEV_CHANGEUPPER:
3484 info = ptr;
3485
3486 /* flush all routes if dev is linked to or unlinked from
3487 * an L3 master device (e.g., VRF)
3488 */
3489 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3490 addrconf_ifdown(dev, 0);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
3493 return NOTIFY_OK;
3494}
3495
3496/*
3497 * addrconf module should be notified of a device going up
3498 */
3499static struct notifier_block ipv6_dev_notf = {
3500 .notifier_call = addrconf_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501};
3502
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003503static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07003504{
3505 struct inet6_dev *idev;
3506 ASSERT_RTNL();
3507
3508 idev = __in6_dev_get(dev);
3509
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003510 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003511 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003512 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003513 ipv6_mc_unmap(idev);
3514}
3515
David Ahern70af9212016-04-08 12:01:21 -07003516static bool addr_is_local(const struct in6_addr *addr)
3517{
3518 return ipv6_addr_type(addr) &
3519 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3520}
3521
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522static int addrconf_ifdown(struct net_device *dev, int how)
3523{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003524 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003525 struct inet6_dev *idev;
David Ahernf1705ec2016-02-24 09:25:37 -08003526 struct inet6_ifaddr *ifa, *tmp;
3527 struct list_head del_list;
3528 int _keep_addr;
3529 bool keep_addr;
David S. Miller73a8bd72011-01-23 23:27:15 -08003530 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
3532 ASSERT_RTNL();
3533
David S. Miller73a8bd72011-01-23 23:27:15 -08003534 rt6_ifdown(net, dev);
3535 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
3537 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003538 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 return -ENODEV;
3540
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003541 /*
3542 * Step 1: remove reference to ipv6 device from parent device.
3543 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003545 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003547
3548 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003549 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
3551 /* Step 1.5: remove snmp6 entry */
3552 snmp6_unregister_dev(idev);
3553
3554 }
3555
David Ahernf1705ec2016-02-24 09:25:37 -08003556 /* aggregate the system setting and interface setting */
3557 _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3558 if (!_keep_addr)
3559 _keep_addr = idev->cnf.keep_addr_on_down;
3560
3561 /* combine the user config with event to determine if permanent
3562 * addresses are to be removed from address hash table
3563 */
Mike Manningbc561632016-08-12 12:02:38 +01003564 keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003565
David S. Miller73a8bd72011-01-23 23:27:15 -08003566 /* Step 2: clear hash table */
3567 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3568 struct hlist_head *h = &inet6_addr_lst[i];
David S. Miller73a8bd72011-01-23 23:27:15 -08003569
3570 spin_lock_bh(&addrconf_hash_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01003571restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08003572 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
David S. Miller73a8bd72011-01-23 23:27:15 -08003573 if (ifa->idev == idev) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003574 addrconf_del_dad_work(ifa);
David Ahernf1705ec2016-02-24 09:25:37 -08003575 /* combined flag + permanent flag decide if
3576 * address is retained on a down event
3577 */
3578 if (!keep_addr ||
David Ahern70af9212016-04-08 12:01:21 -07003579 !(ifa->flags & IFA_F_PERMANENT) ||
3580 addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003581 hlist_del_init_rcu(&ifa->addr_lst);
3582 goto restart;
3583 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003584 }
3585 }
3586 spin_unlock_bh(&addrconf_hash_lock);
3587 }
3588
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 write_lock_bh(&idev->lock);
3590
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003591 addrconf_del_rs_timer(idev);
3592
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003593 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003594 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003595 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596
Denis V. Lunev439e2382008-04-03 13:30:17 -07003597 if (how && del_timer(&idev->regen_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 in6_dev_put(idev);
3599
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003600 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00003601 while (!list_empty(&idev->tempaddr_list)) {
3602 ifa = list_first_entry(&idev->tempaddr_list,
3603 struct inet6_ifaddr, tmp_list);
3604 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 write_unlock_bh(&idev->lock);
3606 spin_lock_bh(&ifa->lock);
3607
3608 if (ifa->ifpub) {
3609 in6_ifa_put(ifa->ifpub);
3610 ifa->ifpub = NULL;
3611 }
3612 spin_unlock_bh(&ifa->lock);
3613 in6_ifa_put(ifa);
3614 write_lock_bh(&idev->lock);
3615 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003616
David Ahernf1705ec2016-02-24 09:25:37 -08003617 /* re-combine the user config with event to determine if permanent
3618 * addresses are to be removed from the interface list
3619 */
Mike Manningbc561632016-08-12 12:02:38 +01003620 keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003621
3622 INIT_LIST_HEAD(&del_list);
3623 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
David Ahern38bd10c2016-04-21 20:56:12 -07003624 struct rt6_info *rt = NULL;
3625
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003626 addrconf_del_dad_work(ifa);
stephen hemminger84e8b802010-03-02 13:32:46 +00003627
David S. Miller73a8bd72011-01-23 23:27:15 -08003628 write_unlock_bh(&idev->lock);
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003629 spin_lock_bh(&ifa->lock);
David Ahernf1705ec2016-02-24 09:25:37 -08003630
David Ahern70af9212016-04-08 12:01:21 -07003631 if (keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3632 !addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003633 /* set state to skip the notifier below */
3634 state = INET6_IFADDR_STATE_DEAD;
3635 ifa->state = 0;
3636 if (!(ifa->flags & IFA_F_NODAD))
3637 ifa->flags |= IFA_F_TENTATIVE;
David Ahern38bd10c2016-04-21 20:56:12 -07003638
3639 rt = ifa->rt;
3640 ifa->rt = NULL;
David Ahernf1705ec2016-02-24 09:25:37 -08003641 } else {
3642 state = ifa->state;
3643 ifa->state = INET6_IFADDR_STATE_DEAD;
3644
Wei Yongjunc8822192016-07-28 16:19:03 +00003645 list_move(&ifa->if_list, &del_list);
David Ahernf1705ec2016-02-24 09:25:37 -08003646 }
3647
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003648 spin_unlock_bh(&ifa->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003649
David Ahern38bd10c2016-04-21 20:56:12 -07003650 if (rt)
3651 ip6_del_rt(rt);
3652
David S. Miller73a8bd72011-01-23 23:27:15 -08003653 if (state != INET6_IFADDR_STATE_DEAD) {
3654 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Cong Wangf88c91d2013-04-14 23:18:43 +08003655 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
Mike Manningea06f712016-07-22 18:32:11 +01003656 } else {
3657 if (idev->cnf.forwarding)
3658 addrconf_leave_anycast(ifa);
3659 addrconf_leave_solict(ifa->idev, &ifa->addr);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00003660 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003661
David S. Miller73a8bd72011-01-23 23:27:15 -08003662 write_lock_bh(&idev->lock);
3663 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003664
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 write_unlock_bh(&idev->lock);
3666
David Ahernf1705ec2016-02-24 09:25:37 -08003667 /* now clean up addresses to be removed */
3668 while (!list_empty(&del_list)) {
3669 ifa = list_first_entry(&del_list,
3670 struct inet6_ifaddr, if_list);
3671 list_del(&ifa->if_list);
3672
3673 in6_ifa_put(ifa);
3674 }
3675
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003676 /* Step 5: Discard anycast and multicast list */
3677 if (how) {
3678 ipv6_ac_destroy_dev(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 ipv6_mc_destroy_dev(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003680 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 ipv6_mc_down(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003685
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003686 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003687 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003688 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 neigh_parms_release(&nd_tbl, idev->nd_parms);
3690 neigh_ifdown(&nd_tbl, dev);
3691 in6_dev_put(idev);
3692 }
3693 return 0;
3694}
3695
3696static void addrconf_rs_timer(unsigned long data)
3697{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003698 struct inet6_dev *idev = (struct inet6_dev *)data;
Cong Wangcaf92bc2013-08-31 13:44:32 +08003699 struct net_device *dev = idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003700 struct in6_addr lladdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003702 write_lock(&idev->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00003703 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 goto out;
3705
Shmulik Ladkani9ba2add2012-12-02 01:44:53 +00003706 if (!ipv6_accept_ra(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003708
3709 /* Announcement received after solicitation was sent */
3710 if (idev->if_flags & IF_RA_RCVD)
3711 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003713 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
Cong Wangcaf92bc2013-08-31 13:44:32 +08003714 write_unlock(&idev->lock);
3715 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3716 ndisc_send_rs(dev, &lladdr,
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003717 &in6addr_linklocal_allrouters);
3718 else
Cong Wangcaf92bc2013-08-31 13:44:32 +08003719 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
Cong Wangcaf92bc2013-08-31 13:44:32 +08003721 write_lock(&idev->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003722 idev->rs_interval = rfc3315_s14_backoff_update(
3723 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003724 /* The wait after the last probe can be shorter */
3725 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3726 idev->cnf.rtr_solicits) ?
3727 idev->cnf.rtr_solicit_delay :
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003728 idev->rs_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 /*
3731 * Note: we do not support deprecated "all on-link"
3732 * assumption any longer.
3733 */
Joe Perches91df42b2012-05-15 14:11:54 +00003734 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 }
3736
3737out:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003738 write_unlock(&idev->lock);
Cong Wangcaf92bc2013-08-31 13:44:32 +08003739put:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003740 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741}
3742
3743/*
3744 * Duplicate Address Detection
3745 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003746static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3747{
3748 unsigned long rand_num;
3749 struct inet6_dev *idev = ifp->idev;
3750
Neil Horman95c385b2007-04-25 17:08:10 -07003751 if (ifp->flags & IFA_F_OPTIMISTIC)
3752 rand_num = 0;
3753 else
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003754 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
Neil Horman95c385b2007-04-25 17:08:10 -07003755
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003756 ifp->dad_probes = idev->cnf.dad_transmits;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003757 addrconf_mod_dad_work(ifp, rand_num);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003758}
3759
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003760static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
3762 struct inet6_dev *idev = ifp->idev;
3763 struct net_device *dev = idev->dev;
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003764 bool notify = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
3766 addrconf_join_solict(dev, &ifp->addr);
3767
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003768 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
3770 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003771 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003772 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774
3775 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09003776 idev->cnf.accept_dad < 1 ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003777 !(ifp->flags&IFA_F_TENTATIVE) ||
3778 ifp->flags & IFA_F_NODAD) {
Brian Haleycc411d02009-09-09 14:41:32 +00003779 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003780 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 read_unlock_bh(&idev->lock);
3782
3783 addrconf_dad_completed(ifp);
3784 return;
3785 }
3786
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003787 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003788 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003789 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003790 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003791 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003792 * - keep it tentative if it is a permanent address.
3793 * - otherwise, kill it.
3794 */
3795 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003796 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003797 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003798 }
Neil Horman95c385b2007-04-25 17:08:10 -07003799
3800 /*
3801 * Optimistic nodes can start receiving
3802 * Frames right away
3803 */
Erik Kline7fd25612014-10-28 18:11:14 +09003804 if (ifp->flags & IFA_F_OPTIMISTIC) {
Neil Horman95c385b2007-04-25 17:08:10 -07003805 ip6_ins_rt(ifp->rt);
Erik Kline7fd25612014-10-28 18:11:14 +09003806 if (ipv6_use_optimistic_addr(idev)) {
3807 /* Because optimistic nodes can use this address,
3808 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3809 */
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003810 notify = true;
Erik Kline7fd25612014-10-28 18:11:14 +09003811 }
3812 }
Neil Horman95c385b2007-04-25 17:08:10 -07003813
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003814 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003816 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 read_unlock_bh(&idev->lock);
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003818 if (notify)
3819 ipv6_ifa_notify(RTM_NEWADDR, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820}
3821
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003822static void addrconf_dad_start(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003824 bool begin_dad = false;
3825
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003826 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003827 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3828 ifp->state = INET6_IFADDR_STATE_PREDAD;
3829 begin_dad = true;
3830 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003831 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003832
3833 if (begin_dad)
3834 addrconf_mod_dad_work(ifp, 0);
3835}
3836
3837static void addrconf_dad_work(struct work_struct *w)
3838{
3839 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3840 struct inet6_ifaddr,
3841 dad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 struct inet6_dev *idev = ifp->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 struct in6_addr mcaddr;
Mike Manning85b51b12016-08-18 14:39:40 +01003844 bool disable_ipv6 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003846 enum {
3847 DAD_PROCESS,
3848 DAD_BEGIN,
3849 DAD_ABORT,
3850 } action = DAD_PROCESS;
3851
3852 rtnl_lock();
3853
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003854 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003855 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3856 action = DAD_BEGIN;
3857 ifp->state = INET6_IFADDR_STATE_DAD;
3858 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3859 action = DAD_ABORT;
3860 ifp->state = INET6_IFADDR_STATE_POSTDAD;
Mike Manning85b51b12016-08-18 14:39:40 +01003861
3862 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6 &&
3863 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
3864 struct in6_addr addr;
3865
3866 addr.s6_addr32[0] = htonl(0xfe800000);
3867 addr.s6_addr32[1] = 0;
3868
3869 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
3870 ipv6_addr_equal(&ifp->addr, &addr)) {
3871 /* DAD failed for link-local based on MAC */
3872 idev->cnf.disable_ipv6 = 1;
3873
3874 pr_info("%s: IPv6 being disabled!\n",
3875 ifp->idev->dev->name);
3876 disable_ipv6 = true;
3877 }
3878 }
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003879 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003880 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003881
3882 if (action == DAD_BEGIN) {
3883 addrconf_dad_begin(ifp);
3884 goto out;
3885 } else if (action == DAD_ABORT) {
Wei Yongjun751eb6b2016-09-05 16:06:31 +08003886 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003887 addrconf_dad_stop(ifp, 1);
Mike Manning85b51b12016-08-18 14:39:40 +01003888 if (disable_ipv6)
3889 addrconf_ifdown(idev->dev, 0);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003890 goto out;
3891 }
3892
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003893 if (!ifp->dad_probes && addrconf_dad_end(ifp))
Herbert Xuf2344a12010-05-18 15:55:27 -07003894 goto out;
3895
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003896 write_lock_bh(&idev->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003897 if (idev->dead || !(idev->if_flags & IF_READY)) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003898 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 goto out;
3900 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003901
3902 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003903 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3904 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003905 write_unlock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003906 goto out;
3907 }
3908
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003909 if (ifp->dad_probes == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 /*
3911 * DAD was successful
3912 */
3913
Brian Haleycc411d02009-09-09 14:41:32 +00003914 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003915 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003916 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917
3918 addrconf_dad_completed(ifp);
3919
3920 goto out;
3921 }
3922
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003923 ifp->dad_probes--;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003924 addrconf_mod_dad_work(ifp,
3925 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
stephen hemminger21809fa2010-02-08 19:48:52 +00003926 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003927 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928
3929 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
Nicolas Dichtel304d8882015-11-27 18:17:05 +01003931 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932out:
3933 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003934 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935}
3936
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003937/* ifp->idev must be at least read locked */
3938static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3939{
3940 struct inet6_ifaddr *ifpiter;
3941 struct inet6_dev *idev = ifp->idev;
3942
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01003943 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3944 if (ifpiter->scope > IFA_LINK)
3945 break;
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003946 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3947 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3948 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
3949 IFA_F_PERMANENT)
3950 return false;
3951 }
3952 return true;
3953}
3954
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3956{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003957 struct net_device *dev = ifp->idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003958 struct in6_addr lladdr;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003959 bool send_rs, send_mld;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003960
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003961 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962
3963 /*
3964 * Configure the address for reception. Now it is valid.
3965 */
3966
3967 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3968
Tore Anderson026359b2011-08-28 23:47:33 +00003969 /* If added prefix is link local and we are prepared to process
3970 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 */
3972
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003973 read_lock_bh(&ifp->idev->lock);
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003974 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003975 send_rs = send_mld &&
3976 ipv6_accept_ra(ifp->idev) &&
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003977 ifp->idev->cnf.rtr_solicits != 0 &&
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003978 (dev->flags&IFF_LOOPBACK) == 0;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003979 read_unlock_bh(&ifp->idev->lock);
3980
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003981 /* While dad is in progress mld report's source address is in6_addrany.
3982 * Resend with proper ll now.
3983 */
3984 if (send_mld)
3985 ipv6_mc_dad_complete(ifp->idev);
3986
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003987 if (send_rs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 /*
3989 * If a host as already performed a random delay
3990 * [...] as part of DAD [...] there is no need
3991 * to delay again before sending the first RS
3992 */
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003993 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003994 return;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003995 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003997 write_lock_bh(&ifp->idev->lock);
3998 spin_lock(&ifp->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003999 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4000 ifp->idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004001 ifp->idev->rs_probes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 ifp->idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004003 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004004 spin_unlock(&ifp->lock);
4005 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 }
4007}
4008
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004009static void addrconf_dad_run(struct inet6_dev *idev)
4010{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004011 struct inet6_ifaddr *ifp;
4012
4013 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00004014 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00004015 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07004016 if (ifp->flags & IFA_F_TENTATIVE &&
4017 ifp->state == INET6_IFADDR_STATE_DAD)
4018 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00004019 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004020 }
4021 read_unlock_bh(&idev->lock);
4022}
4023
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024#ifdef CONFIG_PROC_FS
4025struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08004026 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004028 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029};
4030
Mihai Maruseac1d578302012-01-03 23:31:35 +00004031static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032{
4033 struct inet6_ifaddr *ifa = NULL;
4034 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004035 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00004036 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
Mihai Maruseac1d578302012-01-03 23:31:35 +00004038 /* initial bucket if pos is 0 */
4039 if (pos == 0) {
4040 state->bucket = 0;
4041 state->offset = 0;
4042 }
4043
4044 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08004045 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00004046 addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004047 if (!net_eq(dev_net(ifa->idev->dev), net))
4048 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004049 /* sync with offset */
4050 if (p < state->offset) {
4051 p++;
4052 continue;
4053 }
4054 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004055 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004056 }
4057
4058 /* prepare for next bucket */
4059 state->offset = 0;
4060 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004062 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063}
4064
stephen hemmingerc2e21292010-03-17 20:31:10 +00004065static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4066 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067{
4068 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004069 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
Sasha Levinb67bfe02013-02-27 17:06:00 -08004071 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004072 if (!net_eq(dev_net(ifa->idev->dev), net))
4073 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004074 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004075 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004076 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004077
4078 while (++state->bucket < IN6_ADDR_HSIZE) {
Mihai Maruseac1d578302012-01-03 23:31:35 +00004079 state->offset = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004080 hlist_for_each_entry_rcu_bh(ifa,
stephen hemmingerc2e21292010-03-17 20:31:10 +00004081 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004082 if (!net_eq(dev_net(ifa->idev->dev), net))
4083 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004084 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004085 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08004086 }
4087 }
4088
stephen hemmingerc2e21292010-03-17 20:31:10 +00004089 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090}
4091
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004093 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094{
stephen hemminger5c578aed2010-03-17 20:31:11 +00004095 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00004096 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097}
4098
4099static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4100{
4101 struct inet6_ifaddr *ifa;
4102
4103 ifa = if6_get_next(seq, v);
4104 ++*pos;
4105 return ifa;
4106}
4107
4108static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004109 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110{
stephen hemminger5c578aed2010-03-17 20:31:11 +00004111 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112}
4113
4114static int if6_seq_show(struct seq_file *seq, void *v)
4115{
4116 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Jiri Pirko971a3512013-12-10 13:56:29 +01004117 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07004118 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 ifp->idev->dev->ifindex,
4120 ifp->prefix_len,
4121 ifp->scope,
Jiri Pirko971a3512013-12-10 13:56:29 +01004122 (u8) ifp->flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 ifp->idev->dev->name);
4124 return 0;
4125}
4126
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004127static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 .start = if6_seq_start,
4129 .next = if6_seq_next,
4130 .show = if6_seq_show,
4131 .stop = if6_seq_stop,
4132};
4133
4134static int if6_seq_open(struct inode *inode, struct file *file)
4135{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004136 return seq_open_net(inode, file, &if6_seq_ops,
4137 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138}
4139
Arjan van de Ven9a321442007-02-12 00:55:35 -08004140static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 .owner = THIS_MODULE,
4142 .open = if6_seq_open,
4143 .read = seq_read,
4144 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004145 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146};
4147
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004148static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149{
Gao fengd4beaa62013-02-18 01:34:54 +00004150 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 return -ENOMEM;
4152 return 0;
4153}
4154
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004155static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08004156{
Gao fengece31ff2013-02-18 01:34:56 +00004157 remove_proc_entry("if_inet6", net->proc_net);
Daniel Lezcano3c400902008-01-10 22:42:49 -08004158}
4159
4160static struct pernet_operations if6_proc_net_ops = {
Ian Morris67ba4152014-08-24 21:53:10 +01004161 .init = if6_proc_net_init,
4162 .exit = if6_proc_net_exit,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004163};
4164
4165int __init if6_proc_init(void)
4166{
4167 return register_pernet_subsys(&if6_proc_net_ops);
4168}
4169
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170void if6_proc_exit(void)
4171{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004172 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173}
4174#endif /* CONFIG_PROC_FS */
4175
Amerigo Wang07a93622012-10-29 16:23:10 +00004176#if IS_ENABLED(CONFIG_IPV6_MIP6)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004177/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004178int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004179{
4180 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004181 struct inet6_ifaddr *ifp = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00004182 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004183
stephen hemminger5c578aed2010-03-17 20:31:11 +00004184 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08004185 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09004186 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08004187 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09004188 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004189 (ifp->flags & IFA_F_HOMEADDRESS)) {
4190 ret = 1;
4191 break;
4192 }
4193 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00004194 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004195 return ret;
4196}
4197#endif
4198
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199/*
4200 * Periodic address status verification
4201 */
4202
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004203static void addrconf_verify_rtnl(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004205 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 int i;
4208
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004209 ASSERT_RTNL();
4210
stephen hemminger5c578aed2010-03-17 20:31:11 +00004211 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004213 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004215 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004217 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218restart:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004219 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221
Yasushi Asanofad8da32013-12-31 12:04:19 +09004222 /* When setting preferred_lft to a value not zero or
4223 * infinity, while valid_lft is infinity
4224 * IFA_F_PERMANENT has a non-infinity life time.
4225 */
4226 if ((ifp->flags & IFA_F_PERMANENT) &&
4227 (ifp->prefered_lft == INFINITY_LIFE_TIME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 continue;
4229
4230 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004231 /* We try to batch several events at once. */
4232 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004234 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4235 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 spin_unlock(&ifp->lock);
4237 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 ipv6_del_addr(ifp);
4239 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004240 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4241 spin_unlock(&ifp->lock);
4242 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00004244 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 int deprecate = 0;
4246
4247 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4248 deprecate = 1;
4249 ifp->flags |= IFA_F_DEPRECATED;
4250 }
4251
Yasushi Asanofad8da32013-12-31 12:04:19 +09004252 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4253 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 next = ifp->tstamp + ifp->valid_lft * HZ;
4255
4256 spin_unlock(&ifp->lock);
4257
4258 if (deprecate) {
4259 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
4261 ipv6_ifa_notify(0, ifp);
4262 in6_ifa_put(ifp);
4263 goto restart;
4264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4266 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00004267 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4268 ifp->idev->cnf.dad_transmits *
Jiri Pirko1f9248e2013-12-07 19:26:53 +01004269 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
stephen hemminger88949cf2010-03-17 20:31:17 +00004270
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 if (age >= ifp->prefered_lft - regen_advance) {
4272 struct inet6_ifaddr *ifpub = ifp->ifpub;
4273 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4274 next = ifp->tstamp + ifp->prefered_lft * HZ;
4275 if (!ifp->regen_count && ifpub) {
4276 ifp->regen_count++;
4277 in6_ifa_hold(ifp);
4278 in6_ifa_hold(ifpub);
4279 spin_unlock(&ifp->lock);
stephen hemminger5c578aed2010-03-17 20:31:11 +00004280
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08004281 spin_lock(&ifpub->lock);
4282 ifpub->regen_count = 0;
4283 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 ipv6_create_tempaddr(ifpub, ifp);
4285 in6_ifa_put(ifpub);
4286 in6_ifa_put(ifp);
4287 goto restart;
4288 }
4289 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4290 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4291 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 } else {
4293 /* ifp->prefered_lft <= ifp->valid_lft */
4294 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4295 next = ifp->tstamp + ifp->prefered_lft * HZ;
4296 spin_unlock(&ifp->lock);
4297 }
4298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 }
4300
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004301 next_sec = round_jiffies_up(next);
4302 next_sched = next;
4303
4304 /* If rounded timeout is accurate enough, accept it. */
4305 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4306 next_sched = next_sec;
4307
4308 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4309 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4310 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4311
dingtianhongba3542e2013-08-17 10:27:04 +08004312 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4313 now, next, next_sec, next_sched);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004314 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
stephen hemminger5c578aed2010-03-17 20:31:11 +00004315 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316}
4317
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004318static void addrconf_verify_work(struct work_struct *w)
4319{
4320 rtnl_lock();
4321 addrconf_verify_rtnl();
4322 rtnl_unlock();
4323}
4324
4325static void addrconf_verify(void)
4326{
4327 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4328}
4329
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004330static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4331 struct in6_addr **peer_pfx)
Thomas Graf461d8832006-09-18 00:09:49 -07004332{
4333 struct in6_addr *pfx = NULL;
4334
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004335 *peer_pfx = NULL;
4336
Thomas Graf461d8832006-09-18 00:09:49 -07004337 if (addr)
4338 pfx = nla_data(addr);
4339
4340 if (local) {
4341 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004342 *peer_pfx = pfx;
4343 pfx = nla_data(local);
Thomas Graf461d8832006-09-18 00:09:49 -07004344 }
4345
4346 return pfx;
4347}
4348
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004349static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07004350 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4351 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4352 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
Jiri Pirko479840f2013-12-06 09:45:21 +01004353 [IFA_FLAGS] = { .len = sizeof(u32) },
Thomas Graf461d8832006-09-18 00:09:49 -07004354};
4355
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356static int
Thomas Graf661d2962013-03-21 07:45:29 +00004357inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004359 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07004360 struct ifaddrmsg *ifm;
4361 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004362 struct in6_addr *pfx, *peer_pfx;
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004363 u32 ifa_flags;
Thomas Grafb933f712006-09-18 00:10:19 -07004364 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365
Thomas Grafb933f712006-09-18 00:10:19 -07004366 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4367 if (err < 0)
4368 return err;
4369
4370 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004371 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004372 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 return -EINVAL;
4374
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004375 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4376
4377 /* We ignore other flags so far. */
4378 ifa_flags &= IFA_F_MANAGETEMPADDR;
4379
4380 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4381 ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382}
4383
Jiri Pirko479840f2013-12-06 09:45:21 +01004384static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004385 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004386{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004387 u32 flags;
4388 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004389 unsigned long timeout;
Jiri Pirko53bd6742013-12-06 09:45:22 +01004390 bool was_managetempaddr;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004391 bool had_prefixroute;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09004392
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004393 ASSERT_RTNL();
4394
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004395 if (!valid_lft || (prefered_lft > valid_lft))
4396 return -EINVAL;
4397
Jiri Pirko53bd6742013-12-06 09:45:22 +01004398 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4399 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4400 return -EINVAL;
4401
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004402 timeout = addrconf_timeout_fixup(valid_lft, HZ);
4403 if (addrconf_finite_timeout(timeout)) {
4404 expires = jiffies_to_clock_t(timeout * HZ);
4405 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004406 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004407 } else {
4408 expires = 0;
4409 flags = 0;
4410 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004411 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004412
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004413 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4414 if (addrconf_finite_timeout(timeout)) {
4415 if (timeout == 0)
4416 ifa_flags |= IFA_F_DEPRECATED;
4417 prefered_lft = timeout;
4418 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004419
4420 spin_lock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004421 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004422 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4423 !(ifp->flags & IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004424 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
Thomas Haller761aac72014-01-15 15:36:58 +01004425 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4426 IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004427 ifp->flags |= ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004428 ifp->tstamp = jiffies;
4429 ifp->valid_lft = valid_lft;
4430 ifp->prefered_lft = prefered_lft;
4431
4432 spin_unlock_bh(&ifp->lock);
4433 if (!(ifp->flags&IFA_F_TENTATIVE))
4434 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004435
Thomas Haller761aac72014-01-15 15:36:58 +01004436 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4437 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4438 expires, flags);
Thomas Haller5b84efe2014-01-15 15:36:59 +01004439 } else if (had_prefixroute) {
4440 enum cleanup_prefix_rt_t action;
4441 unsigned long rt_expires;
4442
4443 write_lock_bh(&ifp->idev->lock);
4444 action = check_cleanup_prefix_route(ifp, &rt_expires);
4445 write_unlock_bh(&ifp->idev->lock);
4446
4447 if (action != CLEANUP_PREFIX_RT_NOP) {
4448 cleanup_prefix_route(ifp, rt_expires,
4449 action == CLEANUP_PREFIX_RT_DEL);
4450 }
Thomas Haller761aac72014-01-15 15:36:58 +01004451 }
Jiri Pirko53bd6742013-12-06 09:45:22 +01004452
4453 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4454 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4455 valid_lft = prefered_lft = 0;
4456 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4457 !was_managetempaddr, jiffies);
4458 }
4459
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004460 addrconf_verify_rtnl();
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004461
4462 return 0;
4463}
4464
4465static int
Thomas Graf661d2962013-03-21 07:45:29 +00004466inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004468 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07004469 struct ifaddrmsg *ifm;
4470 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004471 struct in6_addr *pfx, *peer_pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07004472 struct inet6_ifaddr *ifa;
4473 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004474 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
Jiri Pirko479840f2013-12-06 09:45:21 +01004475 u32 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07004476 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477
Thomas Graf461d8832006-09-18 00:09:49 -07004478 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4479 if (err < 0)
4480 return err;
4481
4482 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004483 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004484 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 return -EINVAL;
4486
Thomas Graf461d8832006-09-18 00:09:49 -07004487 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09004488 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07004489
4490 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09004491 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07004492 preferred_lft = ci->ifa_prefered;
4493 } else {
4494 preferred_lft = INFINITY_LIFE_TIME;
4495 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09004496 }
4497
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004498 dev = __dev_get_by_index(net, ifm->ifa_index);
Ian Morris63159f22015-03-29 14:00:04 +01004499 if (!dev)
Thomas Graf7198f8c2006-09-18 00:13:46 -07004500 return -ENODEV;
4501
Jiri Pirko479840f2013-12-06 09:45:21 +01004502 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4503
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004504 /* We ignore other flags so far. */
Thomas Haller761aac72014-01-15 15:36:58 +01004505 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
Madhu Challa93a714d2015-02-25 09:58:35 -08004506 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004507
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08004508 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Ian Morris63159f22015-03-29 14:00:04 +01004509 if (!ifa) {
Thomas Graf7198f8c2006-09-18 00:13:46 -07004510 /*
4511 * It would be best to check for !NLM_F_CREATE here but
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08004512 * userspace already relies on not having to provide this.
Thomas Graf7198f8c2006-09-18 00:13:46 -07004513 */
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004514 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004515 ifm->ifa_prefixlen, ifa_flags,
4516 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004517 }
4518
Thomas Graf7198f8c2006-09-18 00:13:46 -07004519 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4520 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4521 err = -EEXIST;
4522 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004523 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07004524
4525 in6_ifa_put(ifa);
4526
4527 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528}
4529
Jiri Pirko479840f2013-12-06 09:45:21 +01004530static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
Thomas Graf101bb222006-09-18 00:11:52 -07004531 u8 scope, int ifindex)
4532{
4533 struct ifaddrmsg *ifm;
4534
4535 ifm = nlmsg_data(nlh);
4536 ifm->ifa_family = AF_INET6;
4537 ifm->ifa_prefixlen = prefixlen;
4538 ifm->ifa_flags = flags;
4539 ifm->ifa_scope = scope;
4540 ifm->ifa_index = ifindex;
4541}
4542
Thomas Graf85486af2006-09-18 00:11:24 -07004543static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4544 unsigned long tstamp, u32 preferred, u32 valid)
4545{
4546 struct ifa_cacheinfo ci;
4547
Thomas Graf18a31e12010-11-17 04:12:02 +00004548 ci.cstamp = cstamp_delta(cstamp);
4549 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07004550 ci.ifa_prefered = preferred;
4551 ci.ifa_valid = valid;
4552
4553 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4554}
4555
Thomas Graf101bb222006-09-18 00:11:52 -07004556static inline int rt_scope(int ifa_scope)
4557{
4558 if (ifa_scope & IFA_HOST)
4559 return RT_SCOPE_HOST;
4560 else if (ifa_scope & IFA_LINK)
4561 return RT_SCOPE_LINK;
4562 else if (ifa_scope & IFA_SITE)
4563 return RT_SCOPE_SITE;
4564 else
4565 return RT_SCOPE_UNIVERSE;
4566}
4567
Thomas Graf0ab68032006-09-18 00:12:35 -07004568static inline int inet6_ifaddr_msgsize(void)
4569{
Thomas Graf339bf982006-11-10 14:10:15 -08004570 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004571 + nla_total_size(16) /* IFA_LOCAL */
Thomas Graf339bf982006-11-10 14:10:15 -08004572 + nla_total_size(16) /* IFA_ADDRESS */
Jiri Pirko479840f2013-12-06 09:45:21 +01004573 + nla_total_size(sizeof(struct ifa_cacheinfo))
4574 + nla_total_size(4) /* IFA_FLAGS */;
Thomas Graf0ab68032006-09-18 00:12:35 -07004575}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004576
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004578 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07004581 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582
Eric W. Biederman15e47302012-09-07 20:12:54 +00004583 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004584 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004585 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004586
Thomas Graf101bb222006-09-18 00:11:52 -07004587 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4588 ifa->idev->dev->ifindex);
4589
Yasushi Asanofad8da32013-12-31 12:04:19 +09004590 if (!((ifa->flags&IFA_F_PERMANENT) &&
4591 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
Thomas Graf85486af2006-09-18 00:11:24 -07004592 preferred = ifa->prefered_lft;
4593 valid = ifa->valid_lft;
4594 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00004596 if (preferred > tval)
4597 preferred -= tval;
4598 else
4599 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00004600 if (valid != INFINITY_LIFE_TIME) {
4601 if (valid > tval)
4602 valid -= tval;
4603 else
4604 valid = 0;
4605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 }
4607 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07004608 preferred = INFINITY_LIFE_TIME;
4609 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 }
Thomas Graf85486af2006-09-18 00:11:24 -07004611
Cong Wang7996c792013-05-22 05:41:06 +00004612 if (!ipv6_addr_any(&ifa->peer_addr)) {
Jiri Benc930345e2015-03-29 16:59:25 +02004613 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4614 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004615 goto error;
4616 } else
Jiri Benc930345e2015-03-29 16:59:25 +02004617 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004618 goto error;
4619
4620 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4621 goto error;
Thomas Graf85486af2006-09-18 00:11:24 -07004622
Jiri Pirko479840f2013-12-06 09:45:21 +01004623 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4624 goto error;
4625
Johannes Berg053c0952015-01-16 22:09:00 +01004626 nlmsg_end(skb, nlh);
4627 return 0;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004628
4629error:
4630 nlmsg_cancel(skb, nlh);
4631 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632}
4633
4634static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004635 u32 portid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004638 u8 scope = RT_SCOPE_UNIVERSE;
4639 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
Thomas Graf101bb222006-09-18 00:11:52 -07004641 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4642 scope = RT_SCOPE_SITE;
4643
Eric W. Biederman15e47302012-09-07 20:12:54 +00004644 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004645 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004646 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004647
Thomas Graf101bb222006-09-18 00:11:52 -07004648 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004649 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004650 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004651 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4652 nlmsg_cancel(skb, nlh);
4653 return -EMSGSIZE;
4654 }
Thomas Graf85486af2006-09-18 00:11:24 -07004655
Johannes Berg053c0952015-01-16 22:09:00 +01004656 nlmsg_end(skb, nlh);
4657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658}
4659
4660static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004661 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004664 u8 scope = RT_SCOPE_UNIVERSE;
4665 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666
Thomas Graf101bb222006-09-18 00:11:52 -07004667 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4668 scope = RT_SCOPE_SITE;
4669
Eric W. Biederman15e47302012-09-07 20:12:54 +00004670 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004671 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004672 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004673
Thomas Graf101bb222006-09-18 00:11:52 -07004674 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004675 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004676 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004677 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4678 nlmsg_cancel(skb, nlh);
4679 return -EMSGSIZE;
4680 }
Thomas Graf85486af2006-09-18 00:11:24 -07004681
Johannes Berg053c0952015-01-16 22:09:00 +01004682 nlmsg_end(skb, nlh);
4683 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684}
4685
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004686enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 UNICAST_ADDR,
4688 MULTICAST_ADDR,
4689 ANYCAST_ADDR,
4690};
4691
Eric Dumazet234b27c2009-11-12 04:11:50 +00004692/* called with rcu_read_lock() */
4693static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4694 struct netlink_callback *cb, enum addr_type_t type,
4695 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 struct ifmcaddr6 *ifmca;
4698 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004699 int err = 1;
4700 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
Eric Dumazet234b27c2009-11-12 04:11:50 +00004702 read_lock_bh(&idev->lock);
4703 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00004704 case UNICAST_ADDR: {
4705 struct inet6_ifaddr *ifa;
4706
Eric Dumazet234b27c2009-11-12 04:11:50 +00004707 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00004708 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4709 if (++ip_idx < s_ip_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004710 continue;
4711 err = inet6_fill_ifaddr(skb, ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004712 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004713 cb->nlh->nlmsg_seq,
4714 RTM_NEWADDR,
4715 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004716 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004717 break;
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004718 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004720 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00004721 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004722 case MULTICAST_ADDR:
4723 /* multicast address */
4724 for (ifmca = idev->mc_list; ifmca;
4725 ifmca = ifmca->next, ip_idx++) {
4726 if (ip_idx < s_ip_idx)
4727 continue;
4728 err = inet6_fill_ifmcaddr(skb, ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004729 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004730 cb->nlh->nlmsg_seq,
4731 RTM_GETMULTICAST,
4732 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004733 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004734 break;
4735 }
4736 break;
4737 case ANYCAST_ADDR:
4738 /* anycast address */
4739 for (ifaca = idev->ac_list; ifaca;
4740 ifaca = ifaca->aca_next, ip_idx++) {
4741 if (ip_idx < s_ip_idx)
4742 continue;
4743 err = inet6_fill_ifacaddr(skb, ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004744 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004745 cb->nlh->nlmsg_seq,
4746 RTM_GETANYCAST,
4747 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004748 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004749 break;
4750 }
4751 break;
4752 default:
4753 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004755 read_unlock_bh(&idev->lock);
4756 *p_ip_idx = ip_idx;
4757 return err;
4758}
4759
4760static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4761 enum addr_type_t type)
4762{
4763 struct net *net = sock_net(skb->sk);
4764 int h, s_h;
4765 int idx, ip_idx;
4766 int s_idx, s_ip_idx;
4767 struct net_device *dev;
4768 struct inet6_dev *idev;
4769 struct hlist_head *head;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004770
4771 s_h = cb->args[0];
4772 s_idx = idx = cb->args[1];
4773 s_ip_idx = ip_idx = cb->args[2];
4774
4775 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004776 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004777 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4778 idx = 0;
4779 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08004780 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet234b27c2009-11-12 04:11:50 +00004781 if (idx < s_idx)
4782 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07004783 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004784 s_ip_idx = 0;
4785 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004786 idev = __in6_dev_get(dev);
4787 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004788 goto cont;
4789
4790 if (in6_dump_addrs(idev, skb, cb, type,
David S. Miller7b46a642015-01-18 23:36:08 -05004791 s_ip_idx, &ip_idx) < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004792 goto done;
4793cont:
4794 idx++;
4795 }
4796 }
4797done:
4798 rcu_read_unlock();
4799 cb->args[0] = h;
4800 cb->args[1] = idx;
4801 cb->args[2] = ip_idx;
4802
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 return skb->len;
4804}
4805
4806static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4807{
4808 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004809
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 return inet6_dump_addr(skb, cb, type);
4811}
4812
4813static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4814{
4815 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004816
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 return inet6_dump_addr(skb, cb, type);
4818}
4819
4820
4821static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4822{
4823 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004824
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 return inet6_dump_addr(skb, cb, type);
4826}
4827
Thomas Graf661d2962013-03-21 07:45:29 +00004828static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004829{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004830 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004831 struct ifaddrmsg *ifm;
4832 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004833 struct in6_addr *addr = NULL, *peer;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004834 struct net_device *dev = NULL;
4835 struct inet6_ifaddr *ifa;
4836 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004837 int err;
4838
Thomas Graf1b29fc22006-09-18 00:10:50 -07004839 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4840 if (err < 0)
4841 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004842
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004843 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
Ian Morris63159f22015-03-29 14:00:04 +01004844 if (!addr) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004845 err = -EINVAL;
4846 goto errout;
4847 }
4848
4849 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004850 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004851 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004852
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004853 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4854 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004855 err = -EADDRNOTAVAIL;
4856 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004857 }
4858
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004859 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4860 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004861 err = -ENOBUFS;
4862 goto errout_ifa;
4863 }
4864
Eric W. Biederman15e47302012-09-07 20:12:54 +00004865 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004866 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004867 if (err < 0) {
4868 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4869 WARN_ON(err == -EMSGSIZE);
4870 kfree_skb(skb);
4871 goto errout_ifa;
4872 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00004873 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004874errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004875 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004876errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004877 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004878}
4879
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4881{
4882 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004883 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07004884 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885
Thomas Graf0ab68032006-09-18 00:12:35 -07004886 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01004887 if (!skb)
Thomas Graf5d620262006-08-15 00:35:02 -07004888 goto errout;
4889
4890 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004891 if (err < 0) {
4892 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4893 WARN_ON(err == -EMSGSIZE);
4894 kfree_skb(skb);
4895 goto errout;
4896 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004897 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4898 return;
Thomas Graf5d620262006-08-15 00:35:02 -07004899errout:
4900 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004901 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902}
4903
Dave Jonesb6f99a22007-03-22 12:27:49 -07004904static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 __s32 *array, int bytes)
4906{
Thomas Graf04561c12006-11-14 19:53:58 -08004907 BUG_ON(bytes < (DEVCONF_MAX * 4));
4908
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 memset(array, 0, bytes);
4910 array[DEVCONF_FORWARDING] = cnf->forwarding;
4911 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4912 array[DEVCONF_MTU6] = cnf->mtu6;
4913 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4914 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4915 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4916 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4917 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00004918 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4919 jiffies_to_msecs(cnf->rtr_solicit_interval);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004920 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
4921 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
Thomas Graf93908d12010-11-17 01:44:24 +00004922 array[DEVCONF_RTR_SOLICIT_DELAY] =
4923 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02004925 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4926 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4927 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4928 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4930 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4931 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4932 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4933 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004935 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
Hangbin Liu8013d1d72015-07-30 14:28:42 +08004936 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004937 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004938#ifdef CONFIG_IPV6_ROUTER_PREF
4939 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00004940 array[DEVCONF_RTR_PROBE_INTERVAL] =
4941 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08004942#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004943 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4944#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004945#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004946 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004947 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07004948#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4949 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
Erik Kline7fd25612014-10-28 18:11:14 +09004950 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
Neil Horman95c385b2007-04-25 17:08:10 -07004951#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004952#ifdef CONFIG_IPV6_MROUTE
4953 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4954#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09004955 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09004956 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00004957 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00004958 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02004959 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
Ben Greeard9333192014-06-25 14:44:53 -07004960 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
Harout Hedeshianc2943f12015-01-20 10:06:05 -07004961 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
Andy Gospodarek35103d12015-08-13 10:39:01 -04004962 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01004963 /* we omit DEVCONF_STABLE_SECRET for now */
Erik Kline3985e8a2015-07-22 16:38:25 +09004964 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
Johannes Bergabbc3042016-02-04 13:31:19 +01004965 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
Johannes Berg7a02bf82016-02-04 13:31:20 +01004966 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
David Ahernf1705ec2016-02-24 09:25:37 -08004967 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968}
4969
Thomas Grafb382b192010-11-16 04:33:57 +00004970static inline size_t inet6_ifla6_size(void)
4971{
4972 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4973 + nla_total_size(sizeof(struct ifla_cacheinfo))
4974 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4975 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004976 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4977 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
Thomas Grafb382b192010-11-16 04:33:57 +00004978}
4979
Thomas Graf339bf982006-11-10 14:10:15 -08004980static inline size_t inet6_if_nlmsg_size(void)
4981{
4982 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4983 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4984 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4985 + nla_total_size(4) /* IFLA_MTU */
4986 + nla_total_size(4) /* IFLA_LINK */
Andy Gospodarek03443382015-08-13 15:26:35 -04004987 + nla_total_size(1) /* IFLA_OPERSTATE */
Thomas Grafb382b192010-11-16 04:33:57 +00004988 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08004989}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004990
Eric Dumazetbe281e52011-05-19 01:14:23 +00004991static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Jia Heaca05672016-09-30 11:29:03 +08004992 int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004993{
4994 int i;
Jia Heaca05672016-09-30 11:29:03 +08004995 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004996 BUG_ON(pad < 0);
4997
4998 /* Use put_unaligned() because stats may not be aligned for u64. */
Jia Heaca05672016-09-30 11:29:03 +08004999 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5000 for (i = 1; i < ICMP6_MIB_MAX; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00005001 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005002
Jia Heaca05672016-09-30 11:29:03 +08005003 memset(&stats[ICMP6_MIB_MAX], 0, pad);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005004}
5005
WANG Cong698365f2014-05-05 15:55:55 -07005006static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305007 int bytes, size_t syncpoff)
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005008{
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305009 int i, c;
5010 u64 buff[IPSTATS_MIB_MAX];
5011 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5012
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005013 BUG_ON(pad < 0);
5014
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305015 memset(buff, 0, sizeof(buff));
5016 buff[0] = IPSTATS_MIB_MAX;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005017
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305018 for_each_possible_cpu(c) {
5019 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5020 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5021 }
5022
5023 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5024 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005025}
5026
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005027static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5028 int bytes)
5029{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005030 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005031 case IFLA_INET6_STATS:
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305032 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5033 offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005034 break;
5035 case IFLA_INET6_ICMP6STATS:
Jia Heaca05672016-09-30 11:29:03 +08005036 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005037 break;
5038 }
5039}
5040
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005041static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5042 u32 ext_filter_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005044 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08005045 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046
David S. Millerc78679e2012-04-01 20:27:33 -04005047 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5048 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08005050 ci.tstamp = cstamp_delta(idev->tstamp);
5051 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
Jiri Pirko1f9248e2013-12-07 19:26:53 +01005052 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
David S. Millerc78679e2012-04-01 20:27:33 -04005053 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5054 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005055 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
Ian Morris63159f22015-03-29 14:00:04 +01005056 if (!nla)
Thomas Graf04561c12006-11-14 19:53:58 -08005057 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005058 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005060 /* XXX - MC not implemented */
5061
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005062 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5063 return 0;
5064
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005065 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005066 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005067 goto nla_put_failure;
5068 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5069
5070 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005071 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005072 goto nla_put_failure;
5073 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005075 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
Ian Morris63159f22015-03-29 14:00:04 +01005076 if (!nla)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005077 goto nla_put_failure;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005078
5079 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode))
5080 goto nla_put_failure;
5081
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005082 read_lock_bh(&idev->lock);
5083 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5084 read_unlock_bh(&idev->lock);
5085
Thomas Grafb382b192010-11-16 04:33:57 +00005086 return 0;
5087
5088nla_put_failure:
5089 return -EMSGSIZE;
5090}
5091
Arad, Ronenb1974ed2015-10-19 09:23:28 -07005092static size_t inet6_get_link_af_size(const struct net_device *dev,
5093 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005094{
5095 if (!__in6_dev_get(dev))
5096 return 0;
5097
5098 return inet6_ifla6_size();
5099}
5100
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005101static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5102 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005103{
5104 struct inet6_dev *idev = __in6_dev_get(dev);
5105
5106 if (!idev)
5107 return -ENODATA;
5108
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005109 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005110 return -EMSGSIZE;
5111
5112 return 0;
5113}
5114
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005115static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5116{
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005117 struct inet6_ifaddr *ifp;
5118 struct net_device *dev = idev->dev;
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005119 bool clear_token, update_rs = false;
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005120 struct in6_addr ll_addr;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005121
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005122 ASSERT_RTNL();
5123
Ian Morris63159f22015-03-29 14:00:04 +01005124 if (!token)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005125 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005126 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5127 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005128 if (!ipv6_accept_ra(idev))
5129 return -EINVAL;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005130 if (idev->cnf.rtr_solicits == 0)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005131 return -EINVAL;
5132
5133 write_lock_bh(&idev->lock);
5134
5135 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5136 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5137
5138 write_unlock_bh(&idev->lock);
5139
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005140 clear_token = ipv6_addr_any(token);
5141 if (clear_token)
5142 goto update_lft;
5143
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005144 if (!idev->dead && (idev->if_flags & IF_READY) &&
5145 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5146 IFA_F_OPTIMISTIC)) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005147 /* If we're not ready, then normal ifup will take care
5148 * of this. Otherwise, we need to request our rs here.
5149 */
5150 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5151 update_rs = true;
5152 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005153
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005154update_lft:
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005155 write_lock_bh(&idev->lock);
Daniel Borkmannfc403832013-04-09 03:47:15 +00005156
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005157 if (update_rs) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005158 idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005159 idev->rs_interval = rfc3315_s14_backoff_init(
5160 idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005161 idev->rs_probes = 1;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005162 addrconf_mod_rs_timer(idev, idev->rs_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005163 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005164
5165 /* Well, that's kinda nasty ... */
5166 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5167 spin_lock(&ifp->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00005168 if (ifp->tokenized) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005169 ifp->valid_lft = 0;
5170 ifp->prefered_lft = 0;
5171 }
5172 spin_unlock(&ifp->lock);
5173 }
5174
5175 write_unlock_bh(&idev->lock);
Lubomir Rintelb2ed64a2014-10-27 17:39:16 +01005176 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005177 addrconf_verify_rtnl();
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005178 return 0;
5179}
5180
Daniel Borkmann11b1f822015-02-05 14:39:11 +01005181static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5182 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5183 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5184};
5185
5186static int inet6_validate_link_af(const struct net_device *dev,
5187 const struct nlattr *nla)
5188{
5189 struct nlattr *tb[IFLA_INET6_MAX + 1];
5190
5191 if (dev && !__in6_dev_get(dev))
5192 return -EAFNOSUPPORT;
5193
5194 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
5195}
5196
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005197static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5198{
5199 int err = -EINVAL;
5200 struct inet6_dev *idev = __in6_dev_get(dev);
5201 struct nlattr *tb[IFLA_INET6_MAX + 1];
5202
5203 if (!idev)
5204 return -EAFNOSUPPORT;
5205
5206 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
5207 BUG();
5208
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005209 if (tb[IFLA_INET6_TOKEN]) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005210 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005211 if (err)
5212 return err;
5213 }
5214
5215 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5216 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5217
5218 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005219 mode != IN6_ADDR_GEN_MODE_NONE &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01005220 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5221 mode != IN6_ADDR_GEN_MODE_RANDOM)
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005222 return -EINVAL;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005223
5224 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5225 !idev->cnf.stable_secret.initialized &&
5226 !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized)
5227 return -EINVAL;
5228
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005229 idev->addr_gen_mode = mode;
5230 err = 0;
5231 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005232
5233 return err;
5234}
5235
Thomas Grafb382b192010-11-16 04:33:57 +00005236static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005237 u32 portid, u32 seq, int event, unsigned int flags)
Thomas Grafb382b192010-11-16 04:33:57 +00005238{
5239 struct net_device *dev = idev->dev;
5240 struct ifinfomsg *hdr;
5241 struct nlmsghdr *nlh;
5242 void *protoinfo;
5243
Eric W. Biederman15e47302012-09-07 20:12:54 +00005244 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005245 if (!nlh)
Thomas Grafb382b192010-11-16 04:33:57 +00005246 return -EMSGSIZE;
5247
5248 hdr = nlmsg_data(nlh);
5249 hdr->ifi_family = AF_INET6;
5250 hdr->__ifi_pad = 0;
5251 hdr->ifi_type = dev->type;
5252 hdr->ifi_index = dev->ifindex;
5253 hdr->ifi_flags = dev_get_flags(dev);
5254 hdr->ifi_change = 0;
5255
David S. Millerc78679e2012-04-01 20:27:33 -04005256 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5257 (dev->addr_len &&
5258 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5259 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02005260 (dev->ifindex != dev_get_iflink(dev) &&
Andy Gospodarek03443382015-08-13 15:26:35 -04005261 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5262 nla_put_u8(skb, IFLA_OPERSTATE,
5263 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
David S. Millerc78679e2012-04-01 20:27:33 -04005264 goto nla_put_failure;
Thomas Grafb382b192010-11-16 04:33:57 +00005265 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
Ian Morris63159f22015-03-29 14:00:04 +01005266 if (!protoinfo)
Thomas Grafb382b192010-11-16 04:33:57 +00005267 goto nla_put_failure;
5268
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005269 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005270 goto nla_put_failure;
5271
Thomas Graf04561c12006-11-14 19:53:58 -08005272 nla_nest_end(skb, protoinfo);
Johannes Berg053c0952015-01-16 22:09:00 +01005273 nlmsg_end(skb, nlh);
5274 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275
Thomas Graf04561c12006-11-14 19:53:58 -08005276nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005277 nlmsg_cancel(skb, nlh);
5278 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279}
5280
5281static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5282{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005283 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00005284 int h, s_h;
David S. Miller434a8a52009-11-11 18:53:00 -08005285 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 struct net_device *dev;
5287 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00005288 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289
Eric Dumazet84d26972009-11-09 12:11:28 +00005290 s_h = cb->args[0];
5291 s_idx = cb->args[1];
5292
5293 rcu_read_lock();
5294 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5295 idx = 0;
5296 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08005297 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet84d26972009-11-09 12:11:28 +00005298 if (idx < s_idx)
5299 goto cont;
5300 idev = __in6_dev_get(dev);
5301 if (!idev)
5302 goto cont;
5303 if (inet6_fill_ifinfo(skb, idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005304 NETLINK_CB(cb->skb).portid,
Eric Dumazet84d26972009-11-09 12:11:28 +00005305 cb->nlh->nlmsg_seq,
Johannes Berg053c0952015-01-16 22:09:00 +01005306 RTM_NEWLINK, NLM_F_MULTI) < 0)
Eric Dumazet84d26972009-11-09 12:11:28 +00005307 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07005308cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00005309 idx++;
5310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 }
Eric Dumazet84d26972009-11-09 12:11:28 +00005312out:
5313 rcu_read_unlock();
5314 cb->args[1] = idx;
5315 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316
5317 return skb->len;
5318}
5319
5320void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5321{
5322 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005323 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005324 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005325
Thomas Graf339bf982006-11-10 14:10:15 -08005326 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005327 if (!skb)
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005328 goto errout;
5329
5330 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005331 if (err < 0) {
5332 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5333 WARN_ON(err == -EMSGSIZE);
5334 kfree_skb(skb);
5335 goto errout;
5336 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005337 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005338 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005339errout:
5340 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005341 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342}
5343
Thomas Graf339bf982006-11-10 14:10:15 -08005344static inline size_t inet6_prefix_nlmsg_size(void)
5345{
5346 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5347 + nla_total_size(sizeof(struct in6_addr))
5348 + nla_total_size(sizeof(struct prefix_cacheinfo));
5349}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005350
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005352 struct prefix_info *pinfo, u32 portid, u32 seq,
Thomas Graf6051e2f2006-11-14 19:54:19 -08005353 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354{
Thomas Graf6051e2f2006-11-14 19:54:19 -08005355 struct prefixmsg *pmsg;
5356 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 struct prefix_cacheinfo ci;
5358
Eric W. Biederman15e47302012-09-07 20:12:54 +00005359 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005360 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005361 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08005362
5363 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07005365 pmsg->prefix_pad1 = 0;
5366 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 pmsg->prefix_ifindex = idev->dev->ifindex;
5368 pmsg->prefix_len = pinfo->prefix_len;
5369 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07005370 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 pmsg->prefix_flags = 0;
5372 if (pinfo->onlink)
5373 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5374 if (pinfo->autoconf)
5375 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5376
David S. Millerc78679e2012-04-01 20:27:33 -04005377 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5378 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 ci.preferred_time = ntohl(pinfo->prefered);
5380 ci.valid_time = ntohl(pinfo->valid);
David S. Millerc78679e2012-04-01 20:27:33 -04005381 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5382 goto nla_put_failure;
Johannes Berg053c0952015-01-16 22:09:00 +01005383 nlmsg_end(skb, nlh);
5384 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385
Thomas Graf6051e2f2006-11-14 19:54:19 -08005386nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005387 nlmsg_cancel(skb, nlh);
5388 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389}
5390
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005391static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 struct prefix_info *pinfo)
5393{
5394 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005395 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005396 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397
Thomas Graf339bf982006-11-10 14:10:15 -08005398 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005399 if (!skb)
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005400 goto errout;
5401
5402 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005403 if (err < 0) {
5404 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5405 WARN_ON(err == -EMSGSIZE);
5406 kfree_skb(skb);
5407 goto errout;
5408 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005409 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5410 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005411errout:
5412 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08005413 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414}
5415
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5417{
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005418 struct net *net = dev_net(ifp->idev->dev);
5419
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005420 if (event)
5421 ASSERT_RTNL();
5422
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5424
5425 switch (event) {
5426 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07005427 /*
5428 * If the address was optimistic
5429 * we inserted the route at the start of
5430 * our DAD process, so we don't need
5431 * to do it again
5432 */
5433 if (!(ifp->rt->rt6i_node))
5434 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 if (ifp->idev->cnf.forwarding)
5436 addrconf_join_anycast(ifp);
Cong Wang7996c792013-05-22 05:41:06 +00005437 if (!ipv6_addr_any(&ifp->peer_addr))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005438 addrconf_prefix_route(&ifp->peer_addr, 128,
5439 ifp->idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 break;
5441 case RTM_DELADDR:
5442 if (ifp->idev->cnf.forwarding)
5443 addrconf_leave_anycast(ifp);
5444 addrconf_leave_solict(ifp->idev, &ifp->addr);
Cong Wang7996c792013-05-22 05:41:06 +00005445 if (!ipv6_addr_any(&ifp->peer_addr)) {
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005446 struct rt6_info *rt;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005447
Nicolas Dichtele7478df2014-09-03 23:59:22 +02005448 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5449 ifp->idev->dev, 0, 0);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07005450 if (rt)
5451 ip6_del_rt(rt);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005452 }
David Ahern38bd10c2016-04-21 20:56:12 -07005453 if (ifp->rt) {
5454 dst_hold(&ifp->rt->dst);
5455 ip6_del_rt(ifp->rt);
5456 }
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02005457 rt_genid_bump_ipv6(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 break;
5459 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005460 atomic_inc(&net->ipv6.dev_addr_genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461}
5462
5463static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5464{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005465 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 if (likely(ifp->idev->dead == 0))
5467 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005468 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469}
5470
5471#ifdef CONFIG_SYSCTL
5472
5473static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005474int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 void __user *buffer, size_t *lenp, loff_t *ppos)
5476{
5477 int *valp = ctl->data;
5478 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005479 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005480 struct ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 int ret;
5482
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005483 /*
5484 * ctl->data points to idev->cnf.forwarding, we should
5485 * not modify it until we get the rtnl lock.
5486 */
5487 lctl = *ctl;
5488 lctl.data = &val;
5489
5490 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08005492 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00005493 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005494 if (ret)
5495 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005496 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497}
5498
Marcelo Leitner77751422015-02-23 11:17:13 -03005499static
5500int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5501 void __user *buffer, size_t *lenp, loff_t *ppos)
5502{
5503 struct inet6_dev *idev = ctl->extra1;
5504 int min_mtu = IPV6_MIN_MTU;
5505 struct ctl_table lctl;
5506
5507 lctl = *ctl;
5508 lctl.extra1 = &min_mtu;
5509 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5510
5511 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5512}
5513
Brian Haley56d417b2009-06-01 03:07:33 -07005514static void dev_disable_change(struct inet6_dev *idev)
5515{
Cong Wang75538c22013-05-29 11:30:50 +08005516 struct netdev_notifier_info info;
5517
Brian Haley56d417b2009-06-01 03:07:33 -07005518 if (!idev || !idev->dev)
5519 return;
5520
Cong Wang75538c22013-05-29 11:30:50 +08005521 netdev_notifier_info_init(&info, idev->dev);
Brian Haley56d417b2009-06-01 03:07:33 -07005522 if (idev->cnf.disable_ipv6)
Cong Wang75538c22013-05-29 11:30:50 +08005523 addrconf_notify(NULL, NETDEV_DOWN, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005524 else
Cong Wang75538c22013-05-29 11:30:50 +08005525 addrconf_notify(NULL, NETDEV_UP, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005526}
5527
5528static void addrconf_disable_change(struct net *net, __s32 newf)
5529{
5530 struct net_device *dev;
5531 struct inet6_dev *idev;
5532
Eric Dumazetc6d14c82009-11-04 05:43:23 -08005533 rcu_read_lock();
5534 for_each_netdev_rcu(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07005535 idev = __in6_dev_get(dev);
5536 if (idev) {
5537 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5538 idev->cnf.disable_ipv6 = newf;
5539 if (changed)
5540 dev_disable_change(idev);
5541 }
Brian Haley56d417b2009-06-01 03:07:33 -07005542 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -08005543 rcu_read_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07005544}
5545
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005546static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07005547{
5548 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005549 int old;
5550
5551 if (!rtnl_trylock())
5552 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07005553
5554 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005555 old = *p;
5556 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07005557
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005558 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5559 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07005560 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005561 }
Brian Haley56d417b2009-06-01 03:07:33 -07005562
5563 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07005564 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5565 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005566 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07005567 dev_disable_change((struct inet6_dev *)table->extra1);
5568
5569 rtnl_unlock();
5570 return 0;
5571}
5572
5573static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005574int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07005575 void __user *buffer, size_t *lenp, loff_t *ppos)
5576{
5577 int *valp = ctl->data;
5578 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005579 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005580 struct ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07005581 int ret;
5582
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005583 /*
5584 * ctl->data points to idev->cnf.disable_ipv6, we should
5585 * not modify it until we get the rtnl lock.
5586 */
5587 lctl = *ctl;
5588 lctl.data = &val;
5589
5590 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07005591
5592 if (write)
5593 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005594 if (ret)
5595 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07005596 return ret;
5597}
5598
stephen hemmingerc92d5492013-12-17 22:37:14 -08005599static
5600int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5601 void __user *buffer, size_t *lenp, loff_t *ppos)
5602{
5603 int *valp = ctl->data;
5604 int ret;
5605 int old, new;
5606
5607 old = *valp;
5608 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5609 new = *valp;
5610
5611 if (write && old != new) {
5612 struct net *net = ctl->extra2;
5613
5614 if (!rtnl_trylock())
5615 return restart_syscall();
5616
5617 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5618 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5619 NETCONFA_IFINDEX_DEFAULT,
5620 net->ipv6.devconf_dflt);
5621 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5622 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5623 NETCONFA_IFINDEX_ALL,
5624 net->ipv6.devconf_all);
5625 else {
5626 struct inet6_dev *idev = ctl->extra1;
5627
5628 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5629 idev->dev->ifindex,
5630 &idev->cnf);
5631 }
5632 rtnl_unlock();
5633 }
5634
5635 return ret;
5636}
5637
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005638static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5639 void __user *buffer, size_t *lenp,
5640 loff_t *ppos)
5641{
5642 int err;
5643 struct in6_addr addr;
5644 char str[IPV6_MAX_STRLEN];
5645 struct ctl_table lctl = *ctl;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005646 struct net *net = ctl->extra2;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005647 struct ipv6_stable_secret *secret = ctl->data;
5648
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005649 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5650 return -EIO;
5651
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005652 lctl.maxlen = IPV6_MAX_STRLEN;
5653 lctl.data = str;
5654
5655 if (!rtnl_trylock())
5656 return restart_syscall();
5657
5658 if (!write && !secret->initialized) {
5659 err = -EIO;
5660 goto out;
5661 }
5662
WANG Cong5449a5c2015-12-21 10:55:45 -08005663 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
5664 if (err >= sizeof(str)) {
5665 err = -EIO;
5666 goto out;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005667 }
5668
5669 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5670 if (err || !write)
5671 goto out;
5672
5673 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5674 err = -EIO;
5675 goto out;
5676 }
5677
5678 secret->initialized = true;
5679 secret->secret = addr;
5680
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005681 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5682 struct net_device *dev;
5683
5684 for_each_netdev(net, dev) {
5685 struct inet6_dev *idev = __in6_dev_get(dev);
5686
5687 if (idev) {
5688 idev->addr_gen_mode =
5689 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5690 }
5691 }
5692 } else {
5693 struct inet6_dev *idev = ctl->extra1;
5694
5695 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5696 }
5697
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005698out:
5699 rtnl_unlock();
5700
5701 return err;
5702}
stephen hemmingerc92d5492013-12-17 22:37:14 -08005703
Andy Gospodarek35103d12015-08-13 10:39:01 -04005704static
5705int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
5706 int write,
5707 void __user *buffer,
5708 size_t *lenp,
5709 loff_t *ppos)
5710{
5711 int *valp = ctl->data;
5712 int val = *valp;
5713 loff_t pos = *ppos;
5714 struct ctl_table lctl;
5715 int ret;
5716
5717 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
5718 * we should not modify it until we get the rtnl lock.
5719 */
5720 lctl = *ctl;
5721 lctl.data = &val;
5722
5723 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5724
5725 if (write)
5726 ret = addrconf_fixup_linkdown(ctl, valp, val);
5727 if (ret)
5728 *ppos = pos;
5729 return ret;
5730}
5731
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07005732static const int one = 1;
5733static const int two_five_five = 255;
5734
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03005735static const struct ctl_table addrconf_sysctl[] = {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005736 {
5737 .procname = "forwarding",
5738 .data = &ipv6_devconf.forwarding,
5739 .maxlen = sizeof(int),
5740 .mode = 0644,
5741 .proc_handler = addrconf_sysctl_forward,
5742 },
5743 {
5744 .procname = "hop_limit",
5745 .data = &ipv6_devconf.hop_limit,
5746 .maxlen = sizeof(int),
5747 .mode = 0644,
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07005748 .proc_handler = proc_dointvec_minmax,
5749 .extra1 = (void *)&one,
5750 .extra2 = (void *)&two_five_five,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005751 },
5752 {
5753 .procname = "mtu",
5754 .data = &ipv6_devconf.mtu6,
5755 .maxlen = sizeof(int),
5756 .mode = 0644,
5757 .proc_handler = addrconf_sysctl_mtu,
5758 },
5759 {
5760 .procname = "accept_ra",
5761 .data = &ipv6_devconf.accept_ra,
5762 .maxlen = sizeof(int),
5763 .mode = 0644,
5764 .proc_handler = proc_dointvec,
5765 },
5766 {
5767 .procname = "accept_redirects",
5768 .data = &ipv6_devconf.accept_redirects,
5769 .maxlen = sizeof(int),
5770 .mode = 0644,
5771 .proc_handler = proc_dointvec,
5772 },
5773 {
5774 .procname = "autoconf",
5775 .data = &ipv6_devconf.autoconf,
5776 .maxlen = sizeof(int),
5777 .mode = 0644,
5778 .proc_handler = proc_dointvec,
5779 },
5780 {
5781 .procname = "dad_transmits",
5782 .data = &ipv6_devconf.dad_transmits,
5783 .maxlen = sizeof(int),
5784 .mode = 0644,
5785 .proc_handler = proc_dointvec,
5786 },
5787 {
5788 .procname = "router_solicitations",
5789 .data = &ipv6_devconf.rtr_solicits,
5790 .maxlen = sizeof(int),
5791 .mode = 0644,
5792 .proc_handler = proc_dointvec,
5793 },
5794 {
5795 .procname = "router_solicitation_interval",
5796 .data = &ipv6_devconf.rtr_solicit_interval,
5797 .maxlen = sizeof(int),
5798 .mode = 0644,
5799 .proc_handler = proc_dointvec_jiffies,
5800 },
5801 {
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005802 .procname = "router_solicitation_max_interval",
5803 .data = &ipv6_devconf.rtr_solicit_max_interval,
5804 .maxlen = sizeof(int),
5805 .mode = 0644,
5806 .proc_handler = proc_dointvec_jiffies,
5807 },
5808 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005809 .procname = "router_solicitation_delay",
5810 .data = &ipv6_devconf.rtr_solicit_delay,
5811 .maxlen = sizeof(int),
5812 .mode = 0644,
5813 .proc_handler = proc_dointvec_jiffies,
5814 },
5815 {
5816 .procname = "force_mld_version",
5817 .data = &ipv6_devconf.force_mld_version,
5818 .maxlen = sizeof(int),
5819 .mode = 0644,
5820 .proc_handler = proc_dointvec,
5821 },
5822 {
5823 .procname = "mldv1_unsolicited_report_interval",
5824 .data =
5825 &ipv6_devconf.mldv1_unsolicited_report_interval,
5826 .maxlen = sizeof(int),
5827 .mode = 0644,
5828 .proc_handler = proc_dointvec_ms_jiffies,
5829 },
5830 {
5831 .procname = "mldv2_unsolicited_report_interval",
5832 .data =
5833 &ipv6_devconf.mldv2_unsolicited_report_interval,
5834 .maxlen = sizeof(int),
5835 .mode = 0644,
5836 .proc_handler = proc_dointvec_ms_jiffies,
5837 },
5838 {
5839 .procname = "use_tempaddr",
5840 .data = &ipv6_devconf.use_tempaddr,
5841 .maxlen = sizeof(int),
5842 .mode = 0644,
5843 .proc_handler = proc_dointvec,
5844 },
5845 {
5846 .procname = "temp_valid_lft",
5847 .data = &ipv6_devconf.temp_valid_lft,
5848 .maxlen = sizeof(int),
5849 .mode = 0644,
5850 .proc_handler = proc_dointvec,
5851 },
5852 {
5853 .procname = "temp_prefered_lft",
5854 .data = &ipv6_devconf.temp_prefered_lft,
5855 .maxlen = sizeof(int),
5856 .mode = 0644,
5857 .proc_handler = proc_dointvec,
5858 },
5859 {
5860 .procname = "regen_max_retry",
5861 .data = &ipv6_devconf.regen_max_retry,
5862 .maxlen = sizeof(int),
5863 .mode = 0644,
5864 .proc_handler = proc_dointvec,
5865 },
5866 {
5867 .procname = "max_desync_factor",
5868 .data = &ipv6_devconf.max_desync_factor,
5869 .maxlen = sizeof(int),
5870 .mode = 0644,
5871 .proc_handler = proc_dointvec,
5872 },
5873 {
5874 .procname = "max_addresses",
5875 .data = &ipv6_devconf.max_addresses,
5876 .maxlen = sizeof(int),
5877 .mode = 0644,
5878 .proc_handler = proc_dointvec,
5879 },
5880 {
5881 .procname = "accept_ra_defrtr",
5882 .data = &ipv6_devconf.accept_ra_defrtr,
5883 .maxlen = sizeof(int),
5884 .mode = 0644,
5885 .proc_handler = proc_dointvec,
5886 },
5887 {
5888 .procname = "accept_ra_min_hop_limit",
5889 .data = &ipv6_devconf.accept_ra_min_hop_limit,
5890 .maxlen = sizeof(int),
5891 .mode = 0644,
5892 .proc_handler = proc_dointvec,
5893 },
5894 {
5895 .procname = "accept_ra_pinfo",
5896 .data = &ipv6_devconf.accept_ra_pinfo,
5897 .maxlen = sizeof(int),
5898 .mode = 0644,
5899 .proc_handler = proc_dointvec,
5900 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005901#ifdef CONFIG_IPV6_ROUTER_PREF
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005902 {
5903 .procname = "accept_ra_rtr_pref",
5904 .data = &ipv6_devconf.accept_ra_rtr_pref,
5905 .maxlen = sizeof(int),
5906 .mode = 0644,
5907 .proc_handler = proc_dointvec,
5908 },
5909 {
5910 .procname = "router_probe_interval",
5911 .data = &ipv6_devconf.rtr_probe_interval,
5912 .maxlen = sizeof(int),
5913 .mode = 0644,
5914 .proc_handler = proc_dointvec_jiffies,
5915 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08005916#ifdef CONFIG_IPV6_ROUTE_INFO
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005917 {
5918 .procname = "accept_ra_rt_info_max_plen",
5919 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
5920 .maxlen = sizeof(int),
5921 .mode = 0644,
5922 .proc_handler = proc_dointvec,
5923 },
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08005924#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005925#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005926 {
5927 .procname = "proxy_ndp",
5928 .data = &ipv6_devconf.proxy_ndp,
5929 .maxlen = sizeof(int),
5930 .mode = 0644,
5931 .proc_handler = addrconf_sysctl_proxy_ndp,
5932 },
5933 {
5934 .procname = "accept_source_route",
5935 .data = &ipv6_devconf.accept_source_route,
5936 .maxlen = sizeof(int),
5937 .mode = 0644,
5938 .proc_handler = proc_dointvec,
5939 },
Neil Horman95c385b2007-04-25 17:08:10 -07005940#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005941 {
5942 .procname = "optimistic_dad",
5943 .data = &ipv6_devconf.optimistic_dad,
5944 .maxlen = sizeof(int),
5945 .mode = 0644,
5946 .proc_handler = proc_dointvec,
5947 },
5948 {
5949 .procname = "use_optimistic",
5950 .data = &ipv6_devconf.use_optimistic,
5951 .maxlen = sizeof(int),
5952 .mode = 0644,
5953 .proc_handler = proc_dointvec,
5954 },
Neil Horman95c385b2007-04-25 17:08:10 -07005955#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005956#ifdef CONFIG_IPV6_MROUTE
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005957 {
5958 .procname = "mc_forwarding",
5959 .data = &ipv6_devconf.mc_forwarding,
5960 .maxlen = sizeof(int),
5961 .mode = 0444,
5962 .proc_handler = proc_dointvec,
5963 },
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005964#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005965 {
5966 .procname = "disable_ipv6",
5967 .data = &ipv6_devconf.disable_ipv6,
5968 .maxlen = sizeof(int),
5969 .mode = 0644,
5970 .proc_handler = addrconf_sysctl_disable,
5971 },
5972 {
5973 .procname = "accept_dad",
5974 .data = &ipv6_devconf.accept_dad,
5975 .maxlen = sizeof(int),
5976 .mode = 0644,
5977 .proc_handler = proc_dointvec,
5978 },
5979 {
5980 .procname = "force_tllao",
5981 .data = &ipv6_devconf.force_tllao,
5982 .maxlen = sizeof(int),
5983 .mode = 0644,
5984 .proc_handler = proc_dointvec
5985 },
5986 {
5987 .procname = "ndisc_notify",
5988 .data = &ipv6_devconf.ndisc_notify,
5989 .maxlen = sizeof(int),
5990 .mode = 0644,
5991 .proc_handler = proc_dointvec
5992 },
5993 {
5994 .procname = "suppress_frag_ndisc",
5995 .data = &ipv6_devconf.suppress_frag_ndisc,
5996 .maxlen = sizeof(int),
5997 .mode = 0644,
5998 .proc_handler = proc_dointvec
5999 },
6000 {
6001 .procname = "accept_ra_from_local",
6002 .data = &ipv6_devconf.accept_ra_from_local,
6003 .maxlen = sizeof(int),
6004 .mode = 0644,
6005 .proc_handler = proc_dointvec,
6006 },
6007 {
6008 .procname = "accept_ra_mtu",
6009 .data = &ipv6_devconf.accept_ra_mtu,
6010 .maxlen = sizeof(int),
6011 .mode = 0644,
6012 .proc_handler = proc_dointvec,
6013 },
6014 {
6015 .procname = "stable_secret",
6016 .data = &ipv6_devconf.stable_secret,
6017 .maxlen = IPV6_MAX_STRLEN,
6018 .mode = 0600,
6019 .proc_handler = addrconf_sysctl_stable_secret,
6020 },
6021 {
6022 .procname = "use_oif_addrs_only",
6023 .data = &ipv6_devconf.use_oif_addrs_only,
6024 .maxlen = sizeof(int),
6025 .mode = 0644,
6026 .proc_handler = proc_dointvec,
6027 },
6028 {
6029 .procname = "ignore_routes_with_linkdown",
6030 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6031 .maxlen = sizeof(int),
6032 .mode = 0644,
6033 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6034 },
6035 {
6036 .procname = "drop_unicast_in_l2_multicast",
6037 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6038 .maxlen = sizeof(int),
6039 .mode = 0644,
6040 .proc_handler = proc_dointvec,
6041 },
6042 {
6043 .procname = "drop_unsolicited_na",
6044 .data = &ipv6_devconf.drop_unsolicited_na,
6045 .maxlen = sizeof(int),
6046 .mode = 0644,
6047 .proc_handler = proc_dointvec,
6048 },
6049 {
6050 .procname = "keep_addr_on_down",
6051 .data = &ipv6_devconf.keep_addr_on_down,
6052 .maxlen = sizeof(int),
6053 .mode = 0644,
6054 .proc_handler = proc_dointvec,
David Ahernf1705ec2016-02-24 09:25:37 -08006055
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006056 },
6057 {
6058 /* sentinel */
6059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060};
6061
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08006062static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006063 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064{
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006065 int i, ifindex;
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006066 struct ctl_table *table;
Eric W. Biederman6105e292012-04-19 13:41:24 +00006067 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11006068
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006069 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6070 if (!table)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006071 goto out;
6072
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006073 for (i = 0; table[i].data; i++) {
6074 table[i].data += (char *)p - (char *)&ipv6_devconf;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006075 /* If one of these is already set, then it is not safe to
6076 * overwrite either of them: this makes proc_dointvec_minmax
6077 * usable.
6078 */
6079 if (!table[i].extra1 && !table[i].extra2) {
6080 table[i].extra1 = idev; /* embedded; no ref */
6081 table[i].extra2 = net;
6082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084
Eric W. Biederman6105e292012-04-19 13:41:24 +00006085 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006087 p->sysctl_header = register_net_sysctl(net, path, table);
6088 if (!p->sysctl_header)
Eric W. Biederman6105e292012-04-19 13:41:24 +00006089 goto free;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006090
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006091 if (!strcmp(dev_name, "all"))
6092 ifindex = NETCONFA_IFINDEX_ALL;
6093 else if (!strcmp(dev_name, "default"))
6094 ifindex = NETCONFA_IFINDEX_DEFAULT;
6095 else
6096 ifindex = idev->dev->ifindex;
6097 inet6_netconf_notify_devconf(net, NETCONFA_ALL, ifindex, p);
Pavel Emelyanov95897312008-01-10 17:41:45 -08006098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006100free:
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006101 kfree(table);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006102out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08006103 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104}
6105
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006106static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
6107{
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006108 struct ctl_table *table;
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006109
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006110 if (!p->sysctl_header)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006111 return;
6112
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006113 table = p->sysctl_header->ctl_table_arg;
6114 unregister_net_sysctl_table(p->sysctl_header);
6115 p->sysctl_header = NULL;
6116 kfree(table);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006117}
6118
WANG Conga317a2f2014-07-25 15:25:09 -07006119static int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006120{
WANG Conga317a2f2014-07-25 15:25:09 -07006121 int err;
6122
6123 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6124 return -EINVAL;
6125
6126 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6127 &ndisc_ifinfo_sysctl_change);
6128 if (err)
6129 return err;
6130 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6131 idev, &idev->cnf);
6132 if (err)
6133 neigh_sysctl_unregister(idev->nd_parms);
6134
6135 return err;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006136}
6137
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006138static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006140 __addrconf_sysctl_unregister(&idev->cnf);
6141 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142}
6143
6144
6145#endif
6146
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006147static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006148{
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006149 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006150 struct ipv6_devconf *all, *dflt;
6151
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006152 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006153 if (!all)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006154 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006155
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006156 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006157 if (!dflt)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006158 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006159
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006160 /* these will be inherited by all namespaces */
6161 dflt->autoconf = ipv6_defaults.autoconf;
6162 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006163
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006164 dflt->stable_secret.initialized = false;
6165 all->stable_secret.initialized = false;
6166
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006167 net->ipv6.devconf_all = all;
6168 net->ipv6.devconf_dflt = dflt;
6169
6170#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006171 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006172 if (err < 0)
6173 goto err_reg_all;
6174
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006175 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006176 if (err < 0)
6177 goto err_reg_dflt;
6178#endif
6179 return 0;
6180
6181#ifdef CONFIG_SYSCTL
6182err_reg_dflt:
6183 __addrconf_sysctl_unregister(all);
6184err_reg_all:
6185 kfree(dflt);
6186#endif
6187err_alloc_dflt:
6188 kfree(all);
6189err_alloc_all:
6190 return err;
6191}
6192
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006193static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006194{
6195#ifdef CONFIG_SYSCTL
6196 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
6197 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
6198#endif
zhuyj73cf0e922014-11-26 10:25:58 +08006199 kfree(net->ipv6.devconf_dflt);
6200 kfree(net->ipv6.devconf_all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006201}
6202
6203static struct pernet_operations addrconf_ops = {
6204 .init = addrconf_init_net,
6205 .exit = addrconf_exit_net,
6206};
6207
Daniel Borkmann207895fd32015-01-29 12:15:03 +01006208static struct rtnl_af_ops inet6_ops __read_mostly = {
Thomas Grafb382b192010-11-16 04:33:57 +00006209 .family = AF_INET6,
6210 .fill_link_af = inet6_fill_link_af,
6211 .get_link_af_size = inet6_get_link_af_size,
Daniel Borkmann11b1f822015-02-05 14:39:11 +01006212 .validate_link_af = inet6_validate_link_af,
Daniel Borkmannf53adae2013-04-08 04:01:30 +00006213 .set_link_af = inet6_set_link_af,
Thomas Grafb382b192010-11-16 04:33:57 +00006214};
6215
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216/*
6217 * Init / cleanup code
6218 */
6219
6220int __init addrconf_init(void)
6221{
WANG Conga317a2f2014-07-25 15:25:09 -07006222 struct inet6_dev *idev;
stephen hemmingerc2e21292010-03-17 20:31:10 +00006223 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006224
Stephen Hemmingere21e8462010-03-20 16:09:01 -07006225 err = ipv6_addr_label_init();
6226 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00006227 pr_crit("%s: cannot initialize default policy table: %d\n",
6228 __func__, err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006229 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006232 err = register_pernet_subsys(&addrconf_ops);
6233 if (err < 0)
6234 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006235
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006236 addrconf_wq = create_workqueue("ipv6_addrconf");
6237 if (!addrconf_wq) {
6238 err = -ENOMEM;
6239 goto out_nowq;
6240 }
6241
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 /* The addrconf netdev notifier requires that loopback_dev
6243 * has it's ipv6 private information allocated and setup
6244 * before it can bring up and give link-local addresses
6245 * to other devices which are up.
6246 *
6247 * Unfortunately, loopback_dev is not necessarily the first
6248 * entry in the global dev_base list of net devices. In fact,
6249 * it is likely to be the very last entry on that list.
6250 * So this causes the notifier registry below to try and
6251 * give link-local addresses to all devices besides loopback_dev
6252 * first, then loopback_dev, which cases all the non-loopback_dev
6253 * devices to fail to get a link-local address.
6254 *
6255 * So, as a temporary fix, allocate the ipv6 structure for
6256 * loopback_dev first by hand.
6257 * Longer term, all of the dependencies ipv6 has upon the loopback
6258 * device and it being up should be removed.
6259 */
6260 rtnl_lock();
WANG Conga317a2f2014-07-25 15:25:09 -07006261 idev = ipv6_add_dev(init_net.loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 rtnl_unlock();
WANG Conga317a2f2014-07-25 15:25:09 -07006263 if (IS_ERR(idev)) {
6264 err = PTR_ERR(idev);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006265 goto errlo;
WANG Conga317a2f2014-07-25 15:25:09 -07006266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267
stephen hemmingerc2e21292010-03-17 20:31:10 +00006268 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6269 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6270
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 register_netdevice_notifier(&ipv6_dev_notf);
6272
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006273 addrconf_verify();
Thomas Grafc127ea22007-03-22 11:58:32 -07006274
stephen hemminger3678a9d2013-12-30 10:41:32 -08006275 rtnl_af_register(&inet6_ops);
Thomas Grafb382b192010-11-16 04:33:57 +00006276
Greg Rosec7ac8672011-06-10 01:27:09 +00006277 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6278 NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006279 if (err < 0)
6280 goto errout;
6281
6282 /* Only the first call to __rtnl_register can fail */
Greg Rosec7ac8672011-06-10 01:27:09 +00006283 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
6284 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
6285 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6286 inet6_dump_ifaddr, NULL);
6287 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6288 inet6_dump_ifmcaddr, NULL);
6289 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6290 inet6_dump_ifacaddr, NULL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +00006291 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
Nicolas Dichtel7a674202013-03-05 23:42:06 +00006292 inet6_netconf_dump_devconf, NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006293
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006294 ipv6_addr_label_rtnl_register();
6295
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07006297errout:
Thomas Grafb382b192010-11-16 04:33:57 +00006298 rtnl_af_unregister(&inet6_ops);
Thomas Grafc127ea22007-03-22 11:58:32 -07006299 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006300errlo:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006301 destroy_workqueue(addrconf_wq);
6302out_nowq:
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006303 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006304out_addrlabel:
6305 ipv6_addr_label_cleanup();
6306out:
Thomas Grafc127ea22007-03-22 11:58:32 -07006307 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308}
6309
Daniel Lezcano09f77092007-12-13 05:34:58 -08006310void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006312 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 int i;
6314
6315 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006316 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006317 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318
6319 rtnl_lock();
6320
Thomas Grafb382b192010-11-16 04:33:57 +00006321 __rtnl_af_unregister(&inet6_ops);
6322
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006323 /* clean dev list */
6324 for_each_netdev(&init_net, dev) {
6325 if (__in6_dev_get(dev) == NULL)
6326 continue;
6327 addrconf_ifdown(dev, 1);
6328 }
6329 addrconf_ifdown(init_net.loopback_dev, 2);
6330
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332 * Check hash table.
6333 */
stephen hemminger5c578aed2010-03-17 20:31:11 +00006334 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00006335 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6336 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578aed2010-03-17 20:31:11 +00006337 spin_unlock_bh(&addrconf_hash_lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006338 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339 rtnl_unlock();
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006340
6341 destroy_workqueue(addrconf_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342}