blob: 3f30d3a1fda15e1dca448a7413fa9699588a984d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
15/*
16 * Changes:
17 *
18 * Janos Farkas : delete timer on ifdown
19 * <chexum@bankinf.banki.hu>
20 * Andi Kleen : kill double kfree on module
21 * unload.
22 * Maciej W. Rozycki : FDDI support
23 * sekiya@USAGI : Don't send too many RS
24 * packets.
25 * yoshfuji@USAGI : Fixed interval between DAD
26 * packets.
27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
28 * address validation timer.
29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
30 * support.
31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
32 * address on a same interface.
33 * YOSHIFUJI Hideaki @USAGI : ARCnet support
34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
35 * seq_file.
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -080036 * YOSHIFUJI Hideaki @USAGI : improved source address
37 * selection; consider scope,
38 * status etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
40
Joe Perchesf3213832012-05-15 14:11:53 +000041#define pr_fmt(fmt) "IPv6: " fmt
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/errno.h>
44#include <linux/types.h>
Ilpo Järvinena0bffff2009-03-21 13:36:17 -070045#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/socket.h>
47#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/net.h>
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +010049#include <linux/inet.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/in6.h>
51#include <linux/netdevice.h>
Thomas Graf18237302006-08-04 23:04:54 -070052#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/if_arp.h>
54#include <linux/if_arcnet.h>
55#include <linux/if_infiniband.h>
56#include <linux/route.h>
57#include <linux/inetdevice.h>
58#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090059#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifdef CONFIG_SYSCTL
61#include <linux/sysctl.h>
62#endif
Randy Dunlap4fc268d2006-01-11 12:17:47 -080063#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/delay.h>
65#include <linux/notifier.h>
Paulo Marques543537b2005-06-23 00:09:02 -070066#include <linux/string.h>
Eric Dumazetddbe5032012-07-18 08:11:12 +000067#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020069#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <net/sock.h>
71#include <net/snmp.h>
72
Alexander Aring5241c2d2015-12-14 20:55:22 +010073#include <net/6lowpan.h>
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +000074#include <net/firewire.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <net/ipv6.h>
76#include <net/protocol.h>
77#include <net/ndisc.h>
78#include <net/ip6_route.h>
79#include <net/addrconf.h>
80#include <net/tcp.h>
81#include <net/ip.h>
Thomas Graf5d620262006-08-15 00:35:02 -070082#include <net/netlink.h>
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -070083#include <net/pkt_sched.h>
David Ahernca254492015-10-12 11:47:10 -070084#include <net/l3mdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#include <linux/if_tunnel.h>
86#include <linux/rtnetlink.h>
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +000087#include <linux/netconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include <linux/random.h>
Stephen Hemmingere21e8462010-03-20 16:09:01 -070089#include <linux/uaccess.h>
Herbert Xu7f7d9a62007-04-24 21:54:09 -070090#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92#include <linux/proc_fs.h>
93#include <linux/seq_file.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040094#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/* Set to 3 to get tracing... */
97#define ACONF_DEBUG 2
98
99#if ACONF_DEBUG >= 3
dingtianhongba3542e2013-08-17 10:27:04 +0800100#define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#else
dingtianhongba3542e2013-08-17 10:27:04 +0800102#define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#endif
104
105#define INFINITY_LIFE_TIME 0xFFFFFFFF
Thomas Graf18a31e12010-11-17 04:12:02 +0000106
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100107#define IPV6_MAX_STRLEN \
108 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
109
Thomas Graf18a31e12010-11-17 04:12:02 +0000110static inline u32 cstamp_delta(unsigned long cstamp)
111{
112 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
113}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700115static inline s32 rfc3315_s14_backoff_init(s32 irt)
116{
117 /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
118 u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
119 do_div(tmp, 1000000);
120 return (s32)tmp;
121}
122
123static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
124{
125 /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
126 u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
127 do_div(tmp, 1000000);
128 if ((s32)tmp > mrt) {
129 /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
130 tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
131 do_div(tmp, 1000000);
132 }
133 return (s32)tmp;
134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136#ifdef CONFIG_SYSCTL
WANG Conga317a2f2014-07-25 15:25:09 -0700137static int addrconf_sysctl_register(struct inet6_dev *idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800138static void addrconf_sysctl_unregister(struct inet6_dev *idev);
139#else
WANG Conga317a2f2014-07-25 15:25:09 -0700140static inline int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800141{
WANG Conga317a2f2014-07-25 15:25:09 -0700142 return 0;
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800143}
144
145static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
146{
147}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#endif
149
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200150static void ipv6_regen_rndid(struct inet6_dev *idev);
151static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900153static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static int ipv6_count_addresses(struct inet6_dev *idev);
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +0100155static int ipv6_generate_stable_address(struct in6_addr *addr,
156 u8 dad_count,
157 const struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159/*
160 * Configured unicast address hash table
161 */
stephen hemmingerc2e21292010-03-17 20:31:10 +0000162static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
stephen hemminger5c578ae2010-03-17 20:31:11 +0000163static DEFINE_SPINLOCK(addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100165static void addrconf_verify(void);
166static void addrconf_verify_rtnl(void);
167static void addrconf_verify_work(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100169static struct workqueue_struct *addrconf_wq;
170static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
173static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
174
Jiri Pirko93d9b7d2010-03-10 10:28:56 +0000175static void addrconf_type_change(struct net_device *dev,
176 unsigned long event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static int addrconf_ifdown(struct net_device *dev, int how);
178
Romain Kuntz21caa662013-01-09 21:06:03 +0000179static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
180 int plen,
181 const struct net_device *dev,
182 u32 flags, u32 noflags);
183
David S. Millercf22f9a2012-04-14 21:37:40 -0400184static void addrconf_dad_start(struct inet6_ifaddr *ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100185static void addrconf_dad_work(struct work_struct *w);
Paolo Abeni764d3be2016-11-22 16:57:40 +0100186static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900187static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188static void addrconf_rs_timer(unsigned long data);
189static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
190static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
191
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900192static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct prefix_info *pinfo);
David S. Miller3e81c6d2010-03-20 16:18:00 -0700194static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
195 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Adrian Bunk888c8482008-07-22 14:21:58 -0700197static struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 .forwarding = 0,
199 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
200 .mtu6 = IPV6_MIN_MTU,
201 .accept_ra = 1,
202 .accept_redirects = 1,
203 .autoconf = 1,
204 .force_mld_version = 0,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200205 .mldv1_unsolicited_report_interval = 10 * HZ,
206 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 .dad_transmits = 1,
208 .rtr_solicits = MAX_RTR_SOLICITATIONS,
209 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700210 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Ian Morris67ba4152014-08-24 21:53:10 +0100212 .use_tempaddr = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 .temp_valid_lft = TEMP_VALID_LIFETIME,
214 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
215 .regen_max_retry = REGEN_MAX_RETRY,
216 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800218 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700219 .accept_ra_from_local = 0,
Hangbin Liu8013d1d2015-07-30 14:28:42 +0800220 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800221 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800222#ifdef CONFIG_IPV6_ROUTER_PREF
223 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800224 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800225#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzd860b2e2017-03-22 18:19:04 +0900226 .accept_ra_rt_info_min_plen = 0,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800227 .accept_ra_rt_info_max_plen = 0,
228#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800229#endif
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +0900230 .accept_ra_rt_table = 0,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700231 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700232 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900233 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900234 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200235 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700236 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100237 .stable_secret = {
238 .initialized = false,
Erik Kline3985e8a2015-07-22 16:38:25 +0900239 },
240 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400241 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800242 .keep_addr_on_down = 0,
Felix Jiad1f8a9b2017-01-26 16:59:17 +1300243 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244};
245
Brian Haleyab32ea52006-09-22 14:15:41 -0700246static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 .forwarding = 0,
248 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
249 .mtu6 = IPV6_MIN_MTU,
250 .accept_ra = 1,
251 .accept_redirects = 1,
252 .autoconf = 1,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200253 .force_mld_version = 0,
254 .mldv1_unsolicited_report_interval = 10 * HZ,
255 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 .dad_transmits = 1,
257 .rtr_solicits = MAX_RTR_SOLICITATIONS,
258 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700259 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 .use_tempaddr = 0,
262 .temp_valid_lft = TEMP_VALID_LIFETIME,
263 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
264 .regen_max_retry = REGEN_MAX_RETRY,
265 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800267 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700268 .accept_ra_from_local = 0,
Hangbin Liu8013d1d2015-07-30 14:28:42 +0800269 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800270 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800271#ifdef CONFIG_IPV6_ROUTER_PREF
272 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800273 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800274#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzd860b2e2017-03-22 18:19:04 +0900275 .accept_ra_rt_info_min_plen = 0,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800276 .accept_ra_rt_info_max_plen = 0,
277#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800278#endif
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +0900279 .accept_ra_rt_table = 0,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700280 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700281 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900282 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900283 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200284 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700285 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100286 .stable_secret = {
287 .initialized = false,
288 },
Erik Kline3985e8a2015-07-22 16:38:25 +0900289 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400290 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800291 .keep_addr_on_down = 0,
Felix Jiad1f8a9b2017-01-26 16:59:17 +1300292 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293};
294
Mike Manning4bf42a22017-09-25 22:01:36 +0100295/* Check if link is ready: is it up and is a valid qdisc available */
296static inline bool addrconf_link_ready(const struct net_device *dev)
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700297{
Mike Manning4bf42a22017-09-25 22:01:36 +0100298 return netif_oper_up(dev) && !qdisc_tx_is_noop(dev);
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700299}
300
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200301static void addrconf_del_rs_timer(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200303 if (del_timer(&idev->rs_timer))
304 __in6_dev_put(idev);
305}
306
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100307static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200308{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100309 if (cancel_delayed_work(&ifp->dad_work))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 __in6_ifa_put(ifp);
311}
312
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200313static void addrconf_mod_rs_timer(struct inet6_dev *idev,
314 unsigned long when)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200316 if (!timer_pending(&idev->rs_timer))
317 in6_dev_hold(idev);
318 mod_timer(&idev->rs_timer, jiffies + when);
319}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100321static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
322 unsigned long delay)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200323{
Xin Longfded2d72017-06-15 16:33:58 +0800324 in6_ifa_hold(ifp);
325 if (mod_delayed_work(addrconf_wq, &ifp->dad_work, delay))
326 in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700329static int snmp6_alloc_dev(struct inet6_dev *idev)
330{
John Stultz827da442013-10-07 15:51:58 -0700331 int i;
332
WANG Cong698365f2014-05-05 15:55:55 -0700333 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
334 if (!idev->stats.ipv6)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700335 goto err_ip;
John Stultz827da442013-10-07 15:51:58 -0700336
337 for_each_possible_cpu(i) {
338 struct ipstats_mib *addrconf_stats;
WANG Cong698365f2014-05-05 15:55:55 -0700339 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
John Stultz827da442013-10-07 15:51:58 -0700340 u64_stats_init(&addrconf_stats->syncp);
John Stultz827da442013-10-07 15:51:58 -0700341 }
342
343
Eric Dumazetbe281e52011-05-19 01:14:23 +0000344 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
345 GFP_KERNEL);
346 if (!idev->stats.icmpv6dev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700347 goto err_icmp;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000348 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
349 GFP_KERNEL);
350 if (!idev->stats.icmpv6msgdev)
David L Stevens14878f72007-09-16 16:52:35 -0700351 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700352
353 return 0;
354
David L Stevens14878f72007-09-16 16:52:35 -0700355err_icmpmsg:
Eric Dumazetbe281e52011-05-19 01:14:23 +0000356 kfree(idev->stats.icmpv6dev);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700357err_icmp:
WANG Cong698365f2014-05-05 15:55:55 -0700358 free_percpu(idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700359err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700360 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700361}
362
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000363static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 struct inet6_dev *ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700366 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 ASSERT_RTNL();
369
370 if (dev->mtu < IPV6_MIN_MTU)
WANG Conga317a2f2014-07-25 15:25:09 -0700371 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900373 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100374 if (!ndev)
WANG Conga317a2f2014-07-25 15:25:09 -0700375 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Ingo Oeser322f74a2006-03-20 23:01:47 -0800377 rwlock_init(&ndev->lock);
378 ndev->dev = dev;
stephen hemminger502a2ff2010-03-17 20:31:13 +0000379 INIT_LIST_HEAD(&ndev->addr_list);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200380 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
381 (unsigned long)ndev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900382 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100383
384 if (ndev->cnf.stable_secret.initialized)
Felix Jiad1f8a9b2017-01-26 16:59:17 +1300385 ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100386 else
Felix Jiad1f8a9b2017-01-26 16:59:17 +1300387 ndev->cnf.addr_gen_mode = ipv6_devconf_dflt.addr_gen_mode;
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100388
Ingo Oeser322f74a2006-03-20 23:01:47 -0800389 ndev->cnf.mtu6 = dev->mtu;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800390 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
Ian Morris63159f22015-03-29 14:00:04 +0100391 if (!ndev->nd_parms) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800392 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700393 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800394 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700395 if (ndev->cnf.forwarding)
396 dev_disable_lro(dev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800397 /* We refer to the device */
398 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Ingo Oeser322f74a2006-03-20 23:01:47 -0800400 if (snmp6_alloc_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800401 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000402 "%s: cannot allocate memory for statistics; dev=%s.\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800403 __func__, dev->name);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800404 neigh_parms_release(&nd_tbl, ndev->nd_parms);
Roy Li8603e332011-09-20 15:10:16 -0400405 dev_put(dev);
406 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700407 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Ingo Oeser322f74a2006-03-20 23:01:47 -0800410 if (snmp6_register_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800411 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000412 "%s: cannot create /proc/net/dev_snmp6/%s\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800413 __func__, dev->name);
WANG Conga317a2f2014-07-25 15:25:09 -0700414 goto err_release;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200417 /* One reference from device. */
Ingo Oeser322f74a2006-03-20 23:01:47 -0800418 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900420 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
421 ndev->cnf.accept_dad = -1;
422
Amerigo Wang07a93622012-10-29 16:23:10 +0000423#if IS_ENABLED(CONFIG_IPV6_SIT)
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700424 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
Joe Perchesf3213832012-05-15 14:11:53 +0000425 pr_info("%s: Disabled Multicast RS\n", dev->name);
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700426 ndev->cnf.rtr_solicits = 0;
427 }
428#endif
429
stephen hemminger372e6c82010-03-17 20:31:09 +0000430 INIT_LIST_HEAD(&ndev->tempaddr_list);
Jiri Bohac76506a92016-10-13 18:52:15 +0200431 ndev->desync_factor = U32_MAX;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800432 if ((dev->flags&IFF_LOOPBACK) ||
433 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700434 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700435 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700436 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800437 ndev->cnf.use_tempaddr = -1;
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200438 } else
439 ipv6_regen_rndid(ndev);
David S. Miller5d9efa72013-10-28 20:07:50 -0400440
Daniel Borkmann914faa12013-04-09 03:47:14 +0000441 ndev->token = in6addr_any;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800442
Mike Manning4bf42a22017-09-25 22:01:36 +0100443 if (netif_running(dev) && addrconf_link_ready(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700444 ndev->if_flags |= IF_READY;
445
Ingo Oeser322f74a2006-03-20 23:01:47 -0800446 ipv6_mc_init_dev(ndev);
447 ndev->tstamp = jiffies;
WANG Conga317a2f2014-07-25 15:25:09 -0700448 err = addrconf_sysctl_register(ndev);
449 if (err) {
450 ipv6_mc_destroy_dev(ndev);
Sabrina Dubroca2a189f92015-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 hemminger5c578ae2010-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 hemminger5c578ae2010-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;
Mahesh Bandewareb5562d2017-05-12 17:03:39 -0700997 ifa->flags = flags;
998 /* No need to add the TENTATIVE flag for addresses with NODAD */
999 if (!(flags & IFA_F_NODAD))
1000 ifa->flags |= IFA_F_TENTATIVE;
Jiri Benc8a226b22013-08-01 10:41:28 +02001001 ifa->valid_lft = valid_lft;
1002 ifa->prefered_lft = prefered_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 ifa->cstamp = ifa->tstamp = jiffies;
Daniel Borkmann617fe292013-04-09 03:47:16 +00001004 ifa->tokenized = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Keir Fraser57f5f542006-08-29 02:43:49 -07001006 ifa->rt = rt;
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 ifa->idev = idev;
1009 in6_dev_hold(idev);
1010 /* For caller */
1011 in6_ifa_hold(ifa);
1012
1013 /* Add to big hash table */
Eric Dumazetddbe5032012-07-18 08:11:12 +00001014 hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
stephen hemminger5c578ae2010-03-17 20:31:11 +00001016 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
stephen hemminger5c578ae2010-03-17 20:31:11 +00001017 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 write_lock(&idev->lock);
1020 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -07001021 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (ifa->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001024 list_add(&ifa->tmp_list, &idev->tempaddr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 in6_ifa_hold(ifa);
1026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 in6_ifa_hold(ifa);
1029 write_unlock(&idev->lock);
1030out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001031 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -07001033 if (likely(err == 0))
Cong Wangf88c91d2013-04-14 23:18:43 +08001034 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 else {
1036 kfree(ifa);
1037 ifa = ERR_PTR(err);
1038 }
1039
1040 return ifa;
1041out:
stephen hemminger5c578ae2010-03-17 20:31:11 +00001042 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 goto out2;
1044}
1045
Thomas Haller5b84efe2014-01-15 15:36:59 +01001046enum cleanup_prefix_rt_t {
1047 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1048 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1049 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1050};
1051
1052/*
1053 * Check, whether the prefix for ifp would still need a prefix route
1054 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1055 * constants.
1056 *
1057 * 1) we don't purge prefix if address was not permanent.
1058 * prefix is managed by its own lifetime.
1059 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1060 * 3) if there are no addresses, delete prefix.
1061 * 4) if there are still other permanent address(es),
1062 * corresponding prefix is still permanent.
1063 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1064 * don't purge the prefix, assume user space is managing it.
1065 * 6) otherwise, update prefix lifetime to the
1066 * longest valid lifetime among the corresponding
1067 * addresses on the device.
1068 * Note: subsequent RA will update lifetime.
1069 **/
1070static enum cleanup_prefix_rt_t
1071check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1072{
1073 struct inet6_ifaddr *ifa;
1074 struct inet6_dev *idev = ifp->idev;
1075 unsigned long lifetime;
1076 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1077
1078 *expires = jiffies;
1079
1080 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1081 if (ifa == ifp)
1082 continue;
Zhiqiang Liue52cb572019-02-11 10:57:46 +08001083 if (ifa->prefix_len != ifp->prefix_len ||
1084 !ipv6_prefix_equal(&ifa->addr, &ifp->addr,
Thomas Haller5b84efe2014-01-15 15:36:59 +01001085 ifp->prefix_len))
1086 continue;
1087 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1088 return CLEANUP_PREFIX_RT_NOP;
1089
1090 action = CLEANUP_PREFIX_RT_EXPIRE;
1091
1092 spin_lock(&ifa->lock);
1093
1094 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1095 /*
1096 * Note: Because this address is
1097 * not permanent, lifetime <
1098 * LONG_MAX / HZ here.
1099 */
1100 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1101 *expires = ifa->tstamp + lifetime * HZ;
1102 spin_unlock(&ifa->lock);
1103 }
1104
1105 return action;
1106}
1107
1108static void
1109cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
1110{
1111 struct rt6_info *rt;
1112
1113 rt = addrconf_get_prefix_route(&ifp->addr,
1114 ifp->prefix_len,
1115 ifp->idev->dev,
1116 0, RTF_GATEWAY | RTF_DEFAULT);
1117 if (rt) {
1118 if (del_rt)
1119 ip6_del_rt(rt);
1120 else {
1121 if (!(rt->rt6i_flags & RTF_EXPIRES))
1122 rt6_set_expires(rt, expires);
1123 ip6_rt_put(rt);
1124 }
1125 }
1126}
1127
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129/* This function wants to get referenced ifp and releases it before return */
1130
1131static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1132{
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001133 int state;
Thomas Haller5b84efe2014-01-15 15:36:59 +01001134 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1135 unsigned long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001137 ASSERT_RTNL();
1138
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001139 spin_lock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001140 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -07001141 ifp->state = INET6_IFADDR_STATE_DEAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001142 spin_unlock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001143
1144 if (state == INET6_IFADDR_STATE_DEAD)
1145 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
stephen hemminger5c578ae2010-03-17 20:31:11 +00001147 spin_lock_bh(&addrconf_hash_lock);
1148 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578ae2010-03-17 20:31:11 +00001149 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Thomas Haller5b84efe2014-01-15 15:36:59 +01001151 write_lock_bh(&ifp->idev->lock);
David S. Miller5d9efa72013-10-28 20:07:50 -04001152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001154 list_del(&ifp->tmp_list);
1155 if (ifp->ifpub) {
1156 in6_ifa_put(ifp->ifpub);
1157 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
stephen hemminger372e6c82010-03-17 20:31:09 +00001159 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Thomas Haller5b84efe2014-01-15 15:36:59 +01001162 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1163 action = check_cleanup_prefix_route(ifp, &expires);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001164
Thomas Haller5b84efe2014-01-15 15:36:59 +01001165 list_del_init(&ifp->if_list);
1166 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Thomas Haller5b84efe2014-01-15 15:36:59 +01001168 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001170 addrconf_del_dad_work(ifp);
Andrey Vaginb2238562008-07-08 15:13:31 -07001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 ipv6_ifa_notify(RTM_DELADDR, ifp);
1173
Cong Wangf88c91d2013-04-14 23:18:43 +08001174 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Thomas Haller5b84efe2014-01-15 15:36:59 +01001176 if (action != CLEANUP_PREFIX_RT_NOP) {
1177 cleanup_prefix_route(ifp, expires,
1178 action == CLEANUP_PREFIX_RT_DEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180
Daniel Walterc3968a82011-04-13 21:10:57 +00001181 /* clean up prefsrc entries */
1182 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001183out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 in6_ifa_put(ifp);
1185}
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1188{
1189 struct inet6_dev *idev = ifp->idev;
1190 struct in6_addr addr, *tmpaddr;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001191 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001192 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 int tmp_plen;
1194 int ret = 0;
Neil Horman95c385b2007-04-25 17:08:10 -07001195 u32 addr_flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001196 unsigned long now = jiffies;
Jiri Bohac76506a92016-10-13 18:52:15 +02001197 long max_desync_factor;
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001198 s32 cnf_temp_preferred_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Jiri Pirko53bd6742013-12-06 09:45:22 +01001200 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (ift) {
1202 spin_lock_bh(&ift->lock);
1203 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1204 spin_unlock_bh(&ift->lock);
1205 tmpaddr = &addr;
1206 } else {
1207 tmpaddr = NULL;
1208 }
1209retry:
1210 in6_dev_hold(idev);
1211 if (idev->cnf.use_tempaddr <= 0) {
Jiri Pirko53bd6742013-12-06 09:45:22 +01001212 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001213 pr_info("%s: use_tempaddr is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 in6_dev_put(idev);
1215 ret = -1;
1216 goto out;
1217 }
1218 spin_lock_bh(&ifp->lock);
1219 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1220 idev->cnf.use_tempaddr = -1; /*XXX*/
1221 spin_unlock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01001222 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001223 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1224 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 in6_dev_put(idev);
1226 ret = -1;
1227 goto out;
1228 }
1229 in6_ifa_hold(ifp);
1230 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
Jiri Bohac9d6280d2016-10-13 18:50:02 +02001231 ipv6_try_regen_rndid(idev, tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001233 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001235 regen_advance = idev->cnf.regen_max_retry *
Ian Morris67ba4152014-08-24 21:53:10 +01001236 idev->cnf.dad_transmits *
1237 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
Jiri Bohac76506a92016-10-13 18:52:15 +02001238
1239 /* recalculate max_desync_factor each time and update
1240 * idev->desync_factor if it's larger
1241 */
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001242 cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
Jiri Bohac76506a92016-10-13 18:52:15 +02001243 max_desync_factor = min_t(__u32,
1244 idev->cnf.max_desync_factor,
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001245 cnf_temp_preferred_lft - regen_advance);
Jiri Bohac76506a92016-10-13 18:52:15 +02001246
1247 if (unlikely(idev->desync_factor > max_desync_factor)) {
1248 if (max_desync_factor > 0) {
1249 get_random_bytes(&idev->desync_factor,
1250 sizeof(idev->desync_factor));
1251 idev->desync_factor %= max_desync_factor;
1252 } else {
1253 idev->desync_factor = 0;
1254 }
1255 }
1256
1257 tmp_valid_lft = min_t(__u32,
1258 ifp->valid_lft,
1259 idev->cnf.temp_valid_lft + age);
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001260 tmp_prefered_lft = cnf_temp_preferred_lft + age -
Jiri Bohac76506a92016-10-13 18:52:15 +02001261 idev->desync_factor;
Jiri Bohac76506a92016-10-13 18:52:15 +02001262 tmp_prefered_lft = min_t(__u32, ifp->prefered_lft, tmp_prefered_lft);
1263 tmp_plen = ifp->prefix_len;
1264 tmp_tstamp = ifp->tstamp;
1265 spin_unlock_bh(&ifp->lock);
1266
Jiri Pirko53bd6742013-12-06 09:45:22 +01001267 write_unlock_bh(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -07001268
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001269 /* A temporary address is created only if this calculated Preferred
1270 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1271 * an implementation must not create a temporary address with a zero
1272 * Preferred Lifetime.
Heiner Kallweitecab6702014-03-12 22:13:19 +01001273 * Use age calculation as in addrconf_verify to avoid unnecessary
1274 * temporary addresses being generated.
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001275 */
Heiner Kallweitecab6702014-03-12 22:13:19 +01001276 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1277 if (tmp_prefered_lft <= regen_advance + age) {
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001278 in6_ifa_put(ifp);
1279 in6_dev_put(idev);
1280 ret = -1;
1281 goto out;
1282 }
1283
Neil Horman95c385b2007-04-25 17:08:10 -07001284 addr_flags = IFA_F_TEMPORARY;
1285 /* set in addrconf_prefix_rcv() */
1286 if (ifp->flags & IFA_F_OPTIMISTIC)
1287 addr_flags |= IFA_F_OPTIMISTIC;
1288
Hannes Frederic Sowa4b08a8f2013-08-16 13:02:27 +02001289 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1290 ipv6_addr_scope(&addr), addr_flags,
1291 tmp_valid_lft, tmp_prefered_lft);
1292 if (IS_ERR(ift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 in6_ifa_put(ifp);
1294 in6_dev_put(idev);
Joe Perchesf3213832012-05-15 14:11:53 +00001295 pr_info("%s: retry temporary address regeneration\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 tmpaddr = &addr;
Jiri Pirko53bd6742013-12-06 09:45:22 +01001297 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 goto retry;
1299 }
1300
1301 spin_lock_bh(&ift->lock);
1302 ift->ifpub = ifp;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001303 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 ift->tstamp = tmp_tstamp;
1305 spin_unlock_bh(&ift->lock);
1306
David S. Millercf22f9a2012-04-14 21:37:40 -04001307 addrconf_dad_start(ift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 in6_ifa_put(ift);
1309 in6_dev_put(idev);
1310out:
1311 return ret;
1312}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001315 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001317enum {
1318 IPV6_SADDR_RULE_INIT = 0,
1319 IPV6_SADDR_RULE_LOCAL,
1320 IPV6_SADDR_RULE_SCOPE,
1321 IPV6_SADDR_RULE_PREFERRED,
1322#ifdef CONFIG_IPV6_MIP6
1323 IPV6_SADDR_RULE_HOA,
1324#endif
1325 IPV6_SADDR_RULE_OIF,
1326 IPV6_SADDR_RULE_LABEL,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001327 IPV6_SADDR_RULE_PRIVACY,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001328 IPV6_SADDR_RULE_ORCHID,
1329 IPV6_SADDR_RULE_PREFIX,
Erik Kline7fd25612014-10-28 18:11:14 +09001330#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1331 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1332#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001333 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001334};
1335
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001336struct ipv6_saddr_score {
1337 int rule;
1338 int addr_type;
1339 struct inet6_ifaddr *ifa;
1340 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1341 int scopedist;
1342 int matchlen;
1343};
1344
1345struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001346 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001347 int ifindex;
1348 int scope;
1349 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001350 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001351};
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001352
Dave Jonesb6f99a22007-03-22 12:27:49 -07001353static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Ulrich Weber45bb0062010-02-25 23:28:58 +00001355 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001356 return 1;
1357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358}
1359
Erik Kline7fd25612014-10-28 18:11:14 +09001360static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
1361{
1362#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1363 return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
1364#else
1365 return false;
1366#endif
1367}
1368
Benjamin Thery3de23252008-05-28 14:51:24 +02001369static int ipv6_get_saddr_eval(struct net *net,
1370 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001371 struct ipv6_saddr_dst *dst,
1372 int i)
1373{
1374 int ret;
1375
1376 if (i <= score->rule) {
1377 switch (i) {
1378 case IPV6_SADDR_RULE_SCOPE:
1379 ret = score->scopedist;
1380 break;
1381 case IPV6_SADDR_RULE_PREFIX:
1382 ret = score->matchlen;
1383 break;
1384 default:
1385 ret = !!test_bit(i, score->scorebits);
1386 }
1387 goto out;
1388 }
1389
1390 switch (i) {
1391 case IPV6_SADDR_RULE_INIT:
1392 /* Rule 0: remember if hiscore is not ready yet */
1393 ret = !!score->ifa;
1394 break;
1395 case IPV6_SADDR_RULE_LOCAL:
1396 /* Rule 1: Prefer same address */
1397 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1398 break;
1399 case IPV6_SADDR_RULE_SCOPE:
1400 /* Rule 2: Prefer appropriate scope
1401 *
1402 * ret
1403 * ^
1404 * -1 | d 15
1405 * ---+--+-+---> scope
1406 * |
1407 * | d is scope of the destination.
1408 * B-d | \
1409 * | \ <- smaller scope is better if
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08001410 * B-15 | \ if scope is enough for destination.
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001411 * | ret = B - scope (-1 <= scope >= d <= 15).
1412 * d-C-1 | /
1413 * |/ <- greater is better
1414 * -C / if scope is not enough for destination.
1415 * /| ret = scope - C (-1 <= d < scope <= 15).
1416 *
1417 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1418 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1419 * Assume B = 0 and we get C > 29.
1420 */
1421 ret = __ipv6_addr_src_scope(score->addr_type);
1422 if (ret >= dst->scope)
1423 ret = -ret;
1424 else
1425 ret -= 128; /* 30 is enough */
1426 score->scopedist = ret;
1427 break;
1428 case IPV6_SADDR_RULE_PREFERRED:
Erik Kline7fd25612014-10-28 18:11:14 +09001429 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001430 /* Rule 3: Avoid deprecated and optimistic addresses */
Erik Kline7fd25612014-10-28 18:11:14 +09001431 u8 avoid = IFA_F_DEPRECATED;
1432
1433 if (!ipv6_use_optimistic_addr(score->ifa->idev))
1434 avoid |= IFA_F_OPTIMISTIC;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001435 ret = ipv6_saddr_preferred(score->addr_type) ||
Erik Kline7fd25612014-10-28 18:11:14 +09001436 !(score->ifa->flags & avoid);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001437 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001438 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001439#ifdef CONFIG_IPV6_MIP6
1440 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001441 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001442 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001443 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1444 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001445 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001446 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001447#endif
1448 case IPV6_SADDR_RULE_OIF:
1449 /* Rule 5: Prefer outgoing interface */
1450 ret = (!dst->ifindex ||
1451 dst->ifindex == score->ifa->idev->dev->ifindex);
1452 break;
1453 case IPV6_SADDR_RULE_LABEL:
1454 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001455 ret = ipv6_addr_label(net,
1456 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001457 score->ifa->idev->dev->ifindex) == dst->label;
1458 break;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001459 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001460 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001461 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001462 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001463 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001464 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1465 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1466 score->ifa->idev->cnf.use_tempaddr >= 2;
1467 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001468 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001469 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001470 case IPV6_SADDR_RULE_ORCHID:
1471 /* Rule 8-: Prefer ORCHID vs ORCHID or
1472 * non-ORCHID vs non-ORCHID
1473 */
1474 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1475 ipv6_addr_orchid(dst->addr));
1476 break;
1477 case IPV6_SADDR_RULE_PREFIX:
1478 /* Rule 8: Use longest matching prefix */
YOSHIFUJI Hideaki / 吉藤英明91b4b042012-09-10 18:41:07 +00001479 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1480 if (ret > score->ifa->prefix_len)
1481 ret = score->ifa->prefix_len;
1482 score->matchlen = ret;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001483 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001484#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1485 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1486 /* Optimistic addresses still have lower precedence than other
1487 * preferred addresses.
1488 */
1489 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1490 break;
1491#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001492 default:
1493 ret = 0;
1494 }
1495
1496 if (ret)
1497 __set_bit(i, score->scorebits);
1498 score->rule = i;
1499out:
1500 return ret;
1501}
1502
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001503static int __ipv6_dev_get_saddr(struct net *net,
1504 struct ipv6_saddr_dst *dst,
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001505 struct inet6_dev *idev,
1506 struct ipv6_saddr_score *scores,
1507 int hiscore_idx)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001508{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001509 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001510
1511 read_lock_bh(&idev->lock);
1512 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1513 int i;
1514
1515 /*
1516 * - Tentative Address (RFC2462 section 5.4)
1517 * - A tentative address is not considered
1518 * "assigned to an interface" in the traditional
1519 * sense, unless it is also flagged as optimistic.
1520 * - Candidate Source Address (section 4)
1521 * - In any case, anycast addresses, multicast
1522 * addresses, and the unspecified address MUST
1523 * NOT be included in a candidate set.
1524 */
1525 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1526 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1527 continue;
1528
1529 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1530
1531 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1532 score->addr_type & IPV6_ADDR_MULTICAST)) {
1533 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1534 idev->dev->name);
1535 continue;
1536 }
1537
1538 score->rule = -1;
1539 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1540
1541 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1542 int minihiscore, miniscore;
1543
1544 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1545 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1546
1547 if (minihiscore > miniscore) {
1548 if (i == IPV6_SADDR_RULE_SCOPE &&
1549 score->scopedist > 0) {
1550 /*
1551 * special case:
1552 * each remaining entry
1553 * has too small (not enough)
1554 * scope, because ifa entries
1555 * are sorted by their scope
1556 * values.
1557 */
1558 goto out;
1559 }
1560 break;
1561 } else if (minihiscore < miniscore) {
1562 if (hiscore->ifa)
1563 in6_ifa_put(hiscore->ifa);
1564
1565 in6_ifa_hold(score->ifa);
1566
1567 swap(hiscore, score);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001568 hiscore_idx = 1 - hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001569
1570 /* restore our iterator */
1571 score->ifa = hiscore->ifa;
1572
1573 break;
1574 }
1575 }
1576 }
1577out:
1578 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001579 return hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001580}
1581
David Ahernafbac6012016-06-16 16:24:26 -07001582static int ipv6_get_saddr_master(struct net *net,
1583 const struct net_device *dst_dev,
1584 const struct net_device *master,
1585 struct ipv6_saddr_dst *dst,
1586 struct ipv6_saddr_score *scores,
1587 int hiscore_idx)
1588{
1589 struct inet6_dev *idev;
1590
1591 idev = __in6_dev_get(dst_dev);
1592 if (idev)
1593 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1594 scores, hiscore_idx);
1595
1596 idev = __in6_dev_get(master);
1597 if (idev)
1598 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1599 scores, hiscore_idx);
1600
1601 return hiscore_idx;
1602}
1603
Patrick McHardyb3f644f2012-08-26 19:14:14 +02001604int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001605 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001606 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001608 struct ipv6_saddr_score scores[2], *hiscore;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001609 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001610 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001611 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001612 int dst_type;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001613 bool use_oif_addr = false;
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001614 int hiscore_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001616 dst_type = __ipv6_addr_type(daddr);
1617 dst.addr = daddr;
1618 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1619 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001620 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001621 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001622
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001623 scores[hiscore_idx].rule = -1;
1624 scores[hiscore_idx].ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001626 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001627
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001628 /* Candidate Source Address (section 4)
1629 * - multicast and link-local destination address,
1630 * the set of candidate source address MUST only
1631 * include addresses assigned to interfaces
1632 * belonging to the same link as the outgoing
1633 * interface.
1634 * (- For site-local destination addresses, the
1635 * set of candidate source addresses MUST only
1636 * include addresses assigned to interfaces
1637 * belonging to the same site as the outgoing
1638 * interface.)
Erik Kline3985e8a2015-07-22 16:38:25 +09001639 * - "It is RECOMMENDED that the candidate source addresses
1640 * be the set of unicast addresses assigned to the
1641 * interface that will be used to send to the destination
1642 * (the 'outgoing' interface)." (RFC 6724)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001643 */
1644 if (dst_dev) {
Erik Kline3985e8a2015-07-22 16:38:25 +09001645 idev = __in6_dev_get(dst_dev);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001646 if ((dst_type & IPV6_ADDR_MULTICAST) ||
Erik Kline3985e8a2015-07-22 16:38:25 +09001647 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1648 (idev && idev->cnf.use_oif_addrs_only)) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001649 use_oif_addr = true;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001650 }
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001651 }
1652
1653 if (use_oif_addr) {
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001654 if (idev)
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001655 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001656 } else {
David Ahernafbac6012016-06-16 16:24:26 -07001657 const struct net_device *master;
1658 int master_idx = 0;
1659
1660 /* if dst_dev exists and is enslaved to an L3 device, then
1661 * prefer addresses from dst_dev and then the master over
1662 * any other enslaved devices in the L3 domain.
1663 */
1664 master = l3mdev_master_dev_rcu(dst_dev);
1665 if (master) {
1666 master_idx = master->ifindex;
1667
1668 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1669 master, &dst,
1670 scores, hiscore_idx);
1671
1672 if (scores[hiscore_idx].ifa)
1673 goto out;
1674 }
1675
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001676 for_each_netdev_rcu(net, dev) {
David Ahernafbac6012016-06-16 16:24:26 -07001677 /* only consider addresses on devices in the
1678 * same L3 domain
1679 */
1680 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1681 continue;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001682 idev = __in6_dev_get(dev);
1683 if (!idev)
1684 continue;
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001685 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001686 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001687 }
David Ahernafbac6012016-06-16 16:24:26 -07001688
1689out:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001690 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001692 hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001693 if (!hiscore->ifa)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001694 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001695
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001696 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001697 in6_ifa_put(hiscore->ifa);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001698 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001700EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Amerigo Wang89657792013-06-29 21:30:49 +08001702int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001703 u32 banned_flags)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001704{
1705 struct inet6_ifaddr *ifp;
1706 int err = -EADDRNOTAVAIL;
1707
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01001708 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1709 if (ifp->scope > IFA_LINK)
1710 break;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001711 if (ifp->scope == IFA_LINK &&
1712 !(ifp->flags & banned_flags)) {
1713 *addr = ifp->addr;
1714 err = 0;
1715 break;
1716 }
1717 }
1718 return err;
1719}
1720
Neil Horman95c385b2007-04-25 17:08:10 -07001721int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001722 u32 banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
1724 struct inet6_dev *idev;
1725 int err = -EADDRNOTAVAIL;
1726
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001727 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001728 idev = __in6_dev_get(dev);
1729 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 read_lock_bh(&idev->lock);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001731 err = __ipv6_get_lladdr(idev, addr, banned_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 read_unlock_bh(&idev->lock);
1733 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001734 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 return err;
1736}
1737
1738static int ipv6_count_addresses(struct inet6_dev *idev)
1739{
1740 int cnt = 0;
1741 struct inet6_ifaddr *ifp;
1742
1743 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001744 list_for_each_entry(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 cnt++;
1746 read_unlock_bh(&idev->lock);
1747 return cnt;
1748}
1749
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001750int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Florian Westphal2a7851b2013-05-17 03:56:10 +00001751 const struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
Erik Klinec58da4c2015-02-04 20:01:23 +09001753 return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1754}
1755EXPORT_SYMBOL(ipv6_chk_addr);
1756
1757int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1758 const struct net_device *dev, int strict,
1759 u32 banned_flags)
1760{
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001761 struct inet6_ifaddr *ifp;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001762 unsigned int hash = inet6_addr_hash(addr);
Erik Klinec58da4c2015-02-04 20:01:23 +09001763 u32 ifp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
stephen hemminger5c578ae2010-03-17 20:31:11 +00001765 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001766 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001767 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001768 continue;
Erik Klinec58da4c2015-02-04 20:01:23 +09001769 /* Decouple optimistic from tentative for evaluation here.
1770 * Ban optimistic addresses explicitly, when required.
1771 */
1772 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1773 ? (ifp->flags&~IFA_F_TENTATIVE)
1774 : ifp->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 if (ipv6_addr_equal(&ifp->addr, addr) &&
Erik Klinec58da4c2015-02-04 20:01:23 +09001776 !(ifp_flags&banned_flags) &&
Ian Morris63159f22015-03-29 14:00:04 +01001777 (!dev || ifp->idev->dev == dev ||
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001778 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1779 rcu_read_unlock_bh();
1780 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 }
1782 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001783
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001784 rcu_read_unlock_bh();
1785 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
Erik Klinec58da4c2015-02-04 20:01:23 +09001787EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001788
David S. Miller3e81c6d2010-03-20 16:18:00 -07001789static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1790 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
Eric Dumazetddbe5032012-07-18 08:11:12 +00001792 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001793 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Sasha Levinb67bfe02013-02-27 17:06:00 -08001795 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001796 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001797 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001799 if (!dev || ifp->idev->dev == dev)
David S. Miller3e81c6d2010-03-20 16:18:00 -07001800 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
1802 }
David S. Miller3e81c6d2010-03-20 16:18:00 -07001803 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001806/* Compares an address/prefix_len with addresses on device @dev.
1807 * If one is found it returns true.
1808 */
1809bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1810 const unsigned int prefix_len, struct net_device *dev)
1811{
1812 struct inet6_dev *idev;
1813 struct inet6_ifaddr *ifa;
1814 bool ret = false;
1815
1816 rcu_read_lock();
1817 idev = __in6_dev_get(dev);
1818 if (idev) {
1819 read_lock_bh(&idev->lock);
1820 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1821 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1822 if (ret)
1823 break;
1824 }
1825 read_unlock_bh(&idev->lock);
1826 }
1827 rcu_read_unlock();
1828
1829 return ret;
1830}
1831EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1832
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001833int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001834{
1835 struct inet6_dev *idev;
1836 struct inet6_ifaddr *ifa;
1837 int onlink;
1838
1839 onlink = 0;
1840 rcu_read_lock();
1841 idev = __in6_dev_get(dev);
1842 if (idev) {
1843 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001844 list_for_each_entry(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001845 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1846 ifa->prefix_len);
1847 if (onlink)
1848 break;
1849 }
1850 read_unlock_bh(&idev->lock);
1851 }
1852 rcu_read_unlock();
1853 return onlink;
1854}
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001855EXPORT_SYMBOL(ipv6_chk_prefix);
1856
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001857struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001858 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
David S. Millerb79d1d52010-03-25 21:39:21 -07001860 struct inet6_ifaddr *ifp, *result = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001861 unsigned int hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
stephen hemminger5c578ae2010-03-17 20:31:11 +00001863 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001864 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001865 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001866 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001868 if (!dev || ifp->idev->dev == dev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07001870 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 in6_ifa_hold(ifp);
1872 break;
1873 }
1874 }
1875 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001876 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
David S. Millerb79d1d52010-03-25 21:39:21 -07001878 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881/* Gets referenced address, destroys ifaddr */
1882
Brian Haleycc411d02009-09-09 14:41:32 +00001883static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Lubomir Rintel3d171f32016-01-08 13:47:23 +01001885 if (dad_failed)
1886 ifp->flags |= IFA_F_DADFAILED;
1887
Sabrina Dubrocad2c95122017-06-29 16:56:54 +02001888 if (ifp->flags&IFA_F_TEMPORARY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 struct inet6_ifaddr *ifpub;
1890 spin_lock_bh(&ifp->lock);
1891 ifpub = ifp->ifpub;
1892 if (ifpub) {
1893 in6_ifa_hold(ifpub);
1894 spin_unlock_bh(&ifp->lock);
1895 ipv6_create_tempaddr(ifpub, ifp);
1896 in6_ifa_put(ifpub);
1897 } else {
1898 spin_unlock_bh(&ifp->lock);
1899 }
1900 ipv6_del_addr(ifp);
Sabrina Dubrocad2c95122017-06-29 16:56:54 +02001901 } else if (ifp->flags&IFA_F_PERMANENT || !dad_failed) {
1902 spin_lock_bh(&ifp->lock);
1903 addrconf_del_dad_work(ifp);
1904 ifp->flags |= IFA_F_TENTATIVE;
1905 spin_unlock_bh(&ifp->lock);
1906 if (dad_failed)
1907 ipv6_ifa_notify(0, ifp);
1908 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001909 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
1913
Herbert Xuf2344a12010-05-18 15:55:27 -07001914static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1915{
1916 int err = -ENOENT;
1917
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001918 spin_lock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001919 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1920 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1921 err = 0;
1922 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001923 spin_unlock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001924
1925 return err;
1926}
1927
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001928void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1929{
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001930 struct inet6_dev *idev = ifp->idev;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001931 struct net *net = dev_net(ifp->idev->dev);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001932
Ursula Braun853dc2e2010-10-24 23:06:43 +00001933 if (addrconf_dad_end(ifp)) {
1934 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07001935 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00001936 }
Herbert Xuf2344a12010-05-18 15:55:27 -07001937
Joe Perchese87cc472012-05-13 21:56:26 +00001938 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1939 ifp->idev->dev->name, &ifp->addr);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001940
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001941 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001942
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001943 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
1944 int scope = ifp->scope;
1945 u32 flags = ifp->flags;
1946 struct in6_addr new_addr;
1947 struct inet6_ifaddr *ifp2;
1948 u32 valid_lft, preferred_lft;
1949 int pfxlen = ifp->prefix_len;
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001950 int retries = ifp->stable_privacy_retry + 1;
1951
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001952 if (retries > net->ipv6.sysctl.idgen_retries) {
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001953 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
1954 ifp->idev->dev->name);
1955 goto errdad;
1956 }
1957
1958 new_addr = ifp->addr;
1959 if (ipv6_generate_stable_address(&new_addr, retries,
1960 idev))
1961 goto errdad;
1962
1963 valid_lft = ifp->valid_lft;
1964 preferred_lft = ifp->prefered_lft;
1965
1966 spin_unlock_bh(&ifp->lock);
1967
1968 if (idev->cnf.max_addresses &&
1969 ipv6_count_addresses(idev) >=
1970 idev->cnf.max_addresses)
1971 goto lock_errdad;
1972
1973 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
1974 ifp->idev->dev->name);
1975
1976 ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
1977 scope, flags, valid_lft,
1978 preferred_lft);
1979 if (IS_ERR(ifp2))
1980 goto lock_errdad;
1981
1982 spin_lock_bh(&ifp2->lock);
1983 ifp2->stable_privacy_retry = retries;
1984 ifp2->state = INET6_IFADDR_STATE_PREDAD;
1985 spin_unlock_bh(&ifp2->lock);
1986
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001987 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001988 in6_ifa_put(ifp2);
1989lock_errdad:
1990 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001991 }
1992
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001993errdad:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001994 /* transition from _POSTDAD to _ERRDAD */
1995 ifp->state = INET6_IFADDR_STATE_ERRDAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001996 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001997
1998 addrconf_mod_dad_work(ifp, 0);
Wei Yongjun751eb6b2016-09-05 16:06:31 +08001999 in6_ifa_put(ifp);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002000}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002002/* Join to solicited addr multicast group.
2003 * caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002004void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 struct in6_addr maddr;
2007
2008 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2009 return;
2010
2011 addrconf_addr_solict_mult(addr, &maddr);
2012 ipv6_dev_mc_inc(dev, &maddr);
2013}
2014
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002015/* caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002016void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
2018 struct in6_addr maddr;
2019
2020 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2021 return;
2022
2023 addrconf_addr_solict_mult(addr, &maddr);
2024 __ipv6_dev_mc_dec(idev, &maddr);
2025}
2026
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002027/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002028static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
2030 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002031
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002032 if (ifp->prefix_len >= 127) /* RFC 6164 */
Bjørn Mork2bda8a02011-07-05 23:04:13 +00002033 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2035 if (ipv6_addr_any(&addr))
2036 return;
WANG Cong013b4d92014-09-11 15:35:11 -07002037 __ipv6_dev_ac_inc(ifp->idev, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002040/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002041static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
2043 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002044
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002045 if (ifp->prefix_len >= 127) /* RFC 6164 */
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00002046 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2048 if (ipv6_addr_any(&addr))
2049 return;
2050 __ipv6_dev_ac_dec(ifp->idev, &addr);
2051}
2052
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002053static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
2054{
Alexander Aring5241c2d2015-12-14 20:55:22 +01002055 if (dev->addr_len != EUI64_ADDR_LEN)
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002056 return -1;
Alexander Aring5241c2d2015-12-14 20:55:22 +01002057 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
YOSHIFUJI Hideaki / 吉藤英明5e98a362013-01-28 10:44:29 +00002058 eui[0] ^= 2;
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002059 return 0;
2060}
2061
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002062static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2063{
2064 union fwnet_hwaddr *ha;
2065
2066 if (dev->addr_len != FWNET_ALEN)
2067 return -1;
2068
2069 ha = (union fwnet_hwaddr *)dev->dev_addr;
2070
2071 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2072 eui[0] ^= 2;
2073 return 0;
2074}
2075
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002076static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2077{
2078 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2079 if (dev->addr_len != ARCNET_ALEN)
2080 return -1;
2081 memset(eui, 0, 7);
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002082 eui[7] = *(u8 *)dev->dev_addr;
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002083 return 0;
2084}
2085
2086static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2087{
2088 if (dev->addr_len != INFINIBAND_ALEN)
2089 return -1;
2090 memcpy(eui, dev->dev_addr + 12, 8);
2091 eui[0] |= 2;
2092 return 0;
2093}
2094
stephen hemmingerc61393e2010-10-04 20:17:53 +00002095static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002096{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00002097 if (addr == 0)
2098 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002099 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2100 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2101 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2102 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2103 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2104 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2105 eui[1] = 0;
2106 eui[2] = 0x5E;
2107 eui[3] = 0xFE;
2108 memcpy(eui + 4, &addr, 4);
2109 return 0;
2110}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002111
2112static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2113{
2114 if (dev->priv_flags & IFF_ISATAP)
2115 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2116 return -1;
2117}
2118
stephen hemmingeraee80b52011-06-08 10:44:30 +00002119static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2120{
2121 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2122}
2123
Nicolas Dichtele8377352013-08-20 12:16:06 +02002124static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2125{
2126 memcpy(eui, dev->perm_addr, 3);
2127 memcpy(eui + 5, dev->perm_addr + 3, 3);
2128 eui[3] = 0xFF;
2129 eui[4] = 0xFE;
2130 eui[0] ^= 2;
2131 return 0;
2132}
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2135{
2136 switch (dev->type) {
2137 case ARPHRD_ETHER:
2138 case ARPHRD_FDDI:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002139 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002141 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002143 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002144 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002145 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002146 case ARPHRD_IPGRE:
2147 return addrconf_ifid_gre(eui, dev);
Jukka Rissanene74bccb2013-12-11 17:05:36 +02002148 case ARPHRD_6LOWPAN:
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002149 return addrconf_ifid_eui64(eui, dev);
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002150 case ARPHRD_IEEE1394:
2151 return addrconf_ifid_ieee1394(eui, dev);
Nicolas Dichtele8377352013-08-20 12:16:06 +02002152 case ARPHRD_TUNNEL6:
2153 return addrconf_ifid_ip6tnl(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 }
2155 return -1;
2156}
2157
2158static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2159{
2160 int err = -1;
2161 struct inet6_ifaddr *ifp;
2162
2163 read_lock_bh(&idev->lock);
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01002164 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2165 if (ifp->scope > IFA_LINK)
2166 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2168 memcpy(eui, ifp->addr.s6_addr+8, 8);
2169 err = 0;
2170 break;
2171 }
2172 }
2173 read_unlock_bh(&idev->lock);
2174 return err;
2175}
2176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002178static void ipv6_regen_rndid(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08002181 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184 /*
2185 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2186 * check if generated address is not inappropriate
2187 *
2188 * - Reserved subnet anycast (RFC 2526)
2189 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002190 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 * 00-00-5E-FE-xx-xx-xx-xx
2192 * - value 0
2193 * - XXX: already assigned to an address on the device
2194 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002195 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2197 (idev->rndid[7]&0x80))
2198 goto regen;
2199 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2200 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2201 goto regen;
2202 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2203 goto regen;
2204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205}
2206
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002207static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002208{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002210 ipv6_regen_rndid(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +09002213u32 addrconf_rt_table(const struct net_device *dev, u32 default_table) {
2214 /* Determines into what table to put autoconf PIO/RIO/default routes
2215 * learned on this device.
2216 *
2217 * - If 0, use the same table for every device. This puts routes into
2218 * one of RT_TABLE_{PREFIX,INFO,DFLT} depending on the type of route
2219 * (but note that these three are currently all equal to
2220 * RT6_TABLE_MAIN).
2221 * - If > 0, use the specified table.
2222 * - If < 0, put routes into table dev->ifindex + (-rt_table).
2223 */
2224 struct inet6_dev *idev = in6_dev_get(dev);
2225 u32 table;
2226 int sysctl = idev->cnf.accept_ra_rt_table;
2227 if (sysctl == 0) {
2228 table = default_table;
2229 } else if (sysctl > 0) {
2230 table = (u32) sysctl;
2231 } else {
2232 table = (unsigned) dev->ifindex + (-sysctl);
2233 }
2234 in6_dev_put(idev);
2235 return table;
2236}
2237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238/*
2239 * Add prefix route.
2240 */
2241
2242static void
2243addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002244 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Thomas Graf86872cb2006-08-22 00:01:08 -07002246 struct fib6_config cfg = {
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +09002247 .fc_table = l3mdev_fib_table(dev) ? : addrconf_rt_table(dev, RT6_TABLE_PREFIX),
Thomas Graf86872cb2006-08-22 00:01:08 -07002248 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2249 .fc_ifindex = dev->ifindex,
2250 .fc_expires = expires,
2251 .fc_dst_len = plen,
2252 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002253 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07002254 .fc_protocol = RTPROT_KERNEL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002255 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002257 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
2259 /* Prevent useless cloning on PtP SIT.
2260 This thing is done here expecting that the whole
2261 class of non-broadcast devices need not cloning.
2262 */
Amerigo Wang07a93622012-10-29 16:23:10 +00002263#if IS_ENABLED(CONFIG_IPV6_SIT)
Thomas Graf86872cb2006-08-22 00:01:08 -07002264 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2265 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002266#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Thomas Graf86872cb2006-08-22 00:01:08 -07002268 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269}
2270
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002271
2272static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2273 int plen,
2274 const struct net_device *dev,
2275 u32 flags, u32 noflags)
2276{
2277 struct fib6_node *fn;
2278 struct rt6_info *rt = NULL;
2279 struct fib6_table *table;
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +09002280 u32 tb_id = l3mdev_fib_table(dev) ? : addrconf_rt_table(dev, RT6_TABLE_PREFIX);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002281
David Ahernca254492015-10-12 11:47:10 -07002282 table = fib6_get_table(dev_net(dev), tb_id);
Ian Morris63159f22015-03-29 14:00:04 +01002283 if (!table)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002284 return NULL;
2285
Li RongQing5744dd92012-09-11 21:59:01 +00002286 read_lock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002287 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2288 if (!fn)
2289 goto out;
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07002290
2291 noflags |= RTF_CACHE;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002292 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002293 if (rt->dst.dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002294 continue;
2295 if ((rt->rt6i_flags & flags) != flags)
2296 continue;
Romain Kuntz85da53b2013-01-09 15:02:26 +01002297 if ((rt->rt6i_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002298 continue;
2299 dst_hold(&rt->dst);
2300 break;
2301 }
2302out:
Li RongQing5744dd92012-09-11 21:59:01 +00002303 read_unlock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002304 return rt;
2305}
2306
2307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308/* Create "default" multicast route to the interface */
2309
2310static void addrconf_add_mroute(struct net_device *dev)
2311{
Thomas Graf86872cb2006-08-22 00:01:08 -07002312 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002313 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002314 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2315 .fc_ifindex = dev->ifindex,
2316 .fc_dst_len = 8,
2317 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002318 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002319 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Thomas Graf86872cb2006-08-22 00:01:08 -07002321 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2322
2323 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324}
2325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2327{
2328 struct inet6_dev *idev;
2329
2330 ASSERT_RTNL();
2331
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002332 idev = ipv6_find_idev(dev);
2333 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00002334 return ERR_PTR(-ENOBUFS);
2335
2336 if (idev->cnf.disable_ipv6)
2337 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339 /* Add default multicast route */
David Ahernba46ee42016-06-13 13:44:18 -07002340 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
Li Wei4af04ab2011-12-06 21:23:45 +00002341 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return idev;
2344}
2345
Jiri Pirko53bd6742013-12-06 09:45:22 +01002346static void manage_tempaddrs(struct inet6_dev *idev,
2347 struct inet6_ifaddr *ifp,
2348 __u32 valid_lft, __u32 prefered_lft,
2349 bool create, unsigned long now)
2350{
2351 u32 flags;
2352 struct inet6_ifaddr *ift;
2353
2354 read_lock_bh(&idev->lock);
2355 /* update all temporary addresses in the list */
2356 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2357 int age, max_valid, max_prefered;
2358
2359 if (ifp != ift->ifpub)
2360 continue;
2361
2362 /* RFC 4941 section 3.3:
2363 * If a received option will extend the lifetime of a public
2364 * address, the lifetimes of temporary addresses should
2365 * be extended, subject to the overall constraint that no
2366 * temporary addresses should ever remain "valid" or "preferred"
2367 * for a time longer than (TEMP_VALID_LIFETIME) or
2368 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2369 */
2370 age = (now - ift->cstamp) / HZ;
2371 max_valid = idev->cnf.temp_valid_lft - age;
2372 if (max_valid < 0)
2373 max_valid = 0;
2374
2375 max_prefered = idev->cnf.temp_prefered_lft -
Jiri Bohac76506a92016-10-13 18:52:15 +02002376 idev->desync_factor - age;
Jiri Pirko53bd6742013-12-06 09:45:22 +01002377 if (max_prefered < 0)
2378 max_prefered = 0;
2379
2380 if (valid_lft > max_valid)
2381 valid_lft = max_valid;
2382
2383 if (prefered_lft > max_prefered)
2384 prefered_lft = max_prefered;
2385
2386 spin_lock(&ift->lock);
2387 flags = ift->flags;
2388 ift->valid_lft = valid_lft;
2389 ift->prefered_lft = prefered_lft;
2390 ift->tstamp = now;
2391 if (prefered_lft > 0)
2392 ift->flags &= ~IFA_F_DEPRECATED;
2393
2394 spin_unlock(&ift->lock);
2395 if (!(flags&IFA_F_TENTATIVE))
2396 ipv6_ifa_notify(0, ift);
2397 }
2398
2399 if ((create || list_empty(&idev->tempaddr_list)) &&
2400 idev->cnf.use_tempaddr > 0) {
2401 /* When a new public address is created as described
2402 * in [ADDRCONF], also create a new temporary address.
2403 * Also create a temporary address if it's enabled but
2404 * no temporary address currently exists.
2405 */
2406 read_unlock_bh(&idev->lock);
2407 ipv6_create_tempaddr(ifp, NULL);
2408 } else {
2409 read_unlock_bh(&idev->lock);
2410 }
2411}
2412
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002413static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2414{
Felix Jiad1f8a9b2017-01-26 16:59:17 +13002415 return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2416 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002417}
2418
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002419int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2420 const struct prefix_info *pinfo,
2421 struct inet6_dev *in6_dev,
2422 const struct in6_addr *addr, int addr_type,
2423 u32 addr_flags, bool sllao, bool tokenized,
2424 __u32 valid_lft, u32 prefered_lft)
Alexander Aring4f672232016-06-15 21:20:22 +02002425{
2426 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2427 int create = 0, update_lft = 0;
2428
2429 if (!ifp && valid_lft) {
2430 int max_addresses = in6_dev->cnf.max_addresses;
2431
2432#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2433 if (in6_dev->cnf.optimistic_dad &&
2434 !net->ipv6.devconf_all->forwarding && sllao)
2435 addr_flags |= IFA_F_OPTIMISTIC;
2436#endif
2437
2438 /* Do not allow to create too much of autoconfigured
2439 * addresses; this would be too easy way to crash kernel.
2440 */
2441 if (!max_addresses ||
2442 ipv6_count_addresses(in6_dev) < max_addresses)
2443 ifp = ipv6_add_addr(in6_dev, addr, NULL,
2444 pinfo->prefix_len,
2445 addr_type&IPV6_ADDR_SCOPE_MASK,
2446 addr_flags, valid_lft,
2447 prefered_lft);
2448
2449 if (IS_ERR_OR_NULL(ifp))
2450 return -1;
2451
2452 update_lft = 0;
2453 create = 1;
2454 spin_lock_bh(&ifp->lock);
2455 ifp->flags |= IFA_F_MANAGETEMPADDR;
2456 ifp->cstamp = jiffies;
2457 ifp->tokenized = tokenized;
2458 spin_unlock_bh(&ifp->lock);
2459 addrconf_dad_start(ifp);
2460 }
2461
2462 if (ifp) {
2463 u32 flags;
2464 unsigned long now;
2465 u32 stored_lft;
2466
2467 /* update lifetime (RFC2462 5.5.3 e) */
2468 spin_lock_bh(&ifp->lock);
2469 now = jiffies;
2470 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2471 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2472 else
2473 stored_lft = 0;
2474 if (!update_lft && !create && stored_lft) {
2475 const u32 minimum_lft = min_t(u32,
2476 stored_lft, MIN_VALID_LIFETIME);
2477 valid_lft = max(valid_lft, minimum_lft);
2478
2479 /* RFC4862 Section 5.5.3e:
2480 * "Note that the preferred lifetime of the
2481 * corresponding address is always reset to
2482 * the Preferred Lifetime in the received
2483 * Prefix Information option, regardless of
2484 * whether the valid lifetime is also reset or
2485 * ignored."
2486 *
2487 * So we should always update prefered_lft here.
2488 */
2489 update_lft = 1;
2490 }
2491
2492 if (update_lft) {
2493 ifp->valid_lft = valid_lft;
2494 ifp->prefered_lft = prefered_lft;
2495 ifp->tstamp = now;
2496 flags = ifp->flags;
2497 ifp->flags &= ~IFA_F_DEPRECATED;
2498 spin_unlock_bh(&ifp->lock);
2499
2500 if (!(flags&IFA_F_TENTATIVE))
2501 ipv6_ifa_notify(0, ifp);
2502 } else
2503 spin_unlock_bh(&ifp->lock);
2504
2505 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2506 create, now);
2507
2508 in6_ifa_put(ifp);
2509 addrconf_verify();
2510 }
2511
2512 return 0;
2513}
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002514EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
Alexander Aring4f672232016-06-15 21:20:22 +02002515
Neil Hormane6bff992012-01-04 10:49:15 +00002516void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517{
2518 struct prefix_info *pinfo;
2519 __u32 valid_lft;
2520 __u32 prefered_lft;
Alexander Aring4f672232016-06-15 21:20:22 +02002521 int addr_type, err;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002522 u32 addr_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07002524 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
2526 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002527
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 if (len < sizeof(struct prefix_info)) {
dingtianhongba3542e2013-08-17 10:27:04 +08002529 ADBG("addrconf: prefix option too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return;
2531 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 /*
2534 * Validation checks ([ADDRCONF], page 19)
2535 */
2536
2537 addr_type = ipv6_addr_type(&pinfo->prefix);
2538
2539 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2540 return;
2541
2542 valid_lft = ntohl(pinfo->valid);
2543 prefered_lft = ntohl(pinfo->prefered);
2544
2545 if (prefered_lft > valid_lft) {
Joe Perchese87cc472012-05-13 21:56:26 +00002546 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 return;
2548 }
2549
2550 in6_dev = in6_dev_get(dev);
2551
Ian Morris63159f22015-03-29 14:00:04 +01002552 if (!in6_dev) {
Joe Perchese87cc472012-05-13 21:56:26 +00002553 net_dbg_ratelimited("addrconf: device %s not configured\n",
2554 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 return;
2556 }
2557
2558 /*
2559 * Two things going on here:
2560 * 1) Add routes for on-link prefixes
2561 * 2) Configure prefixes with the auto flag set
2562 */
2563
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002564 if (pinfo->onlink) {
2565 struct rt6_info *rt;
2566 unsigned long rt_expires;
2567
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002568 /* Avoid arithmetic overflow. Really, we could
2569 * save rt_expires in seconds, likely valid_lft,
2570 * but it would require division in fib gc, that it
2571 * not good.
2572 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002573 if (HZ > USER_HZ)
2574 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2575 else
2576 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08002577
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002578 if (addrconf_finite_timeout(rt_expires))
2579 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002581 rt = addrconf_get_prefix_route(&pinfo->prefix,
2582 pinfo->prefix_len,
2583 dev,
2584 RTF_ADDRCONF | RTF_PREFIX_RT,
2585 RTF_GATEWAY | RTF_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002587 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002588 /* Autoconf prefix route */
2589 if (valid_lft == 0) {
2590 ip6_del_rt(rt);
2591 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002592 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002593 /* not infinity */
Gao feng1716a962012-04-06 00:13:10 +00002594 rt6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002595 } else {
Gao feng1716a962012-04-06 00:13:10 +00002596 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 }
2598 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002599 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002600 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2601 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002602 /* not infinity */
2603 flags |= RTF_EXPIRES;
2604 expires = jiffies_to_clock_t(rt_expires);
2605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002607 dev, expires, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00002609 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 }
2611
2612 /* Try to figure out our local address for this prefix */
2613
2614 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 struct in6_addr addr;
Alexander Aringf997c552016-06-15 21:20:23 +02002616 bool tokenized = false, dev_addr_generated = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
2618 if (pinfo->prefix_len == 64) {
2619 memcpy(&addr, &pinfo->prefix, 8);
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002620
2621 if (!ipv6_addr_any(&in6_dev->token)) {
2622 read_lock_bh(&in6_dev->lock);
2623 memcpy(addr.s6_addr + 8,
2624 in6_dev->token.s6_addr + 8, 8);
2625 read_unlock_bh(&in6_dev->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00002626 tokenized = true;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002627 } else if (is_addr_mode_generate_stable(in6_dev) &&
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002628 !ipv6_generate_stable_address(&addr, 0,
2629 in6_dev)) {
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002630 addr_flags |= IFA_F_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002631 goto ok;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002632 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2633 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
Alexander Aring4f672232016-06-15 21:20:22 +02002634 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002635 } else {
2636 dev_addr_generated = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 }
2638 goto ok;
2639 }
Joe Perchese87cc472012-05-13 21:56:26 +00002640 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2641 pinfo->prefix_len);
Alexander Aring4f672232016-06-15 21:20:22 +02002642 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
2644ok:
Alexander Aring4f672232016-06-15 21:20:22 +02002645 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2646 &addr, addr_type,
2647 addr_flags, sllao,
2648 tokenized, valid_lft,
2649 prefered_lft);
2650 if (err)
2651 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002652
2653 /* Ignore error case here because previous prefix add addr was
2654 * successful which will be notified.
2655 */
2656 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2657 addr_type, addr_flags, sllao,
2658 tokenized, valid_lft,
2659 prefered_lft,
2660 dev_addr_generated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 }
2662 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
Alexander Aring4f672232016-06-15 21:20:22 +02002663put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 in6_dev_put(in6_dev);
2665}
2666
2667/*
2668 * Set destination address.
2669 * Special case for SIT interfaces where we create a new "virtual"
2670 * device.
2671 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002672int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
2674 struct in6_ifreq ireq;
2675 struct net_device *dev;
2676 int err = -EINVAL;
2677
2678 rtnl_lock();
2679
2680 err = -EFAULT;
2681 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2682 goto err_exit;
2683
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002684 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
2686 err = -ENODEV;
Ian Morris63159f22015-03-29 14:00:04 +01002687 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 goto err_exit;
2689
Amerigo Wang07a93622012-10-29 16:23:10 +00002690#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002692 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 struct ip_tunnel_parm p;
2695
2696 err = -EADDRNOTAVAIL;
2697 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2698 goto err_exit;
2699
2700 memset(&p, 0, sizeof(p));
2701 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2702 p.iph.saddr = 0;
2703 p.iph.version = 4;
2704 p.iph.ihl = 5;
2705 p.iph.protocol = IPPROTO_IPV6;
2706 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002707 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002709 if (ops->ndo_do_ioctl) {
2710 mm_segment_t oldfs = get_fs();
2711
2712 set_fs(KERNEL_DS);
2713 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2714 set_fs(oldfs);
2715 } else
2716 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718 if (err == 0) {
2719 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002720 dev = __dev_get_by_name(net, p.name);
2721 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 goto err_exit;
2723 err = dev_open(dev);
2724 }
2725 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002726#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
2728err_exit:
2729 rtnl_unlock();
2730 return err;
2731}
2732
Madhu Challa93a714d2015-02-25 09:58:35 -08002733static int ipv6_mc_config(struct sock *sk, bool join,
2734 const struct in6_addr *addr, int ifindex)
2735{
2736 int ret;
2737
2738 ASSERT_RTNL();
2739
2740 lock_sock(sk);
2741 if (join)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002742 ret = ipv6_sock_mc_join(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002743 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002744 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002745 release_sock(sk);
2746
2747 return ret;
2748}
2749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750/*
2751 * Manual configuration of address on an interface
2752 */
Jiri Pirko479840f2013-12-06 09:45:21 +01002753static int inet6_addr_add(struct net *net, int ifindex,
2754 const struct in6_addr *pfx,
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002755 const struct in6_addr *peer_pfx,
Jiri Pirko479840f2013-12-06 09:45:21 +01002756 unsigned int plen, __u32 ifa_flags,
2757 __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
2759 struct inet6_ifaddr *ifp;
2760 struct inet6_dev *idev;
2761 struct net_device *dev;
Madhu Challa93a714d2015-02-25 09:58:35 -08002762 unsigned long timeout;
2763 clock_t expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002765 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
2767 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002768
Thomas Graf24ef0da2008-05-28 16:54:22 +02002769 if (plen > 128)
2770 return -EINVAL;
2771
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002772 /* check the lifetime */
2773 if (!valid_lft || prefered_lft > valid_lft)
2774 return -EINVAL;
2775
Jiri Pirko53bd6742013-12-06 09:45:22 +01002776 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2777 return -EINVAL;
2778
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002779 dev = __dev_get_by_index(net, ifindex);
2780 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002782
Brian Haley64e724f2010-07-20 10:34:30 +00002783 idev = addrconf_add_dev(dev);
2784 if (IS_ERR(idev))
2785 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Madhu Challa93a714d2015-02-25 09:58:35 -08002787 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2788 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2789 true, pfx, ifindex);
2790
2791 if (ret < 0)
2792 return ret;
2793 }
2794
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 scope = ipv6_addr_scope(pfx);
2796
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002797 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2798 if (addrconf_finite_timeout(timeout)) {
2799 expires = jiffies_to_clock_t(timeout * HZ);
2800 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002801 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002802 } else {
2803 expires = 0;
2804 flags = 0;
2805 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002806 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002807
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002808 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2809 if (addrconf_finite_timeout(timeout)) {
2810 if (timeout == 0)
2811 ifa_flags |= IFA_F_DEPRECATED;
2812 prefered_lft = timeout;
2813 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002814
Jiri Benc8a226b22013-08-01 10:41:28 +02002815 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2816 valid_lft, prefered_lft);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002817
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 if (!IS_ERR(ifp)) {
Thomas Haller761aac72014-01-15 15:36:58 +01002819 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2820 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2821 expires, flags);
2822 }
2823
Neil Horman95c385b2007-04-25 17:08:10 -07002824 /*
2825 * Note that section 3.1 of RFC 4429 indicates
2826 * that the Optimistic flag should not be set for
2827 * manually configured addresses
2828 */
David S. Millercf22f9a2012-04-14 21:37:40 -04002829 addrconf_dad_start(ifp);
Jiri Pirko53bd6742013-12-06 09:45:22 +01002830 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2831 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2832 true, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002834 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 return 0;
Madhu Challa93a714d2015-02-25 09:58:35 -08002836 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2837 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2838 false, pfx, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 }
2840
2841 return PTR_ERR(ifp);
2842}
2843
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002844static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2845 const struct in6_addr *pfx, unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846{
2847 struct inet6_ifaddr *ifp;
2848 struct inet6_dev *idev;
2849 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002850
Thomas Graf24ef0da2008-05-28 16:54:22 +02002851 if (plen > 128)
2852 return -EINVAL;
2853
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002854 dev = __dev_get_by_index(net, ifindex);
2855 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 return -ENODEV;
2857
Ian Morrise5d08d72014-11-23 21:28:43 +00002858 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01002859 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return -ENXIO;
2861
2862 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002863 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 if (ifp->prefix_len == plen &&
2865 ipv6_addr_equal(pfx, &ifp->addr)) {
2866 in6_ifa_hold(ifp);
2867 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002868
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002869 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2870 (ifa_flags & IFA_F_MANAGETEMPADDR))
2871 manage_tempaddrs(idev, ifp, 0, 0, false,
2872 jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 ipv6_del_addr(ifp);
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002874 addrconf_verify_rtnl();
Madhu Challa93a714d2015-02-25 09:58:35 -08002875 if (ipv6_addr_is_multicast(pfx)) {
2876 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2877 false, pfx, dev->ifindex);
2878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 return 0;
2880 }
2881 }
2882 read_unlock_bh(&idev->lock);
2883 return -EADDRNOTAVAIL;
2884}
2885
2886
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002887int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888{
2889 struct in6_ifreq ireq;
2890 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002891
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002892 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002894
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2896 return -EFAULT;
2897
2898 rtnl_lock();
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002899 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002900 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2901 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 rtnl_unlock();
2903 return err;
2904}
2905
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002906int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
2908 struct in6_ifreq ireq;
2909 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002910
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002911 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 return -EPERM;
2913
2914 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2915 return -EFAULT;
2916
2917 rtnl_lock();
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002918 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002919 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 rtnl_unlock();
2921 return err;
2922}
2923
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002924static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2925 int plen, int scope)
2926{
2927 struct inet6_ifaddr *ifp;
2928
Jiri Benc8a226b22013-08-01 10:41:28 +02002929 ifp = ipv6_add_addr(idev, addr, NULL, plen,
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01002930 scope, IFA_F_PERMANENT,
2931 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002932 if (!IS_ERR(ifp)) {
2933 spin_lock_bh(&ifp->lock);
2934 ifp->flags &= ~IFA_F_TENTATIVE;
2935 spin_unlock_bh(&ifp->lock);
Paolo Abeni764d3be2016-11-22 16:57:40 +01002936 rt_genid_bump_ipv6(dev_net(idev->dev));
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002937 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2938 in6_ifa_put(ifp);
2939 }
2940}
2941
Amerigo Wang07a93622012-10-29 16:23:10 +00002942#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943static void sit_add_v4_addrs(struct inet6_dev *idev)
2944{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 struct in6_addr addr;
2946 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002947 struct net *net = dev_net(idev->dev);
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002948 int scope, plen;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002949 u32 pflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
2951 ASSERT_RTNL();
2952
2953 memset(&addr, 0, sizeof(struct in6_addr));
2954 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2955
2956 if (idev->dev->flags&IFF_POINTOPOINT) {
2957 addr.s6_addr32[0] = htonl(0xfe800000);
2958 scope = IFA_LINK;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002959 plen = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 } else {
2961 scope = IPV6_ADDR_COMPATv4;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002962 plen = 96;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002963 pflags |= RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 }
2965
2966 if (addr.s6_addr32[3]) {
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002967 add_addr(idev, &addr, plen, scope);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002968 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 return;
2970 }
2971
Benjamin Thery6fda7352008-03-05 10:47:47 -08002972 for_each_netdev(net, dev) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002973 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 if (in_dev && (dev->flags & IFF_UP)) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002975 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
2977 int flag = scope;
2978
2979 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
2981 addr.s6_addr32[3] = ifa->ifa_local;
2982
2983 if (ifa->ifa_scope == RT_SCOPE_LINK)
2984 continue;
2985 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2986 if (idev->dev->flags&IFF_POINTOPOINT)
2987 continue;
2988 flag |= IFA_HOST;
2989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002991 add_addr(idev, &addr, plen, flag);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002992 addrconf_prefix_route(&addr, plen, idev->dev, 0,
2993 pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 }
2995 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002998#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
3000static void init_loopback(struct net_device *dev)
3001{
3002 struct inet6_dev *idev;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303003 struct net_device *sp_dev;
3004 struct inet6_ifaddr *sp_ifa;
3005 struct rt6_info *sp_rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
3007 /* ::1 */
3008
3009 ASSERT_RTNL();
3010
Ian Morrise5d08d72014-11-23 21:28:43 +00003011 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003012 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003013 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 return;
3015 }
3016
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003017 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303018
3019 /* Add routes to other interface's IPv6 addresses */
3020 for_each_netdev(dev_net(dev), sp_dev) {
3021 if (!strcmp(sp_dev->name, dev->name))
3022 continue;
3023
3024 idev = __in6_dev_get(sp_dev);
3025 if (!idev)
3026 continue;
3027
3028 read_lock_bh(&idev->lock);
3029 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
3030
3031 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
3032 continue;
3033
Gao feng33d99112014-01-24 16:29:11 +08003034 if (sp_ifa->rt) {
3035 /* This dst has been added to garbage list when
3036 * lo device down, release this obsolete dst and
3037 * reallocate a new router for ifa.
3038 */
Nicolas Dichtela2204452016-10-12 10:10:40 +02003039 if (!atomic_read(&sp_ifa->rt->rt6i_ref)) {
Gao feng33d99112014-01-24 16:29:11 +08003040 ip6_rt_put(sp_ifa->rt);
3041 sp_ifa->rt = NULL;
3042 } else {
3043 continue;
3044 }
3045 }
Gao fenga881ae12013-06-16 11:14:30 +08003046
François-Xavier Le Bail57ec0af2013-12-02 11:28:49 +01003047 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303048
3049 /* Failure cases are ignored */
Gao feng534c8772013-06-02 22:16:21 +00003050 if (!IS_ERR(sp_rt)) {
3051 sp_ifa->rt = sp_rt;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303052 ip6_ins_rt(sp_rt);
Gao feng534c8772013-06-02 22:16:21 +00003053 }
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303054 }
3055 read_unlock_bh(&idev->lock);
3056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057}
3058
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003059void addrconf_add_linklocal(struct inet6_dev *idev,
3060 const struct in6_addr *addr, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003062 struct inet6_ifaddr *ifp;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003063 u32 addr_flags = flags | IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
Neil Horman95c385b2007-04-25 17:08:10 -07003065#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3066 if (idev->cnf.optimistic_dad &&
David Miller702beb82008-07-20 18:17:02 -07003067 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
Neil Horman95c385b2007-04-25 17:08:10 -07003068 addr_flags |= IFA_F_OPTIMISTIC;
3069#endif
3070
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01003071 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
3072 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003074 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
David S. Millercf22f9a2012-04-14 21:37:40 -04003075 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 in6_ifa_put(ifp);
3077 }
3078}
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003079EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003081static bool ipv6_reserved_interfaceid(struct in6_addr address)
3082{
3083 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3084 return true;
3085
3086 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3087 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3088 return true;
3089
3090 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3091 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3092 return true;
3093
3094 return false;
3095}
3096
3097static int ipv6_generate_stable_address(struct in6_addr *address,
3098 u8 dad_count,
3099 const struct inet6_dev *idev)
3100{
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003101 static DEFINE_SPINLOCK(lock);
3102 static __u32 digest[SHA_DIGEST_WORDS];
3103 static __u32 workspace[SHA_WORKSPACE_WORDS];
3104
3105 static union {
3106 char __data[SHA_MESSAGE_BYTES];
3107 struct {
3108 struct in6_addr secret;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003109 __be32 prefix[2];
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003110 unsigned char hwaddr[MAX_ADDR_LEN];
3111 u8 dad_count;
3112 } __packed;
3113 } data;
3114
3115 struct in6_addr secret;
3116 struct in6_addr temp;
3117 struct net *net = dev_net(idev->dev);
3118
3119 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3120
3121 if (idev->cnf.stable_secret.initialized)
3122 secret = idev->cnf.stable_secret.secret;
3123 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3124 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3125 else
3126 return -1;
3127
3128retry:
3129 spin_lock_bh(&lock);
3130
3131 sha_init(digest);
3132 memset(&data, 0, sizeof(data));
3133 memset(workspace, 0, sizeof(workspace));
3134 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003135 data.prefix[0] = address->s6_addr32[0];
3136 data.prefix[1] = address->s6_addr32[1];
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003137 data.secret = secret;
3138 data.dad_count = dad_count;
3139
3140 sha_transform(digest, data.__data, workspace);
3141
3142 temp = *address;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003143 temp.s6_addr32[2] = (__force __be32)digest[0];
3144 temp.s6_addr32[3] = (__force __be32)digest[1];
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003145
3146 spin_unlock_bh(&lock);
3147
3148 if (ipv6_reserved_interfaceid(temp)) {
3149 dad_count++;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01003150 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003151 return -1;
3152 goto retry;
3153 }
3154
3155 *address = temp;
3156 return 0;
3157}
3158
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003159static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3160{
3161 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3162
3163 if (s->initialized)
3164 return;
3165 s = &idev->cnf.stable_secret;
3166 get_random_bytes(&s->secret, sizeof(s->secret));
3167 s->initialized = true;
3168}
3169
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003170static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3171{
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003172 struct in6_addr addr;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003173
David Ahernca254492015-10-12 11:47:10 -07003174 /* no link local addresses on L3 master devices */
3175 if (netif_is_l3_master(idev->dev))
3176 return;
3177
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003178 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3179
Felix Jiad1f8a9b2017-01-26 16:59:17 +13003180 switch (idev->cnf.addr_gen_mode) {
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003181 case IN6_ADDR_GEN_MODE_RANDOM:
3182 ipv6_gen_mode_random_init(idev);
3183 /* fallthrough */
3184 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003185 if (!ipv6_generate_stable_address(&addr, 0, idev))
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003186 addrconf_add_linklocal(idev, &addr,
3187 IFA_F_STABLE_PRIVACY);
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003188 else if (prefix_route)
3189 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003190 break;
3191 case IN6_ADDR_GEN_MODE_EUI64:
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003192 /* addrconf_add_linklocal also adds a prefix_route and we
3193 * only need to care about prefix routes if ipv6_generate_eui64
3194 * couldn't generate one.
3195 */
3196 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003197 addrconf_add_linklocal(idev, &addr, 0);
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003198 else if (prefix_route)
3199 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003200 break;
3201 case IN6_ADDR_GEN_MODE_NONE:
3202 default:
3203 /* will not add any link local address */
3204 break;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003205 }
3206}
3207
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208static void addrconf_dev_config(struct net_device *dev)
3209{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003210 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
3212 ASSERT_RTNL();
3213
Herbert Xu74235a22007-06-14 13:02:55 -07003214 if ((dev->type != ARPHRD_ETHER) &&
3215 (dev->type != ARPHRD_FDDI) &&
Herbert Xu74235a22007-06-14 13:02:55 -07003216 (dev->type != ARPHRD_ARCNET) &&
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00003217 (dev->type != ARPHRD_INFINIBAND) &&
Nicolas Dichtele8377352013-08-20 12:16:06 +02003218 (dev->type != ARPHRD_IEEE1394) &&
Jukka Rissanene74bccb2013-12-11 17:05:36 +02003219 (dev->type != ARPHRD_TUNNEL6) &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003220 (dev->type != ARPHRD_6LOWPAN) &&
3221 (dev->type != ARPHRD_NONE)) {
Herbert Xu74235a22007-06-14 13:02:55 -07003222 /* Alas, we support only Ethernet autoconfiguration. */
3223 return;
3224 }
3225
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00003227 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 return;
3229
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003230 /* this device type has no EUI support */
3231 if (dev->type == ARPHRD_NONE &&
Felix Jiad1f8a9b2017-01-26 16:59:17 +13003232 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3233 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003234
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003235 addrconf_addr_gen(idev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236}
3237
Amerigo Wang07a93622012-10-29 16:23:10 +00003238#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239static void addrconf_sit_config(struct net_device *dev)
3240{
3241 struct inet6_dev *idev;
3242
3243 ASSERT_RTNL();
3244
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003245 /*
3246 * Configure the tunnel with one of our IPv4
3247 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 * our v4 addrs in the tunnel
3249 */
3250
Ian Morrise5d08d72014-11-23 21:28:43 +00003251 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003252 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003253 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 return;
3255 }
3256
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003257 if (dev->priv_flags & IFF_ISATAP) {
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003258 addrconf_addr_gen(idev, false);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003259 return;
3260 }
3261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 sit_add_v4_addrs(idev);
3263
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00003264 if (dev->flags&IFF_POINTOPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003267#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Amerigo Wang07a93622012-10-29 16:23:10 +00003269#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003270static void addrconf_gre_config(struct net_device *dev)
3271{
3272 struct inet6_dev *idev;
stephen hemmingeraee80b52011-06-08 10:44:30 +00003273
stephen hemmingeraee80b52011-06-08 10:44:30 +00003274 ASSERT_RTNL();
3275
Ian Morrise5d08d72014-11-23 21:28:43 +00003276 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003277 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003278 pr_debug("%s: add_dev failed\n", __func__);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003279 return;
3280 }
3281
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003282 addrconf_addr_gen(idev, true);
Hannes Frederic Sowad9e4ce62015-10-08 18:19:39 +02003283 if (dev->flags & IFF_POINTOPOINT)
3284 addrconf_add_mroute(dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003285}
3286#endif
3287
David Ahernf1705ec2016-02-24 09:25:37 -08003288static int fixup_permanent_addr(struct inet6_dev *idev,
3289 struct inet6_ifaddr *ifp)
3290{
David Ahern62817c32017-04-25 09:17:29 -07003291 /* rt6i_ref == 0 means the host route was removed from the
3292 * FIB, for example, if 'lo' device is taken down. In that
3293 * case regenerate the host route.
3294 */
3295 if (!ifp->rt || !atomic_read(&ifp->rt->rt6i_ref)) {
3296 struct rt6_info *rt, *prev;
David Ahernf1705ec2016-02-24 09:25:37 -08003297
3298 rt = addrconf_dst_alloc(idev, &ifp->addr, false);
3299 if (unlikely(IS_ERR(rt)))
3300 return PTR_ERR(rt);
3301
David Ahern62817c32017-04-25 09:17:29 -07003302 /* ifp->rt can be accessed outside of rtnl */
3303 spin_lock(&ifp->lock);
3304 prev = ifp->rt;
David Ahernf1705ec2016-02-24 09:25:37 -08003305 ifp->rt = rt;
David Ahern62817c32017-04-25 09:17:29 -07003306 spin_unlock(&ifp->lock);
3307
3308 ip6_rt_put(prev);
David Ahernf1705ec2016-02-24 09:25:37 -08003309 }
3310
3311 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3312 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3313 idev->dev, 0, 0);
3314 }
3315
David Ahern0f4ac292017-05-02 14:43:44 -07003316 if (ifp->state == INET6_IFADDR_STATE_PREDAD)
3317 addrconf_dad_start(ifp);
David Ahernf1705ec2016-02-24 09:25:37 -08003318
3319 return 0;
3320}
3321
3322static void addrconf_permanent_addr(struct net_device *dev)
3323{
3324 struct inet6_ifaddr *ifp, *tmp;
3325 struct inet6_dev *idev;
3326
3327 idev = __in6_dev_get(dev);
3328 if (!idev)
3329 return;
3330
3331 write_lock_bh(&idev->lock);
3332
3333 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3334 if ((ifp->flags & IFA_F_PERMANENT) &&
3335 fixup_permanent_addr(idev, ifp) < 0) {
3336 write_unlock_bh(&idev->lock);
Eric Dumazetd87890d2017-10-30 22:47:09 -07003337 in6_ifa_hold(ifp);
David Ahernf1705ec2016-02-24 09:25:37 -08003338 ipv6_del_addr(ifp);
3339 write_lock_bh(&idev->lock);
3340
3341 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3342 idev->dev->name, &ifp->addr);
3343 }
3344 }
3345
3346 write_unlock_bh(&idev->lock);
3347}
3348
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003349static int addrconf_notify(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00003350 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351{
Jiri Pirko351638e2013-05-28 01:30:21 +00003352 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
David Ahern4f7f34e2016-04-07 11:10:41 -07003353 struct netdev_notifier_changeupper_info *info;
Eric Dumazet748e2d92012-08-22 21:50:59 +00003354 struct inet6_dev *idev = __in6_dev_get(dev);
WANG Cong0526ff32017-06-21 14:34:58 -07003355 struct net *net = dev_net(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003356 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07003357 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003359 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003360 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07003361 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003362 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -07003363 if (IS_ERR(idev))
3364 return notifier_from_errno(PTR_ERR(idev));
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003365 }
3366 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003367
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003368 case NETDEV_CHANGEMTU:
3369 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3370 if (dev->mtu < IPV6_MIN_MTU) {
WANG Cong0526ff32017-06-21 14:34:58 -07003371 addrconf_ifdown(dev, dev != net->loopback_dev);
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003372 break;
3373 }
3374
3375 if (idev) {
3376 rt6_mtu_change(dev, dev->mtu);
3377 idev->cnf.mtu6 = dev->mtu;
3378 break;
3379 }
3380
3381 /* allocate new idev */
3382 idev = ipv6_add_dev(dev);
3383 if (IS_ERR(idev))
3384 break;
3385
3386 /* device is still not ready */
3387 if (!(idev->if_flags & IF_READY))
3388 break;
3389
3390 run_pending = 1;
3391
3392 /* fall through */
3393
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003395 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07003396 if (dev->flags & IFF_SLAVE)
3397 break;
3398
WANG Cong3ce62a82014-09-11 15:07:16 -07003399 if (idev && idev->cnf.disable_ipv6)
3400 break;
3401
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003402 if (event == NETDEV_UP) {
David Ahern38bd10c2016-04-21 20:56:12 -07003403 /* restore routes for permanent addresses */
3404 addrconf_permanent_addr(dev);
3405
Mike Manning4bf42a22017-09-25 22:01:36 +01003406 if (!addrconf_link_ready(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003407 /* device is not ready yet. */
Joe Perchesf3213832012-05-15 14:11:53 +00003408 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003409 dev->name);
3410 break;
3411 }
Kristian Slavov99081042006-02-08 16:10:53 -08003412
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11003413 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3414 idev = ipv6_add_dev(dev);
3415
WANG Conga317a2f2014-07-25 15:25:09 -07003416 if (!IS_ERR_OR_NULL(idev)) {
Kristian Slavov99081042006-02-08 16:10:53 -08003417 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08003418 run_pending = 1;
3419 }
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003420 } else if (event == NETDEV_CHANGE) {
Mike Manning4bf42a22017-09-25 22:01:36 +01003421 if (!addrconf_link_ready(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003422 /* device is still not ready. */
3423 break;
3424 }
3425
3426 if (idev) {
Linus Lüssing1cadd392017-05-23 21:53:52 -04003427 if (idev->if_flags & IF_READY) {
3428 /* device is already configured -
3429 * but resend MLD reports, we might
3430 * have roamed and need to update
3431 * multicast snooping switches
3432 */
3433 ipv6_mc_up(idev);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003434 break;
Linus Lüssing1cadd392017-05-23 21:53:52 -04003435 }
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003436 idev->if_flags |= IF_READY;
3437 }
3438
Joe Perchesf3213832012-05-15 14:11:53 +00003439 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3440 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003441
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003442 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003443 }
3444
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003445 switch (dev->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +00003446#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 case ARPHRD_SIT:
3448 addrconf_sit_config(dev);
3449 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07003450#endif
Amerigo Wang07a93622012-10-29 16:23:10 +00003451#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003452 case ARPHRD_IPGRE:
3453 addrconf_gre_config(dev);
3454 break;
3455#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 case ARPHRD_LOOPBACK:
3457 init_loopback(dev);
3458 break;
3459
3460 default:
3461 addrconf_dev_config(dev);
3462 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003463 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003464
WANG Conga317a2f2014-07-25 15:25:09 -07003465 if (!IS_ERR_OR_NULL(idev)) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003466 if (run_pending)
3467 addrconf_dad_run(idev);
3468
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003469 /*
3470 * If the MTU changed during the interface down,
3471 * when the interface up, the changed MTU must be
3472 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003474 if (idev->cnf.mtu6 != dev->mtu &&
3475 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 rt6_mtu_change(dev, dev->mtu);
3477 idev->cnf.mtu6 = dev->mtu;
3478 }
3479 idev->tstamp = jiffies;
3480 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003481
3482 /*
3483 * If the changed mtu during down is lower than
3484 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 */
3486 if (dev->mtu < IPV6_MIN_MTU)
WANG Cong0526ff32017-06-21 14:34:58 -07003487 addrconf_ifdown(dev, dev != net->loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 }
3489 break;
3490
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 case NETDEV_DOWN:
3492 case NETDEV_UNREGISTER:
3493 /*
3494 * Remove all addresses from this interface.
3495 */
3496 addrconf_ifdown(dev, event != NETDEV_DOWN);
3497 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003498
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07003501 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003502 addrconf_sysctl_unregister(idev);
WANG Conga317a2f2014-07-25 15:25:09 -07003503 err = addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07003504 if (err)
3505 return notifier_from_errno(err);
WANG Conga317a2f2014-07-25 15:25:09 -07003506 err = snmp6_register_dev(idev);
3507 if (err) {
3508 addrconf_sysctl_unregister(idev);
3509 return notifier_from_errno(err);
3510 }
Stephen Hemminger5632c512007-04-28 21:16:39 -07003511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003513
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003514 case NETDEV_PRE_TYPE_CHANGE:
3515 case NETDEV_POST_TYPE_CHANGE:
Andrew Lunn3ef09522015-12-03 21:12:32 +01003516 if (idev)
3517 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07003518 break;
David Ahern4f7f34e2016-04-07 11:10:41 -07003519
3520 case NETDEV_CHANGEUPPER:
3521 info = ptr;
3522
3523 /* flush all routes if dev is linked to or unlinked from
3524 * an L3 master device (e.g., VRF)
3525 */
3526 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3527 addrconf_ifdown(dev, 0);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
3530 return NOTIFY_OK;
3531}
3532
3533/*
3534 * addrconf module should be notified of a device going up
3535 */
3536static struct notifier_block ipv6_dev_notf = {
3537 .notifier_call = addrconf_notify,
WANG Cong8795ee72017-05-08 10:12:13 -07003538 .priority = ADDRCONF_NOTIFY_PRIORITY,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539};
3540
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003541static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07003542{
3543 struct inet6_dev *idev;
3544 ASSERT_RTNL();
3545
3546 idev = __in6_dev_get(dev);
3547
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003548 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003549 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003550 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003551 ipv6_mc_unmap(idev);
3552}
3553
David Ahern70af9212016-04-08 12:01:21 -07003554static bool addr_is_local(const struct in6_addr *addr)
3555{
3556 return ipv6_addr_type(addr) &
3557 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3558}
3559
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560static int addrconf_ifdown(struct net_device *dev, int how)
3561{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003562 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003563 struct inet6_dev *idev;
David Ahernf1705ec2016-02-24 09:25:37 -08003564 struct inet6_ifaddr *ifa, *tmp;
3565 struct list_head del_list;
3566 int _keep_addr;
3567 bool keep_addr;
David S. Miller73a8bd72011-01-23 23:27:15 -08003568 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
3570 ASSERT_RTNL();
3571
David S. Miller73a8bd72011-01-23 23:27:15 -08003572 rt6_ifdown(net, dev);
3573 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
3575 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003576 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 return -ENODEV;
3578
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003579 /*
3580 * Step 1: remove reference to ipv6 device from parent device.
3581 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003583 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003585
3586 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003587 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588
3589 /* Step 1.5: remove snmp6 entry */
3590 snmp6_unregister_dev(idev);
3591
3592 }
3593
David Ahernf1705ec2016-02-24 09:25:37 -08003594 /* aggregate the system setting and interface setting */
3595 _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3596 if (!_keep_addr)
3597 _keep_addr = idev->cnf.keep_addr_on_down;
3598
3599 /* combine the user config with event to determine if permanent
3600 * addresses are to be removed from address hash table
3601 */
Mike Manningbc561632016-08-12 12:02:38 +01003602 keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003603
David S. Miller73a8bd72011-01-23 23:27:15 -08003604 /* Step 2: clear hash table */
3605 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3606 struct hlist_head *h = &inet6_addr_lst[i];
David S. Miller73a8bd72011-01-23 23:27:15 -08003607
3608 spin_lock_bh(&addrconf_hash_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01003609restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08003610 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
David S. Miller73a8bd72011-01-23 23:27:15 -08003611 if (ifa->idev == idev) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003612 addrconf_del_dad_work(ifa);
David Ahernf1705ec2016-02-24 09:25:37 -08003613 /* combined flag + permanent flag decide if
3614 * address is retained on a down event
3615 */
3616 if (!keep_addr ||
David Ahern70af9212016-04-08 12:01:21 -07003617 !(ifa->flags & IFA_F_PERMANENT) ||
3618 addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003619 hlist_del_init_rcu(&ifa->addr_lst);
3620 goto restart;
3621 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003622 }
3623 }
3624 spin_unlock_bh(&addrconf_hash_lock);
3625 }
3626
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 write_lock_bh(&idev->lock);
3628
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003629 addrconf_del_rs_timer(idev);
3630
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003631 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003632 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003633 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003635 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00003636 while (!list_empty(&idev->tempaddr_list)) {
3637 ifa = list_first_entry(&idev->tempaddr_list,
3638 struct inet6_ifaddr, tmp_list);
3639 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 write_unlock_bh(&idev->lock);
3641 spin_lock_bh(&ifa->lock);
3642
3643 if (ifa->ifpub) {
3644 in6_ifa_put(ifa->ifpub);
3645 ifa->ifpub = NULL;
3646 }
3647 spin_unlock_bh(&ifa->lock);
3648 in6_ifa_put(ifa);
3649 write_lock_bh(&idev->lock);
3650 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003651
David Ahernf1705ec2016-02-24 09:25:37 -08003652 /* re-combine the user config with event to determine if permanent
3653 * addresses are to be removed from the interface list
3654 */
Mike Manningbc561632016-08-12 12:02:38 +01003655 keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003656
3657 INIT_LIST_HEAD(&del_list);
3658 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
David Ahern38bd10c2016-04-21 20:56:12 -07003659 struct rt6_info *rt = NULL;
Rabin Vincentc86872a42017-04-10 08:36:39 +02003660 bool keep;
David Ahern38bd10c2016-04-21 20:56:12 -07003661
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003662 addrconf_del_dad_work(ifa);
stephen hemminger84e8b802010-03-02 13:32:46 +00003663
Rabin Vincentc86872a42017-04-10 08:36:39 +02003664 keep = keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3665 !addr_is_local(&ifa->addr);
3666 if (!keep)
3667 list_move(&ifa->if_list, &del_list);
3668
David S. Miller73a8bd72011-01-23 23:27:15 -08003669 write_unlock_bh(&idev->lock);
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003670 spin_lock_bh(&ifa->lock);
David Ahernf1705ec2016-02-24 09:25:37 -08003671
Rabin Vincentc86872a42017-04-10 08:36:39 +02003672 if (keep) {
David Ahernf1705ec2016-02-24 09:25:37 -08003673 /* set state to skip the notifier below */
3674 state = INET6_IFADDR_STATE_DEAD;
David Ahern0f4ac292017-05-02 14:43:44 -07003675 ifa->state = INET6_IFADDR_STATE_PREDAD;
David Ahernf1705ec2016-02-24 09:25:37 -08003676 if (!(ifa->flags & IFA_F_NODAD))
3677 ifa->flags |= IFA_F_TENTATIVE;
David Ahern38bd10c2016-04-21 20:56:12 -07003678
3679 rt = ifa->rt;
3680 ifa->rt = NULL;
David Ahernf1705ec2016-02-24 09:25:37 -08003681 } else {
3682 state = ifa->state;
3683 ifa->state = INET6_IFADDR_STATE_DEAD;
David Ahernf1705ec2016-02-24 09:25:37 -08003684 }
3685
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003686 spin_unlock_bh(&ifa->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003687
David Ahern38bd10c2016-04-21 20:56:12 -07003688 if (rt)
3689 ip6_del_rt(rt);
3690
David S. Miller73a8bd72011-01-23 23:27:15 -08003691 if (state != INET6_IFADDR_STATE_DEAD) {
3692 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Cong Wangf88c91d2013-04-14 23:18:43 +08003693 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
Mike Manningea06f712016-07-22 18:32:11 +01003694 } else {
3695 if (idev->cnf.forwarding)
3696 addrconf_leave_anycast(ifa);
3697 addrconf_leave_solict(ifa->idev, &ifa->addr);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00003698 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003699
David S. Miller73a8bd72011-01-23 23:27:15 -08003700 write_lock_bh(&idev->lock);
3701 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003702
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 write_unlock_bh(&idev->lock);
3704
David Ahernf1705ec2016-02-24 09:25:37 -08003705 /* now clean up addresses to be removed */
3706 while (!list_empty(&del_list)) {
3707 ifa = list_first_entry(&del_list,
3708 struct inet6_ifaddr, if_list);
3709 list_del(&ifa->if_list);
3710
3711 in6_ifa_put(ifa);
3712 }
3713
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003714 /* Step 5: Discard anycast and multicast list */
3715 if (how) {
3716 ipv6_ac_destroy_dev(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 ipv6_mc_destroy_dev(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003718 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 ipv6_mc_down(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003723
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003724 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003725 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003726 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 neigh_parms_release(&nd_tbl, idev->nd_parms);
3728 neigh_ifdown(&nd_tbl, dev);
3729 in6_dev_put(idev);
3730 }
3731 return 0;
3732}
3733
3734static void addrconf_rs_timer(unsigned long data)
3735{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003736 struct inet6_dev *idev = (struct inet6_dev *)data;
Cong Wangcaf92bc2013-08-31 13:44:32 +08003737 struct net_device *dev = idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003738 struct in6_addr lladdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003740 write_lock(&idev->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00003741 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 goto out;
3743
Shmulik Ladkani9ba2add2012-12-02 01:44:53 +00003744 if (!ipv6_accept_ra(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003746
3747 /* Announcement received after solicitation was sent */
3748 if (idev->if_flags & IF_RA_RCVD)
3749 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003751 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
Cong Wangcaf92bc2013-08-31 13:44:32 +08003752 write_unlock(&idev->lock);
3753 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3754 ndisc_send_rs(dev, &lladdr,
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003755 &in6addr_linklocal_allrouters);
3756 else
Cong Wangcaf92bc2013-08-31 13:44:32 +08003757 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Cong Wangcaf92bc2013-08-31 13:44:32 +08003759 write_lock(&idev->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003760 idev->rs_interval = rfc3315_s14_backoff_update(
3761 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003762 /* The wait after the last probe can be shorter */
3763 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3764 idev->cnf.rtr_solicits) ?
3765 idev->cnf.rtr_solicit_delay :
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003766 idev->rs_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 /*
3769 * Note: we do not support deprecated "all on-link"
3770 * assumption any longer.
3771 */
Joe Perches91df42b2012-05-15 14:11:54 +00003772 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 }
3774
3775out:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003776 write_unlock(&idev->lock);
Cong Wangcaf92bc2013-08-31 13:44:32 +08003777put:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003778 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779}
3780
3781/*
3782 * Duplicate Address Detection
3783 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003784static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3785{
3786 unsigned long rand_num;
3787 struct inet6_dev *idev = ifp->idev;
3788
Neil Horman95c385b2007-04-25 17:08:10 -07003789 if (ifp->flags & IFA_F_OPTIMISTIC)
3790 rand_num = 0;
3791 else
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003792 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
Neil Horman95c385b2007-04-25 17:08:10 -07003793
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003794 ifp->dad_probes = idev->cnf.dad_transmits;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003795 addrconf_mod_dad_work(ifp, rand_num);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003796}
3797
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003798static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799{
3800 struct inet6_dev *idev = ifp->idev;
3801 struct net_device *dev = idev->dev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003802 bool bump_id, notify = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803
3804 addrconf_join_solict(dev, &ifp->addr);
3805
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003806 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807
3808 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003809 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003810 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
3813 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09003814 idev->cnf.accept_dad < 1 ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003815 !(ifp->flags&IFA_F_TENTATIVE) ||
3816 ifp->flags & IFA_F_NODAD) {
Paolo Abeni764d3be2016-11-22 16:57:40 +01003817 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003818 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003819 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 read_unlock_bh(&idev->lock);
3821
Paolo Abeni764d3be2016-11-22 16:57:40 +01003822 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 return;
3824 }
3825
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003826 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003827 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003828 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003829 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003830 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003831 * - keep it tentative if it is a permanent address.
3832 * - otherwise, kill it.
3833 */
3834 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003835 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003836 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003837 }
Neil Horman95c385b2007-04-25 17:08:10 -07003838
3839 /*
3840 * Optimistic nodes can start receiving
3841 * Frames right away
3842 */
Erik Kline7fd25612014-10-28 18:11:14 +09003843 if (ifp->flags & IFA_F_OPTIMISTIC) {
Neil Horman95c385b2007-04-25 17:08:10 -07003844 ip6_ins_rt(ifp->rt);
Erik Kline7fd25612014-10-28 18:11:14 +09003845 if (ipv6_use_optimistic_addr(idev)) {
3846 /* Because optimistic nodes can use this address,
3847 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3848 */
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003849 notify = true;
Erik Kline7fd25612014-10-28 18:11:14 +09003850 }
3851 }
Neil Horman95c385b2007-04-25 17:08:10 -07003852
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003853 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003855 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 read_unlock_bh(&idev->lock);
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003857 if (notify)
3858 ipv6_ifa_notify(RTM_NEWADDR, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859}
3860
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003861static void addrconf_dad_start(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003863 bool begin_dad = false;
3864
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003865 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003866 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3867 ifp->state = INET6_IFADDR_STATE_PREDAD;
3868 begin_dad = true;
3869 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003870 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003871
3872 if (begin_dad)
3873 addrconf_mod_dad_work(ifp, 0);
3874}
3875
3876static void addrconf_dad_work(struct work_struct *w)
3877{
3878 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3879 struct inet6_ifaddr,
3880 dad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 struct inet6_dev *idev = ifp->idev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003882 bool bump_id, disable_ipv6 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 struct in6_addr mcaddr;
3884
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003885 enum {
3886 DAD_PROCESS,
3887 DAD_BEGIN,
3888 DAD_ABORT,
3889 } action = DAD_PROCESS;
3890
3891 rtnl_lock();
3892
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003893 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003894 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3895 action = DAD_BEGIN;
3896 ifp->state = INET6_IFADDR_STATE_DAD;
3897 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3898 action = DAD_ABORT;
3899 ifp->state = INET6_IFADDR_STATE_POSTDAD;
Mike Manning85b51b12016-08-18 14:39:40 +01003900
3901 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6 &&
3902 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
3903 struct in6_addr addr;
3904
3905 addr.s6_addr32[0] = htonl(0xfe800000);
3906 addr.s6_addr32[1] = 0;
3907
3908 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
3909 ipv6_addr_equal(&ifp->addr, &addr)) {
3910 /* DAD failed for link-local based on MAC */
3911 idev->cnf.disable_ipv6 = 1;
3912
3913 pr_info("%s: IPv6 being disabled!\n",
3914 ifp->idev->dev->name);
3915 disable_ipv6 = true;
3916 }
3917 }
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003918 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003919 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003920
3921 if (action == DAD_BEGIN) {
3922 addrconf_dad_begin(ifp);
3923 goto out;
3924 } else if (action == DAD_ABORT) {
Wei Yongjun751eb6b2016-09-05 16:06:31 +08003925 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003926 addrconf_dad_stop(ifp, 1);
Mike Manning85b51b12016-08-18 14:39:40 +01003927 if (disable_ipv6)
3928 addrconf_ifdown(idev->dev, 0);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003929 goto out;
3930 }
3931
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003932 if (!ifp->dad_probes && addrconf_dad_end(ifp))
Herbert Xuf2344a12010-05-18 15:55:27 -07003933 goto out;
3934
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003935 write_lock_bh(&idev->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003936 if (idev->dead || !(idev->if_flags & IF_READY)) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003937 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 goto out;
3939 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003940
3941 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003942 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3943 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003944 write_unlock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003945 goto out;
3946 }
3947
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003948 if (ifp->dad_probes == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 /*
3950 * DAD was successful
3951 */
3952
Paolo Abeni764d3be2016-11-22 16:57:40 +01003953 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003954 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003955 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003956 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
Paolo Abeni764d3be2016-11-22 16:57:40 +01003958 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
3960 goto out;
3961 }
3962
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003963 ifp->dad_probes--;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003964 addrconf_mod_dad_work(ifp,
3965 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
stephen hemminger21809fa2010-02-08 19:48:52 +00003966 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003967 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968
3969 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
Nicolas Dichtel304d8882015-11-27 18:17:05 +01003971 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972out:
3973 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003974 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975}
3976
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003977/* ifp->idev must be at least read locked */
3978static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3979{
3980 struct inet6_ifaddr *ifpiter;
3981 struct inet6_dev *idev = ifp->idev;
3982
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01003983 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3984 if (ifpiter->scope > IFA_LINK)
3985 break;
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003986 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3987 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3988 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
3989 IFA_F_PERMANENT)
3990 return false;
3991 }
3992 return true;
3993}
3994
Paolo Abeni764d3be2016-11-22 16:57:40 +01003995static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003997 struct net_device *dev = ifp->idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003998 struct in6_addr lladdr;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003999 bool send_rs, send_mld;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004000
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004001 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002
4003 /*
4004 * Configure the address for reception. Now it is valid.
4005 */
4006
4007 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4008
Tore Anderson026359b2011-08-28 23:47:33 +00004009 /* If added prefix is link local and we are prepared to process
4010 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 */
4012
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004013 read_lock_bh(&ifp->idev->lock);
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004014 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004015 send_rs = send_mld &&
4016 ipv6_accept_ra(ifp->idev) &&
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004017 ifp->idev->cnf.rtr_solicits != 0 &&
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004018 (dev->flags&IFF_LOOPBACK) == 0;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004019 read_unlock_bh(&ifp->idev->lock);
4020
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004021 /* While dad is in progress mld report's source address is in6_addrany.
4022 * Resend with proper ll now.
4023 */
4024 if (send_mld)
4025 ipv6_mc_dad_complete(ifp->idev);
4026
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004027 if (send_rs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 /*
4029 * If a host as already performed a random delay
4030 * [...] as part of DAD [...] there is no need
4031 * to delay again before sending the first RS
4032 */
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004033 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004034 return;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004035 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004037 write_lock_bh(&ifp->idev->lock);
4038 spin_lock(&ifp->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004039 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4040 ifp->idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004041 ifp->idev->rs_probes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 ifp->idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004043 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004044 spin_unlock(&ifp->lock);
4045 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 }
Paolo Abeni764d3be2016-11-22 16:57:40 +01004047
4048 if (bump_id)
4049 rt_genid_bump_ipv6(dev_net(dev));
Marcus Huewed385ed72017-05-23 21:53:49 -04004050
4051 /* Make sure that a new temporary address will be created
4052 * before this temporary address becomes deprecated.
4053 */
4054 if (ifp->flags & IFA_F_TEMPORARY)
4055 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056}
4057
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004058static void addrconf_dad_run(struct inet6_dev *idev)
4059{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004060 struct inet6_ifaddr *ifp;
4061
4062 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00004063 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00004064 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07004065 if (ifp->flags & IFA_F_TENTATIVE &&
4066 ifp->state == INET6_IFADDR_STATE_DAD)
4067 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00004068 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004069 }
4070 read_unlock_bh(&idev->lock);
4071}
4072
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073#ifdef CONFIG_PROC_FS
4074struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08004075 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004077 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078};
4079
Mihai Maruseac1d578302012-01-03 23:31:35 +00004080static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081{
4082 struct inet6_ifaddr *ifa = NULL;
4083 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004084 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00004085 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
Mihai Maruseac1d578302012-01-03 23:31:35 +00004087 /* initial bucket if pos is 0 */
4088 if (pos == 0) {
4089 state->bucket = 0;
4090 state->offset = 0;
4091 }
4092
4093 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08004094 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00004095 addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004096 if (!net_eq(dev_net(ifa->idev->dev), net))
4097 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004098 /* sync with offset */
4099 if (p < state->offset) {
4100 p++;
4101 continue;
4102 }
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004103 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004104 }
4105
4106 /* prepare for next bucket */
4107 state->offset = 0;
4108 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004110 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111}
4112
stephen hemmingerc2e21292010-03-17 20:31:10 +00004113static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4114 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115{
4116 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004117 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118
Sasha Levinb67bfe02013-02-27 17:06:00 -08004119 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004120 if (!net_eq(dev_net(ifa->idev->dev), net))
4121 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004122 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004123 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004124 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004125
Jeff Barnhillea143552018-09-21 00:45:27 +00004126 state->offset = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004127 while (++state->bucket < IN6_ADDR_HSIZE) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08004128 hlist_for_each_entry_rcu_bh(ifa,
stephen hemmingerc2e21292010-03-17 20:31:10 +00004129 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004130 if (!net_eq(dev_net(ifa->idev->dev), net))
4131 continue;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004132 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08004133 }
4134 }
4135
stephen hemmingerc2e21292010-03-17 20:31:10 +00004136 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137}
4138
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004140 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141{
stephen hemminger5c578ae2010-03-17 20:31:11 +00004142 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00004143 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144}
4145
4146static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4147{
4148 struct inet6_ifaddr *ifa;
4149
4150 ifa = if6_get_next(seq, v);
4151 ++*pos;
4152 return ifa;
4153}
4154
4155static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004156 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157{
stephen hemminger5c578ae2010-03-17 20:31:11 +00004158 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159}
4160
4161static int if6_seq_show(struct seq_file *seq, void *v)
4162{
4163 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Jiri Pirko971a3512013-12-10 13:56:29 +01004164 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07004165 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 ifp->idev->dev->ifindex,
4167 ifp->prefix_len,
4168 ifp->scope,
Jiri Pirko971a3512013-12-10 13:56:29 +01004169 (u8) ifp->flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 ifp->idev->dev->name);
4171 return 0;
4172}
4173
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004174static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 .start = if6_seq_start,
4176 .next = if6_seq_next,
4177 .show = if6_seq_show,
4178 .stop = if6_seq_stop,
4179};
4180
4181static int if6_seq_open(struct inode *inode, struct file *file)
4182{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004183 return seq_open_net(inode, file, &if6_seq_ops,
4184 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185}
4186
Arjan van de Ven9a321442007-02-12 00:55:35 -08004187static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 .owner = THIS_MODULE,
4189 .open = if6_seq_open,
4190 .read = seq_read,
4191 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004192 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193};
4194
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004195static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196{
Gao fengd4beaa62013-02-18 01:34:54 +00004197 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 return -ENOMEM;
4199 return 0;
4200}
4201
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004202static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08004203{
Gao fengece31ff2013-02-18 01:34:56 +00004204 remove_proc_entry("if_inet6", net->proc_net);
Daniel Lezcano3c400902008-01-10 22:42:49 -08004205}
4206
4207static struct pernet_operations if6_proc_net_ops = {
Ian Morris67ba4152014-08-24 21:53:10 +01004208 .init = if6_proc_net_init,
4209 .exit = if6_proc_net_exit,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004210};
4211
4212int __init if6_proc_init(void)
4213{
4214 return register_pernet_subsys(&if6_proc_net_ops);
4215}
4216
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217void if6_proc_exit(void)
4218{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004219 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220}
4221#endif /* CONFIG_PROC_FS */
4222
Amerigo Wang07a93622012-10-29 16:23:10 +00004223#if IS_ENABLED(CONFIG_IPV6_MIP6)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004224/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004225int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004226{
4227 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004228 struct inet6_ifaddr *ifp = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00004229 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004230
stephen hemminger5c578ae2010-03-17 20:31:11 +00004231 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08004232 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09004233 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08004234 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09004235 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004236 (ifp->flags & IFA_F_HOMEADDRESS)) {
4237 ret = 1;
4238 break;
4239 }
4240 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00004241 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004242 return ret;
4243}
4244#endif
4245
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246/*
4247 * Periodic address status verification
4248 */
4249
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004250static void addrconf_verify_rtnl(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004252 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 int i;
4255
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004256 ASSERT_RTNL();
4257
stephen hemminger5c578ae2010-03-17 20:31:11 +00004258 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004260 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004262 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004264 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265restart:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004266 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
Yasushi Asanofad8da32013-12-31 12:04:19 +09004269 /* When setting preferred_lft to a value not zero or
4270 * infinity, while valid_lft is infinity
4271 * IFA_F_PERMANENT has a non-infinity life time.
4272 */
4273 if ((ifp->flags & IFA_F_PERMANENT) &&
4274 (ifp->prefered_lft == INFINITY_LIFE_TIME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 continue;
4276
4277 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004278 /* We try to batch several events at once. */
4279 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004281 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4282 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 spin_unlock(&ifp->lock);
4284 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 ipv6_del_addr(ifp);
4286 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004287 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4288 spin_unlock(&ifp->lock);
4289 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00004291 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 int deprecate = 0;
4293
4294 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4295 deprecate = 1;
4296 ifp->flags |= IFA_F_DEPRECATED;
4297 }
4298
Yasushi Asanofad8da32013-12-31 12:04:19 +09004299 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4300 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 next = ifp->tstamp + ifp->valid_lft * HZ;
4302
4303 spin_unlock(&ifp->lock);
4304
4305 if (deprecate) {
4306 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
4308 ipv6_ifa_notify(0, ifp);
4309 in6_ifa_put(ifp);
4310 goto restart;
4311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4313 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00004314 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4315 ifp->idev->cnf.dad_transmits *
Jiri Pirko1f9248e52013-12-07 19:26:53 +01004316 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
stephen hemminger88949cf2010-03-17 20:31:17 +00004317
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 if (age >= ifp->prefered_lft - regen_advance) {
4319 struct inet6_ifaddr *ifpub = ifp->ifpub;
4320 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4321 next = ifp->tstamp + ifp->prefered_lft * HZ;
4322 if (!ifp->regen_count && ifpub) {
4323 ifp->regen_count++;
4324 in6_ifa_hold(ifp);
4325 in6_ifa_hold(ifpub);
4326 spin_unlock(&ifp->lock);
stephen hemminger5c578ae2010-03-17 20:31:11 +00004327
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08004328 spin_lock(&ifpub->lock);
4329 ifpub->regen_count = 0;
4330 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 ipv6_create_tempaddr(ifpub, ifp);
4332 in6_ifa_put(ifpub);
4333 in6_ifa_put(ifp);
4334 goto restart;
4335 }
4336 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4337 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4338 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 } else {
4340 /* ifp->prefered_lft <= ifp->valid_lft */
4341 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4342 next = ifp->tstamp + ifp->prefered_lft * HZ;
4343 spin_unlock(&ifp->lock);
4344 }
4345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 }
4347
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004348 next_sec = round_jiffies_up(next);
4349 next_sched = next;
4350
4351 /* If rounded timeout is accurate enough, accept it. */
4352 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4353 next_sched = next_sec;
4354
4355 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4356 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4357 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4358
dingtianhongba3542e2013-08-17 10:27:04 +08004359 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4360 now, next, next_sec, next_sched);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004361 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
stephen hemminger5c578ae2010-03-17 20:31:11 +00004362 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363}
4364
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004365static void addrconf_verify_work(struct work_struct *w)
4366{
4367 rtnl_lock();
4368 addrconf_verify_rtnl();
4369 rtnl_unlock();
4370}
4371
4372static void addrconf_verify(void)
4373{
4374 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4375}
4376
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004377static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4378 struct in6_addr **peer_pfx)
Thomas Graf461d8832006-09-18 00:09:49 -07004379{
4380 struct in6_addr *pfx = NULL;
4381
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004382 *peer_pfx = NULL;
4383
Thomas Graf461d8832006-09-18 00:09:49 -07004384 if (addr)
4385 pfx = nla_data(addr);
4386
4387 if (local) {
4388 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004389 *peer_pfx = pfx;
4390 pfx = nla_data(local);
Thomas Graf461d8832006-09-18 00:09:49 -07004391 }
4392
4393 return pfx;
4394}
4395
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004396static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07004397 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4398 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4399 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
Jiri Pirko479840f2013-12-06 09:45:21 +01004400 [IFA_FLAGS] = { .len = sizeof(u32) },
Thomas Graf461d8832006-09-18 00:09:49 -07004401};
4402
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403static int
Thomas Graf661d2962013-03-21 07:45:29 +00004404inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004406 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07004407 struct ifaddrmsg *ifm;
4408 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004409 struct in6_addr *pfx, *peer_pfx;
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004410 u32 ifa_flags;
Thomas Grafb933f712006-09-18 00:10:19 -07004411 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
Thomas Grafb933f712006-09-18 00:10:19 -07004413 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4414 if (err < 0)
4415 return err;
4416
4417 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004418 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004419 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 return -EINVAL;
4421
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004422 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4423
4424 /* We ignore other flags so far. */
4425 ifa_flags &= IFA_F_MANAGETEMPADDR;
4426
4427 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4428 ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429}
4430
Jiri Pirko479840f2013-12-06 09:45:21 +01004431static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004432 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004433{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004434 u32 flags;
4435 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004436 unsigned long timeout;
Jiri Pirko53bd6742013-12-06 09:45:22 +01004437 bool was_managetempaddr;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004438 bool had_prefixroute;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09004439
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004440 ASSERT_RTNL();
4441
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004442 if (!valid_lft || (prefered_lft > valid_lft))
4443 return -EINVAL;
4444
Jiri Pirko53bd6742013-12-06 09:45:22 +01004445 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4446 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4447 return -EINVAL;
4448
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004449 timeout = addrconf_timeout_fixup(valid_lft, HZ);
4450 if (addrconf_finite_timeout(timeout)) {
4451 expires = jiffies_to_clock_t(timeout * HZ);
4452 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004453 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004454 } else {
4455 expires = 0;
4456 flags = 0;
4457 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004458 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004459
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004460 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4461 if (addrconf_finite_timeout(timeout)) {
4462 if (timeout == 0)
4463 ifa_flags |= IFA_F_DEPRECATED;
4464 prefered_lft = timeout;
4465 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004466
4467 spin_lock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004468 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004469 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4470 !(ifp->flags & IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004471 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
Thomas Haller761aac72014-01-15 15:36:58 +01004472 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4473 IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004474 ifp->flags |= ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004475 ifp->tstamp = jiffies;
4476 ifp->valid_lft = valid_lft;
4477 ifp->prefered_lft = prefered_lft;
4478
4479 spin_unlock_bh(&ifp->lock);
4480 if (!(ifp->flags&IFA_F_TENTATIVE))
4481 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004482
Thomas Haller761aac72014-01-15 15:36:58 +01004483 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4484 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4485 expires, flags);
Thomas Haller5b84efe2014-01-15 15:36:59 +01004486 } else if (had_prefixroute) {
4487 enum cleanup_prefix_rt_t action;
4488 unsigned long rt_expires;
4489
4490 write_lock_bh(&ifp->idev->lock);
4491 action = check_cleanup_prefix_route(ifp, &rt_expires);
4492 write_unlock_bh(&ifp->idev->lock);
4493
4494 if (action != CLEANUP_PREFIX_RT_NOP) {
4495 cleanup_prefix_route(ifp, rt_expires,
4496 action == CLEANUP_PREFIX_RT_DEL);
4497 }
Thomas Haller761aac72014-01-15 15:36:58 +01004498 }
Jiri Pirko53bd6742013-12-06 09:45:22 +01004499
4500 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4501 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4502 valid_lft = prefered_lft = 0;
4503 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4504 !was_managetempaddr, jiffies);
4505 }
4506
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004507 addrconf_verify_rtnl();
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004508
4509 return 0;
4510}
4511
4512static int
Thomas Graf661d2962013-03-21 07:45:29 +00004513inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004515 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07004516 struct ifaddrmsg *ifm;
4517 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004518 struct in6_addr *pfx, *peer_pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07004519 struct inet6_ifaddr *ifa;
4520 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004521 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
Jiri Pirko479840f2013-12-06 09:45:21 +01004522 u32 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07004523 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
Thomas Graf461d8832006-09-18 00:09:49 -07004525 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4526 if (err < 0)
4527 return err;
4528
4529 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004530 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004531 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 return -EINVAL;
4533
Thomas Graf461d8832006-09-18 00:09:49 -07004534 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004535 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07004536
4537 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004538 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07004539 preferred_lft = ci->ifa_prefered;
4540 } else {
4541 preferred_lft = INFINITY_LIFE_TIME;
4542 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004543 }
4544
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004545 dev = __dev_get_by_index(net, ifm->ifa_index);
Ian Morris63159f22015-03-29 14:00:04 +01004546 if (!dev)
Thomas Graf7198f8c2006-09-18 00:13:46 -07004547 return -ENODEV;
4548
Jiri Pirko479840f2013-12-06 09:45:21 +01004549 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4550
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004551 /* We ignore other flags so far. */
Thomas Haller761aac72014-01-15 15:36:58 +01004552 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
Madhu Challa93a714d2015-02-25 09:58:35 -08004553 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004554
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08004555 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Ian Morris63159f22015-03-29 14:00:04 +01004556 if (!ifa) {
Thomas Graf7198f8c2006-09-18 00:13:46 -07004557 /*
4558 * It would be best to check for !NLM_F_CREATE here but
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08004559 * userspace already relies on not having to provide this.
Thomas Graf7198f8c2006-09-18 00:13:46 -07004560 */
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004561 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004562 ifm->ifa_prefixlen, ifa_flags,
4563 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004564 }
4565
Thomas Graf7198f8c2006-09-18 00:13:46 -07004566 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4567 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4568 err = -EEXIST;
4569 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004570 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07004571
4572 in6_ifa_put(ifa);
4573
4574 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575}
4576
Jiri Pirko479840f2013-12-06 09:45:21 +01004577static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
Thomas Graf101bb222006-09-18 00:11:52 -07004578 u8 scope, int ifindex)
4579{
4580 struct ifaddrmsg *ifm;
4581
4582 ifm = nlmsg_data(nlh);
4583 ifm->ifa_family = AF_INET6;
4584 ifm->ifa_prefixlen = prefixlen;
4585 ifm->ifa_flags = flags;
4586 ifm->ifa_scope = scope;
4587 ifm->ifa_index = ifindex;
4588}
4589
Thomas Graf85486af2006-09-18 00:11:24 -07004590static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4591 unsigned long tstamp, u32 preferred, u32 valid)
4592{
4593 struct ifa_cacheinfo ci;
4594
Thomas Graf18a31e12010-11-17 04:12:02 +00004595 ci.cstamp = cstamp_delta(cstamp);
4596 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07004597 ci.ifa_prefered = preferred;
4598 ci.ifa_valid = valid;
4599
4600 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4601}
4602
Thomas Graf101bb222006-09-18 00:11:52 -07004603static inline int rt_scope(int ifa_scope)
4604{
4605 if (ifa_scope & IFA_HOST)
4606 return RT_SCOPE_HOST;
4607 else if (ifa_scope & IFA_LINK)
4608 return RT_SCOPE_LINK;
4609 else if (ifa_scope & IFA_SITE)
4610 return RT_SCOPE_SITE;
4611 else
4612 return RT_SCOPE_UNIVERSE;
4613}
4614
Thomas Graf0ab68032006-09-18 00:12:35 -07004615static inline int inet6_ifaddr_msgsize(void)
4616{
Thomas Graf339bf982006-11-10 14:10:15 -08004617 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004618 + nla_total_size(16) /* IFA_LOCAL */
Thomas Graf339bf982006-11-10 14:10:15 -08004619 + nla_total_size(16) /* IFA_ADDRESS */
Jiri Pirko479840f2013-12-06 09:45:21 +01004620 + nla_total_size(sizeof(struct ifa_cacheinfo))
4621 + nla_total_size(4) /* IFA_FLAGS */;
Thomas Graf0ab68032006-09-18 00:12:35 -07004622}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004623
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004625 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07004628 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
Eric W. Biederman15e47302012-09-07 20:12:54 +00004630 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004631 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004632 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004633
Thomas Graf101bb222006-09-18 00:11:52 -07004634 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4635 ifa->idev->dev->ifindex);
4636
Yasushi Asanofad8da32013-12-31 12:04:19 +09004637 if (!((ifa->flags&IFA_F_PERMANENT) &&
4638 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
Thomas Graf85486af2006-09-18 00:11:24 -07004639 preferred = ifa->prefered_lft;
4640 valid = ifa->valid_lft;
4641 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00004643 if (preferred > tval)
4644 preferred -= tval;
4645 else
4646 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00004647 if (valid != INFINITY_LIFE_TIME) {
4648 if (valid > tval)
4649 valid -= tval;
4650 else
4651 valid = 0;
4652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 }
4654 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07004655 preferred = INFINITY_LIFE_TIME;
4656 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 }
Thomas Graf85486af2006-09-18 00:11:24 -07004658
Cong Wang7996c792013-05-22 05:41:06 +00004659 if (!ipv6_addr_any(&ifa->peer_addr)) {
Jiri Benc930345e2015-03-29 16:59:25 +02004660 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4661 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004662 goto error;
4663 } else
Jiri Benc930345e2015-03-29 16:59:25 +02004664 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004665 goto error;
4666
4667 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4668 goto error;
Thomas Graf85486af2006-09-18 00:11:24 -07004669
Jiri Pirko479840f2013-12-06 09:45:21 +01004670 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4671 goto error;
4672
Johannes Berg053c0952015-01-16 22:09:00 +01004673 nlmsg_end(skb, nlh);
4674 return 0;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004675
4676error:
4677 nlmsg_cancel(skb, nlh);
4678 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679}
4680
4681static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004682 u32 portid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004685 u8 scope = RT_SCOPE_UNIVERSE;
4686 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
Thomas Graf101bb222006-09-18 00:11:52 -07004688 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4689 scope = RT_SCOPE_SITE;
4690
Eric W. Biederman15e47302012-09-07 20:12:54 +00004691 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004692 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004693 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004694
Thomas Graf101bb222006-09-18 00:11:52 -07004695 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004696 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004697 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004698 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4699 nlmsg_cancel(skb, nlh);
4700 return -EMSGSIZE;
4701 }
Thomas Graf85486af2006-09-18 00:11:24 -07004702
Johannes Berg053c0952015-01-16 22:09:00 +01004703 nlmsg_end(skb, nlh);
4704 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705}
4706
4707static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004708 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004711 u8 scope = RT_SCOPE_UNIVERSE;
4712 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
Thomas Graf101bb222006-09-18 00:11:52 -07004714 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4715 scope = RT_SCOPE_SITE;
4716
Eric W. Biederman15e47302012-09-07 20:12:54 +00004717 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004718 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004719 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004720
Thomas Graf101bb222006-09-18 00:11:52 -07004721 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004722 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004723 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004724 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4725 nlmsg_cancel(skb, nlh);
4726 return -EMSGSIZE;
4727 }
Thomas Graf85486af2006-09-18 00:11:24 -07004728
Johannes Berg053c0952015-01-16 22:09:00 +01004729 nlmsg_end(skb, nlh);
4730 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731}
4732
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004733enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 UNICAST_ADDR,
4735 MULTICAST_ADDR,
4736 ANYCAST_ADDR,
4737};
4738
Eric Dumazet234b27c2009-11-12 04:11:50 +00004739/* called with rcu_read_lock() */
4740static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4741 struct netlink_callback *cb, enum addr_type_t type,
4742 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 struct ifmcaddr6 *ifmca;
4745 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004746 int err = 1;
4747 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748
Eric Dumazet234b27c2009-11-12 04:11:50 +00004749 read_lock_bh(&idev->lock);
4750 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00004751 case UNICAST_ADDR: {
4752 struct inet6_ifaddr *ifa;
4753
Eric Dumazet234b27c2009-11-12 04:11:50 +00004754 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00004755 list_for_each_entry(ifa, &idev->addr_list, if_list) {
David Ahernf86c55c62018-10-19 10:00:19 -07004756 if (ip_idx < s_ip_idx)
4757 goto next;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004758 err = inet6_fill_ifaddr(skb, ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004759 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004760 cb->nlh->nlmsg_seq,
4761 RTM_NEWADDR,
4762 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004763 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004764 break;
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004765 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
David Ahernf86c55c62018-10-19 10:00:19 -07004766next:
4767 ip_idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004769 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00004770 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004771 case MULTICAST_ADDR:
4772 /* multicast address */
4773 for (ifmca = idev->mc_list; ifmca;
4774 ifmca = ifmca->next, ip_idx++) {
4775 if (ip_idx < s_ip_idx)
4776 continue;
4777 err = inet6_fill_ifmcaddr(skb, ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004778 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004779 cb->nlh->nlmsg_seq,
4780 RTM_GETMULTICAST,
4781 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004782 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004783 break;
4784 }
4785 break;
4786 case ANYCAST_ADDR:
4787 /* anycast address */
4788 for (ifaca = idev->ac_list; ifaca;
4789 ifaca = ifaca->aca_next, ip_idx++) {
4790 if (ip_idx < s_ip_idx)
4791 continue;
4792 err = inet6_fill_ifacaddr(skb, ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004793 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004794 cb->nlh->nlmsg_seq,
4795 RTM_GETANYCAST,
4796 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004797 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004798 break;
4799 }
4800 break;
4801 default:
4802 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004804 read_unlock_bh(&idev->lock);
4805 *p_ip_idx = ip_idx;
4806 return err;
4807}
4808
4809static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4810 enum addr_type_t type)
4811{
4812 struct net *net = sock_net(skb->sk);
4813 int h, s_h;
4814 int idx, ip_idx;
4815 int s_idx, s_ip_idx;
4816 struct net_device *dev;
4817 struct inet6_dev *idev;
4818 struct hlist_head *head;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004819
4820 s_h = cb->args[0];
4821 s_idx = idx = cb->args[1];
4822 s_ip_idx = ip_idx = cb->args[2];
4823
4824 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004825 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004826 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4827 idx = 0;
4828 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08004829 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet234b27c2009-11-12 04:11:50 +00004830 if (idx < s_idx)
4831 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07004832 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004833 s_ip_idx = 0;
4834 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004835 idev = __in6_dev_get(dev);
4836 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004837 goto cont;
4838
4839 if (in6_dump_addrs(idev, skb, cb, type,
David S. Miller7b46a642015-01-18 23:36:08 -05004840 s_ip_idx, &ip_idx) < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004841 goto done;
4842cont:
4843 idx++;
4844 }
4845 }
4846done:
4847 rcu_read_unlock();
4848 cb->args[0] = h;
4849 cb->args[1] = idx;
4850 cb->args[2] = ip_idx;
4851
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 return skb->len;
4853}
4854
4855static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4856{
4857 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004858
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 return inet6_dump_addr(skb, cb, type);
4860}
4861
4862static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4863{
4864 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004865
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 return inet6_dump_addr(skb, cb, type);
4867}
4868
4869
4870static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4871{
4872 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004873
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 return inet6_dump_addr(skb, cb, type);
4875}
4876
Thomas Graf661d2962013-03-21 07:45:29 +00004877static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004878{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004879 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004880 struct ifaddrmsg *ifm;
4881 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004882 struct in6_addr *addr = NULL, *peer;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004883 struct net_device *dev = NULL;
4884 struct inet6_ifaddr *ifa;
4885 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004886 int err;
4887
Thomas Graf1b29fc22006-09-18 00:10:50 -07004888 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4889 if (err < 0)
4890 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004891
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004892 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
Ian Morris63159f22015-03-29 14:00:04 +01004893 if (!addr) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004894 err = -EINVAL;
4895 goto errout;
4896 }
4897
4898 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004899 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004900 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004901
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004902 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4903 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004904 err = -EADDRNOTAVAIL;
4905 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004906 }
4907
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004908 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4909 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004910 err = -ENOBUFS;
4911 goto errout_ifa;
4912 }
4913
Eric W. Biederman15e47302012-09-07 20:12:54 +00004914 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004915 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004916 if (err < 0) {
4917 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4918 WARN_ON(err == -EMSGSIZE);
4919 kfree_skb(skb);
4920 goto errout_ifa;
4921 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00004922 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004923errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004924 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004925errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004926 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004927}
4928
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4930{
4931 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004932 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07004933 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934
Thomas Graf0ab68032006-09-18 00:12:35 -07004935 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01004936 if (!skb)
Thomas Graf5d620262006-08-15 00:35:02 -07004937 goto errout;
4938
4939 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004940 if (err < 0) {
4941 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4942 WARN_ON(err == -EMSGSIZE);
4943 kfree_skb(skb);
4944 goto errout;
4945 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004946 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4947 return;
Thomas Graf5d620262006-08-15 00:35:02 -07004948errout:
4949 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004950 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951}
4952
Dave Jonesb6f99a22007-03-22 12:27:49 -07004953static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 __s32 *array, int bytes)
4955{
Thomas Graf04561c12006-11-14 19:53:58 -08004956 BUG_ON(bytes < (DEVCONF_MAX * 4));
4957
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 memset(array, 0, bytes);
4959 array[DEVCONF_FORWARDING] = cnf->forwarding;
4960 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4961 array[DEVCONF_MTU6] = cnf->mtu6;
4962 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4963 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4964 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4965 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4966 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00004967 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4968 jiffies_to_msecs(cnf->rtr_solicit_interval);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004969 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
4970 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
Thomas Graf93908d12010-11-17 01:44:24 +00004971 array[DEVCONF_RTR_SOLICIT_DELAY] =
4972 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02004974 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4975 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4976 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4977 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4979 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4980 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4981 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4982 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004984 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
Hangbin Liu8013d1d2015-07-30 14:28:42 +08004985 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004986 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004987#ifdef CONFIG_IPV6_ROUTER_PREF
4988 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00004989 array[DEVCONF_RTR_PROBE_INTERVAL] =
4990 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08004991#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzd860b2e2017-03-22 18:19:04 +09004992 array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004993 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4994#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004995#endif
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +09004996 array[DEVCONF_ACCEPT_RA_RT_TABLE] = cnf->accept_ra_rt_table;
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004997 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004998 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07004999#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5000 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
Erik Kline7fd25612014-10-28 18:11:14 +09005001 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
Neil Horman95c385b2007-04-25 17:08:10 -07005002#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005003#ifdef CONFIG_IPV6_MROUTE
5004 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
5005#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09005006 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09005007 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00005008 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00005009 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02005010 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
Ben Greeard9333192014-06-25 14:44:53 -07005011 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
Harout Hedeshianc2943f12015-01-20 10:06:05 -07005012 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
Andy Gospodarek35103d12015-08-13 10:39:01 -04005013 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005014 /* we omit DEVCONF_STABLE_SECRET for now */
Erik Kline3985e8a2015-07-22 16:38:25 +09005015 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
Johannes Bergabbc3042016-02-04 13:31:19 +01005016 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
Johannes Berg7a02bf82016-02-04 13:31:20 +01005017 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
David Ahernf1705ec2016-02-24 09:25:37 -08005018 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
Felix Jiad1f8a9b2017-01-26 16:59:17 +13005019 array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020}
5021
Thomas Grafb382b192010-11-16 04:33:57 +00005022static inline size_t inet6_ifla6_size(void)
5023{
5024 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5025 + nla_total_size(sizeof(struct ifla_cacheinfo))
5026 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5027 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005028 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5029 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
Thomas Grafb382b192010-11-16 04:33:57 +00005030}
5031
Thomas Graf339bf982006-11-10 14:10:15 -08005032static inline size_t inet6_if_nlmsg_size(void)
5033{
5034 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5035 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5036 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5037 + nla_total_size(4) /* IFLA_MTU */
5038 + nla_total_size(4) /* IFLA_LINK */
Andy Gospodarek03443382015-08-13 15:26:35 -04005039 + nla_total_size(1) /* IFLA_OPERSTATE */
Thomas Grafb382b192010-11-16 04:33:57 +00005040 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08005041}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005042
Eric Dumazetbe281e52011-05-19 01:14:23 +00005043static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Jia Heaca05672016-09-30 11:29:03 +08005044 int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005045{
5046 int i;
Jia Heaca05672016-09-30 11:29:03 +08005047 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005048 BUG_ON(pad < 0);
5049
5050 /* Use put_unaligned() because stats may not be aligned for u64. */
Jia Heaca05672016-09-30 11:29:03 +08005051 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5052 for (i = 1; i < ICMP6_MIB_MAX; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00005053 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005054
Jia Heaca05672016-09-30 11:29:03 +08005055 memset(&stats[ICMP6_MIB_MAX], 0, pad);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005056}
5057
WANG Cong698365f2014-05-05 15:55:55 -07005058static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305059 int bytes, size_t syncpoff)
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005060{
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305061 int i, c;
5062 u64 buff[IPSTATS_MIB_MAX];
5063 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5064
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005065 BUG_ON(pad < 0);
5066
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305067 memset(buff, 0, sizeof(buff));
5068 buff[0] = IPSTATS_MIB_MAX;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005069
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305070 for_each_possible_cpu(c) {
5071 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5072 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5073 }
5074
5075 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5076 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005077}
5078
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005079static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5080 int bytes)
5081{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005082 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005083 case IFLA_INET6_STATS:
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305084 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5085 offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005086 break;
5087 case IFLA_INET6_ICMP6STATS:
Jia Heaca05672016-09-30 11:29:03 +08005088 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005089 break;
5090 }
5091}
5092
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005093static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5094 u32 ext_filter_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005096 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08005097 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098
David S. Millerc78679e2012-04-01 20:27:33 -04005099 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5100 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08005102 ci.tstamp = cstamp_delta(idev->tstamp);
5103 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
Jiri Pirko1f9248e52013-12-07 19:26:53 +01005104 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
David S. Millerc78679e2012-04-01 20:27:33 -04005105 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5106 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005107 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
Ian Morris63159f22015-03-29 14:00:04 +01005108 if (!nla)
Thomas Graf04561c12006-11-14 19:53:58 -08005109 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005110 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005112 /* XXX - MC not implemented */
5113
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005114 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5115 return 0;
5116
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005117 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005118 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005119 goto nla_put_failure;
5120 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5121
5122 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005123 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005124 goto nla_put_failure;
5125 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005127 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
Ian Morris63159f22015-03-29 14:00:04 +01005128 if (!nla)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005129 goto nla_put_failure;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005130
Felix Jiad1f8a9b2017-01-26 16:59:17 +13005131 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005132 goto nla_put_failure;
5133
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005134 read_lock_bh(&idev->lock);
5135 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5136 read_unlock_bh(&idev->lock);
5137
Thomas Grafb382b192010-11-16 04:33:57 +00005138 return 0;
5139
5140nla_put_failure:
5141 return -EMSGSIZE;
5142}
5143
Arad, Ronenb1974ed2015-10-19 09:23:28 -07005144static size_t inet6_get_link_af_size(const struct net_device *dev,
5145 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005146{
5147 if (!__in6_dev_get(dev))
5148 return 0;
5149
5150 return inet6_ifla6_size();
5151}
5152
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005153static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5154 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005155{
5156 struct inet6_dev *idev = __in6_dev_get(dev);
5157
5158 if (!idev)
5159 return -ENODATA;
5160
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005161 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005162 return -EMSGSIZE;
5163
5164 return 0;
5165}
5166
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005167static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5168{
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005169 struct inet6_ifaddr *ifp;
5170 struct net_device *dev = idev->dev;
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005171 bool clear_token, update_rs = false;
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005172 struct in6_addr ll_addr;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005173
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005174 ASSERT_RTNL();
5175
Ian Morris63159f22015-03-29 14:00:04 +01005176 if (!token)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005177 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005178 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5179 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005180 if (!ipv6_accept_ra(idev))
5181 return -EINVAL;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005182 if (idev->cnf.rtr_solicits == 0)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005183 return -EINVAL;
5184
5185 write_lock_bh(&idev->lock);
5186
5187 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5188 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5189
5190 write_unlock_bh(&idev->lock);
5191
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005192 clear_token = ipv6_addr_any(token);
5193 if (clear_token)
5194 goto update_lft;
5195
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005196 if (!idev->dead && (idev->if_flags & IF_READY) &&
5197 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5198 IFA_F_OPTIMISTIC)) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005199 /* If we're not ready, then normal ifup will take care
5200 * of this. Otherwise, we need to request our rs here.
5201 */
5202 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5203 update_rs = true;
5204 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005205
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005206update_lft:
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005207 write_lock_bh(&idev->lock);
Daniel Borkmannfc403832013-04-09 03:47:15 +00005208
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005209 if (update_rs) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005210 idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005211 idev->rs_interval = rfc3315_s14_backoff_init(
5212 idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005213 idev->rs_probes = 1;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005214 addrconf_mod_rs_timer(idev, idev->rs_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005215 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005216
5217 /* Well, that's kinda nasty ... */
5218 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5219 spin_lock(&ifp->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00005220 if (ifp->tokenized) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005221 ifp->valid_lft = 0;
5222 ifp->prefered_lft = 0;
5223 }
5224 spin_unlock(&ifp->lock);
5225 }
5226
5227 write_unlock_bh(&idev->lock);
Lubomir Rintelb2ed64a2014-10-27 17:39:16 +01005228 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005229 addrconf_verify_rtnl();
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005230 return 0;
5231}
5232
Daniel Borkmann11b1f822015-02-05 14:39:11 +01005233static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5234 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5235 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5236};
5237
5238static int inet6_validate_link_af(const struct net_device *dev,
5239 const struct nlattr *nla)
5240{
5241 struct nlattr *tb[IFLA_INET6_MAX + 1];
5242
5243 if (dev && !__in6_dev_get(dev))
5244 return -EAFNOSUPPORT;
5245
5246 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
5247}
5248
Felix Jiad1f8a9b2017-01-26 16:59:17 +13005249static int check_addr_gen_mode(int mode)
5250{
5251 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5252 mode != IN6_ADDR_GEN_MODE_NONE &&
5253 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5254 mode != IN6_ADDR_GEN_MODE_RANDOM)
5255 return -EINVAL;
5256 return 1;
5257}
5258
5259static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5260 int mode)
5261{
5262 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5263 !idev->cnf.stable_secret.initialized &&
5264 !net->ipv6.devconf_dflt->stable_secret.initialized)
5265 return -EINVAL;
5266 return 1;
5267}
5268
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005269static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5270{
5271 int err = -EINVAL;
5272 struct inet6_dev *idev = __in6_dev_get(dev);
5273 struct nlattr *tb[IFLA_INET6_MAX + 1];
5274
5275 if (!idev)
5276 return -EAFNOSUPPORT;
5277
5278 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
5279 BUG();
5280
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005281 if (tb[IFLA_INET6_TOKEN]) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005282 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005283 if (err)
5284 return err;
5285 }
5286
5287 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5288 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5289
Felix Jiad1f8a9b2017-01-26 16:59:17 +13005290 if (check_addr_gen_mode(mode) < 0 ||
5291 check_stable_privacy(idev, dev_net(dev), mode) < 0)
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005292 return -EINVAL;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005293
Felix Jiad1f8a9b2017-01-26 16:59:17 +13005294 idev->cnf.addr_gen_mode = mode;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005295 err = 0;
5296 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005297
5298 return err;
5299}
5300
Thomas Grafb382b192010-11-16 04:33:57 +00005301static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005302 u32 portid, u32 seq, int event, unsigned int flags)
Thomas Grafb382b192010-11-16 04:33:57 +00005303{
5304 struct net_device *dev = idev->dev;
5305 struct ifinfomsg *hdr;
5306 struct nlmsghdr *nlh;
5307 void *protoinfo;
5308
Eric W. Biederman15e47302012-09-07 20:12:54 +00005309 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005310 if (!nlh)
Thomas Grafb382b192010-11-16 04:33:57 +00005311 return -EMSGSIZE;
5312
5313 hdr = nlmsg_data(nlh);
5314 hdr->ifi_family = AF_INET6;
5315 hdr->__ifi_pad = 0;
5316 hdr->ifi_type = dev->type;
5317 hdr->ifi_index = dev->ifindex;
5318 hdr->ifi_flags = dev_get_flags(dev);
5319 hdr->ifi_change = 0;
5320
David S. Millerc78679e2012-04-01 20:27:33 -04005321 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5322 (dev->addr_len &&
5323 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5324 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02005325 (dev->ifindex != dev_get_iflink(dev) &&
Andy Gospodarek03443382015-08-13 15:26:35 -04005326 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5327 nla_put_u8(skb, IFLA_OPERSTATE,
5328 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
David S. Millerc78679e2012-04-01 20:27:33 -04005329 goto nla_put_failure;
Thomas Grafb382b192010-11-16 04:33:57 +00005330 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
Ian Morris63159f22015-03-29 14:00:04 +01005331 if (!protoinfo)
Thomas Grafb382b192010-11-16 04:33:57 +00005332 goto nla_put_failure;
5333
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005334 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005335 goto nla_put_failure;
5336
Thomas Graf04561c12006-11-14 19:53:58 -08005337 nla_nest_end(skb, protoinfo);
Johannes Berg053c0952015-01-16 22:09:00 +01005338 nlmsg_end(skb, nlh);
5339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340
Thomas Graf04561c12006-11-14 19:53:58 -08005341nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005342 nlmsg_cancel(skb, nlh);
5343 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344}
5345
5346static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5347{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005348 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00005349 int h, s_h;
David S. Miller434a8a582009-11-11 18:53:00 -08005350 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 struct net_device *dev;
5352 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00005353 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354
Eric Dumazet84d26972009-11-09 12:11:28 +00005355 s_h = cb->args[0];
5356 s_idx = cb->args[1];
5357
5358 rcu_read_lock();
5359 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5360 idx = 0;
5361 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08005362 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet84d26972009-11-09 12:11:28 +00005363 if (idx < s_idx)
5364 goto cont;
5365 idev = __in6_dev_get(dev);
5366 if (!idev)
5367 goto cont;
5368 if (inet6_fill_ifinfo(skb, idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005369 NETLINK_CB(cb->skb).portid,
Eric Dumazet84d26972009-11-09 12:11:28 +00005370 cb->nlh->nlmsg_seq,
Johannes Berg053c0952015-01-16 22:09:00 +01005371 RTM_NEWLINK, NLM_F_MULTI) < 0)
Eric Dumazet84d26972009-11-09 12:11:28 +00005372 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07005373cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00005374 idx++;
5375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 }
Eric Dumazet84d26972009-11-09 12:11:28 +00005377out:
5378 rcu_read_unlock();
5379 cb->args[1] = idx;
5380 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381
5382 return skb->len;
5383}
5384
5385void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5386{
5387 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005388 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005389 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005390
Thomas Graf339bf982006-11-10 14:10:15 -08005391 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005392 if (!skb)
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005393 goto errout;
5394
5395 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005396 if (err < 0) {
5397 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5398 WARN_ON(err == -EMSGSIZE);
5399 kfree_skb(skb);
5400 goto errout;
5401 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005402 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005403 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005404errout:
5405 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005406 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407}
5408
Thomas Graf339bf982006-11-10 14:10:15 -08005409static inline size_t inet6_prefix_nlmsg_size(void)
5410{
5411 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5412 + nla_total_size(sizeof(struct in6_addr))
5413 + nla_total_size(sizeof(struct prefix_cacheinfo));
5414}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005415
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005417 struct prefix_info *pinfo, u32 portid, u32 seq,
Thomas Graf6051e2f2006-11-14 19:54:19 -08005418 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419{
Thomas Graf6051e2f2006-11-14 19:54:19 -08005420 struct prefixmsg *pmsg;
5421 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 struct prefix_cacheinfo ci;
5423
Eric W. Biederman15e47302012-09-07 20:12:54 +00005424 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005425 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005426 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08005427
5428 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07005430 pmsg->prefix_pad1 = 0;
5431 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 pmsg->prefix_ifindex = idev->dev->ifindex;
5433 pmsg->prefix_len = pinfo->prefix_len;
5434 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07005435 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 pmsg->prefix_flags = 0;
5437 if (pinfo->onlink)
5438 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5439 if (pinfo->autoconf)
5440 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5441
David S. Millerc78679e2012-04-01 20:27:33 -04005442 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5443 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 ci.preferred_time = ntohl(pinfo->prefered);
5445 ci.valid_time = ntohl(pinfo->valid);
David S. Millerc78679e2012-04-01 20:27:33 -04005446 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5447 goto nla_put_failure;
Johannes Berg053c0952015-01-16 22:09:00 +01005448 nlmsg_end(skb, nlh);
5449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
Thomas Graf6051e2f2006-11-14 19:54:19 -08005451nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005452 nlmsg_cancel(skb, nlh);
5453 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454}
5455
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005456static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 struct prefix_info *pinfo)
5458{
5459 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005460 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005461 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
Thomas Graf339bf982006-11-10 14:10:15 -08005463 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005464 if (!skb)
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005465 goto errout;
5466
5467 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005468 if (err < 0) {
5469 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5470 WARN_ON(err == -EMSGSIZE);
5471 kfree_skb(skb);
5472 goto errout;
5473 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005474 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5475 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005476errout:
5477 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08005478 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479}
5480
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5482{
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005483 struct net *net = dev_net(ifp->idev->dev);
5484
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005485 if (event)
5486 ASSERT_RTNL();
5487
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5489
5490 switch (event) {
5491 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07005492 /*
5493 * If the address was optimistic
5494 * we inserted the route at the start of
5495 * our DAD process, so we don't need
5496 * to do it again
5497 */
Wei Wang43c792a2017-08-25 15:03:10 -07005498 if (!rcu_access_pointer(ifp->rt->rt6i_node))
Neil Horman95c385b2007-04-25 17:08:10 -07005499 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 if (ifp->idev->cnf.forwarding)
5501 addrconf_join_anycast(ifp);
Cong Wang7996c792013-05-22 05:41:06 +00005502 if (!ipv6_addr_any(&ifp->peer_addr))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005503 addrconf_prefix_route(&ifp->peer_addr, 128,
5504 ifp->idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 break;
5506 case RTM_DELADDR:
5507 if (ifp->idev->cnf.forwarding)
5508 addrconf_leave_anycast(ifp);
5509 addrconf_leave_solict(ifp->idev, &ifp->addr);
Cong Wang7996c792013-05-22 05:41:06 +00005510 if (!ipv6_addr_any(&ifp->peer_addr)) {
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005511 struct rt6_info *rt;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005512
Nicolas Dichtele7478df2014-09-03 23:59:22 +02005513 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5514 ifp->idev->dev, 0, 0);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07005515 if (rt)
5516 ip6_del_rt(rt);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005517 }
David Ahern38bd10c2016-04-21 20:56:12 -07005518 if (ifp->rt) {
5519 dst_hold(&ifp->rt->dst);
5520 ip6_del_rt(ifp->rt);
5521 }
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02005522 rt_genid_bump_ipv6(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 break;
5524 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005525 atomic_inc(&net->ipv6.dev_addr_genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526}
5527
5528static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5529{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005530 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 if (likely(ifp->idev->dead == 0))
5532 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005533 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534}
5535
5536#ifdef CONFIG_SYSCTL
5537
5538static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005539int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 void __user *buffer, size_t *lenp, loff_t *ppos)
5541{
5542 int *valp = ctl->data;
5543 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005544 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005545 struct ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 int ret;
5547
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005548 /*
5549 * ctl->data points to idev->cnf.forwarding, we should
5550 * not modify it until we get the rtnl lock.
5551 */
5552 lctl = *ctl;
5553 lctl.data = &val;
5554
5555 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08005557 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00005558 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005559 if (ret)
5560 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005561 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562}
5563
Marcelo Leitner77751422015-02-23 11:17:13 -03005564static
5565int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5566 void __user *buffer, size_t *lenp, loff_t *ppos)
5567{
5568 struct inet6_dev *idev = ctl->extra1;
5569 int min_mtu = IPV6_MIN_MTU;
5570 struct ctl_table lctl;
5571
5572 lctl = *ctl;
5573 lctl.extra1 = &min_mtu;
5574 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5575
5576 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5577}
5578
Brian Haley56d417b2009-06-01 03:07:33 -07005579static void dev_disable_change(struct inet6_dev *idev)
5580{
Cong Wang75538c22013-05-29 11:30:50 +08005581 struct netdev_notifier_info info;
5582
Brian Haley56d417b2009-06-01 03:07:33 -07005583 if (!idev || !idev->dev)
5584 return;
5585
Cong Wang75538c22013-05-29 11:30:50 +08005586 netdev_notifier_info_init(&info, idev->dev);
Brian Haley56d417b2009-06-01 03:07:33 -07005587 if (idev->cnf.disable_ipv6)
Cong Wang75538c22013-05-29 11:30:50 +08005588 addrconf_notify(NULL, NETDEV_DOWN, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005589 else
Cong Wang75538c22013-05-29 11:30:50 +08005590 addrconf_notify(NULL, NETDEV_UP, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005591}
5592
5593static void addrconf_disable_change(struct net *net, __s32 newf)
5594{
5595 struct net_device *dev;
5596 struct inet6_dev *idev;
5597
Kefeng Wang79453ab2017-01-19 16:26:21 +08005598 for_each_netdev(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07005599 idev = __in6_dev_get(dev);
5600 if (idev) {
5601 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5602 idev->cnf.disable_ipv6 = newf;
5603 if (changed)
5604 dev_disable_change(idev);
5605 }
Brian Haley56d417b2009-06-01 03:07:33 -07005606 }
Brian Haley56d417b2009-06-01 03:07:33 -07005607}
5608
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005609static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07005610{
5611 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005612 int old;
5613
5614 if (!rtnl_trylock())
5615 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07005616
5617 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005618 old = *p;
5619 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07005620
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005621 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5622 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07005623 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005624 }
Brian Haley56d417b2009-06-01 03:07:33 -07005625
5626 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07005627 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5628 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005629 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07005630 dev_disable_change((struct inet6_dev *)table->extra1);
5631
5632 rtnl_unlock();
5633 return 0;
5634}
5635
5636static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005637int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07005638 void __user *buffer, size_t *lenp, loff_t *ppos)
5639{
5640 int *valp = ctl->data;
5641 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005642 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005643 struct ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07005644 int ret;
5645
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005646 /*
5647 * ctl->data points to idev->cnf.disable_ipv6, we should
5648 * not modify it until we get the rtnl lock.
5649 */
5650 lctl = *ctl;
5651 lctl.data = &val;
5652
5653 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07005654
5655 if (write)
5656 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005657 if (ret)
5658 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07005659 return ret;
5660}
5661
stephen hemmingerc92d5492013-12-17 22:37:14 -08005662static
5663int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5664 void __user *buffer, size_t *lenp, loff_t *ppos)
5665{
5666 int *valp = ctl->data;
5667 int ret;
5668 int old, new;
5669
5670 old = *valp;
5671 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5672 new = *valp;
5673
5674 if (write && old != new) {
5675 struct net *net = ctl->extra2;
5676
5677 if (!rtnl_trylock())
5678 return restart_syscall();
5679
5680 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5681 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5682 NETCONFA_IFINDEX_DEFAULT,
5683 net->ipv6.devconf_dflt);
5684 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5685 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5686 NETCONFA_IFINDEX_ALL,
5687 net->ipv6.devconf_all);
5688 else {
5689 struct inet6_dev *idev = ctl->extra1;
5690
5691 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5692 idev->dev->ifindex,
5693 &idev->cnf);
5694 }
5695 rtnl_unlock();
5696 }
5697
5698 return ret;
5699}
5700
Felix Jiad1f8a9b2017-01-26 16:59:17 +13005701static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
5702 void __user *buffer, size_t *lenp,
5703 loff_t *ppos)
5704{
5705 int ret = 0;
5706 int new_val;
5707 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
5708 struct net *net = (struct net *)ctl->extra2;
5709
5710 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5711
5712 if (write) {
5713 new_val = *((int *)ctl->data);
5714
5715 if (check_addr_gen_mode(new_val) < 0)
5716 return -EINVAL;
5717
5718 /* request for default */
5719 if (&net->ipv6.devconf_dflt->addr_gen_mode == ctl->data) {
5720 ipv6_devconf_dflt.addr_gen_mode = new_val;
5721
5722 /* request for individual net device */
5723 } else {
5724 if (!idev)
5725 return ret;
5726
5727 if (check_stable_privacy(idev, net, new_val) < 0)
5728 return -EINVAL;
5729
5730 if (idev->cnf.addr_gen_mode != new_val) {
5731 idev->cnf.addr_gen_mode = new_val;
5732 rtnl_lock();
5733 addrconf_dev_config(idev->dev);
5734 rtnl_unlock();
5735 }
5736 }
5737 }
5738
5739 return ret;
5740}
5741
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005742static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5743 void __user *buffer, size_t *lenp,
5744 loff_t *ppos)
5745{
5746 int err;
5747 struct in6_addr addr;
5748 char str[IPV6_MAX_STRLEN];
5749 struct ctl_table lctl = *ctl;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005750 struct net *net = ctl->extra2;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005751 struct ipv6_stable_secret *secret = ctl->data;
5752
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005753 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5754 return -EIO;
5755
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005756 lctl.maxlen = IPV6_MAX_STRLEN;
5757 lctl.data = str;
5758
5759 if (!rtnl_trylock())
5760 return restart_syscall();
5761
5762 if (!write && !secret->initialized) {
5763 err = -EIO;
5764 goto out;
5765 }
5766
WANG Cong5449a5c2015-12-21 10:55:45 -08005767 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
5768 if (err >= sizeof(str)) {
5769 err = -EIO;
5770 goto out;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005771 }
5772
5773 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5774 if (err || !write)
5775 goto out;
5776
5777 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5778 err = -EIO;
5779 goto out;
5780 }
5781
5782 secret->initialized = true;
5783 secret->secret = addr;
5784
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005785 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5786 struct net_device *dev;
5787
5788 for_each_netdev(net, dev) {
5789 struct inet6_dev *idev = __in6_dev_get(dev);
5790
5791 if (idev) {
Felix Jiad1f8a9b2017-01-26 16:59:17 +13005792 idev->cnf.addr_gen_mode =
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005793 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5794 }
5795 }
5796 } else {
5797 struct inet6_dev *idev = ctl->extra1;
5798
Felix Jiad1f8a9b2017-01-26 16:59:17 +13005799 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005800 }
5801
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005802out:
5803 rtnl_unlock();
5804
5805 return err;
5806}
stephen hemmingerc92d5492013-12-17 22:37:14 -08005807
Andy Gospodarek35103d12015-08-13 10:39:01 -04005808static
5809int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
5810 int write,
5811 void __user *buffer,
5812 size_t *lenp,
5813 loff_t *ppos)
5814{
5815 int *valp = ctl->data;
5816 int val = *valp;
5817 loff_t pos = *ppos;
5818 struct ctl_table lctl;
5819 int ret;
5820
5821 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
5822 * we should not modify it until we get the rtnl lock.
5823 */
5824 lctl = *ctl;
5825 lctl.data = &val;
5826
5827 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5828
5829 if (write)
5830 ret = addrconf_fixup_linkdown(ctl, valp, val);
5831 if (ret)
5832 *ppos = pos;
5833 return ret;
5834}
5835
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07005836static int minus_one = -1;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07005837static const int one = 1;
5838static const int two_five_five = 255;
5839
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03005840static const struct ctl_table addrconf_sysctl[] = {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005841 {
5842 .procname = "forwarding",
5843 .data = &ipv6_devconf.forwarding,
5844 .maxlen = sizeof(int),
5845 .mode = 0644,
5846 .proc_handler = addrconf_sysctl_forward,
5847 },
5848 {
5849 .procname = "hop_limit",
5850 .data = &ipv6_devconf.hop_limit,
5851 .maxlen = sizeof(int),
5852 .mode = 0644,
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07005853 .proc_handler = proc_dointvec_minmax,
5854 .extra1 = (void *)&one,
5855 .extra2 = (void *)&two_five_five,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005856 },
5857 {
5858 .procname = "mtu",
5859 .data = &ipv6_devconf.mtu6,
5860 .maxlen = sizeof(int),
5861 .mode = 0644,
5862 .proc_handler = addrconf_sysctl_mtu,
5863 },
5864 {
5865 .procname = "accept_ra",
5866 .data = &ipv6_devconf.accept_ra,
5867 .maxlen = sizeof(int),
5868 .mode = 0644,
5869 .proc_handler = proc_dointvec,
5870 },
5871 {
5872 .procname = "accept_redirects",
5873 .data = &ipv6_devconf.accept_redirects,
5874 .maxlen = sizeof(int),
5875 .mode = 0644,
5876 .proc_handler = proc_dointvec,
5877 },
5878 {
5879 .procname = "autoconf",
5880 .data = &ipv6_devconf.autoconf,
5881 .maxlen = sizeof(int),
5882 .mode = 0644,
5883 .proc_handler = proc_dointvec,
5884 },
5885 {
5886 .procname = "dad_transmits",
5887 .data = &ipv6_devconf.dad_transmits,
5888 .maxlen = sizeof(int),
5889 .mode = 0644,
5890 .proc_handler = proc_dointvec,
5891 },
5892 {
5893 .procname = "router_solicitations",
5894 .data = &ipv6_devconf.rtr_solicits,
5895 .maxlen = sizeof(int),
5896 .mode = 0644,
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07005897 .proc_handler = proc_dointvec_minmax,
5898 .extra1 = &minus_one,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005899 },
5900 {
5901 .procname = "router_solicitation_interval",
5902 .data = &ipv6_devconf.rtr_solicit_interval,
5903 .maxlen = sizeof(int),
5904 .mode = 0644,
5905 .proc_handler = proc_dointvec_jiffies,
5906 },
5907 {
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005908 .procname = "router_solicitation_max_interval",
5909 .data = &ipv6_devconf.rtr_solicit_max_interval,
5910 .maxlen = sizeof(int),
5911 .mode = 0644,
5912 .proc_handler = proc_dointvec_jiffies,
5913 },
5914 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005915 .procname = "router_solicitation_delay",
5916 .data = &ipv6_devconf.rtr_solicit_delay,
5917 .maxlen = sizeof(int),
5918 .mode = 0644,
5919 .proc_handler = proc_dointvec_jiffies,
5920 },
5921 {
5922 .procname = "force_mld_version",
5923 .data = &ipv6_devconf.force_mld_version,
5924 .maxlen = sizeof(int),
5925 .mode = 0644,
5926 .proc_handler = proc_dointvec,
5927 },
5928 {
5929 .procname = "mldv1_unsolicited_report_interval",
5930 .data =
5931 &ipv6_devconf.mldv1_unsolicited_report_interval,
5932 .maxlen = sizeof(int),
5933 .mode = 0644,
5934 .proc_handler = proc_dointvec_ms_jiffies,
5935 },
5936 {
5937 .procname = "mldv2_unsolicited_report_interval",
5938 .data =
5939 &ipv6_devconf.mldv2_unsolicited_report_interval,
5940 .maxlen = sizeof(int),
5941 .mode = 0644,
5942 .proc_handler = proc_dointvec_ms_jiffies,
5943 },
5944 {
5945 .procname = "use_tempaddr",
5946 .data = &ipv6_devconf.use_tempaddr,
5947 .maxlen = sizeof(int),
5948 .mode = 0644,
5949 .proc_handler = proc_dointvec,
5950 },
5951 {
5952 .procname = "temp_valid_lft",
5953 .data = &ipv6_devconf.temp_valid_lft,
5954 .maxlen = sizeof(int),
5955 .mode = 0644,
5956 .proc_handler = proc_dointvec,
5957 },
5958 {
5959 .procname = "temp_prefered_lft",
5960 .data = &ipv6_devconf.temp_prefered_lft,
5961 .maxlen = sizeof(int),
5962 .mode = 0644,
5963 .proc_handler = proc_dointvec,
5964 },
5965 {
5966 .procname = "regen_max_retry",
5967 .data = &ipv6_devconf.regen_max_retry,
5968 .maxlen = sizeof(int),
5969 .mode = 0644,
5970 .proc_handler = proc_dointvec,
5971 },
5972 {
5973 .procname = "max_desync_factor",
5974 .data = &ipv6_devconf.max_desync_factor,
5975 .maxlen = sizeof(int),
5976 .mode = 0644,
5977 .proc_handler = proc_dointvec,
5978 },
5979 {
5980 .procname = "max_addresses",
5981 .data = &ipv6_devconf.max_addresses,
5982 .maxlen = sizeof(int),
5983 .mode = 0644,
5984 .proc_handler = proc_dointvec,
5985 },
5986 {
5987 .procname = "accept_ra_defrtr",
5988 .data = &ipv6_devconf.accept_ra_defrtr,
5989 .maxlen = sizeof(int),
5990 .mode = 0644,
5991 .proc_handler = proc_dointvec,
5992 },
5993 {
5994 .procname = "accept_ra_min_hop_limit",
5995 .data = &ipv6_devconf.accept_ra_min_hop_limit,
5996 .maxlen = sizeof(int),
5997 .mode = 0644,
5998 .proc_handler = proc_dointvec,
5999 },
6000 {
6001 .procname = "accept_ra_pinfo",
6002 .data = &ipv6_devconf.accept_ra_pinfo,
6003 .maxlen = sizeof(int),
6004 .mode = 0644,
6005 .proc_handler = proc_dointvec,
6006 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08006007#ifdef CONFIG_IPV6_ROUTER_PREF
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006008 {
6009 .procname = "accept_ra_rtr_pref",
6010 .data = &ipv6_devconf.accept_ra_rtr_pref,
6011 .maxlen = sizeof(int),
6012 .mode = 0644,
6013 .proc_handler = proc_dointvec,
6014 },
6015 {
6016 .procname = "router_probe_interval",
6017 .data = &ipv6_devconf.rtr_probe_interval,
6018 .maxlen = sizeof(int),
6019 .mode = 0644,
6020 .proc_handler = proc_dointvec_jiffies,
6021 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08006022#ifdef CONFIG_IPV6_ROUTE_INFO
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006023 {
Joel Scherpelzd860b2e2017-03-22 18:19:04 +09006024 .procname = "accept_ra_rt_info_min_plen",
6025 .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
6026 .maxlen = sizeof(int),
6027 .mode = 0644,
6028 .proc_handler = proc_dointvec,
6029 },
6030 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006031 .procname = "accept_ra_rt_info_max_plen",
6032 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
6033 .maxlen = sizeof(int),
6034 .mode = 0644,
6035 .proc_handler = proc_dointvec,
6036 },
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08006037#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08006038#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006039 {
Lorenzo Colitti6b1064d2014-03-26 19:35:41 +09006040 .procname = "accept_ra_rt_table",
6041 .data = &ipv6_devconf.accept_ra_rt_table,
6042 .maxlen = sizeof(int),
6043 .mode = 0644,
6044 .proc_handler = proc_dointvec,
6045 },
6046 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006047 .procname = "proxy_ndp",
6048 .data = &ipv6_devconf.proxy_ndp,
6049 .maxlen = sizeof(int),
6050 .mode = 0644,
6051 .proc_handler = addrconf_sysctl_proxy_ndp,
6052 },
6053 {
6054 .procname = "accept_source_route",
6055 .data = &ipv6_devconf.accept_source_route,
6056 .maxlen = sizeof(int),
6057 .mode = 0644,
6058 .proc_handler = proc_dointvec,
6059 },
Neil Horman95c385b2007-04-25 17:08:10 -07006060#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006061 {
6062 .procname = "optimistic_dad",
6063 .data = &ipv6_devconf.optimistic_dad,
6064 .maxlen = sizeof(int),
6065 .mode = 0644,
6066 .proc_handler = proc_dointvec,
6067 },
6068 {
6069 .procname = "use_optimistic",
6070 .data = &ipv6_devconf.use_optimistic,
6071 .maxlen = sizeof(int),
6072 .mode = 0644,
6073 .proc_handler = proc_dointvec,
6074 },
Neil Horman95c385b2007-04-25 17:08:10 -07006075#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006076#ifdef CONFIG_IPV6_MROUTE
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006077 {
6078 .procname = "mc_forwarding",
6079 .data = &ipv6_devconf.mc_forwarding,
6080 .maxlen = sizeof(int),
6081 .mode = 0444,
6082 .proc_handler = proc_dointvec,
6083 },
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006084#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006085 {
6086 .procname = "disable_ipv6",
6087 .data = &ipv6_devconf.disable_ipv6,
6088 .maxlen = sizeof(int),
6089 .mode = 0644,
6090 .proc_handler = addrconf_sysctl_disable,
6091 },
6092 {
6093 .procname = "accept_dad",
6094 .data = &ipv6_devconf.accept_dad,
6095 .maxlen = sizeof(int),
6096 .mode = 0644,
6097 .proc_handler = proc_dointvec,
6098 },
6099 {
6100 .procname = "force_tllao",
6101 .data = &ipv6_devconf.force_tllao,
6102 .maxlen = sizeof(int),
6103 .mode = 0644,
6104 .proc_handler = proc_dointvec
6105 },
6106 {
6107 .procname = "ndisc_notify",
6108 .data = &ipv6_devconf.ndisc_notify,
6109 .maxlen = sizeof(int),
6110 .mode = 0644,
6111 .proc_handler = proc_dointvec
6112 },
6113 {
6114 .procname = "suppress_frag_ndisc",
6115 .data = &ipv6_devconf.suppress_frag_ndisc,
6116 .maxlen = sizeof(int),
6117 .mode = 0644,
6118 .proc_handler = proc_dointvec
6119 },
6120 {
6121 .procname = "accept_ra_from_local",
6122 .data = &ipv6_devconf.accept_ra_from_local,
6123 .maxlen = sizeof(int),
6124 .mode = 0644,
6125 .proc_handler = proc_dointvec,
6126 },
6127 {
6128 .procname = "accept_ra_mtu",
6129 .data = &ipv6_devconf.accept_ra_mtu,
6130 .maxlen = sizeof(int),
6131 .mode = 0644,
6132 .proc_handler = proc_dointvec,
6133 },
6134 {
6135 .procname = "stable_secret",
6136 .data = &ipv6_devconf.stable_secret,
6137 .maxlen = IPV6_MAX_STRLEN,
6138 .mode = 0600,
6139 .proc_handler = addrconf_sysctl_stable_secret,
6140 },
6141 {
6142 .procname = "use_oif_addrs_only",
6143 .data = &ipv6_devconf.use_oif_addrs_only,
6144 .maxlen = sizeof(int),
6145 .mode = 0644,
6146 .proc_handler = proc_dointvec,
6147 },
6148 {
6149 .procname = "ignore_routes_with_linkdown",
6150 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6151 .maxlen = sizeof(int),
6152 .mode = 0644,
6153 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6154 },
6155 {
6156 .procname = "drop_unicast_in_l2_multicast",
6157 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6158 .maxlen = sizeof(int),
6159 .mode = 0644,
6160 .proc_handler = proc_dointvec,
6161 },
6162 {
6163 .procname = "drop_unsolicited_na",
6164 .data = &ipv6_devconf.drop_unsolicited_na,
6165 .maxlen = sizeof(int),
6166 .mode = 0644,
6167 .proc_handler = proc_dointvec,
6168 },
6169 {
6170 .procname = "keep_addr_on_down",
6171 .data = &ipv6_devconf.keep_addr_on_down,
6172 .maxlen = sizeof(int),
6173 .mode = 0644,
6174 .proc_handler = proc_dointvec,
Felix Jiad1f8a9b2017-01-26 16:59:17 +13006175 },
6176 {
6177 .procname = "addr_gen_mode",
6178 .data = &ipv6_devconf.addr_gen_mode,
6179 .maxlen = sizeof(int),
6180 .mode = 0644,
6181 .proc_handler = addrconf_sysctl_addr_gen_mode,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006182 },
6183 {
6184 /* sentinel */
6185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186};
6187
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08006188static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006189 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190{
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006191 int i, ifindex;
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006192 struct ctl_table *table;
Eric W. Biederman6105e292012-04-19 13:41:24 +00006193 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11006194
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006195 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6196 if (!table)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006197 goto out;
6198
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006199 for (i = 0; table[i].data; i++) {
6200 table[i].data += (char *)p - (char *)&ipv6_devconf;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006201 /* If one of these is already set, then it is not safe to
6202 * overwrite either of them: this makes proc_dointvec_minmax
6203 * usable.
6204 */
6205 if (!table[i].extra1 && !table[i].extra2) {
6206 table[i].extra1 = idev; /* embedded; no ref */
6207 table[i].extra2 = net;
6208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210
Eric W. Biederman6105e292012-04-19 13:41:24 +00006211 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006213 p->sysctl_header = register_net_sysctl(net, path, table);
6214 if (!p->sysctl_header)
Eric W. Biederman6105e292012-04-19 13:41:24 +00006215 goto free;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006216
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006217 if (!strcmp(dev_name, "all"))
6218 ifindex = NETCONFA_IFINDEX_ALL;
6219 else if (!strcmp(dev_name, "default"))
6220 ifindex = NETCONFA_IFINDEX_DEFAULT;
6221 else
6222 ifindex = idev->dev->ifindex;
6223 inet6_netconf_notify_devconf(net, NETCONFA_ALL, ifindex, p);
Pavel Emelyanov95897312008-01-10 17:41:45 -08006224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006226free:
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006227 kfree(table);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006228out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08006229 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230}
6231
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006232static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
6233{
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006234 struct ctl_table *table;
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006235
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006236 if (!p->sysctl_header)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006237 return;
6238
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006239 table = p->sysctl_header->ctl_table_arg;
6240 unregister_net_sysctl_table(p->sysctl_header);
6241 p->sysctl_header = NULL;
6242 kfree(table);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006243}
6244
WANG Conga317a2f2014-07-25 15:25:09 -07006245static int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006246{
WANG Conga317a2f2014-07-25 15:25:09 -07006247 int err;
6248
6249 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6250 return -EINVAL;
6251
6252 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6253 &ndisc_ifinfo_sysctl_change);
6254 if (err)
6255 return err;
6256 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6257 idev, &idev->cnf);
6258 if (err)
6259 neigh_sysctl_unregister(idev->nd_parms);
6260
6261 return err;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006262}
6263
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006264static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006266 __addrconf_sysctl_unregister(&idev->cnf);
6267 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268}
6269
6270
6271#endif
6272
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006273static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006274{
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006275 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006276 struct ipv6_devconf *all, *dflt;
6277
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006278 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006279 if (!all)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006280 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006281
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006282 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006283 if (!dflt)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006284 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006285
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006286 /* these will be inherited by all namespaces */
6287 dflt->autoconf = ipv6_defaults.autoconf;
6288 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006289
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006290 dflt->stable_secret.initialized = false;
6291 all->stable_secret.initialized = false;
6292
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006293 net->ipv6.devconf_all = all;
6294 net->ipv6.devconf_dflt = dflt;
6295
6296#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006297 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006298 if (err < 0)
6299 goto err_reg_all;
6300
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006301 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006302 if (err < 0)
6303 goto err_reg_dflt;
6304#endif
6305 return 0;
6306
6307#ifdef CONFIG_SYSCTL
6308err_reg_dflt:
6309 __addrconf_sysctl_unregister(all);
6310err_reg_all:
6311 kfree(dflt);
6312#endif
6313err_alloc_dflt:
6314 kfree(all);
6315err_alloc_all:
6316 return err;
6317}
6318
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006319static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006320{
6321#ifdef CONFIG_SYSCTL
6322 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
6323 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
6324#endif
zhuyj73cf0e92014-11-26 10:25:58 +08006325 kfree(net->ipv6.devconf_dflt);
6326 kfree(net->ipv6.devconf_all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006327}
6328
6329static struct pernet_operations addrconf_ops = {
6330 .init = addrconf_init_net,
6331 .exit = addrconf_exit_net,
6332};
6333
Daniel Borkmann207895f2015-01-29 12:15:03 +01006334static struct rtnl_af_ops inet6_ops __read_mostly = {
Thomas Grafb382b192010-11-16 04:33:57 +00006335 .family = AF_INET6,
6336 .fill_link_af = inet6_fill_link_af,
6337 .get_link_af_size = inet6_get_link_af_size,
Daniel Borkmann11b1f822015-02-05 14:39:11 +01006338 .validate_link_af = inet6_validate_link_af,
Daniel Borkmannf53adae2013-04-08 04:01:30 +00006339 .set_link_af = inet6_set_link_af,
Thomas Grafb382b192010-11-16 04:33:57 +00006340};
6341
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342/*
6343 * Init / cleanup code
6344 */
6345
6346int __init addrconf_init(void)
6347{
WANG Conga317a2f2014-07-25 15:25:09 -07006348 struct inet6_dev *idev;
stephen hemmingerc2e21292010-03-17 20:31:10 +00006349 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006350
Stephen Hemmingere21e8462010-03-20 16:09:01 -07006351 err = ipv6_addr_label_init();
6352 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00006353 pr_crit("%s: cannot initialize default policy table: %d\n",
6354 __func__, err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006355 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006358 err = register_pernet_subsys(&addrconf_ops);
6359 if (err < 0)
6360 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006361
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006362 addrconf_wq = create_workqueue("ipv6_addrconf");
6363 if (!addrconf_wq) {
6364 err = -ENOMEM;
6365 goto out_nowq;
6366 }
6367
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368 /* The addrconf netdev notifier requires that loopback_dev
6369 * has it's ipv6 private information allocated and setup
6370 * before it can bring up and give link-local addresses
6371 * to other devices which are up.
6372 *
6373 * Unfortunately, loopback_dev is not necessarily the first
6374 * entry in the global dev_base list of net devices. In fact,
6375 * it is likely to be the very last entry on that list.
6376 * So this causes the notifier registry below to try and
6377 * give link-local addresses to all devices besides loopback_dev
6378 * first, then loopback_dev, which cases all the non-loopback_dev
6379 * devices to fail to get a link-local address.
6380 *
6381 * So, as a temporary fix, allocate the ipv6 structure for
6382 * loopback_dev first by hand.
6383 * Longer term, all of the dependencies ipv6 has upon the loopback
6384 * device and it being up should be removed.
6385 */
6386 rtnl_lock();
WANG Conga317a2f2014-07-25 15:25:09 -07006387 idev = ipv6_add_dev(init_net.loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 rtnl_unlock();
WANG Conga317a2f2014-07-25 15:25:09 -07006389 if (IS_ERR(idev)) {
6390 err = PTR_ERR(idev);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006391 goto errlo;
WANG Conga317a2f2014-07-25 15:25:09 -07006392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393
WANG Congecbd3ed2017-05-03 22:07:31 -07006394 ip6_route_init_special_entries();
6395
stephen hemmingerc2e21292010-03-17 20:31:10 +00006396 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6397 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6398
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 register_netdevice_notifier(&ipv6_dev_notf);
6400
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006401 addrconf_verify();
Thomas Grafc127ea22007-03-22 11:58:32 -07006402
stephen hemminger3678a9d2013-12-30 10:41:32 -08006403 rtnl_af_register(&inet6_ops);
Thomas Grafb382b192010-11-16 04:33:57 +00006404
Greg Rosec7ac8672011-06-10 01:27:09 +00006405 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6406 NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006407 if (err < 0)
6408 goto errout;
6409
6410 /* Only the first call to __rtnl_register can fail */
Greg Rosec7ac8672011-06-10 01:27:09 +00006411 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
6412 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
6413 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6414 inet6_dump_ifaddr, NULL);
6415 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6416 inet6_dump_ifmcaddr, NULL);
6417 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6418 inet6_dump_ifacaddr, NULL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +00006419 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
Nicolas Dichtel7a674202013-03-05 23:42:06 +00006420 inet6_netconf_dump_devconf, NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006421
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006422 ipv6_addr_label_rtnl_register();
6423
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07006425errout:
Thomas Grafb382b192010-11-16 04:33:57 +00006426 rtnl_af_unregister(&inet6_ops);
Thomas Grafc127ea22007-03-22 11:58:32 -07006427 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006428errlo:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006429 destroy_workqueue(addrconf_wq);
6430out_nowq:
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006431 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006432out_addrlabel:
6433 ipv6_addr_label_cleanup();
6434out:
Thomas Grafc127ea22007-03-22 11:58:32 -07006435 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436}
6437
Daniel Lezcano09f77092007-12-13 05:34:58 -08006438void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006440 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441 int i;
6442
6443 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006444 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006445 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446
6447 rtnl_lock();
6448
Thomas Grafb382b192010-11-16 04:33:57 +00006449 __rtnl_af_unregister(&inet6_ops);
6450
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006451 /* clean dev list */
6452 for_each_netdev(&init_net, dev) {
6453 if (__in6_dev_get(dev) == NULL)
6454 continue;
6455 addrconf_ifdown(dev, 1);
6456 }
6457 addrconf_ifdown(init_net.loopback_dev, 2);
6458
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460 * Check hash table.
6461 */
stephen hemminger5c578ae2010-03-17 20:31:11 +00006462 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00006463 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6464 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578ae2010-03-17 20:31:11 +00006465 spin_unlock_bh(&addrconf_hash_lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006466 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 rtnl_unlock();
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006468
6469 destroy_workqueue(addrconf_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470}