blob: b22796e707d389eba285d6b67f91b8c8cca5d9ba [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010046#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/socket.h>
48#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/net.h>
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +010050#include <linux/inet.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/in6.h>
52#include <linux/netdevice.h>
Thomas Graf18237302006-08-04 23:04:54 -070053#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/if_arp.h>
55#include <linux/if_arcnet.h>
56#include <linux/if_infiniband.h>
57#include <linux/route.h>
58#include <linux/inetdevice.h>
59#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090060#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#ifdef CONFIG_SYSCTL
62#include <linux/sysctl.h>
63#endif
Randy Dunlap4fc268d2006-01-11 12:17:47 -080064#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/delay.h>
66#include <linux/notifier.h>
Paulo Marques543537b2005-06-23 00:09:02 -070067#include <linux/string.h>
Eric Dumazetddbe5032012-07-18 08:11:12 +000068#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020070#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <net/sock.h>
72#include <net/snmp.h>
73
Alexander Aring5241c2d2015-12-14 20:55:22 +010074#include <net/6lowpan.h>
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +000075#include <net/firewire.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <net/ipv6.h>
77#include <net/protocol.h>
78#include <net/ndisc.h>
79#include <net/ip6_route.h>
80#include <net/addrconf.h>
81#include <net/tcp.h>
82#include <net/ip.h>
Thomas Graf5d620262006-08-15 00:35:02 -070083#include <net/netlink.h>
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -070084#include <net/pkt_sched.h>
David Ahernca254492015-10-12 11:47:10 -070085#include <net/l3mdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include <linux/if_tunnel.h>
87#include <linux/rtnetlink.h>
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +000088#include <linux/netconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <linux/random.h>
Stephen Hemmingere21e8462010-03-20 16:09:01 -070090#include <linux/uaccess.h>
Herbert Xu7f7d9a62007-04-24 21:54:09 -070091#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93#include <linux/proc_fs.h>
94#include <linux/seq_file.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040095#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/* Set to 3 to get tracing... */
98#define ACONF_DEBUG 2
99
100#if ACONF_DEBUG >= 3
dingtianhongba3542e2013-08-17 10:27:04 +0800101#define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#else
dingtianhongba3542e2013-08-17 10:27:04 +0800103#define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#endif
105
106#define INFINITY_LIFE_TIME 0xFFFFFFFF
Thomas Graf18a31e12010-11-17 04:12:02 +0000107
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100108#define IPV6_MAX_STRLEN \
109 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
110
Thomas Graf18a31e12010-11-17 04:12:02 +0000111static inline u32 cstamp_delta(unsigned long cstamp)
112{
113 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
114}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700116static inline s32 rfc3315_s14_backoff_init(s32 irt)
117{
118 /* multiply 'initial retransmission time' by 0.9 .. 1.1 */
119 u64 tmp = (900000 + prandom_u32() % 200001) * (u64)irt;
120 do_div(tmp, 1000000);
121 return (s32)tmp;
122}
123
124static inline s32 rfc3315_s14_backoff_update(s32 rt, s32 mrt)
125{
126 /* multiply 'retransmission timeout' by 1.9 .. 2.1 */
127 u64 tmp = (1900000 + prandom_u32() % 200001) * (u64)rt;
128 do_div(tmp, 1000000);
129 if ((s32)tmp > mrt) {
130 /* multiply 'maximum retransmission time' by 0.9 .. 1.1 */
131 tmp = (900000 + prandom_u32() % 200001) * (u64)mrt;
132 do_div(tmp, 1000000);
133 }
134 return (s32)tmp;
135}
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137#ifdef CONFIG_SYSCTL
WANG Conga317a2f2014-07-25 15:25:09 -0700138static int addrconf_sysctl_register(struct inet6_dev *idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800139static void addrconf_sysctl_unregister(struct inet6_dev *idev);
140#else
WANG Conga317a2f2014-07-25 15:25:09 -0700141static inline int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800142{
WANG Conga317a2f2014-07-25 15:25:09 -0700143 return 0;
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800144}
145
146static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
147{
148}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149#endif
150
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200151static void ipv6_regen_rndid(struct inet6_dev *idev);
152static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900154static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static int ipv6_count_addresses(struct inet6_dev *idev);
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +0100156static int ipv6_generate_stable_address(struct in6_addr *addr,
157 u8 dad_count,
158 const struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160/*
161 * Configured unicast address hash table
162 */
stephen hemmingerc2e21292010-03-17 20:31:10 +0000163static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
stephen hemminger5c578aed2010-03-17 20:31:11 +0000164static DEFINE_SPINLOCK(addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100166static void addrconf_verify(void);
167static void addrconf_verify_rtnl(void);
168static void addrconf_verify_work(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100170static struct workqueue_struct *addrconf_wq;
171static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
174static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
175
Jiri Pirko93d9b7d2010-03-10 10:28:56 +0000176static void addrconf_type_change(struct net_device *dev,
177 unsigned long event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178static int addrconf_ifdown(struct net_device *dev, int how);
179
Romain Kuntz21caa662013-01-09 21:06:03 +0000180static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
181 int plen,
182 const struct net_device *dev,
183 u32 flags, u32 noflags);
184
David S. Millercf22f9a2012-04-14 21:37:40 -0400185static void addrconf_dad_start(struct inet6_ifaddr *ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100186static void addrconf_dad_work(struct work_struct *w);
Paolo Abeni764d3be2016-11-22 16:57:40 +0100187static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900188static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static void addrconf_rs_timer(unsigned long data);
190static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
191static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
192
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900193static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct prefix_info *pinfo);
David S. Miller3e81c6d2010-03-20 16:18:00 -0700195static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
196 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Adrian Bunk888c8482008-07-22 14:21:58 -0700198static struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 .forwarding = 0,
200 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
201 .mtu6 = IPV6_MIN_MTU,
202 .accept_ra = 1,
203 .accept_redirects = 1,
204 .autoconf = 1,
205 .force_mld_version = 0,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200206 .mldv1_unsolicited_report_interval = 10 * HZ,
207 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 .dad_transmits = 1,
209 .rtr_solicits = MAX_RTR_SOLICITATIONS,
210 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700211 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Ian Morris67ba4152014-08-24 21:53:10 +0100213 .use_tempaddr = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 .temp_valid_lft = TEMP_VALID_LIFETIME,
215 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
216 .regen_max_retry = REGEN_MAX_RETRY,
217 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800219 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700220 .accept_ra_from_local = 0,
Hangbin Liu8013d1d72015-07-30 14:28:42 +0800221 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800222 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800223#ifdef CONFIG_IPV6_ROUTER_PREF
224 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800225 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800226#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzbbea1242017-03-22 18:19:04 +0900227 .accept_ra_rt_info_min_plen = 0,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800228 .accept_ra_rt_info_max_plen = 0,
229#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800230#endif
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,
David Lebrun1ababeb2016-11-08 14:57:39 +0100243 .seg6_enabled = 0,
David Lebrunbf355b82016-11-08 14:57:42 +0100244#ifdef CONFIG_IPV6_SEG6_HMAC
245 .seg6_require_hmac = 0,
246#endif
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800247 .enhanced_dad = 1,
Felix Jiad35a00b2017-01-26 16:59:17 +1300248 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
David Forsterdf789fe2017-02-23 16:27:18 +0000249 .disable_policy = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250};
251
Brian Haleyab32ea52006-09-22 14:15:41 -0700252static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 .forwarding = 0,
254 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
255 .mtu6 = IPV6_MIN_MTU,
256 .accept_ra = 1,
257 .accept_redirects = 1,
258 .autoconf = 1,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200259 .force_mld_version = 0,
260 .mldv1_unsolicited_report_interval = 10 * HZ,
261 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 .dad_transmits = 1,
263 .rtr_solicits = MAX_RTR_SOLICITATIONS,
264 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -0700265 .rtr_solicit_max_interval = RTR_SOLICITATION_MAX_INTERVAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 .use_tempaddr = 0,
268 .temp_valid_lft = TEMP_VALID_LIFETIME,
269 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
270 .regen_max_retry = REGEN_MAX_RETRY,
271 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800273 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700274 .accept_ra_from_local = 0,
Hangbin Liu8013d1d72015-07-30 14:28:42 +0800275 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800276 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800277#ifdef CONFIG_IPV6_ROUTER_PREF
278 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e163562006-03-20 17:05:47 -0800279 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800280#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzbbea1242017-03-22 18:19:04 +0900281 .accept_ra_rt_info_min_plen = 0,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800282 .accept_ra_rt_info_max_plen = 0,
283#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800284#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700285 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700286 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900287 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900288 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200289 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700290 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100291 .stable_secret = {
292 .initialized = false,
293 },
Erik Kline3985e8a2015-07-22 16:38:25 +0900294 .use_oif_addrs_only = 0,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400295 .ignore_routes_with_linkdown = 0,
David Ahernf1705ec2016-02-24 09:25:37 -0800296 .keep_addr_on_down = 0,
David Lebrun1ababeb2016-11-08 14:57:39 +0100297 .seg6_enabled = 0,
David Lebrunbf355b82016-11-08 14:57:42 +0100298#ifdef CONFIG_IPV6_SEG6_HMAC
299 .seg6_require_hmac = 0,
300#endif
Erik Nordmarkadc176c2016-12-02 14:00:08 -0800301 .enhanced_dad = 1,
Felix Jiad35a00b2017-01-26 16:59:17 +1300302 .addr_gen_mode = IN6_ADDR_GEN_MODE_EUI64,
David Forsterdf789fe2017-02-23 16:27:18 +0000303 .disable_policy = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304};
305
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700306/* Check if a valid qdisc is available */
David S. Miller05297942008-07-08 23:01:27 -0700307static inline bool addrconf_qdisc_ok(const struct net_device *dev)
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700308{
David S. Miller05297942008-07-08 23:01:27 -0700309 return !qdisc_tx_is_noop(dev);
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700310}
311
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200312static void addrconf_del_rs_timer(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200314 if (del_timer(&idev->rs_timer))
315 __in6_dev_put(idev);
316}
317
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100318static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200319{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100320 if (cancel_delayed_work(&ifp->dad_work))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 __in6_ifa_put(ifp);
322}
323
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200324static void addrconf_mod_rs_timer(struct inet6_dev *idev,
325 unsigned long when)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200327 if (!timer_pending(&idev->rs_timer))
328 in6_dev_hold(idev);
329 mod_timer(&idev->rs_timer, jiffies + when);
330}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100332static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
333 unsigned long delay)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200334{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100335 if (!delayed_work_pending(&ifp->dad_work))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200336 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100337 mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700340static int snmp6_alloc_dev(struct inet6_dev *idev)
341{
John Stultz827da442013-10-07 15:51:58 -0700342 int i;
343
WANG Cong698365f2014-05-05 15:55:55 -0700344 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
345 if (!idev->stats.ipv6)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700346 goto err_ip;
John Stultz827da442013-10-07 15:51:58 -0700347
348 for_each_possible_cpu(i) {
349 struct ipstats_mib *addrconf_stats;
WANG Cong698365f2014-05-05 15:55:55 -0700350 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
John Stultz827da442013-10-07 15:51:58 -0700351 u64_stats_init(&addrconf_stats->syncp);
John Stultz827da442013-10-07 15:51:58 -0700352 }
353
354
Eric Dumazetbe281e52011-05-19 01:14:23 +0000355 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
356 GFP_KERNEL);
357 if (!idev->stats.icmpv6dev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700358 goto err_icmp;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000359 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
360 GFP_KERNEL);
361 if (!idev->stats.icmpv6msgdev)
David L Stevens14878f72007-09-16 16:52:35 -0700362 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700363
364 return 0;
365
David L Stevens14878f72007-09-16 16:52:35 -0700366err_icmpmsg:
Eric Dumazetbe281e52011-05-19 01:14:23 +0000367 kfree(idev->stats.icmpv6dev);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700368err_icmp:
WANG Cong698365f2014-05-05 15:55:55 -0700369 free_percpu(idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700370err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700371 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700372}
373
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000374static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 struct inet6_dev *ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700377 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 ASSERT_RTNL();
380
381 if (dev->mtu < IPV6_MIN_MTU)
WANG Conga317a2f2014-07-25 15:25:09 -0700382 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900384 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100385 if (!ndev)
WANG Conga317a2f2014-07-25 15:25:09 -0700386 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Ingo Oeser322f74a2006-03-20 23:01:47 -0800388 rwlock_init(&ndev->lock);
389 ndev->dev = dev;
stephen hemminger502a2ff2010-03-17 20:31:13 +0000390 INIT_LIST_HEAD(&ndev->addr_list);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200391 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
392 (unsigned long)ndev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900393 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100394
395 if (ndev->cnf.stable_secret.initialized)
Felix Jiad35a00b2017-01-26 16:59:17 +1300396 ndev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100397 else
Felix Jiad35a00b2017-01-26 16:59:17 +1300398 ndev->cnf.addr_gen_mode = ipv6_devconf_dflt.addr_gen_mode;
Hannes Frederic Sowa9b29c692015-12-15 22:59:12 +0100399
Ingo Oeser322f74a2006-03-20 23:01:47 -0800400 ndev->cnf.mtu6 = dev->mtu;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800401 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
Ian Morris63159f22015-03-29 14:00:04 +0100402 if (!ndev->nd_parms) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800403 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700404 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800405 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700406 if (ndev->cnf.forwarding)
407 dev_disable_lro(dev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800408 /* We refer to the device */
409 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Ingo Oeser322f74a2006-03-20 23:01:47 -0800411 if (snmp6_alloc_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800412 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000413 "%s: cannot allocate memory for statistics; dev=%s.\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800414 __func__, dev->name);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800415 neigh_parms_release(&nd_tbl, ndev->nd_parms);
Roy Li8603e332011-09-20 15:10:16 -0400416 dev_put(dev);
417 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700418 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Ingo Oeser322f74a2006-03-20 23:01:47 -0800421 if (snmp6_register_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800422 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000423 "%s: cannot create /proc/net/dev_snmp6/%s\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800424 __func__, dev->name);
WANG Conga317a2f2014-07-25 15:25:09 -0700425 goto err_release;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200428 /* One reference from device. */
Ingo Oeser322f74a2006-03-20 23:01:47 -0800429 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900431 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
432 ndev->cnf.accept_dad = -1;
433
Amerigo Wang07a93622012-10-29 16:23:10 +0000434#if IS_ENABLED(CONFIG_IPV6_SIT)
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700435 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
Joe Perchesf3213832012-05-15 14:11:53 +0000436 pr_info("%s: Disabled Multicast RS\n", dev->name);
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700437 ndev->cnf.rtr_solicits = 0;
438 }
439#endif
440
stephen hemminger372e6c82010-03-17 20:31:09 +0000441 INIT_LIST_HEAD(&ndev->tempaddr_list);
Jiri Bohac76506a92016-10-13 18:52:15 +0200442 ndev->desync_factor = U32_MAX;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800443 if ((dev->flags&IFF_LOOPBACK) ||
444 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700445 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700446 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700447 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800448 ndev->cnf.use_tempaddr = -1;
Jiri Bohac9d6280d2016-10-13 18:50:02 +0200449 } else
450 ipv6_regen_rndid(ndev);
David S. Miller5d9efa72013-10-28 20:07:50 -0400451
Daniel Borkmann914faa12013-04-09 03:47:14 +0000452 ndev->token = in6addr_any;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800453
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700454 if (netif_running(dev) && addrconf_qdisc_ok(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700455 ndev->if_flags |= IF_READY;
456
Ingo Oeser322f74a2006-03-20 23:01:47 -0800457 ipv6_mc_init_dev(ndev);
458 ndev->tstamp = jiffies;
WANG Conga317a2f2014-07-25 15:25:09 -0700459 err = addrconf_sysctl_register(ndev);
460 if (err) {
461 ipv6_mc_destroy_dev(ndev);
Sabrina Dubroca2a189f9e2015-11-04 14:47:53 +0100462 snmp6_unregister_dev(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700463 goto err_release;
464 }
David L Stevens30c4cf52007-01-04 12:31:14 -0800465 /* protected by rtnl_lock */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000466 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800467
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000468 /* Join interface-local all-node multicast group */
469 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
470
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800471 /* Join all-node multicast group */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900472 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800473
Li Weid6ddef92012-03-05 14:45:17 +0000474 /* Join all-router multicast group if forwarding is set */
Li Wei8b2aaed2012-03-07 14:58:07 +0000475 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
Li Weid6ddef92012-03-05 14:45:17 +0000476 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700479
480err_release:
481 neigh_parms_release(&nd_tbl, ndev->nd_parms);
482 ndev->dead = 1;
483 in6_dev_finish_destroy(ndev);
484 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000487static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct inet6_dev *idev;
490
491 ASSERT_RTNL();
492
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700493 idev = __in6_dev_get(dev);
494 if (!idev) {
495 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -0700496 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 return NULL;
498 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (dev->flags&IFF_UP)
501 ipv6_mc_up(idev);
502 return idev;
503}
504
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000505static int inet6_netconf_msgsize_devconf(int type)
506{
507 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
508 + nla_total_size(4); /* NETCONFA_IFINDEX */
Zhang Shengju136ba622016-03-10 08:55:50 +0000509 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000510
Zhang Shengju136ba622016-03-10 08:55:50 +0000511 if (type == NETCONFA_ALL)
512 all = true;
513
514 if (all || type == NETCONFA_FORWARDING)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000515 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500516#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000517 if (all || type == NETCONFA_MC_FORWARDING)
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000518 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500519#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000520 if (all || type == NETCONFA_PROXY_NEIGH)
stephen hemmingerc92d5492013-12-17 22:37:14 -0800521 size += nla_total_size(4);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000522
Zhang Shengju136ba622016-03-10 08:55:50 +0000523 if (all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN)
Andy Gospodarek35103d12015-08-13 10:39:01 -0400524 size += nla_total_size(4);
525
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000526 return size;
527}
528
529static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
530 struct ipv6_devconf *devconf, u32 portid,
531 u32 seq, int event, unsigned int flags,
532 int type)
533{
534 struct nlmsghdr *nlh;
535 struct netconfmsg *ncm;
Zhang Shengju136ba622016-03-10 08:55:50 +0000536 bool all = false;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000537
538 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
539 flags);
Ian Morris63159f22015-03-29 14:00:04 +0100540 if (!nlh)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000541 return -EMSGSIZE;
542
Zhang Shengju136ba622016-03-10 08:55:50 +0000543 if (type == NETCONFA_ALL)
544 all = true;
545
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000546 ncm = nlmsg_data(nlh);
547 ncm->ncm_family = AF_INET6;
548
549 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
550 goto nla_put_failure;
551
David Ahern23452172017-03-28 14:28:05 -0700552 if (!devconf)
553 goto out;
554
Zhang Shengju136ba622016-03-10 08:55:50 +0000555 if ((all || type == NETCONFA_FORWARDING) &&
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000556 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
557 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500558#ifdef CONFIG_IPV6_MROUTE
Zhang Shengju136ba622016-03-10 08:55:50 +0000559 if ((all || type == NETCONFA_MC_FORWARDING) &&
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000560 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
561 devconf->mc_forwarding) < 0)
562 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500563#endif
Zhang Shengju136ba622016-03-10 08:55:50 +0000564 if ((all || type == NETCONFA_PROXY_NEIGH) &&
stephen hemmingerc92d5492013-12-17 22:37:14 -0800565 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
566 goto nla_put_failure;
567
Zhang Shengju136ba622016-03-10 08:55:50 +0000568 if ((all || type == NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN) &&
Andy Gospodarek35103d12015-08-13 10:39:01 -0400569 nla_put_s32(skb, NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
570 devconf->ignore_routes_with_linkdown) < 0)
571 goto nla_put_failure;
572
David Ahern23452172017-03-28 14:28:05 -0700573out:
Johannes Berg053c0952015-01-16 22:09:00 +0100574 nlmsg_end(skb, nlh);
575 return 0;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000576
577nla_put_failure:
578 nlmsg_cancel(skb, nlh);
579 return -EMSGSIZE;
580}
581
David Ahern85b3daa2017-03-28 14:28:04 -0700582void inet6_netconf_notify_devconf(struct net *net, int event, int type,
583 int ifindex, struct ipv6_devconf *devconf)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000584{
585 struct sk_buff *skb;
586 int err = -ENOBUFS;
587
Eric Dumazet927265b2016-07-08 05:46:04 +0200588 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100589 if (!skb)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000590 goto errout;
591
592 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
David Ahern85b3daa2017-03-28 14:28:04 -0700593 event, 0, type);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000594 if (err < 0) {
595 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
596 WARN_ON(err == -EMSGSIZE);
597 kfree_skb(skb);
598 goto errout;
599 }
Eric Dumazet927265b2016-07-08 05:46:04 +0200600 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_KERNEL);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000601 return;
602errout:
Cong Dingbd779022012-12-18 12:08:56 +0000603 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000604}
605
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000606static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
607 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
608 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
stephen hemmingerc92d5492013-12-17 22:37:14 -0800609 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
Andy Gospodarek35103d12015-08-13 10:39:01 -0400610 [NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN] = { .len = sizeof(int) },
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000611};
612
613static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
Thomas Graf661d2962013-03-21 07:45:29 +0000614 struct nlmsghdr *nlh)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000615{
616 struct net *net = sock_net(in_skb->sk);
617 struct nlattr *tb[NETCONFA_MAX+1];
618 struct netconfmsg *ncm;
619 struct sk_buff *skb;
620 struct ipv6_devconf *devconf;
621 struct inet6_dev *in6_dev;
622 struct net_device *dev;
623 int ifindex;
624 int err;
625
626 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
627 devconf_ipv6_policy);
628 if (err < 0)
629 goto errout;
630
Anton Protopopova97eb332016-02-16 21:43:16 -0500631 err = -EINVAL;
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000632 if (!tb[NETCONFA_IFINDEX])
633 goto errout;
634
635 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
636 switch (ifindex) {
637 case NETCONFA_IFINDEX_ALL:
638 devconf = net->ipv6.devconf_all;
639 break;
640 case NETCONFA_IFINDEX_DEFAULT:
641 devconf = net->ipv6.devconf_dflt;
642 break;
643 default:
644 dev = __dev_get_by_index(net, ifindex);
Ian Morris63159f22015-03-29 14:00:04 +0100645 if (!dev)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000646 goto errout;
647 in6_dev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +0100648 if (!in6_dev)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000649 goto errout;
650 devconf = &in6_dev->cnf;
651 break;
652 }
653
654 err = -ENOBUFS;
Zhang Shengju136ba622016-03-10 08:55:50 +0000655 skb = nlmsg_new(inet6_netconf_msgsize_devconf(NETCONFA_ALL), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +0100656 if (!skb)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000657 goto errout;
658
659 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
660 NETLINK_CB(in_skb).portid,
661 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
Zhang Shengju136ba622016-03-10 08:55:50 +0000662 NETCONFA_ALL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000663 if (err < 0) {
664 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
665 WARN_ON(err == -EMSGSIZE);
666 kfree_skb(skb);
667 goto errout;
668 }
669 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
670errout:
671 return err;
672}
673
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000674static int inet6_netconf_dump_devconf(struct sk_buff *skb,
675 struct netlink_callback *cb)
676{
677 struct net *net = sock_net(skb->sk);
678 int h, s_h;
679 int idx, s_idx;
680 struct net_device *dev;
681 struct inet6_dev *idev;
682 struct hlist_head *head;
683
684 s_h = cb->args[0];
685 s_idx = idx = cb->args[1];
686
687 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
688 idx = 0;
689 head = &net->dev_index_head[h];
690 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000691 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
692 net->dev_base_seq;
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000693 hlist_for_each_entry_rcu(dev, head, index_hlist) {
694 if (idx < s_idx)
695 goto cont;
696 idev = __in6_dev_get(dev);
697 if (!idev)
698 goto cont;
699
700 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
701 &idev->cnf,
702 NETLINK_CB(cb->skb).portid,
703 cb->nlh->nlmsg_seq,
704 RTM_NEWNETCONF,
705 NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000706 NETCONFA_ALL) < 0) {
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000707 rcu_read_unlock();
708 goto done;
709 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000710 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000711cont:
712 idx++;
713 }
714 rcu_read_unlock();
715 }
716 if (h == NETDEV_HASHENTRIES) {
717 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
718 net->ipv6.devconf_all,
719 NETLINK_CB(cb->skb).portid,
720 cb->nlh->nlmsg_seq,
721 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000722 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000723 goto done;
724 else
725 h++;
726 }
727 if (h == NETDEV_HASHENTRIES + 1) {
728 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
729 net->ipv6.devconf_dflt,
730 NETLINK_CB(cb->skb).portid,
731 cb->nlh->nlmsg_seq,
732 RTM_NEWNETCONF, NLM_F_MULTI,
Zhang Shengju136ba622016-03-10 08:55:50 +0000733 NETCONFA_ALL) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000734 goto done;
735 else
736 h++;
737 }
738done:
739 cb->args[0] = h;
740 cb->args[1] = idx;
741
742 return skb->len;
743}
744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745#ifdef CONFIG_SYSCTL
746static void dev_forward_change(struct inet6_dev *idev)
747{
748 struct net_device *dev;
749 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 if (!idev)
752 return;
753 dev = idev->dev;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700754 if (idev->cnf.forwarding)
755 dev_disable_lro(dev);
Amerigo Wang1a940832012-10-28 17:43:53 +0000756 if (dev->flags & IFF_MULTICAST) {
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000757 if (idev->cnf.forwarding) {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900758 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000759 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
760 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
761 } else {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900762 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000763 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
764 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
stephen hemminger502a2ff2010-03-17 20:31:13 +0000767
768 list_for_each_entry(ifa, &idev->addr_list, if_list) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800769 if (ifa->flags&IFA_F_TENTATIVE)
770 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (idev->cnf.forwarding)
772 addrconf_join_anycast(ifa);
773 else
774 addrconf_leave_anycast(ifa);
775 }
David Ahern85b3daa2017-03-28 14:28:04 -0700776 inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
777 NETCONFA_FORWARDING,
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000778 dev->ifindex, &idev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
781
Pavel Emelyanove1869322008-01-10 17:43:50 -0800782static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 struct net_device *dev;
785 struct inet6_dev *idev;
786
Ben Hutchings4acd4942012-08-14 08:54:51 +0000787 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 idev = __in6_dev_get(dev);
789 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800790 int changed = (!idev->cnf.forwarding) ^ (!newf);
791 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (changed)
793 dev_forward_change(idev);
794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800797
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000798static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800799{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800800 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000801 int old;
802
803 if (!rtnl_trylock())
804 return restart_syscall();
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800805
806 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000807 old = *p;
808 *p = newf;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800809
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000810 if (p == &net->ipv6.devconf_dflt->forwarding) {
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000811 if ((!newf) ^ (!old))
David Ahern85b3daa2017-03-28 14:28:04 -0700812 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
813 NETCONFA_FORWARDING,
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000814 NETCONFA_IFINDEX_DEFAULT,
815 net->ipv6.devconf_dflt);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000816 rtnl_unlock();
817 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +0000818 }
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000819
Pavel Emelyanove1869322008-01-10 17:43:50 -0800820 if (p == &net->ipv6.devconf_all->forwarding) {
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200821 int old_dflt = net->ipv6.devconf_dflt->forwarding;
822
Pavel Emelyanove1869322008-01-10 17:43:50 -0800823 net->ipv6.devconf_dflt->forwarding = newf;
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200824 if ((!newf) ^ (!old_dflt))
David Ahern85b3daa2017-03-28 14:28:04 -0700825 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
826 NETCONFA_FORWARDING,
Nicolas Dichteld26c6382016-08-30 10:09:21 +0200827 NETCONFA_IFINDEX_DEFAULT,
828 net->ipv6.devconf_dflt);
829
Pavel Emelyanove1869322008-01-10 17:43:50 -0800830 addrconf_forward_change(net, newf);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000831 if ((!newf) ^ (!old))
David Ahern85b3daa2017-03-28 14:28:04 -0700832 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
833 NETCONFA_FORWARDING,
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000834 NETCONFA_IFINDEX_ALL,
835 net->ipv6.devconf_all);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000836 } else if ((!newf) ^ (!old))
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800837 dev_forward_change((struct inet6_dev *)table->extra1);
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700838 rtnl_unlock();
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800839
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000840 if (newf)
Daniel Lezcano7b4da532008-03-04 13:47:14 -0800841 rt6_purge_dflt_routers(net);
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000842 return 1;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800843}
Andy Gospodarek35103d12015-08-13 10:39:01 -0400844
845static void addrconf_linkdown_change(struct net *net, __s32 newf)
846{
847 struct net_device *dev;
848 struct inet6_dev *idev;
849
850 for_each_netdev(net, dev) {
851 idev = __in6_dev_get(dev);
852 if (idev) {
853 int changed = (!idev->cnf.ignore_routes_with_linkdown) ^ (!newf);
854
855 idev->cnf.ignore_routes_with_linkdown = newf;
856 if (changed)
857 inet6_netconf_notify_devconf(dev_net(dev),
David Ahern85b3daa2017-03-28 14:28:04 -0700858 RTM_NEWNETCONF,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400859 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
860 dev->ifindex,
861 &idev->cnf);
862 }
863 }
864}
865
866static int addrconf_fixup_linkdown(struct ctl_table *table, int *p, int newf)
867{
868 struct net *net;
869 int old;
870
871 if (!rtnl_trylock())
872 return restart_syscall();
873
874 net = (struct net *)table->extra2;
875 old = *p;
876 *p = newf;
877
878 if (p == &net->ipv6.devconf_dflt->ignore_routes_with_linkdown) {
879 if ((!newf) ^ (!old))
880 inet6_netconf_notify_devconf(net,
David Ahern85b3daa2017-03-28 14:28:04 -0700881 RTM_NEWNETCONF,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400882 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
883 NETCONFA_IFINDEX_DEFAULT,
884 net->ipv6.devconf_dflt);
885 rtnl_unlock();
886 return 0;
887 }
888
889 if (p == &net->ipv6.devconf_all->ignore_routes_with_linkdown) {
890 net->ipv6.devconf_dflt->ignore_routes_with_linkdown = newf;
891 addrconf_linkdown_change(net, newf);
892 if ((!newf) ^ (!old))
893 inet6_netconf_notify_devconf(net,
David Ahern85b3daa2017-03-28 14:28:04 -0700894 RTM_NEWNETCONF,
Andy Gospodarek35103d12015-08-13 10:39:01 -0400895 NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN,
896 NETCONFA_IFINDEX_ALL,
897 net->ipv6.devconf_all);
898 }
899 rtnl_unlock();
900
901 return 1;
902}
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904#endif
905
stephen hemminger5c578aed2010-03-17 20:31:11 +0000906/* Nobody refers to this ifaddr, destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
908{
stephen hemmingerc2e21292010-03-17 20:31:10 +0000909 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911#ifdef NET_REFCNT_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +0000912 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913#endif
914
915 in6_dev_put(ifp->idev);
916
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100917 if (cancel_delayed_work(&ifp->dad_work))
918 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
919 ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Herbert Xue9d3e082010-05-18 15:36:06 -0700921 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
Joe Perchesf3213832012-05-15 14:11:53 +0000922 pr_warn("Freeing alive inet6 address %p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return;
924 }
Amerigo Wang94e187c2012-10-29 00:13:19 +0000925 ip6_rt_put(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Lai Jiangshane5785982011-03-15 18:00:14 +0800927 kfree_rcu(ifp, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
Brian Haleye55ffac2006-07-10 15:25:51 -0700930static void
931ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
932{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000933 struct list_head *p;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700934 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700935
936 /*
937 * Each device address list is sorted in order of scope -
938 * global before linklocal.
939 */
stephen hemminger502a2ff2010-03-17 20:31:13 +0000940 list_for_each(p, &idev->addr_list) {
941 struct inet6_ifaddr *ifa
942 = list_entry(p, struct inet6_ifaddr, if_list);
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700943 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700944 break;
945 }
946
David S. Millerb54c9b92010-03-25 21:25:30 -0700947 list_add_tail(&ifp->if_list, p);
Brian Haleye55ffac2006-07-10 15:25:51 -0700948}
949
Eric Dumazetddbe5032012-07-18 08:11:12 +0000950static u32 inet6_addr_hash(const struct in6_addr *addr)
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900951{
Eric Dumazetddbe5032012-07-18 08:11:12 +0000952 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900953}
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955/* On success it returns ifp with increased reference count */
956
957static struct inet6_ifaddr *
Jiri Pirko3f8f5292013-08-01 10:41:27 +0200958ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
959 const struct in6_addr *peer_addr, int pfxlen,
Jiri Benc8a226b22013-08-01 10:41:28 +0200960 int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
David Forsterdf789fe2017-02-23 16:27:18 +0000962 struct net *net = dev_net(idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 struct inet6_ifaddr *ifa = NULL;
964 struct rt6_info *rt;
stephen hemminger3a88a812010-03-17 20:31:12 +0000965 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 int err = 0;
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +0900967 int addr_type = ipv6_addr_type(addr);
968
969 if (addr_type == IPV6_ADDR_ANY ||
970 addr_type & IPV6_ADDR_MULTICAST ||
971 (!(idev->dev->flags & IFF_LOOPBACK) &&
972 addr_type & IPV6_ADDR_LOOPBACK))
973 return ERR_PTR(-EADDRNOTAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700975 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (idev->dead) {
977 err = -ENODEV; /*XXX*/
978 goto out2;
979 }
980
Brian Haley56d417b2009-06-01 03:07:33 -0700981 if (idev->cnf.disable_ipv6) {
Brian Haley9bdd8d42009-03-18 18:22:48 -0700982 err = -EACCES;
983 goto out2;
984 }
985
stephen hemminger5c578aed2010-03-17 20:31:11 +0000986 spin_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 /* Ignore adding duplicate addresses on an interface */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900989 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
dingtianhongba3542e2013-08-17 10:27:04 +0800990 ADBG("ipv6_add_addr: already assigned\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 err = -EEXIST;
992 goto out;
993 }
994
Ingo Oeser322f74a2006-03-20 23:01:47 -0800995 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Ian Morris63159f22015-03-29 14:00:04 +0100997 if (!ifa) {
dingtianhongba3542e2013-08-17 10:27:04 +0800998 ADBG("ipv6_add_addr: malloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 err = -ENOBUFS;
1000 goto out;
1001 }
1002
David S. Miller8f031512011-12-06 16:48:14 -05001003 rt = addrconf_dst_alloc(idev, addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (IS_ERR(rt)) {
1005 err = PTR_ERR(rt);
1006 goto out;
1007 }
1008
David Forsterdf789fe2017-02-23 16:27:18 +00001009 if (net->ipv6.devconf_all->disable_policy ||
1010 idev->cnf.disable_policy)
1011 rt->dst.flags |= DST_NOPOLICY;
1012
Jiri Pirkobba24892013-12-07 19:26:57 +01001013 neigh_parms_data_state_setall(idev->nd_parms);
1014
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001015 ifa->addr = *addr;
Jiri Pirko3f8f5292013-08-01 10:41:27 +02001016 if (peer_addr)
1017 ifa->peer_addr = *peer_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 spin_lock_init(&ifa->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001020 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001021 INIT_HLIST_NODE(&ifa->addr_lst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 ifa->scope = scope;
1023 ifa->prefix_len = pfxlen;
1024 ifa->flags = flags | IFA_F_TENTATIVE;
Jiri Benc8a226b22013-08-01 10:41:28 +02001025 ifa->valid_lft = valid_lft;
1026 ifa->prefered_lft = prefered_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 ifa->cstamp = ifa->tstamp = jiffies;
Daniel Borkmann617fe292013-04-09 03:47:16 +00001028 ifa->tokenized = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Keir Fraser57f5f542006-08-29 02:43:49 -07001030 ifa->rt = rt;
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 ifa->idev = idev;
1033 in6_dev_hold(idev);
1034 /* For caller */
1035 in6_ifa_hold(ifa);
1036
1037 /* Add to big hash table */
Eric Dumazetddbe5032012-07-18 08:11:12 +00001038 hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
stephen hemminger5c578aed2010-03-17 20:31:11 +00001040 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
stephen hemminger5c578aed2010-03-17 20:31:11 +00001041 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 write_lock(&idev->lock);
1044 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -07001045 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (ifa->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001048 list_add(&ifa->tmp_list, &idev->tempaddr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 in6_ifa_hold(ifa);
1050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 in6_ifa_hold(ifa);
1053 write_unlock(&idev->lock);
1054out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001055 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -07001057 if (likely(err == 0))
Cong Wangf88c91d2013-04-14 23:18:43 +08001058 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 else {
1060 kfree(ifa);
1061 ifa = ERR_PTR(err);
1062 }
1063
1064 return ifa;
1065out:
stephen hemminger5c578aed2010-03-17 20:31:11 +00001066 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 goto out2;
1068}
1069
Thomas Haller5b84efe2014-01-15 15:36:59 +01001070enum cleanup_prefix_rt_t {
1071 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
1072 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
1073 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
1074};
1075
1076/*
1077 * Check, whether the prefix for ifp would still need a prefix route
1078 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
1079 * constants.
1080 *
1081 * 1) we don't purge prefix if address was not permanent.
1082 * prefix is managed by its own lifetime.
1083 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
1084 * 3) if there are no addresses, delete prefix.
1085 * 4) if there are still other permanent address(es),
1086 * corresponding prefix is still permanent.
1087 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
1088 * don't purge the prefix, assume user space is managing it.
1089 * 6) otherwise, update prefix lifetime to the
1090 * longest valid lifetime among the corresponding
1091 * addresses on the device.
1092 * Note: subsequent RA will update lifetime.
1093 **/
1094static enum cleanup_prefix_rt_t
1095check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
1096{
1097 struct inet6_ifaddr *ifa;
1098 struct inet6_dev *idev = ifp->idev;
1099 unsigned long lifetime;
1100 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
1101
1102 *expires = jiffies;
1103
1104 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1105 if (ifa == ifp)
1106 continue;
1107 if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
1108 ifp->prefix_len))
1109 continue;
1110 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
1111 return CLEANUP_PREFIX_RT_NOP;
1112
1113 action = CLEANUP_PREFIX_RT_EXPIRE;
1114
1115 spin_lock(&ifa->lock);
1116
1117 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
1118 /*
1119 * Note: Because this address is
1120 * not permanent, lifetime <
1121 * LONG_MAX / HZ here.
1122 */
1123 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
1124 *expires = ifa->tstamp + lifetime * HZ;
1125 spin_unlock(&ifa->lock);
1126 }
1127
1128 return action;
1129}
1130
1131static void
1132cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
1133{
1134 struct rt6_info *rt;
1135
1136 rt = addrconf_get_prefix_route(&ifp->addr,
1137 ifp->prefix_len,
1138 ifp->idev->dev,
1139 0, RTF_GATEWAY | RTF_DEFAULT);
1140 if (rt) {
1141 if (del_rt)
1142 ip6_del_rt(rt);
1143 else {
1144 if (!(rt->rt6i_flags & RTF_EXPIRES))
1145 rt6_set_expires(rt, expires);
1146 ip6_rt_put(rt);
1147 }
1148 }
1149}
1150
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152/* This function wants to get referenced ifp and releases it before return */
1153
1154static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1155{
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001156 int state;
Thomas Haller5b84efe2014-01-15 15:36:59 +01001157 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1158 unsigned long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001160 ASSERT_RTNL();
1161
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001162 spin_lock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001163 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -07001164 ifp->state = INET6_IFADDR_STATE_DEAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001165 spin_unlock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001166
1167 if (state == INET6_IFADDR_STATE_DEAD)
1168 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
stephen hemminger5c578aed2010-03-17 20:31:11 +00001170 spin_lock_bh(&addrconf_hash_lock);
1171 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578aed2010-03-17 20:31:11 +00001172 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Thomas Haller5b84efe2014-01-15 15:36:59 +01001174 write_lock_bh(&ifp->idev->lock);
David S. Miller5d9efa72013-10-28 20:07:50 -04001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001177 list_del(&ifp->tmp_list);
1178 if (ifp->ifpub) {
1179 in6_ifa_put(ifp->ifpub);
1180 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
stephen hemminger372e6c82010-03-17 20:31:09 +00001182 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Thomas Haller5b84efe2014-01-15 15:36:59 +01001185 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1186 action = check_cleanup_prefix_route(ifp, &expires);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001187
Thomas Haller5b84efe2014-01-15 15:36:59 +01001188 list_del_init(&ifp->if_list);
1189 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Thomas Haller5b84efe2014-01-15 15:36:59 +01001191 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001193 addrconf_del_dad_work(ifp);
Andrey Vaginb2238562008-07-08 15:13:31 -07001194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 ipv6_ifa_notify(RTM_DELADDR, ifp);
1196
Cong Wangf88c91d2013-04-14 23:18:43 +08001197 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Thomas Haller5b84efe2014-01-15 15:36:59 +01001199 if (action != CLEANUP_PREFIX_RT_NOP) {
1200 cleanup_prefix_route(ifp, expires,
1201 action == CLEANUP_PREFIX_RT_DEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
1203
Daniel Walterc3968a82011-04-13 21:10:57 +00001204 /* clean up prefsrc entries */
1205 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001206out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 in6_ifa_put(ifp);
1208}
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1211{
1212 struct inet6_dev *idev = ifp->idev;
1213 struct in6_addr addr, *tmpaddr;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001214 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001215 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 int tmp_plen;
1217 int ret = 0;
Neil Horman95c385b2007-04-25 17:08:10 -07001218 u32 addr_flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001219 unsigned long now = jiffies;
Jiri Bohac76506a92016-10-13 18:52:15 +02001220 long max_desync_factor;
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001221 s32 cnf_temp_preferred_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Jiri Pirko53bd6742013-12-06 09:45:22 +01001223 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (ift) {
1225 spin_lock_bh(&ift->lock);
1226 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1227 spin_unlock_bh(&ift->lock);
1228 tmpaddr = &addr;
1229 } else {
1230 tmpaddr = NULL;
1231 }
1232retry:
1233 in6_dev_hold(idev);
1234 if (idev->cnf.use_tempaddr <= 0) {
Jiri Pirko53bd6742013-12-06 09:45:22 +01001235 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001236 pr_info("%s: use_tempaddr is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 in6_dev_put(idev);
1238 ret = -1;
1239 goto out;
1240 }
1241 spin_lock_bh(&ifp->lock);
1242 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1243 idev->cnf.use_tempaddr = -1; /*XXX*/
1244 spin_unlock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01001245 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001246 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1247 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 in6_dev_put(idev);
1249 ret = -1;
1250 goto out;
1251 }
1252 in6_ifa_hold(ifp);
1253 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
Jiri Bohac9d6280d2016-10-13 18:50:02 +02001254 ipv6_try_regen_rndid(idev, tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001256 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001258 regen_advance = idev->cnf.regen_max_retry *
Ian Morris67ba4152014-08-24 21:53:10 +01001259 idev->cnf.dad_transmits *
1260 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
Jiri Bohac76506a92016-10-13 18:52:15 +02001261
1262 /* recalculate max_desync_factor each time and update
1263 * idev->desync_factor if it's larger
1264 */
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001265 cnf_temp_preferred_lft = READ_ONCE(idev->cnf.temp_prefered_lft);
Jiri Bohac76506a92016-10-13 18:52:15 +02001266 max_desync_factor = min_t(__u32,
1267 idev->cnf.max_desync_factor,
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001268 cnf_temp_preferred_lft - regen_advance);
Jiri Bohac76506a92016-10-13 18:52:15 +02001269
1270 if (unlikely(idev->desync_factor > max_desync_factor)) {
1271 if (max_desync_factor > 0) {
1272 get_random_bytes(&idev->desync_factor,
1273 sizeof(idev->desync_factor));
1274 idev->desync_factor %= max_desync_factor;
1275 } else {
1276 idev->desync_factor = 0;
1277 }
1278 }
1279
1280 tmp_valid_lft = min_t(__u32,
1281 ifp->valid_lft,
1282 idev->cnf.temp_valid_lft + age);
Jiri Bohac7aa8e632016-10-20 12:29:26 +02001283 tmp_prefered_lft = cnf_temp_preferred_lft + age -
Jiri Bohac76506a92016-10-13 18:52:15 +02001284 idev->desync_factor;
Jiri Bohac76506a92016-10-13 18:52:15 +02001285 tmp_prefered_lft = min_t(__u32, ifp->prefered_lft, tmp_prefered_lft);
1286 tmp_plen = ifp->prefix_len;
1287 tmp_tstamp = ifp->tstamp;
1288 spin_unlock_bh(&ifp->lock);
1289
Jiri Pirko53bd6742013-12-06 09:45:22 +01001290 write_unlock_bh(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -07001291
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001292 /* A temporary address is created only if this calculated Preferred
1293 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1294 * an implementation must not create a temporary address with a zero
1295 * Preferred Lifetime.
Heiner Kallweitecab6702014-03-12 22:13:19 +01001296 * Use age calculation as in addrconf_verify to avoid unnecessary
1297 * temporary addresses being generated.
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001298 */
Heiner Kallweitecab6702014-03-12 22:13:19 +01001299 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1300 if (tmp_prefered_lft <= regen_advance + age) {
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001301 in6_ifa_put(ifp);
1302 in6_dev_put(idev);
1303 ret = -1;
1304 goto out;
1305 }
1306
Neil Horman95c385b2007-04-25 17:08:10 -07001307 addr_flags = IFA_F_TEMPORARY;
1308 /* set in addrconf_prefix_rcv() */
1309 if (ifp->flags & IFA_F_OPTIMISTIC)
1310 addr_flags |= IFA_F_OPTIMISTIC;
1311
Hannes Frederic Sowa4b08a8f2013-08-16 13:02:27 +02001312 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1313 ipv6_addr_scope(&addr), addr_flags,
1314 tmp_valid_lft, tmp_prefered_lft);
1315 if (IS_ERR(ift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 in6_ifa_put(ifp);
1317 in6_dev_put(idev);
Joe Perchesf3213832012-05-15 14:11:53 +00001318 pr_info("%s: retry temporary address regeneration\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 tmpaddr = &addr;
Jiri Pirko53bd6742013-12-06 09:45:22 +01001320 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 goto retry;
1322 }
1323
1324 spin_lock_bh(&ift->lock);
1325 ift->ifpub = ifp;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001326 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 ift->tstamp = tmp_tstamp;
1328 spin_unlock_bh(&ift->lock);
1329
David S. Millercf22f9a2012-04-14 21:37:40 -04001330 addrconf_dad_start(ift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 in6_ifa_put(ift);
1332 in6_dev_put(idev);
1333out:
1334 return ret;
1335}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337/*
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001338 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001340enum {
1341 IPV6_SADDR_RULE_INIT = 0,
1342 IPV6_SADDR_RULE_LOCAL,
1343 IPV6_SADDR_RULE_SCOPE,
1344 IPV6_SADDR_RULE_PREFERRED,
1345#ifdef CONFIG_IPV6_MIP6
1346 IPV6_SADDR_RULE_HOA,
1347#endif
1348 IPV6_SADDR_RULE_OIF,
1349 IPV6_SADDR_RULE_LABEL,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001350 IPV6_SADDR_RULE_PRIVACY,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001351 IPV6_SADDR_RULE_ORCHID,
1352 IPV6_SADDR_RULE_PREFIX,
Erik Kline7fd25612014-10-28 18:11:14 +09001353#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1354 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1355#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001356 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001357};
1358
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001359struct ipv6_saddr_score {
1360 int rule;
1361 int addr_type;
1362 struct inet6_ifaddr *ifa;
1363 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1364 int scopedist;
1365 int matchlen;
1366};
1367
1368struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001369 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001370 int ifindex;
1371 int scope;
1372 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001373 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001374};
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001375
Dave Jonesb6f99a22007-03-22 12:27:49 -07001376static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Ulrich Weber45bb0062010-02-25 23:28:58 +00001378 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001379 return 1;
1380 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
Erik Kline7fd25612014-10-28 18:11:14 +09001383static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
1384{
1385#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1386 return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
1387#else
1388 return false;
1389#endif
1390}
1391
Benjamin Thery3de23252008-05-28 14:51:24 +02001392static int ipv6_get_saddr_eval(struct net *net,
1393 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001394 struct ipv6_saddr_dst *dst,
1395 int i)
1396{
1397 int ret;
1398
1399 if (i <= score->rule) {
1400 switch (i) {
1401 case IPV6_SADDR_RULE_SCOPE:
1402 ret = score->scopedist;
1403 break;
1404 case IPV6_SADDR_RULE_PREFIX:
1405 ret = score->matchlen;
1406 break;
1407 default:
1408 ret = !!test_bit(i, score->scorebits);
1409 }
1410 goto out;
1411 }
1412
1413 switch (i) {
1414 case IPV6_SADDR_RULE_INIT:
1415 /* Rule 0: remember if hiscore is not ready yet */
1416 ret = !!score->ifa;
1417 break;
1418 case IPV6_SADDR_RULE_LOCAL:
1419 /* Rule 1: Prefer same address */
1420 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1421 break;
1422 case IPV6_SADDR_RULE_SCOPE:
1423 /* Rule 2: Prefer appropriate scope
1424 *
1425 * ret
1426 * ^
1427 * -1 | d 15
1428 * ---+--+-+---> scope
1429 * |
1430 * | d is scope of the destination.
1431 * B-d | \
1432 * | \ <- smaller scope is better if
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08001433 * B-15 | \ if scope is enough for destination.
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001434 * | ret = B - scope (-1 <= scope >= d <= 15).
1435 * d-C-1 | /
1436 * |/ <- greater is better
1437 * -C / if scope is not enough for destination.
1438 * /| ret = scope - C (-1 <= d < scope <= 15).
1439 *
1440 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1441 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1442 * Assume B = 0 and we get C > 29.
1443 */
1444 ret = __ipv6_addr_src_scope(score->addr_type);
1445 if (ret >= dst->scope)
1446 ret = -ret;
1447 else
1448 ret -= 128; /* 30 is enough */
1449 score->scopedist = ret;
1450 break;
1451 case IPV6_SADDR_RULE_PREFERRED:
Erik Kline7fd25612014-10-28 18:11:14 +09001452 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001453 /* Rule 3: Avoid deprecated and optimistic addresses */
Erik Kline7fd25612014-10-28 18:11:14 +09001454 u8 avoid = IFA_F_DEPRECATED;
1455
1456 if (!ipv6_use_optimistic_addr(score->ifa->idev))
1457 avoid |= IFA_F_OPTIMISTIC;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001458 ret = ipv6_saddr_preferred(score->addr_type) ||
Erik Kline7fd25612014-10-28 18:11:14 +09001459 !(score->ifa->flags & avoid);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001460 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001461 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001462#ifdef CONFIG_IPV6_MIP6
1463 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001464 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001465 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001466 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1467 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
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#endif
1471 case IPV6_SADDR_RULE_OIF:
1472 /* Rule 5: Prefer outgoing interface */
1473 ret = (!dst->ifindex ||
1474 dst->ifindex == score->ifa->idev->dev->ifindex);
1475 break;
1476 case IPV6_SADDR_RULE_LABEL:
1477 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001478 ret = ipv6_addr_label(net,
1479 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001480 score->ifa->idev->dev->ifindex) == dst->label;
1481 break;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001482 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001483 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001484 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001485 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001486 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001487 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1488 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1489 score->ifa->idev->cnf.use_tempaddr >= 2;
1490 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001491 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001492 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001493 case IPV6_SADDR_RULE_ORCHID:
1494 /* Rule 8-: Prefer ORCHID vs ORCHID or
1495 * non-ORCHID vs non-ORCHID
1496 */
1497 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1498 ipv6_addr_orchid(dst->addr));
1499 break;
1500 case IPV6_SADDR_RULE_PREFIX:
1501 /* Rule 8: Use longest matching prefix */
YOSHIFUJI Hideaki / 吉藤英明91b4b042012-09-10 18:41:07 +00001502 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1503 if (ret > score->ifa->prefix_len)
1504 ret = score->ifa->prefix_len;
1505 score->matchlen = ret;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001506 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001507#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1508 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1509 /* Optimistic addresses still have lower precedence than other
1510 * preferred addresses.
1511 */
1512 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1513 break;
1514#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001515 default:
1516 ret = 0;
1517 }
1518
1519 if (ret)
1520 __set_bit(i, score->scorebits);
1521 score->rule = i;
1522out:
1523 return ret;
1524}
1525
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001526static int __ipv6_dev_get_saddr(struct net *net,
1527 struct ipv6_saddr_dst *dst,
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001528 struct inet6_dev *idev,
1529 struct ipv6_saddr_score *scores,
1530 int hiscore_idx)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001531{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001532 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001533
1534 read_lock_bh(&idev->lock);
1535 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1536 int i;
1537
1538 /*
1539 * - Tentative Address (RFC2462 section 5.4)
1540 * - A tentative address is not considered
1541 * "assigned to an interface" in the traditional
1542 * sense, unless it is also flagged as optimistic.
1543 * - Candidate Source Address (section 4)
1544 * - In any case, anycast addresses, multicast
1545 * addresses, and the unspecified address MUST
1546 * NOT be included in a candidate set.
1547 */
1548 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1549 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1550 continue;
1551
1552 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1553
1554 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1555 score->addr_type & IPV6_ADDR_MULTICAST)) {
1556 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1557 idev->dev->name);
1558 continue;
1559 }
1560
1561 score->rule = -1;
1562 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1563
1564 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1565 int minihiscore, miniscore;
1566
1567 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1568 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1569
1570 if (minihiscore > miniscore) {
1571 if (i == IPV6_SADDR_RULE_SCOPE &&
1572 score->scopedist > 0) {
1573 /*
1574 * special case:
1575 * each remaining entry
1576 * has too small (not enough)
1577 * scope, because ifa entries
1578 * are sorted by their scope
1579 * values.
1580 */
1581 goto out;
1582 }
1583 break;
1584 } else if (minihiscore < miniscore) {
1585 if (hiscore->ifa)
1586 in6_ifa_put(hiscore->ifa);
1587
1588 in6_ifa_hold(score->ifa);
1589
1590 swap(hiscore, score);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001591 hiscore_idx = 1 - hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001592
1593 /* restore our iterator */
1594 score->ifa = hiscore->ifa;
1595
1596 break;
1597 }
1598 }
1599 }
1600out:
1601 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001602 return hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001603}
1604
David Ahernafbac6012016-06-16 16:24:26 -07001605static int ipv6_get_saddr_master(struct net *net,
1606 const struct net_device *dst_dev,
1607 const struct net_device *master,
1608 struct ipv6_saddr_dst *dst,
1609 struct ipv6_saddr_score *scores,
1610 int hiscore_idx)
1611{
1612 struct inet6_dev *idev;
1613
1614 idev = __in6_dev_get(dst_dev);
1615 if (idev)
1616 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1617 scores, hiscore_idx);
1618
1619 idev = __in6_dev_get(master);
1620 if (idev)
1621 hiscore_idx = __ipv6_dev_get_saddr(net, dst, idev,
1622 scores, hiscore_idx);
1623
1624 return hiscore_idx;
1625}
1626
Patrick McHardyb3f644f2012-08-26 19:14:14 +02001627int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001628 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001629 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001631 struct ipv6_saddr_score scores[2], *hiscore;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001632 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001633 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001634 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001635 int dst_type;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001636 bool use_oif_addr = false;
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001637 int hiscore_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001639 dst_type = __ipv6_addr_type(daddr);
1640 dst.addr = daddr;
1641 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1642 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001643 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001644 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001645
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001646 scores[hiscore_idx].rule = -1;
1647 scores[hiscore_idx].ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001649 rcu_read_lock();
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001650
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001651 /* Candidate Source Address (section 4)
1652 * - multicast and link-local destination address,
1653 * the set of candidate source address MUST only
1654 * include addresses assigned to interfaces
1655 * belonging to the same link as the outgoing
1656 * interface.
1657 * (- For site-local destination addresses, the
1658 * set of candidate source addresses MUST only
1659 * include addresses assigned to interfaces
1660 * belonging to the same site as the outgoing
1661 * interface.)
Erik Kline3985e8a2015-07-22 16:38:25 +09001662 * - "It is RECOMMENDED that the candidate source addresses
1663 * be the set of unicast addresses assigned to the
1664 * interface that will be used to send to the destination
1665 * (the 'outgoing' interface)." (RFC 6724)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001666 */
1667 if (dst_dev) {
Erik Kline3985e8a2015-07-22 16:38:25 +09001668 idev = __in6_dev_get(dst_dev);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001669 if ((dst_type & IPV6_ADDR_MULTICAST) ||
Erik Kline3985e8a2015-07-22 16:38:25 +09001670 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1671 (idev && idev->cnf.use_oif_addrs_only)) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001672 use_oif_addr = true;
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001673 }
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001674 }
1675
1676 if (use_oif_addr) {
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001677 if (idev)
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001678 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001679 } else {
David Ahernafbac6012016-06-16 16:24:26 -07001680 const struct net_device *master;
1681 int master_idx = 0;
1682
1683 /* if dst_dev exists and is enslaved to an L3 device, then
1684 * prefer addresses from dst_dev and then the master over
1685 * any other enslaved devices in the L3 domain.
1686 */
1687 master = l3mdev_master_dev_rcu(dst_dev);
1688 if (master) {
1689 master_idx = master->ifindex;
1690
1691 hiscore_idx = ipv6_get_saddr_master(net, dst_dev,
1692 master, &dst,
1693 scores, hiscore_idx);
1694
1695 if (scores[hiscore_idx].ifa)
1696 goto out;
1697 }
1698
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001699 for_each_netdev_rcu(net, dev) {
David Ahernafbac6012016-06-16 16:24:26 -07001700 /* only consider addresses on devices in the
1701 * same L3 domain
1702 */
1703 if (l3mdev_master_ifindex_rcu(dev) != master_idx)
1704 continue;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001705 idev = __in6_dev_get(dev);
1706 if (!idev)
1707 continue;
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001708 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001709 }
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001710 }
David Ahernafbac6012016-06-16 16:24:26 -07001711
1712out:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001713 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001715 hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001716 if (!hiscore->ifa)
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001717 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001718
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001719 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001720 in6_ifa_put(hiscore->ifa);
YOSHIFUJI Hideaki072047e42005-11-08 09:38:30 -08001721 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001723EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Amerigo Wang89657792013-06-29 21:30:49 +08001725int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001726 u32 banned_flags)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001727{
1728 struct inet6_ifaddr *ifp;
1729 int err = -EADDRNOTAVAIL;
1730
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01001731 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1732 if (ifp->scope > IFA_LINK)
1733 break;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001734 if (ifp->scope == IFA_LINK &&
1735 !(ifp->flags & banned_flags)) {
1736 *addr = ifp->addr;
1737 err = 0;
1738 break;
1739 }
1740 }
1741 return err;
1742}
1743
Neil Horman95c385b2007-04-25 17:08:10 -07001744int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001745 u32 banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
1747 struct inet6_dev *idev;
1748 int err = -EADDRNOTAVAIL;
1749
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001750 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001751 idev = __in6_dev_get(dev);
1752 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 read_lock_bh(&idev->lock);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001754 err = __ipv6_get_lladdr(idev, addr, banned_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 read_unlock_bh(&idev->lock);
1756 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001757 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 return err;
1759}
1760
1761static int ipv6_count_addresses(struct inet6_dev *idev)
1762{
1763 int cnt = 0;
1764 struct inet6_ifaddr *ifp;
1765
1766 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001767 list_for_each_entry(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 cnt++;
1769 read_unlock_bh(&idev->lock);
1770 return cnt;
1771}
1772
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001773int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Florian Westphal2a7851b2013-05-17 03:56:10 +00001774 const struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Erik Klinec58da4c2015-02-04 20:01:23 +09001776 return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1777}
1778EXPORT_SYMBOL(ipv6_chk_addr);
1779
1780int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1781 const struct net_device *dev, int strict,
1782 u32 banned_flags)
1783{
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001784 struct inet6_ifaddr *ifp;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001785 unsigned int hash = inet6_addr_hash(addr);
Erik Klinec58da4c2015-02-04 20:01:23 +09001786 u32 ifp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
stephen hemminger5c578aed2010-03-17 20:31:11 +00001788 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001789 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001790 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001791 continue;
Erik Klinec58da4c2015-02-04 20:01:23 +09001792 /* Decouple optimistic from tentative for evaluation here.
1793 * Ban optimistic addresses explicitly, when required.
1794 */
1795 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1796 ? (ifp->flags&~IFA_F_TENTATIVE)
1797 : ifp->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (ipv6_addr_equal(&ifp->addr, addr) &&
Erik Klinec58da4c2015-02-04 20:01:23 +09001799 !(ifp_flags&banned_flags) &&
Ian Morris63159f22015-03-29 14:00:04 +01001800 (!dev || ifp->idev->dev == dev ||
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001801 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1802 rcu_read_unlock_bh();
1803 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
1805 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00001806
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001807 rcu_read_unlock_bh();
1808 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
Erik Klinec58da4c2015-02-04 20:01:23 +09001810EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001811
David S. Miller3e81c6d2010-03-20 16:18:00 -07001812static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1813 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Eric Dumazetddbe5032012-07-18 08:11:12 +00001815 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001816 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Sasha Levinb67bfe02013-02-27 17:06:00 -08001818 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001819 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001820 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001822 if (!dev || ifp->idev->dev == dev)
David S. Miller3e81c6d2010-03-20 16:18:00 -07001823 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
1825 }
David S. Miller3e81c6d2010-03-20 16:18:00 -07001826 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827}
1828
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001829/* Compares an address/prefix_len with addresses on device @dev.
1830 * If one is found it returns true.
1831 */
1832bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1833 const unsigned int prefix_len, struct net_device *dev)
1834{
1835 struct inet6_dev *idev;
1836 struct inet6_ifaddr *ifa;
1837 bool ret = false;
1838
1839 rcu_read_lock();
1840 idev = __in6_dev_get(dev);
1841 if (idev) {
1842 read_lock_bh(&idev->lock);
1843 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1844 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1845 if (ret)
1846 break;
1847 }
1848 read_unlock_bh(&idev->lock);
1849 }
1850 rcu_read_unlock();
1851
1852 return ret;
1853}
1854EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1855
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001856int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001857{
1858 struct inet6_dev *idev;
1859 struct inet6_ifaddr *ifa;
1860 int onlink;
1861
1862 onlink = 0;
1863 rcu_read_lock();
1864 idev = __in6_dev_get(dev);
1865 if (idev) {
1866 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001867 list_for_each_entry(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001868 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1869 ifa->prefix_len);
1870 if (onlink)
1871 break;
1872 }
1873 read_unlock_bh(&idev->lock);
1874 }
1875 rcu_read_unlock();
1876 return onlink;
1877}
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001878EXPORT_SYMBOL(ipv6_chk_prefix);
1879
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001880struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001881 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
David S. Millerb79d1d52010-03-25 21:39:21 -07001883 struct inet6_ifaddr *ifp, *result = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001884 unsigned int hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
stephen hemminger5c578aed2010-03-17 20:31:11 +00001886 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001887 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001888 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001889 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001891 if (!dev || ifp->idev->dev == dev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07001893 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 in6_ifa_hold(ifp);
1895 break;
1896 }
1897 }
1898 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00001899 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
David S. Millerb79d1d52010-03-25 21:39:21 -07001901 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902}
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904/* Gets referenced address, destroys ifaddr */
1905
Brian Haleycc411d02009-09-09 14:41:32 +00001906static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
Lubomir Rintel3d171f32016-01-08 13:47:23 +01001908 if (dad_failed)
1909 ifp->flags |= IFA_F_DADFAILED;
1910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (ifp->flags&IFA_F_PERMANENT) {
1912 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001913 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 ifp->flags |= IFA_F_TENTATIVE;
1915 spin_unlock_bh(&ifp->lock);
Herbert Xue2577a02010-03-13 12:23:29 -08001916 if (dad_failed)
1917 ipv6_ifa_notify(0, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 } else if (ifp->flags&IFA_F_TEMPORARY) {
1920 struct inet6_ifaddr *ifpub;
1921 spin_lock_bh(&ifp->lock);
1922 ifpub = ifp->ifpub;
1923 if (ifpub) {
1924 in6_ifa_hold(ifpub);
1925 spin_unlock_bh(&ifp->lock);
1926 ipv6_create_tempaddr(ifpub, ifp);
1927 in6_ifa_put(ifpub);
1928 } else {
1929 spin_unlock_bh(&ifp->lock);
1930 }
1931 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001932 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935}
1936
Herbert Xuf2344a12010-05-18 15:55:27 -07001937static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1938{
1939 int err = -ENOENT;
1940
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001941 spin_lock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001942 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1943 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1944 err = 0;
1945 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001946 spin_unlock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001947
1948 return err;
1949}
1950
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001951void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1952{
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001953 struct inet6_dev *idev = ifp->idev;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001954 struct net *net = dev_net(ifp->idev->dev);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001955
Ursula Braun853dc2e2010-10-24 23:06:43 +00001956 if (addrconf_dad_end(ifp)) {
1957 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07001958 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00001959 }
Herbert Xuf2344a12010-05-18 15:55:27 -07001960
Joe Perchese87cc472012-05-13 21:56:26 +00001961 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1962 ifp->idev->dev->name, &ifp->addr);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001963
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001964 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001965
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001966 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
1967 int scope = ifp->scope;
1968 u32 flags = ifp->flags;
1969 struct in6_addr new_addr;
1970 struct inet6_ifaddr *ifp2;
1971 u32 valid_lft, preferred_lft;
1972 int pfxlen = ifp->prefix_len;
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001973 int retries = ifp->stable_privacy_retry + 1;
1974
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001975 if (retries > net->ipv6.sysctl.idgen_retries) {
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001976 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
1977 ifp->idev->dev->name);
1978 goto errdad;
1979 }
1980
1981 new_addr = ifp->addr;
1982 if (ipv6_generate_stable_address(&new_addr, retries,
1983 idev))
1984 goto errdad;
1985
1986 valid_lft = ifp->valid_lft;
1987 preferred_lft = ifp->prefered_lft;
1988
1989 spin_unlock_bh(&ifp->lock);
1990
1991 if (idev->cnf.max_addresses &&
1992 ipv6_count_addresses(idev) >=
1993 idev->cnf.max_addresses)
1994 goto lock_errdad;
1995
1996 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
1997 ifp->idev->dev->name);
1998
1999 ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
2000 scope, flags, valid_lft,
2001 preferred_lft);
2002 if (IS_ERR(ifp2))
2003 goto lock_errdad;
2004
2005 spin_lock_bh(&ifp2->lock);
2006 ifp2->stable_privacy_retry = retries;
2007 ifp2->state = INET6_IFADDR_STATE_PREDAD;
2008 spin_unlock_bh(&ifp2->lock);
2009
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01002010 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002011 in6_ifa_put(ifp2);
2012lock_errdad:
2013 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09002014 }
2015
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01002016errdad:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002017 /* transition from _POSTDAD to _ERRDAD */
2018 ifp->state = INET6_IFADDR_STATE_ERRDAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01002019 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002020
2021 addrconf_mod_dad_work(ifp, 0);
Wei Yongjun751eb6b2016-09-05 16:06:31 +08002022 in6_ifa_put(ifp);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002023}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002025/* Join to solicited addr multicast group.
2026 * caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002027void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 struct in6_addr maddr;
2030
2031 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2032 return;
2033
2034 addrconf_addr_solict_mult(addr, &maddr);
2035 ipv6_dev_mc_inc(dev, &maddr);
2036}
2037
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002038/* caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002039void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
2041 struct in6_addr maddr;
2042
2043 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
2044 return;
2045
2046 addrconf_addr_solict_mult(addr, &maddr);
2047 __ipv6_dev_mc_dec(idev, &maddr);
2048}
2049
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002050/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002051static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
2053 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002054
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002055 if (ifp->prefix_len >= 127) /* RFC 6164 */
Bjørn Mork2bda8a02011-07-05 23:04:13 +00002056 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2058 if (ipv6_addr_any(&addr))
2059 return;
WANG Cong013b4d92014-09-11 15:35:11 -07002060 __ipv6_dev_ac_inc(ifp->idev, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061}
2062
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02002063/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03002064static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
2066 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002067
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01002068 if (ifp->prefix_len >= 127) /* RFC 6164 */
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00002069 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
2071 if (ipv6_addr_any(&addr))
2072 return;
2073 __ipv6_dev_ac_dec(ifp->idev, &addr);
2074}
2075
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002076static int addrconf_ifid_6lowpan(u8 *eui, struct net_device *dev)
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002077{
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002078 switch (dev->addr_len) {
2079 case ETH_ALEN:
2080 return addrconf_ifid_eui48(eui, dev);
2081 case EUI64_ADDR_LEN:
2082 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
2083 eui[0] ^= 2;
2084 break;
2085 default:
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002086 return -1;
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002087 }
2088
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002089 return 0;
2090}
2091
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002092static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2093{
2094 union fwnet_hwaddr *ha;
2095
2096 if (dev->addr_len != FWNET_ALEN)
2097 return -1;
2098
2099 ha = (union fwnet_hwaddr *)dev->dev_addr;
2100
2101 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2102 eui[0] ^= 2;
2103 return 0;
2104}
2105
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002106static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2107{
2108 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2109 if (dev->addr_len != ARCNET_ALEN)
2110 return -1;
2111 memset(eui, 0, 7);
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002112 eui[7] = *(u8 *)dev->dev_addr;
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002113 return 0;
2114}
2115
2116static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2117{
2118 if (dev->addr_len != INFINIBAND_ALEN)
2119 return -1;
2120 memcpy(eui, dev->dev_addr + 12, 8);
2121 eui[0] |= 2;
2122 return 0;
2123}
2124
stephen hemmingerc61393e2010-10-04 20:17:53 +00002125static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002126{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00002127 if (addr == 0)
2128 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002129 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2130 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2131 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2132 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2133 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2134 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2135 eui[1] = 0;
2136 eui[2] = 0x5E;
2137 eui[3] = 0xFE;
2138 memcpy(eui + 4, &addr, 4);
2139 return 0;
2140}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002141
2142static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2143{
2144 if (dev->priv_flags & IFF_ISATAP)
2145 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2146 return -1;
2147}
2148
stephen hemmingeraee80b52011-06-08 10:44:30 +00002149static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2150{
2151 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2152}
2153
Nicolas Dichtele8377352013-08-20 12:16:06 +02002154static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2155{
2156 memcpy(eui, dev->perm_addr, 3);
2157 memcpy(eui + 5, dev->perm_addr + 3, 3);
2158 eui[3] = 0xFF;
2159 eui[4] = 0xFE;
2160 eui[0] ^= 2;
2161 return 0;
2162}
2163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2165{
2166 switch (dev->type) {
2167 case ARPHRD_ETHER:
2168 case ARPHRD_FDDI:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002169 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002171 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002173 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002174 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002175 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002176 case ARPHRD_IPGRE:
Felix Jia45ce0fd2017-01-26 16:59:18 +13002177 case ARPHRD_TUNNEL:
stephen hemmingeraee80b52011-06-08 10:44:30 +00002178 return addrconf_ifid_gre(eui, dev);
Jukka Rissanene74bccb82013-12-11 17:05:36 +02002179 case ARPHRD_6LOWPAN:
Alexander Aring8a7a4b42017-03-12 10:19:36 +02002180 return addrconf_ifid_6lowpan(eui, dev);
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002181 case ARPHRD_IEEE1394:
2182 return addrconf_ifid_ieee1394(eui, dev);
Nicolas Dichtele8377352013-08-20 12:16:06 +02002183 case ARPHRD_TUNNEL6:
Felix Jia45ce0fd2017-01-26 16:59:18 +13002184 case ARPHRD_IP6GRE:
Nicolas Dichtele8377352013-08-20 12:16:06 +02002185 return addrconf_ifid_ip6tnl(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 }
2187 return -1;
2188}
2189
2190static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2191{
2192 int err = -1;
2193 struct inet6_ifaddr *ifp;
2194
2195 read_lock_bh(&idev->lock);
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01002196 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2197 if (ifp->scope > IFA_LINK)
2198 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2200 memcpy(eui, ifp->addr.s6_addr+8, 8);
2201 err = 0;
2202 break;
2203 }
2204 }
2205 read_unlock_bh(&idev->lock);
2206 return err;
2207}
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002210static void ipv6_regen_rndid(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08002213 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
2216 /*
2217 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2218 * check if generated address is not inappropriate
2219 *
2220 * - Reserved subnet anycast (RFC 2526)
2221 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002222 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 * 00-00-5E-FE-xx-xx-xx-xx
2224 * - value 0
2225 * - XXX: already assigned to an address on the device
2226 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002227 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2229 (idev->rndid[7]&0x80))
2230 goto regen;
2231 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2232 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2233 goto regen;
2234 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2235 goto regen;
2236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237}
2238
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002239static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002240{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002242 ipv6_regen_rndid(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
2245/*
2246 * Add prefix route.
2247 */
2248
2249static void
2250addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002251 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Thomas Graf86872cb2006-08-22 00:01:08 -07002253 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002254 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
Thomas Graf86872cb2006-08-22 00:01:08 -07002255 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2256 .fc_ifindex = dev->ifindex,
2257 .fc_expires = expires,
2258 .fc_dst_len = plen,
2259 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002260 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07002261 .fc_protocol = RTPROT_KERNEL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002262 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002264 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 /* Prevent useless cloning on PtP SIT.
2267 This thing is done here expecting that the whole
2268 class of non-broadcast devices need not cloning.
2269 */
Amerigo Wang07a93622012-10-29 16:23:10 +00002270#if IS_ENABLED(CONFIG_IPV6_SIT)
Thomas Graf86872cb2006-08-22 00:01:08 -07002271 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2272 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002273#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Thomas Graf86872cb2006-08-22 00:01:08 -07002275 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276}
2277
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002278
2279static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2280 int plen,
2281 const struct net_device *dev,
2282 u32 flags, u32 noflags)
2283{
2284 struct fib6_node *fn;
2285 struct rt6_info *rt = NULL;
2286 struct fib6_table *table;
David Ahernca254492015-10-12 11:47:10 -07002287 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002288
David Ahernca254492015-10-12 11:47:10 -07002289 table = fib6_get_table(dev_net(dev), tb_id);
Ian Morris63159f22015-03-29 14:00:04 +01002290 if (!table)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002291 return NULL;
2292
Li RongQing5744dd92012-09-11 21:59:01 +00002293 read_lock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002294 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2295 if (!fn)
2296 goto out;
Martin KaFai Lau1f56a01f2015-04-28 13:03:03 -07002297
2298 noflags |= RTF_CACHE;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002299 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002300 if (rt->dst.dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002301 continue;
2302 if ((rt->rt6i_flags & flags) != flags)
2303 continue;
Romain Kuntz85da53b2013-01-09 15:02:26 +01002304 if ((rt->rt6i_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002305 continue;
2306 dst_hold(&rt->dst);
2307 break;
2308 }
2309out:
Li RongQing5744dd92012-09-11 21:59:01 +00002310 read_unlock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002311 return rt;
2312}
2313
2314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315/* Create "default" multicast route to the interface */
2316
2317static void addrconf_add_mroute(struct net_device *dev)
2318{
Thomas Graf86872cb2006-08-22 00:01:08 -07002319 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002320 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002321 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2322 .fc_ifindex = dev->ifindex,
2323 .fc_dst_len = 8,
2324 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002325 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002326 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
Thomas Graf86872cb2006-08-22 00:01:08 -07002328 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2329
2330 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331}
2332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2334{
2335 struct inet6_dev *idev;
2336
2337 ASSERT_RTNL();
2338
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002339 idev = ipv6_find_idev(dev);
2340 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00002341 return ERR_PTR(-ENOBUFS);
2342
2343 if (idev->cnf.disable_ipv6)
2344 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
2346 /* Add default multicast route */
David Ahernba46ee42016-06-13 13:44:18 -07002347 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
Li Wei4af04ab2011-12-06 21:23:45 +00002348 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 return idev;
2351}
2352
Jiri Pirko53bd6742013-12-06 09:45:22 +01002353static void manage_tempaddrs(struct inet6_dev *idev,
2354 struct inet6_ifaddr *ifp,
2355 __u32 valid_lft, __u32 prefered_lft,
2356 bool create, unsigned long now)
2357{
2358 u32 flags;
2359 struct inet6_ifaddr *ift;
2360
2361 read_lock_bh(&idev->lock);
2362 /* update all temporary addresses in the list */
2363 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2364 int age, max_valid, max_prefered;
2365
2366 if (ifp != ift->ifpub)
2367 continue;
2368
2369 /* RFC 4941 section 3.3:
2370 * If a received option will extend the lifetime of a public
2371 * address, the lifetimes of temporary addresses should
2372 * be extended, subject to the overall constraint that no
2373 * temporary addresses should ever remain "valid" or "preferred"
2374 * for a time longer than (TEMP_VALID_LIFETIME) or
2375 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2376 */
2377 age = (now - ift->cstamp) / HZ;
2378 max_valid = idev->cnf.temp_valid_lft - age;
2379 if (max_valid < 0)
2380 max_valid = 0;
2381
2382 max_prefered = idev->cnf.temp_prefered_lft -
Jiri Bohac76506a92016-10-13 18:52:15 +02002383 idev->desync_factor - age;
Jiri Pirko53bd6742013-12-06 09:45:22 +01002384 if (max_prefered < 0)
2385 max_prefered = 0;
2386
2387 if (valid_lft > max_valid)
2388 valid_lft = max_valid;
2389
2390 if (prefered_lft > max_prefered)
2391 prefered_lft = max_prefered;
2392
2393 spin_lock(&ift->lock);
2394 flags = ift->flags;
2395 ift->valid_lft = valid_lft;
2396 ift->prefered_lft = prefered_lft;
2397 ift->tstamp = now;
2398 if (prefered_lft > 0)
2399 ift->flags &= ~IFA_F_DEPRECATED;
2400
2401 spin_unlock(&ift->lock);
2402 if (!(flags&IFA_F_TENTATIVE))
2403 ipv6_ifa_notify(0, ift);
2404 }
2405
2406 if ((create || list_empty(&idev->tempaddr_list)) &&
2407 idev->cnf.use_tempaddr > 0) {
2408 /* When a new public address is created as described
2409 * in [ADDRCONF], also create a new temporary address.
2410 * Also create a temporary address if it's enabled but
2411 * no temporary address currently exists.
2412 */
2413 read_unlock_bh(&idev->lock);
2414 ipv6_create_tempaddr(ifp, NULL);
2415 } else {
2416 read_unlock_bh(&idev->lock);
2417 }
2418}
2419
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002420static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2421{
Felix Jiad35a00b2017-01-26 16:59:17 +13002422 return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2423 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002424}
2425
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002426int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2427 const struct prefix_info *pinfo,
2428 struct inet6_dev *in6_dev,
2429 const struct in6_addr *addr, int addr_type,
2430 u32 addr_flags, bool sllao, bool tokenized,
2431 __u32 valid_lft, u32 prefered_lft)
Alexander Aring4f672232016-06-15 21:20:22 +02002432{
2433 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2434 int create = 0, update_lft = 0;
2435
2436 if (!ifp && valid_lft) {
2437 int max_addresses = in6_dev->cnf.max_addresses;
2438
2439#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2440 if (in6_dev->cnf.optimistic_dad &&
2441 !net->ipv6.devconf_all->forwarding && sllao)
2442 addr_flags |= IFA_F_OPTIMISTIC;
2443#endif
2444
2445 /* Do not allow to create too much of autoconfigured
2446 * addresses; this would be too easy way to crash kernel.
2447 */
2448 if (!max_addresses ||
2449 ipv6_count_addresses(in6_dev) < max_addresses)
2450 ifp = ipv6_add_addr(in6_dev, addr, NULL,
2451 pinfo->prefix_len,
2452 addr_type&IPV6_ADDR_SCOPE_MASK,
2453 addr_flags, valid_lft,
2454 prefered_lft);
2455
2456 if (IS_ERR_OR_NULL(ifp))
2457 return -1;
2458
2459 update_lft = 0;
2460 create = 1;
2461 spin_lock_bh(&ifp->lock);
2462 ifp->flags |= IFA_F_MANAGETEMPADDR;
2463 ifp->cstamp = jiffies;
2464 ifp->tokenized = tokenized;
2465 spin_unlock_bh(&ifp->lock);
2466 addrconf_dad_start(ifp);
2467 }
2468
2469 if (ifp) {
2470 u32 flags;
2471 unsigned long now;
2472 u32 stored_lft;
2473
2474 /* update lifetime (RFC2462 5.5.3 e) */
2475 spin_lock_bh(&ifp->lock);
2476 now = jiffies;
2477 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2478 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2479 else
2480 stored_lft = 0;
2481 if (!update_lft && !create && stored_lft) {
2482 const u32 minimum_lft = min_t(u32,
2483 stored_lft, MIN_VALID_LIFETIME);
2484 valid_lft = max(valid_lft, minimum_lft);
2485
2486 /* RFC4862 Section 5.5.3e:
2487 * "Note that the preferred lifetime of the
2488 * corresponding address is always reset to
2489 * the Preferred Lifetime in the received
2490 * Prefix Information option, regardless of
2491 * whether the valid lifetime is also reset or
2492 * ignored."
2493 *
2494 * So we should always update prefered_lft here.
2495 */
2496 update_lft = 1;
2497 }
2498
2499 if (update_lft) {
2500 ifp->valid_lft = valid_lft;
2501 ifp->prefered_lft = prefered_lft;
2502 ifp->tstamp = now;
2503 flags = ifp->flags;
2504 ifp->flags &= ~IFA_F_DEPRECATED;
2505 spin_unlock_bh(&ifp->lock);
2506
2507 if (!(flags&IFA_F_TENTATIVE))
2508 ipv6_ifa_notify(0, ifp);
2509 } else
2510 spin_unlock_bh(&ifp->lock);
2511
2512 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2513 create, now);
2514
2515 in6_ifa_put(ifp);
2516 addrconf_verify();
2517 }
2518
2519 return 0;
2520}
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002521EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
Alexander Aring4f672232016-06-15 21:20:22 +02002522
Neil Hormane6bff992012-01-04 10:49:15 +00002523void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
2525 struct prefix_info *pinfo;
2526 __u32 valid_lft;
2527 __u32 prefered_lft;
Alexander Aring4f672232016-06-15 21:20:22 +02002528 int addr_type, err;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002529 u32 addr_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07002531 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
2533 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002534
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 if (len < sizeof(struct prefix_info)) {
dingtianhongba3542e2013-08-17 10:27:04 +08002536 ADBG("addrconf: prefix option too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 return;
2538 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 /*
2541 * Validation checks ([ADDRCONF], page 19)
2542 */
2543
2544 addr_type = ipv6_addr_type(&pinfo->prefix);
2545
2546 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2547 return;
2548
2549 valid_lft = ntohl(pinfo->valid);
2550 prefered_lft = ntohl(pinfo->prefered);
2551
2552 if (prefered_lft > valid_lft) {
Joe Perchese87cc472012-05-13 21:56:26 +00002553 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 return;
2555 }
2556
2557 in6_dev = in6_dev_get(dev);
2558
Ian Morris63159f22015-03-29 14:00:04 +01002559 if (!in6_dev) {
Joe Perchese87cc472012-05-13 21:56:26 +00002560 net_dbg_ratelimited("addrconf: device %s not configured\n",
2561 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 return;
2563 }
2564
2565 /*
2566 * Two things going on here:
2567 * 1) Add routes for on-link prefixes
2568 * 2) Configure prefixes with the auto flag set
2569 */
2570
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002571 if (pinfo->onlink) {
2572 struct rt6_info *rt;
2573 unsigned long rt_expires;
2574
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002575 /* Avoid arithmetic overflow. Really, we could
2576 * save rt_expires in seconds, likely valid_lft,
2577 * but it would require division in fib gc, that it
2578 * not good.
2579 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002580 if (HZ > USER_HZ)
2581 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2582 else
2583 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08002584
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002585 if (addrconf_finite_timeout(rt_expires))
2586 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002588 rt = addrconf_get_prefix_route(&pinfo->prefix,
2589 pinfo->prefix_len,
2590 dev,
2591 RTF_ADDRCONF | RTF_PREFIX_RT,
2592 RTF_GATEWAY | RTF_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002594 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002595 /* Autoconf prefix route */
2596 if (valid_lft == 0) {
2597 ip6_del_rt(rt);
2598 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002599 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002600 /* not infinity */
Gao feng1716a962012-04-06 00:13:10 +00002601 rt6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002602 } else {
Gao feng1716a962012-04-06 00:13:10 +00002603 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 }
2605 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002606 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002607 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2608 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002609 /* not infinity */
2610 flags |= RTF_EXPIRES;
2611 expires = jiffies_to_clock_t(rt_expires);
2612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002614 dev, expires, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00002616 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
2618
2619 /* Try to figure out our local address for this prefix */
2620
2621 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 struct in6_addr addr;
Alexander Aringf997c552016-06-15 21:20:23 +02002623 bool tokenized = false, dev_addr_generated = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
2625 if (pinfo->prefix_len == 64) {
2626 memcpy(&addr, &pinfo->prefix, 8);
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002627
2628 if (!ipv6_addr_any(&in6_dev->token)) {
2629 read_lock_bh(&in6_dev->lock);
2630 memcpy(addr.s6_addr + 8,
2631 in6_dev->token.s6_addr + 8, 8);
2632 read_unlock_bh(&in6_dev->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00002633 tokenized = true;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002634 } else if (is_addr_mode_generate_stable(in6_dev) &&
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01002635 !ipv6_generate_stable_address(&addr, 0,
2636 in6_dev)) {
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002637 addr_flags |= IFA_F_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01002638 goto ok;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002639 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2640 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
Alexander Aring4f672232016-06-15 21:20:22 +02002641 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002642 } else {
2643 dev_addr_generated = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 }
2645 goto ok;
2646 }
Joe Perchese87cc472012-05-13 21:56:26 +00002647 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2648 pinfo->prefix_len);
Alexander Aring4f672232016-06-15 21:20:22 +02002649 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
2651ok:
Alexander Aring4f672232016-06-15 21:20:22 +02002652 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2653 &addr, addr_type,
2654 addr_flags, sllao,
2655 tokenized, valid_lft,
2656 prefered_lft);
2657 if (err)
2658 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002659
2660 /* Ignore error case here because previous prefix add addr was
2661 * successful which will be notified.
2662 */
2663 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2664 addr_type, addr_flags, sllao,
2665 tokenized, valid_lft,
2666 prefered_lft,
2667 dev_addr_generated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 }
2669 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
Alexander Aring4f672232016-06-15 21:20:22 +02002670put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 in6_dev_put(in6_dev);
2672}
2673
2674/*
2675 * Set destination address.
2676 * Special case for SIT interfaces where we create a new "virtual"
2677 * device.
2678 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002679int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680{
2681 struct in6_ifreq ireq;
2682 struct net_device *dev;
2683 int err = -EINVAL;
2684
2685 rtnl_lock();
2686
2687 err = -EFAULT;
2688 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2689 goto err_exit;
2690
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002691 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
2693 err = -ENODEV;
Ian Morris63159f22015-03-29 14:00:04 +01002694 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 goto err_exit;
2696
Amerigo Wang07a93622012-10-29 16:23:10 +00002697#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002699 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 struct ip_tunnel_parm p;
2702
2703 err = -EADDRNOTAVAIL;
2704 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2705 goto err_exit;
2706
2707 memset(&p, 0, sizeof(p));
2708 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2709 p.iph.saddr = 0;
2710 p.iph.version = 4;
2711 p.iph.ihl = 5;
2712 p.iph.protocol = IPPROTO_IPV6;
2713 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002714 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002716 if (ops->ndo_do_ioctl) {
2717 mm_segment_t oldfs = get_fs();
2718
2719 set_fs(KERNEL_DS);
2720 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2721 set_fs(oldfs);
2722 } else
2723 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
2725 if (err == 0) {
2726 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002727 dev = __dev_get_by_name(net, p.name);
2728 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 goto err_exit;
2730 err = dev_open(dev);
2731 }
2732 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002733#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
2735err_exit:
2736 rtnl_unlock();
2737 return err;
2738}
2739
Madhu Challa93a714d2015-02-25 09:58:35 -08002740static int ipv6_mc_config(struct sock *sk, bool join,
2741 const struct in6_addr *addr, int ifindex)
2742{
2743 int ret;
2744
2745 ASSERT_RTNL();
2746
2747 lock_sock(sk);
2748 if (join)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002749 ret = ipv6_sock_mc_join(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002750 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002751 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002752 release_sock(sk);
2753
2754 return ret;
2755}
2756
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757/*
2758 * Manual configuration of address on an interface
2759 */
Jiri Pirko479840f2013-12-06 09:45:21 +01002760static int inet6_addr_add(struct net *net, int ifindex,
2761 const struct in6_addr *pfx,
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002762 const struct in6_addr *peer_pfx,
Jiri Pirko479840f2013-12-06 09:45:21 +01002763 unsigned int plen, __u32 ifa_flags,
2764 __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
2766 struct inet6_ifaddr *ifp;
2767 struct inet6_dev *idev;
2768 struct net_device *dev;
Madhu Challa93a714d2015-02-25 09:58:35 -08002769 unsigned long timeout;
2770 clock_t expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002772 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
2774 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002775
Thomas Graf24ef0da2008-05-28 16:54:22 +02002776 if (plen > 128)
2777 return -EINVAL;
2778
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09002779 /* check the lifetime */
2780 if (!valid_lft || prefered_lft > valid_lft)
2781 return -EINVAL;
2782
Jiri Pirko53bd6742013-12-06 09:45:22 +01002783 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2784 return -EINVAL;
2785
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002786 dev = __dev_get_by_index(net, ifindex);
2787 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002789
Brian Haley64e724f2010-07-20 10:34:30 +00002790 idev = addrconf_add_dev(dev);
2791 if (IS_ERR(idev))
2792 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Madhu Challa93a714d2015-02-25 09:58:35 -08002794 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2795 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2796 true, pfx, ifindex);
2797
2798 if (ret < 0)
2799 return ret;
2800 }
2801
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 scope = ipv6_addr_scope(pfx);
2803
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002804 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2805 if (addrconf_finite_timeout(timeout)) {
2806 expires = jiffies_to_clock_t(timeout * HZ);
2807 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002808 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002809 } else {
2810 expires = 0;
2811 flags = 0;
2812 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002813 }
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09002814
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002815 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2816 if (addrconf_finite_timeout(timeout)) {
2817 if (timeout == 0)
2818 ifa_flags |= IFA_F_DEPRECATED;
2819 prefered_lft = timeout;
2820 }
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09002821
Jiri Benc8a226b22013-08-01 10:41:28 +02002822 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2823 valid_lft, prefered_lft);
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09002824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 if (!IS_ERR(ifp)) {
Thomas Haller761aac72014-01-15 15:36:58 +01002826 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2827 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2828 expires, flags);
2829 }
2830
Neil Horman95c385b2007-04-25 17:08:10 -07002831 /*
2832 * Note that section 3.1 of RFC 4429 indicates
2833 * that the Optimistic flag should not be set for
2834 * manually configured addresses
2835 */
David S. Millercf22f9a2012-04-14 21:37:40 -04002836 addrconf_dad_start(ifp);
Jiri Pirko53bd6742013-12-06 09:45:22 +01002837 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2838 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2839 true, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002841 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 return 0;
Madhu Challa93a714d2015-02-25 09:58:35 -08002843 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2844 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2845 false, pfx, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 }
2847
2848 return PTR_ERR(ifp);
2849}
2850
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002851static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2852 const struct in6_addr *pfx, unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853{
2854 struct inet6_ifaddr *ifp;
2855 struct inet6_dev *idev;
2856 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002857
Thomas Graf24ef0da2008-05-28 16:54:22 +02002858 if (plen > 128)
2859 return -EINVAL;
2860
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002861 dev = __dev_get_by_index(net, ifindex);
2862 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 return -ENODEV;
2864
Ian Morrise5d08d72014-11-23 21:28:43 +00002865 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01002866 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 return -ENXIO;
2868
2869 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002870 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 if (ifp->prefix_len == plen &&
2872 ipv6_addr_equal(pfx, &ifp->addr)) {
2873 in6_ifa_hold(ifp);
2874 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002875
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002876 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2877 (ifa_flags & IFA_F_MANAGETEMPADDR))
2878 manage_tempaddrs(idev, ifp, 0, 0, false,
2879 jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 ipv6_del_addr(ifp);
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002881 addrconf_verify_rtnl();
Madhu Challa93a714d2015-02-25 09:58:35 -08002882 if (ipv6_addr_is_multicast(pfx)) {
2883 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2884 false, pfx, dev->ifindex);
2885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 return 0;
2887 }
2888 }
2889 read_unlock_bh(&idev->lock);
2890 return -EADDRNOTAVAIL;
2891}
2892
2893
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002894int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
2896 struct in6_ifreq ireq;
2897 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002898
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002899 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2903 return -EFAULT;
2904
2905 rtnl_lock();
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002906 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002907 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2908 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 rtnl_unlock();
2910 return err;
2911}
2912
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002913int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914{
2915 struct in6_ifreq ireq;
2916 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002917
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002918 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 return -EPERM;
2920
2921 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2922 return -EFAULT;
2923
2924 rtnl_lock();
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002925 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002926 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 rtnl_unlock();
2928 return err;
2929}
2930
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002931static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2932 int plen, int scope)
2933{
2934 struct inet6_ifaddr *ifp;
2935
Jiri Benc8a226b22013-08-01 10:41:28 +02002936 ifp = ipv6_add_addr(idev, addr, NULL, plen,
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01002937 scope, IFA_F_PERMANENT,
2938 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002939 if (!IS_ERR(ifp)) {
2940 spin_lock_bh(&ifp->lock);
2941 ifp->flags &= ~IFA_F_TENTATIVE;
2942 spin_unlock_bh(&ifp->lock);
Paolo Abeni764d3be2016-11-22 16:57:40 +01002943 rt_genid_bump_ipv6(dev_net(idev->dev));
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002944 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2945 in6_ifa_put(ifp);
2946 }
2947}
2948
Amerigo Wang07a93622012-10-29 16:23:10 +00002949#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950static void sit_add_v4_addrs(struct inet6_dev *idev)
2951{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 struct in6_addr addr;
2953 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002954 struct net *net = dev_net(idev->dev);
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002955 int scope, plen;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002956 u32 pflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
2958 ASSERT_RTNL();
2959
2960 memset(&addr, 0, sizeof(struct in6_addr));
2961 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2962
2963 if (idev->dev->flags&IFF_POINTOPOINT) {
2964 addr.s6_addr32[0] = htonl(0xfe800000);
2965 scope = IFA_LINK;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002966 plen = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 } else {
2968 scope = IPV6_ADDR_COMPATv4;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002969 plen = 96;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002970 pflags |= RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 }
2972
2973 if (addr.s6_addr32[3]) {
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002974 add_addr(idev, &addr, plen, scope);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002975 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 return;
2977 }
2978
Benjamin Thery6fda7352008-03-05 10:47:47 -08002979 for_each_netdev(net, dev) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002980 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if (in_dev && (dev->flags & IFF_UP)) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002982 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
2984 int flag = scope;
2985
2986 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
2988 addr.s6_addr32[3] = ifa->ifa_local;
2989
2990 if (ifa->ifa_scope == RT_SCOPE_LINK)
2991 continue;
2992 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2993 if (idev->dev->flags&IFF_POINTOPOINT)
2994 continue;
2995 flag |= IFA_HOST;
2996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002998 add_addr(idev, &addr, plen, flag);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002999 addrconf_prefix_route(&addr, plen, idev->dev, 0,
3000 pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 }
3002 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003005#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
3007static void init_loopback(struct net_device *dev)
3008{
3009 struct inet6_dev *idev;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303010 struct net_device *sp_dev;
3011 struct inet6_ifaddr *sp_ifa;
3012 struct rt6_info *sp_rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
3014 /* ::1 */
3015
3016 ASSERT_RTNL();
3017
Ian Morrise5d08d72014-11-23 21:28:43 +00003018 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003019 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003020 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 return;
3022 }
3023
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003024 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303025
3026 /* Add routes to other interface's IPv6 addresses */
3027 for_each_netdev(dev_net(dev), sp_dev) {
3028 if (!strcmp(sp_dev->name, dev->name))
3029 continue;
3030
3031 idev = __in6_dev_get(sp_dev);
3032 if (!idev)
3033 continue;
3034
3035 read_lock_bh(&idev->lock);
3036 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
3037
3038 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
3039 continue;
3040
Gao feng33d99112014-01-24 16:29:11 +08003041 if (sp_ifa->rt) {
3042 /* This dst has been added to garbage list when
3043 * lo device down, release this obsolete dst and
3044 * reallocate a new router for ifa.
3045 */
Nicolas Dichtela2204452016-10-12 10:10:40 +02003046 if (!atomic_read(&sp_ifa->rt->rt6i_ref)) {
Gao feng33d99112014-01-24 16:29:11 +08003047 ip6_rt_put(sp_ifa->rt);
3048 sp_ifa->rt = NULL;
3049 } else {
3050 continue;
3051 }
3052 }
Gao fenga881ae12013-06-16 11:14:30 +08003053
François-Xavier Le Bail57ec0af2013-12-02 11:28:49 +01003054 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303055
3056 /* Failure cases are ignored */
Gao feng534c8772013-06-02 22:16:21 +00003057 if (!IS_ERR(sp_rt)) {
3058 sp_ifa->rt = sp_rt;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303059 ip6_ins_rt(sp_rt);
Gao feng534c8772013-06-02 22:16:21 +00003060 }
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303061 }
3062 read_unlock_bh(&idev->lock);
3063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064}
3065
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003066void addrconf_add_linklocal(struct inet6_dev *idev,
3067 const struct in6_addr *addr, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003069 struct inet6_ifaddr *ifp;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003070 u32 addr_flags = flags | IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Neil Horman95c385b2007-04-25 17:08:10 -07003072#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3073 if (idev->cnf.optimistic_dad &&
David Miller702beb82008-07-20 18:17:02 -07003074 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
Neil Horman95c385b2007-04-25 17:08:10 -07003075 addr_flags |= IFA_F_OPTIMISTIC;
3076#endif
3077
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01003078 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
3079 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003081 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
David S. Millercf22f9a2012-04-14 21:37:40 -04003082 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 in6_ifa_put(ifp);
3084 }
3085}
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003086EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003088static bool ipv6_reserved_interfaceid(struct in6_addr address)
3089{
3090 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3091 return true;
3092
3093 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3094 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3095 return true;
3096
3097 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3098 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3099 return true;
3100
3101 return false;
3102}
3103
3104static int ipv6_generate_stable_address(struct in6_addr *address,
3105 u8 dad_count,
3106 const struct inet6_dev *idev)
3107{
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003108 static DEFINE_SPINLOCK(lock);
3109 static __u32 digest[SHA_DIGEST_WORDS];
3110 static __u32 workspace[SHA_WORKSPACE_WORDS];
3111
3112 static union {
3113 char __data[SHA_MESSAGE_BYTES];
3114 struct {
3115 struct in6_addr secret;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003116 __be32 prefix[2];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003117 unsigned char hwaddr[MAX_ADDR_LEN];
3118 u8 dad_count;
3119 } __packed;
3120 } data;
3121
3122 struct in6_addr secret;
3123 struct in6_addr temp;
3124 struct net *net = dev_net(idev->dev);
3125
3126 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3127
3128 if (idev->cnf.stable_secret.initialized)
3129 secret = idev->cnf.stable_secret.secret;
3130 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3131 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3132 else
3133 return -1;
3134
3135retry:
3136 spin_lock_bh(&lock);
3137
3138 sha_init(digest);
3139 memset(&data, 0, sizeof(data));
3140 memset(workspace, 0, sizeof(workspace));
3141 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003142 data.prefix[0] = address->s6_addr32[0];
3143 data.prefix[1] = address->s6_addr32[1];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003144 data.secret = secret;
3145 data.dad_count = dad_count;
3146
3147 sha_transform(digest, data.__data, workspace);
3148
3149 temp = *address;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003150 temp.s6_addr32[2] = (__force __be32)digest[0];
3151 temp.s6_addr32[3] = (__force __be32)digest[1];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003152
3153 spin_unlock_bh(&lock);
3154
3155 if (ipv6_reserved_interfaceid(temp)) {
3156 dad_count++;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01003157 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003158 return -1;
3159 goto retry;
3160 }
3161
3162 *address = temp;
3163 return 0;
3164}
3165
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003166static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3167{
3168 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3169
3170 if (s->initialized)
3171 return;
3172 s = &idev->cnf.stable_secret;
3173 get_random_bytes(&s->secret, sizeof(s->secret));
3174 s->initialized = true;
3175}
3176
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003177static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3178{
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003179 struct in6_addr addr;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003180
David Ahernca254492015-10-12 11:47:10 -07003181 /* no link local addresses on L3 master devices */
3182 if (netif_is_l3_master(idev->dev))
3183 return;
3184
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003185 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3186
Felix Jiad35a00b2017-01-26 16:59:17 +13003187 switch (idev->cnf.addr_gen_mode) {
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003188 case IN6_ADDR_GEN_MODE_RANDOM:
3189 ipv6_gen_mode_random_init(idev);
3190 /* fallthrough */
3191 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003192 if (!ipv6_generate_stable_address(&addr, 0, idev))
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003193 addrconf_add_linklocal(idev, &addr,
3194 IFA_F_STABLE_PRIVACY);
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003195 else if (prefix_route)
3196 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003197 break;
3198 case IN6_ADDR_GEN_MODE_EUI64:
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003199 /* addrconf_add_linklocal also adds a prefix_route and we
3200 * only need to care about prefix routes if ipv6_generate_eui64
3201 * couldn't generate one.
3202 */
3203 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003204 addrconf_add_linklocal(idev, &addr, 0);
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003205 else if (prefix_route)
3206 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003207 break;
3208 case IN6_ADDR_GEN_MODE_NONE:
3209 default:
3210 /* will not add any link local address */
3211 break;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003212 }
3213}
3214
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215static void addrconf_dev_config(struct net_device *dev)
3216{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003217 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
3219 ASSERT_RTNL();
3220
Herbert Xu74235a22007-06-14 13:02:55 -07003221 if ((dev->type != ARPHRD_ETHER) &&
3222 (dev->type != ARPHRD_FDDI) &&
Herbert Xu74235a22007-06-14 13:02:55 -07003223 (dev->type != ARPHRD_ARCNET) &&
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00003224 (dev->type != ARPHRD_INFINIBAND) &&
Nicolas Dichtele8377352013-08-20 12:16:06 +02003225 (dev->type != ARPHRD_IEEE1394) &&
Jukka Rissanene74bccb82013-12-11 17:05:36 +02003226 (dev->type != ARPHRD_TUNNEL6) &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003227 (dev->type != ARPHRD_6LOWPAN) &&
Felix Jia45ce0fd2017-01-26 16:59:18 +13003228 (dev->type != ARPHRD_IP6GRE) &&
3229 (dev->type != ARPHRD_IPGRE) &&
3230 (dev->type != ARPHRD_TUNNEL) &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003231 (dev->type != ARPHRD_NONE)) {
Herbert Xu74235a22007-06-14 13:02:55 -07003232 /* Alas, we support only Ethernet autoconfiguration. */
3233 return;
3234 }
3235
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00003237 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 return;
3239
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003240 /* this device type has no EUI support */
3241 if (dev->type == ARPHRD_NONE &&
Felix Jiad35a00b2017-01-26 16:59:17 +13003242 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3243 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003244
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003245 addrconf_addr_gen(idev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246}
3247
Amerigo Wang07a93622012-10-29 16:23:10 +00003248#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249static void addrconf_sit_config(struct net_device *dev)
3250{
3251 struct inet6_dev *idev;
3252
3253 ASSERT_RTNL();
3254
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003255 /*
3256 * Configure the tunnel with one of our IPv4
3257 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 * our v4 addrs in the tunnel
3259 */
3260
Ian Morrise5d08d72014-11-23 21:28:43 +00003261 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003262 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003263 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 return;
3265 }
3266
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003267 if (dev->priv_flags & IFF_ISATAP) {
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003268 addrconf_addr_gen(idev, false);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003269 return;
3270 }
3271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 sit_add_v4_addrs(idev);
3273
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00003274 if (dev->flags&IFF_POINTOPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003277#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
Amerigo Wang07a93622012-10-29 16:23:10 +00003279#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003280static void addrconf_gre_config(struct net_device *dev)
3281{
3282 struct inet6_dev *idev;
stephen hemmingeraee80b52011-06-08 10:44:30 +00003283
stephen hemmingeraee80b52011-06-08 10:44:30 +00003284 ASSERT_RTNL();
3285
Ian Morrise5d08d72014-11-23 21:28:43 +00003286 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003287 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003288 pr_debug("%s: add_dev failed\n", __func__);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003289 return;
3290 }
3291
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003292 addrconf_addr_gen(idev, true);
Hannes Frederic Sowad9e4ce62015-10-08 18:19:39 +02003293 if (dev->flags & IFF_POINTOPOINT)
3294 addrconf_add_mroute(dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003295}
3296#endif
3297
David Ahernf1705ec2016-02-24 09:25:37 -08003298static int fixup_permanent_addr(struct inet6_dev *idev,
3299 struct inet6_ifaddr *ifp)
3300{
3301 if (!ifp->rt) {
3302 struct rt6_info *rt;
3303
3304 rt = addrconf_dst_alloc(idev, &ifp->addr, false);
3305 if (unlikely(IS_ERR(rt)))
3306 return PTR_ERR(rt);
3307
3308 ifp->rt = rt;
3309 }
3310
3311 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3312 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3313 idev->dev, 0, 0);
3314 }
3315
3316 addrconf_dad_start(ifp);
3317
3318 return 0;
3319}
3320
3321static void addrconf_permanent_addr(struct net_device *dev)
3322{
3323 struct inet6_ifaddr *ifp, *tmp;
3324 struct inet6_dev *idev;
3325
3326 idev = __in6_dev_get(dev);
3327 if (!idev)
3328 return;
3329
3330 write_lock_bh(&idev->lock);
3331
3332 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3333 if ((ifp->flags & IFA_F_PERMANENT) &&
3334 fixup_permanent_addr(idev, ifp) < 0) {
3335 write_unlock_bh(&idev->lock);
3336 ipv6_del_addr(ifp);
3337 write_lock_bh(&idev->lock);
3338
3339 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3340 idev->dev->name, &ifp->addr);
3341 }
3342 }
3343
3344 write_unlock_bh(&idev->lock);
3345}
3346
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003347static int addrconf_notify(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00003348 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349{
Jiri Pirko351638e2013-05-28 01:30:21 +00003350 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
David Ahern4f7f34e2016-04-07 11:10:41 -07003351 struct netdev_notifier_changeupper_info *info;
Eric Dumazet748e2d92012-08-22 21:50:59 +00003352 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003353 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07003354 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003356 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003357 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07003358 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003359 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -07003360 if (IS_ERR(idev))
3361 return notifier_from_errno(PTR_ERR(idev));
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003362 }
3363 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003364
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003365 case NETDEV_CHANGEMTU:
3366 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3367 if (dev->mtu < IPV6_MIN_MTU) {
3368 addrconf_ifdown(dev, 1);
3369 break;
3370 }
3371
3372 if (idev) {
3373 rt6_mtu_change(dev, dev->mtu);
3374 idev->cnf.mtu6 = dev->mtu;
3375 break;
3376 }
3377
3378 /* allocate new idev */
3379 idev = ipv6_add_dev(dev);
3380 if (IS_ERR(idev))
3381 break;
3382
3383 /* device is still not ready */
3384 if (!(idev->if_flags & IF_READY))
3385 break;
3386
3387 run_pending = 1;
3388
3389 /* fall through */
3390
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003392 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07003393 if (dev->flags & IFF_SLAVE)
3394 break;
3395
WANG Cong3ce62a82014-09-11 15:07:16 -07003396 if (idev && idev->cnf.disable_ipv6)
3397 break;
3398
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003399 if (event == NETDEV_UP) {
David Ahern38bd10c2016-04-21 20:56:12 -07003400 /* restore routes for permanent addresses */
3401 addrconf_permanent_addr(dev);
3402
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07003403 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003404 /* device is not ready yet. */
Joe Perchesf3213832012-05-15 14:11:53 +00003405 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003406 dev->name);
3407 break;
3408 }
Kristian Slavov99081042006-02-08 16:10:53 -08003409
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11003410 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3411 idev = ipv6_add_dev(dev);
3412
WANG Conga317a2f2014-07-25 15:25:09 -07003413 if (!IS_ERR_OR_NULL(idev)) {
Kristian Slavov99081042006-02-08 16:10:53 -08003414 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08003415 run_pending = 1;
3416 }
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003417 } else if (event == NETDEV_CHANGE) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07003418 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003419 /* device is still not ready. */
3420 break;
3421 }
3422
3423 if (idev) {
Linus Lüssinga088d1d2017-02-03 08:11:03 +01003424 if (idev->if_flags & IF_READY) {
3425 /* device is already configured -
3426 * but resend MLD reports, we might
3427 * have roamed and need to update
3428 * multicast snooping switches
3429 */
3430 ipv6_mc_up(idev);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003431 break;
Linus Lüssinga088d1d2017-02-03 08:11:03 +01003432 }
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003433 idev->if_flags |= IF_READY;
3434 }
3435
Joe Perchesf3213832012-05-15 14:11:53 +00003436 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3437 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003438
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003439 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003440 }
3441
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003442 switch (dev->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +00003443#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 case ARPHRD_SIT:
3445 addrconf_sit_config(dev);
3446 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07003447#endif
Amerigo Wang07a93622012-10-29 16:23:10 +00003448#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003449 case ARPHRD_IPGRE:
3450 addrconf_gre_config(dev);
3451 break;
3452#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 case ARPHRD_LOOPBACK:
3454 init_loopback(dev);
3455 break;
3456
3457 default:
3458 addrconf_dev_config(dev);
3459 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003460 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003461
WANG Conga317a2f2014-07-25 15:25:09 -07003462 if (!IS_ERR_OR_NULL(idev)) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003463 if (run_pending)
3464 addrconf_dad_run(idev);
3465
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003466 /*
3467 * If the MTU changed during the interface down,
3468 * when the interface up, the changed MTU must be
3469 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003471 if (idev->cnf.mtu6 != dev->mtu &&
3472 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 rt6_mtu_change(dev, dev->mtu);
3474 idev->cnf.mtu6 = dev->mtu;
3475 }
3476 idev->tstamp = jiffies;
3477 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003478
3479 /*
3480 * If the changed mtu during down is lower than
3481 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 */
3483 if (dev->mtu < IPV6_MIN_MTU)
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003484 addrconf_ifdown(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 }
3486 break;
3487
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 case NETDEV_DOWN:
3489 case NETDEV_UNREGISTER:
3490 /*
3491 * Remove all addresses from this interface.
3492 */
3493 addrconf_ifdown(dev, event != NETDEV_DOWN);
3494 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003495
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07003498 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003499 addrconf_sysctl_unregister(idev);
WANG Conga317a2f2014-07-25 15:25:09 -07003500 err = addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07003501 if (err)
3502 return notifier_from_errno(err);
WANG Conga317a2f2014-07-25 15:25:09 -07003503 err = snmp6_register_dev(idev);
3504 if (err) {
3505 addrconf_sysctl_unregister(idev);
3506 return notifier_from_errno(err);
3507 }
Stephen Hemminger5632c512007-04-28 21:16:39 -07003508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003510
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003511 case NETDEV_PRE_TYPE_CHANGE:
3512 case NETDEV_POST_TYPE_CHANGE:
Andrew Lunn3ef09522015-12-03 21:12:32 +01003513 if (idev)
3514 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07003515 break;
David Ahern4f7f34e2016-04-07 11:10:41 -07003516
3517 case NETDEV_CHANGEUPPER:
3518 info = ptr;
3519
3520 /* flush all routes if dev is linked to or unlinked from
3521 * an L3 master device (e.g., VRF)
3522 */
3523 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3524 addrconf_ifdown(dev, 0);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
3527 return NOTIFY_OK;
3528}
3529
3530/*
3531 * addrconf module should be notified of a device going up
3532 */
3533static struct notifier_block ipv6_dev_notf = {
3534 .notifier_call = addrconf_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535};
3536
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003537static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07003538{
3539 struct inet6_dev *idev;
3540 ASSERT_RTNL();
3541
3542 idev = __in6_dev_get(dev);
3543
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003544 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003545 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003546 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003547 ipv6_mc_unmap(idev);
3548}
3549
David Ahern70af9212016-04-08 12:01:21 -07003550static bool addr_is_local(const struct in6_addr *addr)
3551{
3552 return ipv6_addr_type(addr) &
3553 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3554}
3555
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556static int addrconf_ifdown(struct net_device *dev, int how)
3557{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003558 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003559 struct inet6_dev *idev;
David Ahernf1705ec2016-02-24 09:25:37 -08003560 struct inet6_ifaddr *ifa, *tmp;
3561 struct list_head del_list;
3562 int _keep_addr;
3563 bool keep_addr;
David S. Miller73a8bd72011-01-23 23:27:15 -08003564 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
3566 ASSERT_RTNL();
3567
David S. Miller73a8bd72011-01-23 23:27:15 -08003568 rt6_ifdown(net, dev);
3569 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
3571 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003572 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 return -ENODEV;
3574
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003575 /*
3576 * Step 1: remove reference to ipv6 device from parent device.
3577 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003579 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003581
3582 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003583 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
3585 /* Step 1.5: remove snmp6 entry */
3586 snmp6_unregister_dev(idev);
3587
3588 }
3589
David Ahernf1705ec2016-02-24 09:25:37 -08003590 /* aggregate the system setting and interface setting */
3591 _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3592 if (!_keep_addr)
3593 _keep_addr = idev->cnf.keep_addr_on_down;
3594
3595 /* combine the user config with event to determine if permanent
3596 * addresses are to be removed from address hash table
3597 */
Mike Manningbc561632016-08-12 12:02:38 +01003598 keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003599
David S. Miller73a8bd72011-01-23 23:27:15 -08003600 /* Step 2: clear hash table */
3601 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3602 struct hlist_head *h = &inet6_addr_lst[i];
David S. Miller73a8bd72011-01-23 23:27:15 -08003603
3604 spin_lock_bh(&addrconf_hash_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01003605restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08003606 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
David S. Miller73a8bd72011-01-23 23:27:15 -08003607 if (ifa->idev == idev) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003608 addrconf_del_dad_work(ifa);
David Ahernf1705ec2016-02-24 09:25:37 -08003609 /* combined flag + permanent flag decide if
3610 * address is retained on a down event
3611 */
3612 if (!keep_addr ||
David Ahern70af9212016-04-08 12:01:21 -07003613 !(ifa->flags & IFA_F_PERMANENT) ||
3614 addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003615 hlist_del_init_rcu(&ifa->addr_lst);
3616 goto restart;
3617 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003618 }
3619 }
3620 spin_unlock_bh(&addrconf_hash_lock);
3621 }
3622
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 write_lock_bh(&idev->lock);
3624
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003625 addrconf_del_rs_timer(idev);
3626
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003627 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003628 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003629 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003631 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00003632 while (!list_empty(&idev->tempaddr_list)) {
3633 ifa = list_first_entry(&idev->tempaddr_list,
3634 struct inet6_ifaddr, tmp_list);
3635 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 write_unlock_bh(&idev->lock);
3637 spin_lock_bh(&ifa->lock);
3638
3639 if (ifa->ifpub) {
3640 in6_ifa_put(ifa->ifpub);
3641 ifa->ifpub = NULL;
3642 }
3643 spin_unlock_bh(&ifa->lock);
3644 in6_ifa_put(ifa);
3645 write_lock_bh(&idev->lock);
3646 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003647
David Ahernf1705ec2016-02-24 09:25:37 -08003648 /* re-combine the user config with event to determine if permanent
3649 * addresses are to be removed from the interface list
3650 */
Mike Manningbc561632016-08-12 12:02:38 +01003651 keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003652
3653 INIT_LIST_HEAD(&del_list);
3654 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
David Ahern38bd10c2016-04-21 20:56:12 -07003655 struct rt6_info *rt = NULL;
3656
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003657 addrconf_del_dad_work(ifa);
stephen hemminger84e8b802010-03-02 13:32:46 +00003658
David S. Miller73a8bd72011-01-23 23:27:15 -08003659 write_unlock_bh(&idev->lock);
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003660 spin_lock_bh(&ifa->lock);
David Ahernf1705ec2016-02-24 09:25:37 -08003661
David Ahern70af9212016-04-08 12:01:21 -07003662 if (keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3663 !addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003664 /* set state to skip the notifier below */
3665 state = INET6_IFADDR_STATE_DEAD;
3666 ifa->state = 0;
3667 if (!(ifa->flags & IFA_F_NODAD))
3668 ifa->flags |= IFA_F_TENTATIVE;
David Ahern38bd10c2016-04-21 20:56:12 -07003669
3670 rt = ifa->rt;
3671 ifa->rt = NULL;
David Ahernf1705ec2016-02-24 09:25:37 -08003672 } else {
3673 state = ifa->state;
3674 ifa->state = INET6_IFADDR_STATE_DEAD;
3675
Wei Yongjunc8822192016-07-28 16:19:03 +00003676 list_move(&ifa->if_list, &del_list);
David Ahernf1705ec2016-02-24 09:25:37 -08003677 }
3678
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003679 spin_unlock_bh(&ifa->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003680
David Ahern38bd10c2016-04-21 20:56:12 -07003681 if (rt)
3682 ip6_del_rt(rt);
3683
David S. Miller73a8bd72011-01-23 23:27:15 -08003684 if (state != INET6_IFADDR_STATE_DEAD) {
3685 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Cong Wangf88c91d2013-04-14 23:18:43 +08003686 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
Mike Manningea06f712016-07-22 18:32:11 +01003687 } else {
3688 if (idev->cnf.forwarding)
3689 addrconf_leave_anycast(ifa);
3690 addrconf_leave_solict(ifa->idev, &ifa->addr);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00003691 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003692
David S. Miller73a8bd72011-01-23 23:27:15 -08003693 write_lock_bh(&idev->lock);
3694 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003695
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 write_unlock_bh(&idev->lock);
3697
David Ahernf1705ec2016-02-24 09:25:37 -08003698 /* now clean up addresses to be removed */
3699 while (!list_empty(&del_list)) {
3700 ifa = list_first_entry(&del_list,
3701 struct inet6_ifaddr, if_list);
3702 list_del(&ifa->if_list);
3703
3704 in6_ifa_put(ifa);
3705 }
3706
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003707 /* Step 5: Discard anycast and multicast list */
3708 if (how) {
3709 ipv6_ac_destroy_dev(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 ipv6_mc_destroy_dev(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003711 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 ipv6_mc_down(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003716
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003717 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003718 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003719 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 neigh_parms_release(&nd_tbl, idev->nd_parms);
3721 neigh_ifdown(&nd_tbl, dev);
3722 in6_dev_put(idev);
3723 }
3724 return 0;
3725}
3726
3727static void addrconf_rs_timer(unsigned long data)
3728{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003729 struct inet6_dev *idev = (struct inet6_dev *)data;
Cong Wangcaf92bc2013-08-31 13:44:32 +08003730 struct net_device *dev = idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003731 struct in6_addr lladdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003733 write_lock(&idev->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00003734 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 goto out;
3736
Shmulik Ladkani9ba2add2012-12-02 01:44:53 +00003737 if (!ipv6_accept_ra(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003739
3740 /* Announcement received after solicitation was sent */
3741 if (idev->if_flags & IF_RA_RCVD)
3742 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003744 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
Cong Wangcaf92bc2013-08-31 13:44:32 +08003745 write_unlock(&idev->lock);
3746 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3747 ndisc_send_rs(dev, &lladdr,
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003748 &in6addr_linklocal_allrouters);
3749 else
Cong Wangcaf92bc2013-08-31 13:44:32 +08003750 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
Cong Wangcaf92bc2013-08-31 13:44:32 +08003752 write_lock(&idev->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003753 idev->rs_interval = rfc3315_s14_backoff_update(
3754 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003755 /* The wait after the last probe can be shorter */
3756 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3757 idev->cnf.rtr_solicits) ?
3758 idev->cnf.rtr_solicit_delay :
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003759 idev->rs_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 /*
3762 * Note: we do not support deprecated "all on-link"
3763 * assumption any longer.
3764 */
Joe Perches91df42b2012-05-15 14:11:54 +00003765 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 }
3767
3768out:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003769 write_unlock(&idev->lock);
Cong Wangcaf92bc2013-08-31 13:44:32 +08003770put:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003771 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772}
3773
3774/*
3775 * Duplicate Address Detection
3776 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003777static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3778{
3779 unsigned long rand_num;
3780 struct inet6_dev *idev = ifp->idev;
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003781 u64 nonce;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003782
Neil Horman95c385b2007-04-25 17:08:10 -07003783 if (ifp->flags & IFA_F_OPTIMISTIC)
3784 rand_num = 0;
3785 else
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003786 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
Neil Horman95c385b2007-04-25 17:08:10 -07003787
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003788 nonce = 0;
3789 if (idev->cnf.enhanced_dad ||
3790 dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
3791 do
3792 get_random_bytes(&nonce, 6);
3793 while (nonce == 0);
3794 }
3795 ifp->dad_nonce = nonce;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003796 ifp->dad_probes = idev->cnf.dad_transmits;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003797 addrconf_mod_dad_work(ifp, rand_num);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003798}
3799
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003800static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801{
3802 struct inet6_dev *idev = ifp->idev;
3803 struct net_device *dev = idev->dev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003804 bool bump_id, notify = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
3806 addrconf_join_solict(dev, &ifp->addr);
3807
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003808 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809
3810 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003811 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003812 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
3815 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09003816 idev->cnf.accept_dad < 1 ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003817 !(ifp->flags&IFA_F_TENTATIVE) ||
3818 ifp->flags & IFA_F_NODAD) {
Paolo Abeni764d3be2016-11-22 16:57:40 +01003819 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003820 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003821 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 read_unlock_bh(&idev->lock);
3823
Paolo Abeni764d3be2016-11-22 16:57:40 +01003824 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 return;
3826 }
3827
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003828 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003829 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003830 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003831 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003832 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003833 * - keep it tentative if it is a permanent address.
3834 * - otherwise, kill it.
3835 */
3836 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003837 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003838 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003839 }
Neil Horman95c385b2007-04-25 17:08:10 -07003840
3841 /*
3842 * Optimistic nodes can start receiving
3843 * Frames right away
3844 */
Erik Kline7fd25612014-10-28 18:11:14 +09003845 if (ifp->flags & IFA_F_OPTIMISTIC) {
Neil Horman95c385b2007-04-25 17:08:10 -07003846 ip6_ins_rt(ifp->rt);
Erik Kline7fd25612014-10-28 18:11:14 +09003847 if (ipv6_use_optimistic_addr(idev)) {
3848 /* Because optimistic nodes can use this address,
3849 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3850 */
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003851 notify = true;
Erik Kline7fd25612014-10-28 18:11:14 +09003852 }
3853 }
Neil Horman95c385b2007-04-25 17:08:10 -07003854
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003855 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003857 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 read_unlock_bh(&idev->lock);
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003859 if (notify)
3860 ipv6_ifa_notify(RTM_NEWADDR, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861}
3862
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003863static void addrconf_dad_start(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003865 bool begin_dad = false;
3866
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003867 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003868 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3869 ifp->state = INET6_IFADDR_STATE_PREDAD;
3870 begin_dad = true;
3871 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003872 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003873
3874 if (begin_dad)
3875 addrconf_mod_dad_work(ifp, 0);
3876}
3877
3878static void addrconf_dad_work(struct work_struct *w)
3879{
3880 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3881 struct inet6_ifaddr,
3882 dad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 struct inet6_dev *idev = ifp->idev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003884 bool bump_id, disable_ipv6 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 struct in6_addr mcaddr;
3886
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003887 enum {
3888 DAD_PROCESS,
3889 DAD_BEGIN,
3890 DAD_ABORT,
3891 } action = DAD_PROCESS;
3892
3893 rtnl_lock();
3894
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003895 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003896 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3897 action = DAD_BEGIN;
3898 ifp->state = INET6_IFADDR_STATE_DAD;
3899 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3900 action = DAD_ABORT;
3901 ifp->state = INET6_IFADDR_STATE_POSTDAD;
Mike Manning85b51b12016-08-18 14:39:40 +01003902
3903 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6 &&
3904 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
3905 struct in6_addr addr;
3906
3907 addr.s6_addr32[0] = htonl(0xfe800000);
3908 addr.s6_addr32[1] = 0;
3909
3910 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
3911 ipv6_addr_equal(&ifp->addr, &addr)) {
3912 /* DAD failed for link-local based on MAC */
3913 idev->cnf.disable_ipv6 = 1;
3914
3915 pr_info("%s: IPv6 being disabled!\n",
3916 ifp->idev->dev->name);
3917 disable_ipv6 = true;
3918 }
3919 }
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003920 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003921 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003922
3923 if (action == DAD_BEGIN) {
3924 addrconf_dad_begin(ifp);
3925 goto out;
3926 } else if (action == DAD_ABORT) {
Wei Yongjun751eb6b2016-09-05 16:06:31 +08003927 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003928 addrconf_dad_stop(ifp, 1);
Mike Manning85b51b12016-08-18 14:39:40 +01003929 if (disable_ipv6)
3930 addrconf_ifdown(idev->dev, 0);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003931 goto out;
3932 }
3933
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003934 if (!ifp->dad_probes && addrconf_dad_end(ifp))
Herbert Xuf2344a12010-05-18 15:55:27 -07003935 goto out;
3936
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003937 write_lock_bh(&idev->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003938 if (idev->dead || !(idev->if_flags & IF_READY)) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003939 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 goto out;
3941 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003942
3943 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003944 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3945 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003946 write_unlock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003947 goto out;
3948 }
3949
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003950 if (ifp->dad_probes == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 /*
3952 * DAD was successful
3953 */
3954
Paolo Abeni764d3be2016-11-22 16:57:40 +01003955 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003956 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003957 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003958 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
Paolo Abeni764d3be2016-11-22 16:57:40 +01003960 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961
3962 goto out;
3963 }
3964
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003965 ifp->dad_probes--;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003966 addrconf_mod_dad_work(ifp,
3967 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
stephen hemminger21809fa2010-02-08 19:48:52 +00003968 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003969 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
3971 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003973 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
3974 ifp->dad_nonce);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975out:
3976 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003977 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978}
3979
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003980/* ifp->idev must be at least read locked */
3981static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3982{
3983 struct inet6_ifaddr *ifpiter;
3984 struct inet6_dev *idev = ifp->idev;
3985
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01003986 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3987 if (ifpiter->scope > IFA_LINK)
3988 break;
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003989 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3990 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3991 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
3992 IFA_F_PERMANENT)
3993 return false;
3994 }
3995 return true;
3996}
3997
Paolo Abeni764d3be2016-11-22 16:57:40 +01003998static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004000 struct net_device *dev = ifp->idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004001 struct in6_addr lladdr;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004002 bool send_rs, send_mld;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004003
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004004 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
4006 /*
4007 * Configure the address for reception. Now it is valid.
4008 */
4009
4010 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4011
Tore Anderson026359b2011-08-28 23:47:33 +00004012 /* If added prefix is link local and we are prepared to process
4013 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 */
4015
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004016 read_lock_bh(&ifp->idev->lock);
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004017 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004018 send_rs = send_mld &&
4019 ipv6_accept_ra(ifp->idev) &&
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004020 ifp->idev->cnf.rtr_solicits != 0 &&
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004021 (dev->flags&IFF_LOOPBACK) == 0;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004022 read_unlock_bh(&ifp->idev->lock);
4023
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004024 /* While dad is in progress mld report's source address is in6_addrany.
4025 * Resend with proper ll now.
4026 */
4027 if (send_mld)
4028 ipv6_mc_dad_complete(ifp->idev);
4029
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004030 if (send_rs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 /*
4032 * If a host as already performed a random delay
4033 * [...] as part of DAD [...] there is no need
4034 * to delay again before sending the first RS
4035 */
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004036 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004037 return;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004038 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004040 write_lock_bh(&ifp->idev->lock);
4041 spin_lock(&ifp->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004042 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4043 ifp->idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004044 ifp->idev->rs_probes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 ifp->idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004046 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004047 spin_unlock(&ifp->lock);
4048 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 }
Paolo Abeni764d3be2016-11-22 16:57:40 +01004050
4051 if (bump_id)
4052 rt_genid_bump_ipv6(dev_net(dev));
Marcus Huewea11a7f72017-02-06 18:34:56 +01004053
4054 /* Make sure that a new temporary address will be created
4055 * before this temporary address becomes deprecated.
4056 */
4057 if (ifp->flags & IFA_F_TEMPORARY)
4058 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059}
4060
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004061static void addrconf_dad_run(struct inet6_dev *idev)
4062{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004063 struct inet6_ifaddr *ifp;
4064
4065 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00004066 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00004067 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07004068 if (ifp->flags & IFA_F_TENTATIVE &&
4069 ifp->state == INET6_IFADDR_STATE_DAD)
4070 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00004071 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004072 }
4073 read_unlock_bh(&idev->lock);
4074}
4075
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076#ifdef CONFIG_PROC_FS
4077struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08004078 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004080 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081};
4082
Mihai Maruseac1d578302012-01-03 23:31:35 +00004083static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084{
4085 struct inet6_ifaddr *ifa = NULL;
4086 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004087 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00004088 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
Mihai Maruseac1d578302012-01-03 23:31:35 +00004090 /* initial bucket if pos is 0 */
4091 if (pos == 0) {
4092 state->bucket = 0;
4093 state->offset = 0;
4094 }
4095
4096 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08004097 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00004098 addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004099 if (!net_eq(dev_net(ifa->idev->dev), net))
4100 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004101 /* sync with offset */
4102 if (p < state->offset) {
4103 p++;
4104 continue;
4105 }
4106 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004107 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004108 }
4109
4110 /* prepare for next bucket */
4111 state->offset = 0;
4112 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004114 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115}
4116
stephen hemmingerc2e21292010-03-17 20:31:10 +00004117static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4118 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119{
4120 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004121 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122
Sasha Levinb67bfe02013-02-27 17:06:00 -08004123 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004124 if (!net_eq(dev_net(ifa->idev->dev), net))
4125 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004126 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004127 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004128 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004129
4130 while (++state->bucket < IN6_ADDR_HSIZE) {
Mihai Maruseac1d578302012-01-03 23:31:35 +00004131 state->offset = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004132 hlist_for_each_entry_rcu_bh(ifa,
stephen hemmingerc2e21292010-03-17 20:31:10 +00004133 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004134 if (!net_eq(dev_net(ifa->idev->dev), net))
4135 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004136 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004137 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08004138 }
4139 }
4140
stephen hemmingerc2e21292010-03-17 20:31:10 +00004141 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142}
4143
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004145 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146{
stephen hemminger5c578aed2010-03-17 20:31:11 +00004147 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00004148 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149}
4150
4151static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4152{
4153 struct inet6_ifaddr *ifa;
4154
4155 ifa = if6_get_next(seq, v);
4156 ++*pos;
4157 return ifa;
4158}
4159
4160static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004161 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162{
stephen hemminger5c578aed2010-03-17 20:31:11 +00004163 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164}
4165
4166static int if6_seq_show(struct seq_file *seq, void *v)
4167{
4168 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Jiri Pirko971a3512013-12-10 13:56:29 +01004169 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07004170 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 ifp->idev->dev->ifindex,
4172 ifp->prefix_len,
4173 ifp->scope,
Jiri Pirko971a3512013-12-10 13:56:29 +01004174 (u8) ifp->flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 ifp->idev->dev->name);
4176 return 0;
4177}
4178
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004179static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 .start = if6_seq_start,
4181 .next = if6_seq_next,
4182 .show = if6_seq_show,
4183 .stop = if6_seq_stop,
4184};
4185
4186static int if6_seq_open(struct inode *inode, struct file *file)
4187{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004188 return seq_open_net(inode, file, &if6_seq_ops,
4189 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190}
4191
Arjan van de Ven9a321442007-02-12 00:55:35 -08004192static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 .owner = THIS_MODULE,
4194 .open = if6_seq_open,
4195 .read = seq_read,
4196 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004197 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198};
4199
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004200static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201{
Gao fengd4beaa62013-02-18 01:34:54 +00004202 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 return -ENOMEM;
4204 return 0;
4205}
4206
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004207static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08004208{
Gao fengece31ff2013-02-18 01:34:56 +00004209 remove_proc_entry("if_inet6", net->proc_net);
Daniel Lezcano3c400902008-01-10 22:42:49 -08004210}
4211
4212static struct pernet_operations if6_proc_net_ops = {
Ian Morris67ba4152014-08-24 21:53:10 +01004213 .init = if6_proc_net_init,
4214 .exit = if6_proc_net_exit,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004215};
4216
4217int __init if6_proc_init(void)
4218{
4219 return register_pernet_subsys(&if6_proc_net_ops);
4220}
4221
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222void if6_proc_exit(void)
4223{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004224 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225}
4226#endif /* CONFIG_PROC_FS */
4227
Amerigo Wang07a93622012-10-29 16:23:10 +00004228#if IS_ENABLED(CONFIG_IPV6_MIP6)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004229/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004230int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004231{
4232 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004233 struct inet6_ifaddr *ifp = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00004234 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004235
stephen hemminger5c578aed2010-03-17 20:31:11 +00004236 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08004237 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09004238 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08004239 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09004240 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004241 (ifp->flags & IFA_F_HOMEADDRESS)) {
4242 ret = 1;
4243 break;
4244 }
4245 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00004246 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004247 return ret;
4248}
4249#endif
4250
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251/*
4252 * Periodic address status verification
4253 */
4254
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004255static void addrconf_verify_rtnl(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004257 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 int i;
4260
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004261 ASSERT_RTNL();
4262
stephen hemminger5c578aed2010-03-17 20:31:11 +00004263 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004265 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004267 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004269 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270restart:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004271 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
Yasushi Asanofad8da32013-12-31 12:04:19 +09004274 /* When setting preferred_lft to a value not zero or
4275 * infinity, while valid_lft is infinity
4276 * IFA_F_PERMANENT has a non-infinity life time.
4277 */
4278 if ((ifp->flags & IFA_F_PERMANENT) &&
4279 (ifp->prefered_lft == INFINITY_LIFE_TIME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 continue;
4281
4282 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004283 /* We try to batch several events at once. */
4284 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004286 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4287 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 spin_unlock(&ifp->lock);
4289 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 ipv6_del_addr(ifp);
4291 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004292 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4293 spin_unlock(&ifp->lock);
4294 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00004296 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 int deprecate = 0;
4298
4299 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4300 deprecate = 1;
4301 ifp->flags |= IFA_F_DEPRECATED;
4302 }
4303
Yasushi Asanofad8da32013-12-31 12:04:19 +09004304 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4305 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 next = ifp->tstamp + ifp->valid_lft * HZ;
4307
4308 spin_unlock(&ifp->lock);
4309
4310 if (deprecate) {
4311 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312
4313 ipv6_ifa_notify(0, ifp);
4314 in6_ifa_put(ifp);
4315 goto restart;
4316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4318 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00004319 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4320 ifp->idev->cnf.dad_transmits *
Jiri Pirko1f9248e2013-12-07 19:26:53 +01004321 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
stephen hemminger88949cf2010-03-17 20:31:17 +00004322
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 if (age >= ifp->prefered_lft - regen_advance) {
4324 struct inet6_ifaddr *ifpub = ifp->ifpub;
4325 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4326 next = ifp->tstamp + ifp->prefered_lft * HZ;
4327 if (!ifp->regen_count && ifpub) {
4328 ifp->regen_count++;
4329 in6_ifa_hold(ifp);
4330 in6_ifa_hold(ifpub);
4331 spin_unlock(&ifp->lock);
stephen hemminger5c578aed2010-03-17 20:31:11 +00004332
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08004333 spin_lock(&ifpub->lock);
4334 ifpub->regen_count = 0;
4335 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 ipv6_create_tempaddr(ifpub, ifp);
4337 in6_ifa_put(ifpub);
4338 in6_ifa_put(ifp);
4339 goto restart;
4340 }
4341 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4342 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4343 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 } else {
4345 /* ifp->prefered_lft <= ifp->valid_lft */
4346 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4347 next = ifp->tstamp + ifp->prefered_lft * HZ;
4348 spin_unlock(&ifp->lock);
4349 }
4350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 }
4352
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004353 next_sec = round_jiffies_up(next);
4354 next_sched = next;
4355
4356 /* If rounded timeout is accurate enough, accept it. */
4357 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4358 next_sched = next_sec;
4359
4360 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4361 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4362 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4363
dingtianhongba3542e2013-08-17 10:27:04 +08004364 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4365 now, next, next_sec, next_sched);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004366 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
stephen hemminger5c578aed2010-03-17 20:31:11 +00004367 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368}
4369
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004370static void addrconf_verify_work(struct work_struct *w)
4371{
4372 rtnl_lock();
4373 addrconf_verify_rtnl();
4374 rtnl_unlock();
4375}
4376
4377static void addrconf_verify(void)
4378{
4379 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4380}
4381
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004382static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4383 struct in6_addr **peer_pfx)
Thomas Graf461d8832006-09-18 00:09:49 -07004384{
4385 struct in6_addr *pfx = NULL;
4386
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004387 *peer_pfx = NULL;
4388
Thomas Graf461d8832006-09-18 00:09:49 -07004389 if (addr)
4390 pfx = nla_data(addr);
4391
4392 if (local) {
4393 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004394 *peer_pfx = pfx;
4395 pfx = nla_data(local);
Thomas Graf461d8832006-09-18 00:09:49 -07004396 }
4397
4398 return pfx;
4399}
4400
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004401static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07004402 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4403 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4404 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
Jiri Pirko479840f2013-12-06 09:45:21 +01004405 [IFA_FLAGS] = { .len = sizeof(u32) },
Thomas Graf461d8832006-09-18 00:09:49 -07004406};
4407
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408static int
Thomas Graf661d2962013-03-21 07:45:29 +00004409inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004411 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07004412 struct ifaddrmsg *ifm;
4413 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004414 struct in6_addr *pfx, *peer_pfx;
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004415 u32 ifa_flags;
Thomas Grafb933f712006-09-18 00:10:19 -07004416 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
Thomas Grafb933f712006-09-18 00:10:19 -07004418 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4419 if (err < 0)
4420 return err;
4421
4422 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004423 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004424 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 return -EINVAL;
4426
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004427 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4428
4429 /* We ignore other flags so far. */
4430 ifa_flags &= IFA_F_MANAGETEMPADDR;
4431
4432 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4433 ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434}
4435
Jiri Pirko479840f2013-12-06 09:45:21 +01004436static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004437 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004438{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004439 u32 flags;
4440 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004441 unsigned long timeout;
Jiri Pirko53bd6742013-12-06 09:45:22 +01004442 bool was_managetempaddr;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004443 bool had_prefixroute;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09004444
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004445 ASSERT_RTNL();
4446
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004447 if (!valid_lft || (prefered_lft > valid_lft))
4448 return -EINVAL;
4449
Jiri Pirko53bd6742013-12-06 09:45:22 +01004450 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4451 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4452 return -EINVAL;
4453
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004454 timeout = addrconf_timeout_fixup(valid_lft, HZ);
4455 if (addrconf_finite_timeout(timeout)) {
4456 expires = jiffies_to_clock_t(timeout * HZ);
4457 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004458 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004459 } else {
4460 expires = 0;
4461 flags = 0;
4462 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004463 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004464
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004465 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4466 if (addrconf_finite_timeout(timeout)) {
4467 if (timeout == 0)
4468 ifa_flags |= IFA_F_DEPRECATED;
4469 prefered_lft = timeout;
4470 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004471
4472 spin_lock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004473 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004474 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4475 !(ifp->flags & IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004476 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
Thomas Haller761aac72014-01-15 15:36:58 +01004477 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4478 IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004479 ifp->flags |= ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004480 ifp->tstamp = jiffies;
4481 ifp->valid_lft = valid_lft;
4482 ifp->prefered_lft = prefered_lft;
4483
4484 spin_unlock_bh(&ifp->lock);
4485 if (!(ifp->flags&IFA_F_TENTATIVE))
4486 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004487
Thomas Haller761aac72014-01-15 15:36:58 +01004488 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4489 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4490 expires, flags);
Thomas Haller5b84efe2014-01-15 15:36:59 +01004491 } else if (had_prefixroute) {
4492 enum cleanup_prefix_rt_t action;
4493 unsigned long rt_expires;
4494
4495 write_lock_bh(&ifp->idev->lock);
4496 action = check_cleanup_prefix_route(ifp, &rt_expires);
4497 write_unlock_bh(&ifp->idev->lock);
4498
4499 if (action != CLEANUP_PREFIX_RT_NOP) {
4500 cleanup_prefix_route(ifp, rt_expires,
4501 action == CLEANUP_PREFIX_RT_DEL);
4502 }
Thomas Haller761aac72014-01-15 15:36:58 +01004503 }
Jiri Pirko53bd6742013-12-06 09:45:22 +01004504
4505 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4506 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4507 valid_lft = prefered_lft = 0;
4508 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4509 !was_managetempaddr, jiffies);
4510 }
4511
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004512 addrconf_verify_rtnl();
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004513
4514 return 0;
4515}
4516
4517static int
Thomas Graf661d2962013-03-21 07:45:29 +00004518inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004520 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07004521 struct ifaddrmsg *ifm;
4522 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004523 struct in6_addr *pfx, *peer_pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07004524 struct inet6_ifaddr *ifa;
4525 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004526 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
Jiri Pirko479840f2013-12-06 09:45:21 +01004527 u32 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07004528 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
Thomas Graf461d8832006-09-18 00:09:49 -07004530 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4531 if (err < 0)
4532 return err;
4533
4534 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004535 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004536 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 return -EINVAL;
4538
Thomas Graf461d8832006-09-18 00:09:49 -07004539 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09004540 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07004541
4542 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09004543 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07004544 preferred_lft = ci->ifa_prefered;
4545 } else {
4546 preferred_lft = INFINITY_LIFE_TIME;
4547 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA0778769d2006-07-28 18:12:10 +09004548 }
4549
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004550 dev = __dev_get_by_index(net, ifm->ifa_index);
Ian Morris63159f22015-03-29 14:00:04 +01004551 if (!dev)
Thomas Graf7198f8c2006-09-18 00:13:46 -07004552 return -ENODEV;
4553
Jiri Pirko479840f2013-12-06 09:45:21 +01004554 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4555
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004556 /* We ignore other flags so far. */
Thomas Haller761aac72014-01-15 15:36:58 +01004557 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
Madhu Challa93a714d2015-02-25 09:58:35 -08004558 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004559
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08004560 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Ian Morris63159f22015-03-29 14:00:04 +01004561 if (!ifa) {
Thomas Graf7198f8c2006-09-18 00:13:46 -07004562 /*
4563 * It would be best to check for !NLM_F_CREATE here but
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08004564 * userspace already relies on not having to provide this.
Thomas Graf7198f8c2006-09-18 00:13:46 -07004565 */
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004566 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004567 ifm->ifa_prefixlen, ifa_flags,
4568 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004569 }
4570
Thomas Graf7198f8c2006-09-18 00:13:46 -07004571 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4572 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4573 err = -EEXIST;
4574 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004575 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07004576
4577 in6_ifa_put(ifa);
4578
4579 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580}
4581
Jiri Pirko479840f2013-12-06 09:45:21 +01004582static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
Thomas Graf101bb222006-09-18 00:11:52 -07004583 u8 scope, int ifindex)
4584{
4585 struct ifaddrmsg *ifm;
4586
4587 ifm = nlmsg_data(nlh);
4588 ifm->ifa_family = AF_INET6;
4589 ifm->ifa_prefixlen = prefixlen;
4590 ifm->ifa_flags = flags;
4591 ifm->ifa_scope = scope;
4592 ifm->ifa_index = ifindex;
4593}
4594
Thomas Graf85486af2006-09-18 00:11:24 -07004595static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4596 unsigned long tstamp, u32 preferred, u32 valid)
4597{
4598 struct ifa_cacheinfo ci;
4599
Thomas Graf18a31e12010-11-17 04:12:02 +00004600 ci.cstamp = cstamp_delta(cstamp);
4601 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07004602 ci.ifa_prefered = preferred;
4603 ci.ifa_valid = valid;
4604
4605 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4606}
4607
Thomas Graf101bb222006-09-18 00:11:52 -07004608static inline int rt_scope(int ifa_scope)
4609{
4610 if (ifa_scope & IFA_HOST)
4611 return RT_SCOPE_HOST;
4612 else if (ifa_scope & IFA_LINK)
4613 return RT_SCOPE_LINK;
4614 else if (ifa_scope & IFA_SITE)
4615 return RT_SCOPE_SITE;
4616 else
4617 return RT_SCOPE_UNIVERSE;
4618}
4619
Thomas Graf0ab68032006-09-18 00:12:35 -07004620static inline int inet6_ifaddr_msgsize(void)
4621{
Thomas Graf339bf982006-11-10 14:10:15 -08004622 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004623 + nla_total_size(16) /* IFA_LOCAL */
Thomas Graf339bf982006-11-10 14:10:15 -08004624 + nla_total_size(16) /* IFA_ADDRESS */
Jiri Pirko479840f2013-12-06 09:45:21 +01004625 + nla_total_size(sizeof(struct ifa_cacheinfo))
4626 + nla_total_size(4) /* IFA_FLAGS */;
Thomas Graf0ab68032006-09-18 00:12:35 -07004627}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004628
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004630 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07004633 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634
Eric W. Biederman15e47302012-09-07 20:12:54 +00004635 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004636 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004637 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004638
Thomas Graf101bb222006-09-18 00:11:52 -07004639 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4640 ifa->idev->dev->ifindex);
4641
Yasushi Asanofad8da32013-12-31 12:04:19 +09004642 if (!((ifa->flags&IFA_F_PERMANENT) &&
4643 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
Thomas Graf85486af2006-09-18 00:11:24 -07004644 preferred = ifa->prefered_lft;
4645 valid = ifa->valid_lft;
4646 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00004648 if (preferred > tval)
4649 preferred -= tval;
4650 else
4651 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00004652 if (valid != INFINITY_LIFE_TIME) {
4653 if (valid > tval)
4654 valid -= tval;
4655 else
4656 valid = 0;
4657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 }
4659 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07004660 preferred = INFINITY_LIFE_TIME;
4661 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 }
Thomas Graf85486af2006-09-18 00:11:24 -07004663
Cong Wang7996c792013-05-22 05:41:06 +00004664 if (!ipv6_addr_any(&ifa->peer_addr)) {
Jiri Benc930345e2015-03-29 16:59:25 +02004665 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4666 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004667 goto error;
4668 } else
Jiri Benc930345e2015-03-29 16:59:25 +02004669 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004670 goto error;
4671
4672 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4673 goto error;
Thomas Graf85486af2006-09-18 00:11:24 -07004674
Jiri Pirko479840f2013-12-06 09:45:21 +01004675 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4676 goto error;
4677
Johannes Berg053c0952015-01-16 22:09:00 +01004678 nlmsg_end(skb, nlh);
4679 return 0;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004680
4681error:
4682 nlmsg_cancel(skb, nlh);
4683 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684}
4685
4686static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004687 u32 portid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004690 u8 scope = RT_SCOPE_UNIVERSE;
4691 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692
Thomas Graf101bb222006-09-18 00:11:52 -07004693 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4694 scope = RT_SCOPE_SITE;
4695
Eric W. Biederman15e47302012-09-07 20:12:54 +00004696 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004697 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004698 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004699
Thomas Graf101bb222006-09-18 00:11:52 -07004700 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004701 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004702 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004703 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4704 nlmsg_cancel(skb, nlh);
4705 return -EMSGSIZE;
4706 }
Thomas Graf85486af2006-09-18 00:11:24 -07004707
Johannes Berg053c0952015-01-16 22:09:00 +01004708 nlmsg_end(skb, nlh);
4709 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710}
4711
4712static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004713 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004716 u8 scope = RT_SCOPE_UNIVERSE;
4717 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718
Thomas Graf101bb222006-09-18 00:11:52 -07004719 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4720 scope = RT_SCOPE_SITE;
4721
Eric W. Biederman15e47302012-09-07 20:12:54 +00004722 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004723 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004724 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004725
Thomas Graf101bb222006-09-18 00:11:52 -07004726 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004727 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004728 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004729 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4730 nlmsg_cancel(skb, nlh);
4731 return -EMSGSIZE;
4732 }
Thomas Graf85486af2006-09-18 00:11:24 -07004733
Johannes Berg053c0952015-01-16 22:09:00 +01004734 nlmsg_end(skb, nlh);
4735 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736}
4737
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004738enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 UNICAST_ADDR,
4740 MULTICAST_ADDR,
4741 ANYCAST_ADDR,
4742};
4743
Eric Dumazet234b27c2009-11-12 04:11:50 +00004744/* called with rcu_read_lock() */
4745static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4746 struct netlink_callback *cb, enum addr_type_t type,
4747 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 struct ifmcaddr6 *ifmca;
4750 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004751 int err = 1;
4752 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753
Eric Dumazet234b27c2009-11-12 04:11:50 +00004754 read_lock_bh(&idev->lock);
4755 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00004756 case UNICAST_ADDR: {
4757 struct inet6_ifaddr *ifa;
4758
Eric Dumazet234b27c2009-11-12 04:11:50 +00004759 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00004760 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4761 if (++ip_idx < s_ip_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004762 continue;
4763 err = inet6_fill_ifaddr(skb, ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004764 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004765 cb->nlh->nlmsg_seq,
4766 RTM_NEWADDR,
4767 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004768 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004769 break;
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004770 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004772 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00004773 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004774 case MULTICAST_ADDR:
4775 /* multicast address */
4776 for (ifmca = idev->mc_list; ifmca;
4777 ifmca = ifmca->next, ip_idx++) {
4778 if (ip_idx < s_ip_idx)
4779 continue;
4780 err = inet6_fill_ifmcaddr(skb, ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004781 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004782 cb->nlh->nlmsg_seq,
4783 RTM_GETMULTICAST,
4784 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004785 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004786 break;
4787 }
4788 break;
4789 case ANYCAST_ADDR:
4790 /* anycast address */
4791 for (ifaca = idev->ac_list; ifaca;
4792 ifaca = ifaca->aca_next, ip_idx++) {
4793 if (ip_idx < s_ip_idx)
4794 continue;
4795 err = inet6_fill_ifacaddr(skb, ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004796 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004797 cb->nlh->nlmsg_seq,
4798 RTM_GETANYCAST,
4799 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004800 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004801 break;
4802 }
4803 break;
4804 default:
4805 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004807 read_unlock_bh(&idev->lock);
4808 *p_ip_idx = ip_idx;
4809 return err;
4810}
4811
4812static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4813 enum addr_type_t type)
4814{
4815 struct net *net = sock_net(skb->sk);
4816 int h, s_h;
4817 int idx, ip_idx;
4818 int s_idx, s_ip_idx;
4819 struct net_device *dev;
4820 struct inet6_dev *idev;
4821 struct hlist_head *head;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004822
4823 s_h = cb->args[0];
4824 s_idx = idx = cb->args[1];
4825 s_ip_idx = ip_idx = cb->args[2];
4826
4827 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004828 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004829 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4830 idx = 0;
4831 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08004832 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet234b27c2009-11-12 04:11:50 +00004833 if (idx < s_idx)
4834 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07004835 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004836 s_ip_idx = 0;
4837 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004838 idev = __in6_dev_get(dev);
4839 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004840 goto cont;
4841
4842 if (in6_dump_addrs(idev, skb, cb, type,
David S. Miller7b46a642015-01-18 23:36:08 -05004843 s_ip_idx, &ip_idx) < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004844 goto done;
4845cont:
4846 idx++;
4847 }
4848 }
4849done:
4850 rcu_read_unlock();
4851 cb->args[0] = h;
4852 cb->args[1] = idx;
4853 cb->args[2] = ip_idx;
4854
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 return skb->len;
4856}
4857
4858static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4859{
4860 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004861
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 return inet6_dump_addr(skb, cb, type);
4863}
4864
4865static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4866{
4867 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004868
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 return inet6_dump_addr(skb, cb, type);
4870}
4871
4872
4873static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4874{
4875 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004876
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 return inet6_dump_addr(skb, cb, type);
4878}
4879
Thomas Graf661d2962013-03-21 07:45:29 +00004880static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004881{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004882 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004883 struct ifaddrmsg *ifm;
4884 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004885 struct in6_addr *addr = NULL, *peer;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004886 struct net_device *dev = NULL;
4887 struct inet6_ifaddr *ifa;
4888 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004889 int err;
4890
Thomas Graf1b29fc22006-09-18 00:10:50 -07004891 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4892 if (err < 0)
4893 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004894
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004895 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
Ian Morris63159f22015-03-29 14:00:04 +01004896 if (!addr) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004897 err = -EINVAL;
4898 goto errout;
4899 }
4900
4901 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004902 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004903 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004904
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004905 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4906 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004907 err = -EADDRNOTAVAIL;
4908 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004909 }
4910
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004911 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4912 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004913 err = -ENOBUFS;
4914 goto errout_ifa;
4915 }
4916
Eric W. Biederman15e47302012-09-07 20:12:54 +00004917 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004918 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004919 if (err < 0) {
4920 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4921 WARN_ON(err == -EMSGSIZE);
4922 kfree_skb(skb);
4923 goto errout_ifa;
4924 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00004925 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004926errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004927 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004928errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004929 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004930}
4931
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4933{
4934 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004935 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07004936 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
Mahesh Bandewarf784ad32017-01-04 15:01:01 -08004938 /* Don't send DELADDR notification for TENTATIVE address,
4939 * since NEWADDR notification is sent only after removing
4940 * TENTATIVE flag.
4941 */
4942 if (ifa->flags & IFA_F_TENTATIVE && event == RTM_DELADDR)
4943 return;
4944
Thomas Graf0ab68032006-09-18 00:12:35 -07004945 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01004946 if (!skb)
Thomas Graf5d620262006-08-15 00:35:02 -07004947 goto errout;
4948
4949 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004950 if (err < 0) {
4951 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4952 WARN_ON(err == -EMSGSIZE);
4953 kfree_skb(skb);
4954 goto errout;
4955 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004956 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4957 return;
Thomas Graf5d620262006-08-15 00:35:02 -07004958errout:
4959 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004960 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961}
4962
Dave Jonesb6f99a22007-03-22 12:27:49 -07004963static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 __s32 *array, int bytes)
4965{
Thomas Graf04561c12006-11-14 19:53:58 -08004966 BUG_ON(bytes < (DEVCONF_MAX * 4));
4967
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 memset(array, 0, bytes);
4969 array[DEVCONF_FORWARDING] = cnf->forwarding;
4970 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4971 array[DEVCONF_MTU6] = cnf->mtu6;
4972 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4973 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4974 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4975 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4976 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00004977 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4978 jiffies_to_msecs(cnf->rtr_solicit_interval);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004979 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
4980 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
Thomas Graf93908d12010-11-17 01:44:24 +00004981 array[DEVCONF_RTR_SOLICIT_DELAY] =
4982 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02004984 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4985 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4986 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4987 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4989 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4990 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4991 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4992 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004994 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
Hangbin Liu8013d1d72015-07-30 14:28:42 +08004995 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004996 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004997#ifdef CONFIG_IPV6_ROUTER_PREF
4998 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00004999 array[DEVCONF_RTR_PROBE_INTERVAL] =
5000 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08005001#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzbbea1242017-03-22 18:19:04 +09005002 array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08005003 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
5004#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005005#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07005006 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07005007 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07005008#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5009 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
Erik Kline7fd25612014-10-28 18:11:14 +09005010 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
Neil Horman95c385b2007-04-25 17:08:10 -07005011#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005012#ifdef CONFIG_IPV6_MROUTE
5013 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
5014#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09005015 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09005016 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00005017 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00005018 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02005019 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
Ben Greeard9333192014-06-25 14:44:53 -07005020 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
Harout Hedeshianc2943f12015-01-20 10:06:05 -07005021 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
Andy Gospodarek35103d12015-08-13 10:39:01 -04005022 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005023 /* we omit DEVCONF_STABLE_SECRET for now */
Erik Kline3985e8a2015-07-22 16:38:25 +09005024 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
Johannes Bergabbc3042016-02-04 13:31:19 +01005025 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
Johannes Berg7a02bf82016-02-04 13:31:20 +01005026 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
David Ahernf1705ec2016-02-24 09:25:37 -08005027 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
David Lebrun1ababeb2016-11-08 14:57:39 +01005028 array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
David Lebrunbf355b82016-11-08 14:57:42 +01005029#ifdef CONFIG_IPV6_SEG6_HMAC
5030 array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5031#endif
Erik Nordmarkadc176c2016-12-02 14:00:08 -08005032 array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
Felix Jiad35a00b2017-01-26 16:59:17 +13005033 array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
David Forsterdf789fe2017-02-23 16:27:18 +00005034 array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035}
5036
Thomas Grafb382b192010-11-16 04:33:57 +00005037static inline size_t inet6_ifla6_size(void)
5038{
5039 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5040 + nla_total_size(sizeof(struct ifla_cacheinfo))
5041 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5042 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005043 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5044 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
Thomas Grafb382b192010-11-16 04:33:57 +00005045}
5046
Thomas Graf339bf982006-11-10 14:10:15 -08005047static inline size_t inet6_if_nlmsg_size(void)
5048{
5049 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5050 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5051 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5052 + nla_total_size(4) /* IFLA_MTU */
5053 + nla_total_size(4) /* IFLA_LINK */
Andy Gospodarek03443382015-08-13 15:26:35 -04005054 + nla_total_size(1) /* IFLA_OPERSTATE */
Thomas Grafb382b192010-11-16 04:33:57 +00005055 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08005056}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005057
Eric Dumazetbe281e52011-05-19 01:14:23 +00005058static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Jia Heaca05672016-09-30 11:29:03 +08005059 int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005060{
5061 int i;
Jia Heaca05672016-09-30 11:29:03 +08005062 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005063 BUG_ON(pad < 0);
5064
5065 /* Use put_unaligned() because stats may not be aligned for u64. */
Jia Heaca05672016-09-30 11:29:03 +08005066 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5067 for (i = 1; i < ICMP6_MIB_MAX; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00005068 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005069
Jia Heaca05672016-09-30 11:29:03 +08005070 memset(&stats[ICMP6_MIB_MAX], 0, pad);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005071}
5072
WANG Cong698365f2014-05-05 15:55:55 -07005073static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305074 int bytes, size_t syncpoff)
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005075{
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305076 int i, c;
5077 u64 buff[IPSTATS_MIB_MAX];
5078 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5079
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005080 BUG_ON(pad < 0);
5081
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305082 memset(buff, 0, sizeof(buff));
5083 buff[0] = IPSTATS_MIB_MAX;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005084
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305085 for_each_possible_cpu(c) {
5086 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5087 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5088 }
5089
5090 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5091 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005092}
5093
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005094static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5095 int bytes)
5096{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005097 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005098 case IFLA_INET6_STATS:
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305099 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5100 offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005101 break;
5102 case IFLA_INET6_ICMP6STATS:
Jia Heaca05672016-09-30 11:29:03 +08005103 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005104 break;
5105 }
5106}
5107
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005108static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5109 u32 ext_filter_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005111 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08005112 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113
David S. Millerc78679e2012-04-01 20:27:33 -04005114 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5115 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08005117 ci.tstamp = cstamp_delta(idev->tstamp);
5118 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
Jiri Pirko1f9248e2013-12-07 19:26:53 +01005119 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
David S. Millerc78679e2012-04-01 20:27:33 -04005120 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5121 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005122 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
Ian Morris63159f22015-03-29 14:00:04 +01005123 if (!nla)
Thomas Graf04561c12006-11-14 19:53:58 -08005124 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005125 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005127 /* XXX - MC not implemented */
5128
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005129 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5130 return 0;
5131
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005132 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005133 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005134 goto nla_put_failure;
5135 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5136
5137 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005138 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005139 goto nla_put_failure;
5140 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005142 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
Ian Morris63159f22015-03-29 14:00:04 +01005143 if (!nla)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005144 goto nla_put_failure;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005145
Felix Jiad35a00b2017-01-26 16:59:17 +13005146 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005147 goto nla_put_failure;
5148
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005149 read_lock_bh(&idev->lock);
5150 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5151 read_unlock_bh(&idev->lock);
5152
Thomas Grafb382b192010-11-16 04:33:57 +00005153 return 0;
5154
5155nla_put_failure:
5156 return -EMSGSIZE;
5157}
5158
Arad, Ronenb1974ed2015-10-19 09:23:28 -07005159static size_t inet6_get_link_af_size(const struct net_device *dev,
5160 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005161{
5162 if (!__in6_dev_get(dev))
5163 return 0;
5164
5165 return inet6_ifla6_size();
5166}
5167
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005168static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5169 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005170{
5171 struct inet6_dev *idev = __in6_dev_get(dev);
5172
5173 if (!idev)
5174 return -ENODATA;
5175
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005176 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005177 return -EMSGSIZE;
5178
5179 return 0;
5180}
5181
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005182static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5183{
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005184 struct inet6_ifaddr *ifp;
5185 struct net_device *dev = idev->dev;
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005186 bool clear_token, update_rs = false;
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005187 struct in6_addr ll_addr;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005188
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005189 ASSERT_RTNL();
5190
Ian Morris63159f22015-03-29 14:00:04 +01005191 if (!token)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005192 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005193 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5194 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005195 if (!ipv6_accept_ra(idev))
5196 return -EINVAL;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005197 if (idev->cnf.rtr_solicits == 0)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005198 return -EINVAL;
5199
5200 write_lock_bh(&idev->lock);
5201
5202 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5203 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5204
5205 write_unlock_bh(&idev->lock);
5206
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005207 clear_token = ipv6_addr_any(token);
5208 if (clear_token)
5209 goto update_lft;
5210
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005211 if (!idev->dead && (idev->if_flags & IF_READY) &&
5212 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5213 IFA_F_OPTIMISTIC)) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005214 /* If we're not ready, then normal ifup will take care
5215 * of this. Otherwise, we need to request our rs here.
5216 */
5217 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5218 update_rs = true;
5219 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005220
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005221update_lft:
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005222 write_lock_bh(&idev->lock);
Daniel Borkmannfc403832013-04-09 03:47:15 +00005223
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005224 if (update_rs) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005225 idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005226 idev->rs_interval = rfc3315_s14_backoff_init(
5227 idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005228 idev->rs_probes = 1;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005229 addrconf_mod_rs_timer(idev, idev->rs_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005230 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005231
5232 /* Well, that's kinda nasty ... */
5233 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5234 spin_lock(&ifp->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00005235 if (ifp->tokenized) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005236 ifp->valid_lft = 0;
5237 ifp->prefered_lft = 0;
5238 }
5239 spin_unlock(&ifp->lock);
5240 }
5241
5242 write_unlock_bh(&idev->lock);
Lubomir Rintelb2ed64a2014-10-27 17:39:16 +01005243 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005244 addrconf_verify_rtnl();
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005245 return 0;
5246}
5247
Daniel Borkmann11b1f822015-02-05 14:39:11 +01005248static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5249 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5250 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5251};
5252
5253static int inet6_validate_link_af(const struct net_device *dev,
5254 const struct nlattr *nla)
5255{
5256 struct nlattr *tb[IFLA_INET6_MAX + 1];
5257
5258 if (dev && !__in6_dev_get(dev))
5259 return -EAFNOSUPPORT;
5260
5261 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
5262}
5263
Felix Jiad35a00b2017-01-26 16:59:17 +13005264static int check_addr_gen_mode(int mode)
5265{
5266 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5267 mode != IN6_ADDR_GEN_MODE_NONE &&
5268 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5269 mode != IN6_ADDR_GEN_MODE_RANDOM)
5270 return -EINVAL;
5271 return 1;
5272}
5273
5274static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5275 int mode)
5276{
5277 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5278 !idev->cnf.stable_secret.initialized &&
5279 !net->ipv6.devconf_dflt->stable_secret.initialized)
5280 return -EINVAL;
5281 return 1;
5282}
5283
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005284static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5285{
5286 int err = -EINVAL;
5287 struct inet6_dev *idev = __in6_dev_get(dev);
5288 struct nlattr *tb[IFLA_INET6_MAX + 1];
5289
5290 if (!idev)
5291 return -EAFNOSUPPORT;
5292
5293 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
5294 BUG();
5295
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005296 if (tb[IFLA_INET6_TOKEN]) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005297 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005298 if (err)
5299 return err;
5300 }
5301
5302 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5303 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5304
Felix Jiad35a00b2017-01-26 16:59:17 +13005305 if (check_addr_gen_mode(mode) < 0 ||
5306 check_stable_privacy(idev, dev_net(dev), mode) < 0)
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005307 return -EINVAL;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005308
Felix Jiad35a00b2017-01-26 16:59:17 +13005309 idev->cnf.addr_gen_mode = mode;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005310 err = 0;
5311 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005312
5313 return err;
5314}
5315
Thomas Grafb382b192010-11-16 04:33:57 +00005316static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005317 u32 portid, u32 seq, int event, unsigned int flags)
Thomas Grafb382b192010-11-16 04:33:57 +00005318{
5319 struct net_device *dev = idev->dev;
5320 struct ifinfomsg *hdr;
5321 struct nlmsghdr *nlh;
5322 void *protoinfo;
5323
Eric W. Biederman15e47302012-09-07 20:12:54 +00005324 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005325 if (!nlh)
Thomas Grafb382b192010-11-16 04:33:57 +00005326 return -EMSGSIZE;
5327
5328 hdr = nlmsg_data(nlh);
5329 hdr->ifi_family = AF_INET6;
5330 hdr->__ifi_pad = 0;
5331 hdr->ifi_type = dev->type;
5332 hdr->ifi_index = dev->ifindex;
5333 hdr->ifi_flags = dev_get_flags(dev);
5334 hdr->ifi_change = 0;
5335
David S. Millerc78679e2012-04-01 20:27:33 -04005336 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5337 (dev->addr_len &&
5338 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5339 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02005340 (dev->ifindex != dev_get_iflink(dev) &&
Andy Gospodarek03443382015-08-13 15:26:35 -04005341 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5342 nla_put_u8(skb, IFLA_OPERSTATE,
5343 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
David S. Millerc78679e2012-04-01 20:27:33 -04005344 goto nla_put_failure;
Thomas Grafb382b192010-11-16 04:33:57 +00005345 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
Ian Morris63159f22015-03-29 14:00:04 +01005346 if (!protoinfo)
Thomas Grafb382b192010-11-16 04:33:57 +00005347 goto nla_put_failure;
5348
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005349 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005350 goto nla_put_failure;
5351
Thomas Graf04561c12006-11-14 19:53:58 -08005352 nla_nest_end(skb, protoinfo);
Johannes Berg053c0952015-01-16 22:09:00 +01005353 nlmsg_end(skb, nlh);
5354 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355
Thomas Graf04561c12006-11-14 19:53:58 -08005356nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005357 nlmsg_cancel(skb, nlh);
5358 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359}
5360
5361static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5362{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005363 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00005364 int h, s_h;
David S. Miller434a8a52009-11-11 18:53:00 -08005365 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 struct net_device *dev;
5367 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00005368 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
Eric Dumazet84d26972009-11-09 12:11:28 +00005370 s_h = cb->args[0];
5371 s_idx = cb->args[1];
5372
5373 rcu_read_lock();
5374 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5375 idx = 0;
5376 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08005377 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet84d26972009-11-09 12:11:28 +00005378 if (idx < s_idx)
5379 goto cont;
5380 idev = __in6_dev_get(dev);
5381 if (!idev)
5382 goto cont;
5383 if (inet6_fill_ifinfo(skb, idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005384 NETLINK_CB(cb->skb).portid,
Eric Dumazet84d26972009-11-09 12:11:28 +00005385 cb->nlh->nlmsg_seq,
Johannes Berg053c0952015-01-16 22:09:00 +01005386 RTM_NEWLINK, NLM_F_MULTI) < 0)
Eric Dumazet84d26972009-11-09 12:11:28 +00005387 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07005388cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00005389 idx++;
5390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 }
Eric Dumazet84d26972009-11-09 12:11:28 +00005392out:
5393 rcu_read_unlock();
5394 cb->args[1] = idx;
5395 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396
5397 return skb->len;
5398}
5399
5400void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5401{
5402 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005403 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005404 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005405
Thomas Graf339bf982006-11-10 14:10:15 -08005406 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005407 if (!skb)
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005408 goto errout;
5409
5410 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005411 if (err < 0) {
5412 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5413 WARN_ON(err == -EMSGSIZE);
5414 kfree_skb(skb);
5415 goto errout;
5416 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005417 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005418 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005419errout:
5420 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005421 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422}
5423
Thomas Graf339bf982006-11-10 14:10:15 -08005424static inline size_t inet6_prefix_nlmsg_size(void)
5425{
5426 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5427 + nla_total_size(sizeof(struct in6_addr))
5428 + nla_total_size(sizeof(struct prefix_cacheinfo));
5429}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005430
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005432 struct prefix_info *pinfo, u32 portid, u32 seq,
Thomas Graf6051e2f2006-11-14 19:54:19 -08005433 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434{
Thomas Graf6051e2f2006-11-14 19:54:19 -08005435 struct prefixmsg *pmsg;
5436 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 struct prefix_cacheinfo ci;
5438
Eric W. Biederman15e47302012-09-07 20:12:54 +00005439 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005440 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005441 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08005442
5443 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07005445 pmsg->prefix_pad1 = 0;
5446 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 pmsg->prefix_ifindex = idev->dev->ifindex;
5448 pmsg->prefix_len = pinfo->prefix_len;
5449 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07005450 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 pmsg->prefix_flags = 0;
5452 if (pinfo->onlink)
5453 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5454 if (pinfo->autoconf)
5455 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5456
David S. Millerc78679e2012-04-01 20:27:33 -04005457 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5458 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 ci.preferred_time = ntohl(pinfo->prefered);
5460 ci.valid_time = ntohl(pinfo->valid);
David S. Millerc78679e2012-04-01 20:27:33 -04005461 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5462 goto nla_put_failure;
Johannes Berg053c0952015-01-16 22:09:00 +01005463 nlmsg_end(skb, nlh);
5464 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465
Thomas Graf6051e2f2006-11-14 19:54:19 -08005466nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005467 nlmsg_cancel(skb, nlh);
5468 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469}
5470
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005471static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 struct prefix_info *pinfo)
5473{
5474 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005475 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005476 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477
Thomas Graf339bf982006-11-10 14:10:15 -08005478 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005479 if (!skb)
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005480 goto errout;
5481
5482 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005483 if (err < 0) {
5484 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5485 WARN_ON(err == -EMSGSIZE);
5486 kfree_skb(skb);
5487 goto errout;
5488 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005489 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5490 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005491errout:
5492 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08005493 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494}
5495
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5497{
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005498 struct net *net = dev_net(ifp->idev->dev);
5499
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005500 if (event)
5501 ASSERT_RTNL();
5502
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5504
5505 switch (event) {
5506 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07005507 /*
5508 * If the address was optimistic
5509 * we inserted the route at the start of
5510 * our DAD process, so we don't need
5511 * to do it again
5512 */
5513 if (!(ifp->rt->rt6i_node))
5514 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 if (ifp->idev->cnf.forwarding)
5516 addrconf_join_anycast(ifp);
Cong Wang7996c792013-05-22 05:41:06 +00005517 if (!ipv6_addr_any(&ifp->peer_addr))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005518 addrconf_prefix_route(&ifp->peer_addr, 128,
5519 ifp->idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 break;
5521 case RTM_DELADDR:
5522 if (ifp->idev->cnf.forwarding)
5523 addrconf_leave_anycast(ifp);
5524 addrconf_leave_solict(ifp->idev, &ifp->addr);
Cong Wang7996c792013-05-22 05:41:06 +00005525 if (!ipv6_addr_any(&ifp->peer_addr)) {
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005526 struct rt6_info *rt;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005527
Nicolas Dichtele7478df2014-09-03 23:59:22 +02005528 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5529 ifp->idev->dev, 0, 0);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07005530 if (rt)
5531 ip6_del_rt(rt);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005532 }
David Ahern38bd10c2016-04-21 20:56:12 -07005533 if (ifp->rt) {
5534 dst_hold(&ifp->rt->dst);
5535 ip6_del_rt(ifp->rt);
5536 }
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02005537 rt_genid_bump_ipv6(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 break;
5539 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005540 atomic_inc(&net->ipv6.dev_addr_genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541}
5542
5543static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5544{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005545 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 if (likely(ifp->idev->dead == 0))
5547 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005548 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549}
5550
5551#ifdef CONFIG_SYSCTL
5552
5553static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005554int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 void __user *buffer, size_t *lenp, loff_t *ppos)
5556{
5557 int *valp = ctl->data;
5558 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005559 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005560 struct ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 int ret;
5562
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005563 /*
5564 * ctl->data points to idev->cnf.forwarding, we should
5565 * not modify it until we get the rtnl lock.
5566 */
5567 lctl = *ctl;
5568 lctl.data = &val;
5569
5570 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08005572 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00005573 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005574 if (ret)
5575 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005576 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577}
5578
Marcelo Leitner77751422015-02-23 11:17:13 -03005579static
5580int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5581 void __user *buffer, size_t *lenp, loff_t *ppos)
5582{
5583 struct inet6_dev *idev = ctl->extra1;
5584 int min_mtu = IPV6_MIN_MTU;
5585 struct ctl_table lctl;
5586
5587 lctl = *ctl;
5588 lctl.extra1 = &min_mtu;
5589 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5590
5591 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5592}
5593
Brian Haley56d417b2009-06-01 03:07:33 -07005594static void dev_disable_change(struct inet6_dev *idev)
5595{
Cong Wang75538c22013-05-29 11:30:50 +08005596 struct netdev_notifier_info info;
5597
Brian Haley56d417b2009-06-01 03:07:33 -07005598 if (!idev || !idev->dev)
5599 return;
5600
Cong Wang75538c22013-05-29 11:30:50 +08005601 netdev_notifier_info_init(&info, idev->dev);
Brian Haley56d417b2009-06-01 03:07:33 -07005602 if (idev->cnf.disable_ipv6)
Cong Wang75538c22013-05-29 11:30:50 +08005603 addrconf_notify(NULL, NETDEV_DOWN, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005604 else
Cong Wang75538c22013-05-29 11:30:50 +08005605 addrconf_notify(NULL, NETDEV_UP, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005606}
5607
5608static void addrconf_disable_change(struct net *net, __s32 newf)
5609{
5610 struct net_device *dev;
5611 struct inet6_dev *idev;
5612
Kefeng Wang03e4def2017-01-19 16:26:21 +08005613 for_each_netdev(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07005614 idev = __in6_dev_get(dev);
5615 if (idev) {
5616 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5617 idev->cnf.disable_ipv6 = newf;
5618 if (changed)
5619 dev_disable_change(idev);
5620 }
Brian Haley56d417b2009-06-01 03:07:33 -07005621 }
Brian Haley56d417b2009-06-01 03:07:33 -07005622}
5623
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005624static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07005625{
5626 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005627 int old;
5628
5629 if (!rtnl_trylock())
5630 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07005631
5632 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005633 old = *p;
5634 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07005635
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005636 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5637 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07005638 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005639 }
Brian Haley56d417b2009-06-01 03:07:33 -07005640
5641 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07005642 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5643 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005644 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07005645 dev_disable_change((struct inet6_dev *)table->extra1);
5646
5647 rtnl_unlock();
5648 return 0;
5649}
5650
5651static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005652int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07005653 void __user *buffer, size_t *lenp, loff_t *ppos)
5654{
5655 int *valp = ctl->data;
5656 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005657 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005658 struct ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07005659 int ret;
5660
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005661 /*
5662 * ctl->data points to idev->cnf.disable_ipv6, we should
5663 * not modify it until we get the rtnl lock.
5664 */
5665 lctl = *ctl;
5666 lctl.data = &val;
5667
5668 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07005669
5670 if (write)
5671 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005672 if (ret)
5673 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07005674 return ret;
5675}
5676
stephen hemmingerc92d5492013-12-17 22:37:14 -08005677static
5678int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5679 void __user *buffer, size_t *lenp, loff_t *ppos)
5680{
5681 int *valp = ctl->data;
5682 int ret;
5683 int old, new;
5684
5685 old = *valp;
5686 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5687 new = *valp;
5688
5689 if (write && old != new) {
5690 struct net *net = ctl->extra2;
5691
5692 if (!rtnl_trylock())
5693 return restart_syscall();
5694
5695 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
David Ahern85b3daa2017-03-28 14:28:04 -07005696 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
5697 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08005698 NETCONFA_IFINDEX_DEFAULT,
5699 net->ipv6.devconf_dflt);
5700 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
David Ahern85b3daa2017-03-28 14:28:04 -07005701 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
5702 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08005703 NETCONFA_IFINDEX_ALL,
5704 net->ipv6.devconf_all);
5705 else {
5706 struct inet6_dev *idev = ctl->extra1;
5707
David Ahern85b3daa2017-03-28 14:28:04 -07005708 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
5709 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08005710 idev->dev->ifindex,
5711 &idev->cnf);
5712 }
5713 rtnl_unlock();
5714 }
5715
5716 return ret;
5717}
5718
Felix Jiad35a00b2017-01-26 16:59:17 +13005719static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
5720 void __user *buffer, size_t *lenp,
5721 loff_t *ppos)
5722{
5723 int ret = 0;
5724 int new_val;
5725 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
5726 struct net *net = (struct net *)ctl->extra2;
5727
Felix Jia8c171d62017-02-27 12:41:23 +13005728 if (!rtnl_trylock())
5729 return restart_syscall();
5730
Felix Jiad35a00b2017-01-26 16:59:17 +13005731 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5732
5733 if (write) {
5734 new_val = *((int *)ctl->data);
5735
Felix Jia8c171d62017-02-27 12:41:23 +13005736 if (check_addr_gen_mode(new_val) < 0) {
5737 ret = -EINVAL;
5738 goto out;
5739 }
Felix Jiad35a00b2017-01-26 16:59:17 +13005740
5741 /* request for default */
5742 if (&net->ipv6.devconf_dflt->addr_gen_mode == ctl->data) {
5743 ipv6_devconf_dflt.addr_gen_mode = new_val;
5744
5745 /* request for individual net device */
5746 } else {
5747 if (!idev)
Felix Jia8c171d62017-02-27 12:41:23 +13005748 goto out;
Felix Jiad35a00b2017-01-26 16:59:17 +13005749
Felix Jia8c171d62017-02-27 12:41:23 +13005750 if (check_stable_privacy(idev, net, new_val) < 0) {
5751 ret = -EINVAL;
5752 goto out;
5753 }
Felix Jiad35a00b2017-01-26 16:59:17 +13005754
5755 if (idev->cnf.addr_gen_mode != new_val) {
5756 idev->cnf.addr_gen_mode = new_val;
Felix Jiad35a00b2017-01-26 16:59:17 +13005757 addrconf_dev_config(idev->dev);
Felix Jiad35a00b2017-01-26 16:59:17 +13005758 }
5759 }
5760 }
5761
Felix Jia8c171d62017-02-27 12:41:23 +13005762out:
5763 rtnl_unlock();
5764
Felix Jiad35a00b2017-01-26 16:59:17 +13005765 return ret;
5766}
5767
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005768static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5769 void __user *buffer, size_t *lenp,
5770 loff_t *ppos)
5771{
5772 int err;
5773 struct in6_addr addr;
5774 char str[IPV6_MAX_STRLEN];
5775 struct ctl_table lctl = *ctl;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005776 struct net *net = ctl->extra2;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005777 struct ipv6_stable_secret *secret = ctl->data;
5778
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005779 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5780 return -EIO;
5781
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005782 lctl.maxlen = IPV6_MAX_STRLEN;
5783 lctl.data = str;
5784
5785 if (!rtnl_trylock())
5786 return restart_syscall();
5787
5788 if (!write && !secret->initialized) {
5789 err = -EIO;
5790 goto out;
5791 }
5792
WANG Cong5449a5c2015-12-21 10:55:45 -08005793 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
5794 if (err >= sizeof(str)) {
5795 err = -EIO;
5796 goto out;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005797 }
5798
5799 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5800 if (err || !write)
5801 goto out;
5802
5803 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5804 err = -EIO;
5805 goto out;
5806 }
5807
5808 secret->initialized = true;
5809 secret->secret = addr;
5810
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005811 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5812 struct net_device *dev;
5813
5814 for_each_netdev(net, dev) {
5815 struct inet6_dev *idev = __in6_dev_get(dev);
5816
5817 if (idev) {
Felix Jiad35a00b2017-01-26 16:59:17 +13005818 idev->cnf.addr_gen_mode =
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005819 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5820 }
5821 }
5822 } else {
5823 struct inet6_dev *idev = ctl->extra1;
5824
Felix Jiad35a00b2017-01-26 16:59:17 +13005825 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005826 }
5827
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005828out:
5829 rtnl_unlock();
5830
5831 return err;
5832}
stephen hemmingerc92d5492013-12-17 22:37:14 -08005833
Andy Gospodarek35103d12015-08-13 10:39:01 -04005834static
5835int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
5836 int write,
5837 void __user *buffer,
5838 size_t *lenp,
5839 loff_t *ppos)
5840{
5841 int *valp = ctl->data;
5842 int val = *valp;
5843 loff_t pos = *ppos;
5844 struct ctl_table lctl;
5845 int ret;
5846
5847 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
5848 * we should not modify it until we get the rtnl lock.
5849 */
5850 lctl = *ctl;
5851 lctl.data = &val;
5852
5853 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5854
5855 if (write)
5856 ret = addrconf_fixup_linkdown(ctl, valp, val);
5857 if (ret)
5858 *ppos = pos;
5859 return ret;
5860}
5861
David Forsterdf789fe2017-02-23 16:27:18 +00005862static
5863void addrconf_set_nopolicy(struct rt6_info *rt, int action)
5864{
5865 if (rt) {
5866 if (action)
5867 rt->dst.flags |= DST_NOPOLICY;
5868 else
5869 rt->dst.flags &= ~DST_NOPOLICY;
5870 }
5871}
5872
5873static
5874void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
5875{
5876 struct inet6_ifaddr *ifa;
5877
5878 read_lock_bh(&idev->lock);
5879 list_for_each_entry(ifa, &idev->addr_list, if_list) {
5880 spin_lock(&ifa->lock);
5881 if (ifa->rt) {
5882 struct rt6_info *rt = ifa->rt;
5883 struct fib6_table *table = rt->rt6i_table;
5884 int cpu;
5885
5886 read_lock(&table->tb6_lock);
5887 addrconf_set_nopolicy(ifa->rt, val);
5888 if (rt->rt6i_pcpu) {
5889 for_each_possible_cpu(cpu) {
5890 struct rt6_info **rtp;
5891
5892 rtp = per_cpu_ptr(rt->rt6i_pcpu, cpu);
5893 addrconf_set_nopolicy(*rtp, val);
5894 }
5895 }
5896 read_unlock(&table->tb6_lock);
5897 }
5898 spin_unlock(&ifa->lock);
5899 }
5900 read_unlock_bh(&idev->lock);
5901}
5902
5903static
5904int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
5905{
5906 struct inet6_dev *idev;
5907 struct net *net;
5908
5909 if (!rtnl_trylock())
5910 return restart_syscall();
5911
5912 *valp = val;
5913
5914 net = (struct net *)ctl->extra2;
5915 if (valp == &net->ipv6.devconf_dflt->disable_policy) {
5916 rtnl_unlock();
5917 return 0;
5918 }
5919
5920 if (valp == &net->ipv6.devconf_all->disable_policy) {
5921 struct net_device *dev;
5922
5923 for_each_netdev(net, dev) {
5924 idev = __in6_dev_get(dev);
5925 if (idev)
5926 addrconf_disable_policy_idev(idev, val);
5927 }
5928 } else {
5929 idev = (struct inet6_dev *)ctl->extra1;
5930 addrconf_disable_policy_idev(idev, val);
5931 }
5932
5933 rtnl_unlock();
5934 return 0;
5935}
5936
5937static
5938int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
5939 void __user *buffer, size_t *lenp,
5940 loff_t *ppos)
5941{
5942 int *valp = ctl->data;
5943 int val = *valp;
5944 loff_t pos = *ppos;
5945 struct ctl_table lctl;
5946 int ret;
5947
5948 lctl = *ctl;
5949 lctl.data = &val;
5950 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5951
5952 if (write && (*valp != val))
5953 ret = addrconf_disable_policy(ctl, valp, val);
5954
5955 if (ret)
5956 *ppos = pos;
5957
5958 return ret;
5959}
5960
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07005961static int minus_one = -1;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07005962static const int one = 1;
5963static const int two_five_five = 255;
5964
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03005965static const struct ctl_table addrconf_sysctl[] = {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005966 {
5967 .procname = "forwarding",
5968 .data = &ipv6_devconf.forwarding,
5969 .maxlen = sizeof(int),
5970 .mode = 0644,
5971 .proc_handler = addrconf_sysctl_forward,
5972 },
5973 {
5974 .procname = "hop_limit",
5975 .data = &ipv6_devconf.hop_limit,
5976 .maxlen = sizeof(int),
5977 .mode = 0644,
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07005978 .proc_handler = proc_dointvec_minmax,
5979 .extra1 = (void *)&one,
5980 .extra2 = (void *)&two_five_five,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005981 },
5982 {
5983 .procname = "mtu",
5984 .data = &ipv6_devconf.mtu6,
5985 .maxlen = sizeof(int),
5986 .mode = 0644,
5987 .proc_handler = addrconf_sysctl_mtu,
5988 },
5989 {
5990 .procname = "accept_ra",
5991 .data = &ipv6_devconf.accept_ra,
5992 .maxlen = sizeof(int),
5993 .mode = 0644,
5994 .proc_handler = proc_dointvec,
5995 },
5996 {
5997 .procname = "accept_redirects",
5998 .data = &ipv6_devconf.accept_redirects,
5999 .maxlen = sizeof(int),
6000 .mode = 0644,
6001 .proc_handler = proc_dointvec,
6002 },
6003 {
6004 .procname = "autoconf",
6005 .data = &ipv6_devconf.autoconf,
6006 .maxlen = sizeof(int),
6007 .mode = 0644,
6008 .proc_handler = proc_dointvec,
6009 },
6010 {
6011 .procname = "dad_transmits",
6012 .data = &ipv6_devconf.dad_transmits,
6013 .maxlen = sizeof(int),
6014 .mode = 0644,
6015 .proc_handler = proc_dointvec,
6016 },
6017 {
6018 .procname = "router_solicitations",
6019 .data = &ipv6_devconf.rtr_solicits,
6020 .maxlen = sizeof(int),
6021 .mode = 0644,
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07006022 .proc_handler = proc_dointvec_minmax,
6023 .extra1 = &minus_one,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006024 },
6025 {
6026 .procname = "router_solicitation_interval",
6027 .data = &ipv6_devconf.rtr_solicit_interval,
6028 .maxlen = sizeof(int),
6029 .mode = 0644,
6030 .proc_handler = proc_dointvec_jiffies,
6031 },
6032 {
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07006033 .procname = "router_solicitation_max_interval",
6034 .data = &ipv6_devconf.rtr_solicit_max_interval,
6035 .maxlen = sizeof(int),
6036 .mode = 0644,
6037 .proc_handler = proc_dointvec_jiffies,
6038 },
6039 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006040 .procname = "router_solicitation_delay",
6041 .data = &ipv6_devconf.rtr_solicit_delay,
6042 .maxlen = sizeof(int),
6043 .mode = 0644,
6044 .proc_handler = proc_dointvec_jiffies,
6045 },
6046 {
6047 .procname = "force_mld_version",
6048 .data = &ipv6_devconf.force_mld_version,
6049 .maxlen = sizeof(int),
6050 .mode = 0644,
6051 .proc_handler = proc_dointvec,
6052 },
6053 {
6054 .procname = "mldv1_unsolicited_report_interval",
6055 .data =
6056 &ipv6_devconf.mldv1_unsolicited_report_interval,
6057 .maxlen = sizeof(int),
6058 .mode = 0644,
6059 .proc_handler = proc_dointvec_ms_jiffies,
6060 },
6061 {
6062 .procname = "mldv2_unsolicited_report_interval",
6063 .data =
6064 &ipv6_devconf.mldv2_unsolicited_report_interval,
6065 .maxlen = sizeof(int),
6066 .mode = 0644,
6067 .proc_handler = proc_dointvec_ms_jiffies,
6068 },
6069 {
6070 .procname = "use_tempaddr",
6071 .data = &ipv6_devconf.use_tempaddr,
6072 .maxlen = sizeof(int),
6073 .mode = 0644,
6074 .proc_handler = proc_dointvec,
6075 },
6076 {
6077 .procname = "temp_valid_lft",
6078 .data = &ipv6_devconf.temp_valid_lft,
6079 .maxlen = sizeof(int),
6080 .mode = 0644,
6081 .proc_handler = proc_dointvec,
6082 },
6083 {
6084 .procname = "temp_prefered_lft",
6085 .data = &ipv6_devconf.temp_prefered_lft,
6086 .maxlen = sizeof(int),
6087 .mode = 0644,
6088 .proc_handler = proc_dointvec,
6089 },
6090 {
6091 .procname = "regen_max_retry",
6092 .data = &ipv6_devconf.regen_max_retry,
6093 .maxlen = sizeof(int),
6094 .mode = 0644,
6095 .proc_handler = proc_dointvec,
6096 },
6097 {
6098 .procname = "max_desync_factor",
6099 .data = &ipv6_devconf.max_desync_factor,
6100 .maxlen = sizeof(int),
6101 .mode = 0644,
6102 .proc_handler = proc_dointvec,
6103 },
6104 {
6105 .procname = "max_addresses",
6106 .data = &ipv6_devconf.max_addresses,
6107 .maxlen = sizeof(int),
6108 .mode = 0644,
6109 .proc_handler = proc_dointvec,
6110 },
6111 {
6112 .procname = "accept_ra_defrtr",
6113 .data = &ipv6_devconf.accept_ra_defrtr,
6114 .maxlen = sizeof(int),
6115 .mode = 0644,
6116 .proc_handler = proc_dointvec,
6117 },
6118 {
6119 .procname = "accept_ra_min_hop_limit",
6120 .data = &ipv6_devconf.accept_ra_min_hop_limit,
6121 .maxlen = sizeof(int),
6122 .mode = 0644,
6123 .proc_handler = proc_dointvec,
6124 },
6125 {
6126 .procname = "accept_ra_pinfo",
6127 .data = &ipv6_devconf.accept_ra_pinfo,
6128 .maxlen = sizeof(int),
6129 .mode = 0644,
6130 .proc_handler = proc_dointvec,
6131 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08006132#ifdef CONFIG_IPV6_ROUTER_PREF
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006133 {
6134 .procname = "accept_ra_rtr_pref",
6135 .data = &ipv6_devconf.accept_ra_rtr_pref,
6136 .maxlen = sizeof(int),
6137 .mode = 0644,
6138 .proc_handler = proc_dointvec,
6139 },
6140 {
6141 .procname = "router_probe_interval",
6142 .data = &ipv6_devconf.rtr_probe_interval,
6143 .maxlen = sizeof(int),
6144 .mode = 0644,
6145 .proc_handler = proc_dointvec_jiffies,
6146 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08006147#ifdef CONFIG_IPV6_ROUTE_INFO
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006148 {
Joel Scherpelzbbea1242017-03-22 18:19:04 +09006149 .procname = "accept_ra_rt_info_min_plen",
6150 .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
6151 .maxlen = sizeof(int),
6152 .mode = 0644,
6153 .proc_handler = proc_dointvec,
6154 },
6155 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006156 .procname = "accept_ra_rt_info_max_plen",
6157 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
6158 .maxlen = sizeof(int),
6159 .mode = 0644,
6160 .proc_handler = proc_dointvec,
6161 },
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08006162#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08006163#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006164 {
6165 .procname = "proxy_ndp",
6166 .data = &ipv6_devconf.proxy_ndp,
6167 .maxlen = sizeof(int),
6168 .mode = 0644,
6169 .proc_handler = addrconf_sysctl_proxy_ndp,
6170 },
6171 {
6172 .procname = "accept_source_route",
6173 .data = &ipv6_devconf.accept_source_route,
6174 .maxlen = sizeof(int),
6175 .mode = 0644,
6176 .proc_handler = proc_dointvec,
6177 },
Neil Horman95c385b2007-04-25 17:08:10 -07006178#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006179 {
6180 .procname = "optimistic_dad",
6181 .data = &ipv6_devconf.optimistic_dad,
6182 .maxlen = sizeof(int),
6183 .mode = 0644,
6184 .proc_handler = proc_dointvec,
6185 },
6186 {
6187 .procname = "use_optimistic",
6188 .data = &ipv6_devconf.use_optimistic,
6189 .maxlen = sizeof(int),
6190 .mode = 0644,
6191 .proc_handler = proc_dointvec,
6192 },
Neil Horman95c385b2007-04-25 17:08:10 -07006193#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006194#ifdef CONFIG_IPV6_MROUTE
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006195 {
6196 .procname = "mc_forwarding",
6197 .data = &ipv6_devconf.mc_forwarding,
6198 .maxlen = sizeof(int),
6199 .mode = 0444,
6200 .proc_handler = proc_dointvec,
6201 },
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006202#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006203 {
6204 .procname = "disable_ipv6",
6205 .data = &ipv6_devconf.disable_ipv6,
6206 .maxlen = sizeof(int),
6207 .mode = 0644,
6208 .proc_handler = addrconf_sysctl_disable,
6209 },
6210 {
6211 .procname = "accept_dad",
6212 .data = &ipv6_devconf.accept_dad,
6213 .maxlen = sizeof(int),
6214 .mode = 0644,
6215 .proc_handler = proc_dointvec,
6216 },
6217 {
6218 .procname = "force_tllao",
6219 .data = &ipv6_devconf.force_tllao,
6220 .maxlen = sizeof(int),
6221 .mode = 0644,
6222 .proc_handler = proc_dointvec
6223 },
6224 {
6225 .procname = "ndisc_notify",
6226 .data = &ipv6_devconf.ndisc_notify,
6227 .maxlen = sizeof(int),
6228 .mode = 0644,
6229 .proc_handler = proc_dointvec
6230 },
6231 {
6232 .procname = "suppress_frag_ndisc",
6233 .data = &ipv6_devconf.suppress_frag_ndisc,
6234 .maxlen = sizeof(int),
6235 .mode = 0644,
6236 .proc_handler = proc_dointvec
6237 },
6238 {
6239 .procname = "accept_ra_from_local",
6240 .data = &ipv6_devconf.accept_ra_from_local,
6241 .maxlen = sizeof(int),
6242 .mode = 0644,
6243 .proc_handler = proc_dointvec,
6244 },
6245 {
6246 .procname = "accept_ra_mtu",
6247 .data = &ipv6_devconf.accept_ra_mtu,
6248 .maxlen = sizeof(int),
6249 .mode = 0644,
6250 .proc_handler = proc_dointvec,
6251 },
6252 {
6253 .procname = "stable_secret",
6254 .data = &ipv6_devconf.stable_secret,
6255 .maxlen = IPV6_MAX_STRLEN,
6256 .mode = 0600,
6257 .proc_handler = addrconf_sysctl_stable_secret,
6258 },
6259 {
6260 .procname = "use_oif_addrs_only",
6261 .data = &ipv6_devconf.use_oif_addrs_only,
6262 .maxlen = sizeof(int),
6263 .mode = 0644,
6264 .proc_handler = proc_dointvec,
6265 },
6266 {
6267 .procname = "ignore_routes_with_linkdown",
6268 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6269 .maxlen = sizeof(int),
6270 .mode = 0644,
6271 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6272 },
6273 {
6274 .procname = "drop_unicast_in_l2_multicast",
6275 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6276 .maxlen = sizeof(int),
6277 .mode = 0644,
6278 .proc_handler = proc_dointvec,
6279 },
6280 {
6281 .procname = "drop_unsolicited_na",
6282 .data = &ipv6_devconf.drop_unsolicited_na,
6283 .maxlen = sizeof(int),
6284 .mode = 0644,
6285 .proc_handler = proc_dointvec,
6286 },
6287 {
6288 .procname = "keep_addr_on_down",
6289 .data = &ipv6_devconf.keep_addr_on_down,
6290 .maxlen = sizeof(int),
6291 .mode = 0644,
6292 .proc_handler = proc_dointvec,
David Ahernf1705ec2016-02-24 09:25:37 -08006293
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006294 },
6295 {
David Lebrun1ababeb2016-11-08 14:57:39 +01006296 .procname = "seg6_enabled",
6297 .data = &ipv6_devconf.seg6_enabled,
6298 .maxlen = sizeof(int),
6299 .mode = 0644,
6300 .proc_handler = proc_dointvec,
6301 },
David Lebrunbf355b82016-11-08 14:57:42 +01006302#ifdef CONFIG_IPV6_SEG6_HMAC
6303 {
6304 .procname = "seg6_require_hmac",
6305 .data = &ipv6_devconf.seg6_require_hmac,
6306 .maxlen = sizeof(int),
6307 .mode = 0644,
6308 .proc_handler = proc_dointvec,
6309 },
6310#endif
David Lebrun1ababeb2016-11-08 14:57:39 +01006311 {
Erik Nordmarkadc176c2016-12-02 14:00:08 -08006312 .procname = "enhanced_dad",
6313 .data = &ipv6_devconf.enhanced_dad,
6314 .maxlen = sizeof(int),
6315 .mode = 0644,
6316 .proc_handler = proc_dointvec,
6317 },
6318 {
Felix Jiad35a00b2017-01-26 16:59:17 +13006319 .procname = "addr_gen_mode",
6320 .data = &ipv6_devconf.addr_gen_mode,
6321 .maxlen = sizeof(int),
6322 .mode = 0644,
6323 .proc_handler = addrconf_sysctl_addr_gen_mode,
6324 },
6325 {
David Forsterdf789fe2017-02-23 16:27:18 +00006326 .procname = "disable_policy",
6327 .data = &ipv6_devconf.disable_policy,
6328 .maxlen = sizeof(int),
6329 .mode = 0644,
6330 .proc_handler = addrconf_sysctl_disable_policy,
6331 },
6332 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006333 /* sentinel */
6334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335};
6336
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08006337static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006338 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339{
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006340 int i, ifindex;
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006341 struct ctl_table *table;
Eric W. Biederman6105e292012-04-19 13:41:24 +00006342 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11006343
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006344 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6345 if (!table)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006346 goto out;
6347
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006348 for (i = 0; table[i].data; i++) {
6349 table[i].data += (char *)p - (char *)&ipv6_devconf;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006350 /* If one of these is already set, then it is not safe to
6351 * overwrite either of them: this makes proc_dointvec_minmax
6352 * usable.
6353 */
6354 if (!table[i].extra1 && !table[i].extra2) {
6355 table[i].extra1 = idev; /* embedded; no ref */
6356 table[i].extra2 = net;
6357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359
Eric W. Biederman6105e292012-04-19 13:41:24 +00006360 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006362 p->sysctl_header = register_net_sysctl(net, path, table);
6363 if (!p->sysctl_header)
Eric W. Biederman6105e292012-04-19 13:41:24 +00006364 goto free;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006365
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006366 if (!strcmp(dev_name, "all"))
6367 ifindex = NETCONFA_IFINDEX_ALL;
6368 else if (!strcmp(dev_name, "default"))
6369 ifindex = NETCONFA_IFINDEX_DEFAULT;
6370 else
6371 ifindex = idev->dev->ifindex;
David Ahern85b3daa2017-03-28 14:28:04 -07006372 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
6373 ifindex, p);
Pavel Emelyanov95897312008-01-10 17:41:45 -08006374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006376free:
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006377 kfree(table);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006378out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08006379 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380}
6381
David Ahern23452172017-03-28 14:28:05 -07006382static void __addrconf_sysctl_unregister(struct net *net,
6383 struct ipv6_devconf *p, int ifindex)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006384{
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006385 struct ctl_table *table;
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006386
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006387 if (!p->sysctl_header)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006388 return;
6389
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006390 table = p->sysctl_header->ctl_table_arg;
6391 unregister_net_sysctl_table(p->sysctl_header);
6392 p->sysctl_header = NULL;
6393 kfree(table);
David Ahern23452172017-03-28 14:28:05 -07006394
6395 inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006396}
6397
WANG Conga317a2f2014-07-25 15:25:09 -07006398static int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006399{
WANG Conga317a2f2014-07-25 15:25:09 -07006400 int err;
6401
6402 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6403 return -EINVAL;
6404
6405 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6406 &ndisc_ifinfo_sysctl_change);
6407 if (err)
6408 return err;
6409 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6410 idev, &idev->cnf);
6411 if (err)
6412 neigh_sysctl_unregister(idev->nd_parms);
6413
6414 return err;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006415}
6416
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006417static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418{
David Ahern23452172017-03-28 14:28:05 -07006419 __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
6420 idev->dev->ifindex);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006421 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422}
6423
6424
6425#endif
6426
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006427static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006428{
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006429 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006430 struct ipv6_devconf *all, *dflt;
6431
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006432 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006433 if (!all)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006434 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006435
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006436 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006437 if (!dflt)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006438 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006439
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006440 /* these will be inherited by all namespaces */
6441 dflt->autoconf = ipv6_defaults.autoconf;
6442 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006443
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006444 dflt->stable_secret.initialized = false;
6445 all->stable_secret.initialized = false;
6446
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006447 net->ipv6.devconf_all = all;
6448 net->ipv6.devconf_dflt = dflt;
6449
6450#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006451 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006452 if (err < 0)
6453 goto err_reg_all;
6454
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006455 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006456 if (err < 0)
6457 goto err_reg_dflt;
6458#endif
6459 return 0;
6460
6461#ifdef CONFIG_SYSCTL
6462err_reg_dflt:
David Ahern23452172017-03-28 14:28:05 -07006463 __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006464err_reg_all:
6465 kfree(dflt);
6466#endif
6467err_alloc_dflt:
6468 kfree(all);
6469err_alloc_all:
6470 return err;
6471}
6472
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006473static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006474{
6475#ifdef CONFIG_SYSCTL
David Ahern23452172017-03-28 14:28:05 -07006476 __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
6477 NETCONFA_IFINDEX_DEFAULT);
6478 __addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
6479 NETCONFA_IFINDEX_ALL);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006480#endif
zhuyj73cf0e922014-11-26 10:25:58 +08006481 kfree(net->ipv6.devconf_dflt);
6482 kfree(net->ipv6.devconf_all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006483}
6484
6485static struct pernet_operations addrconf_ops = {
6486 .init = addrconf_init_net,
6487 .exit = addrconf_exit_net,
6488};
6489
Daniel Borkmann207895fd32015-01-29 12:15:03 +01006490static struct rtnl_af_ops inet6_ops __read_mostly = {
Thomas Grafb382b192010-11-16 04:33:57 +00006491 .family = AF_INET6,
6492 .fill_link_af = inet6_fill_link_af,
6493 .get_link_af_size = inet6_get_link_af_size,
Daniel Borkmann11b1f822015-02-05 14:39:11 +01006494 .validate_link_af = inet6_validate_link_af,
Daniel Borkmannf53adae2013-04-08 04:01:30 +00006495 .set_link_af = inet6_set_link_af,
Thomas Grafb382b192010-11-16 04:33:57 +00006496};
6497
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498/*
6499 * Init / cleanup code
6500 */
6501
6502int __init addrconf_init(void)
6503{
WANG Conga317a2f2014-07-25 15:25:09 -07006504 struct inet6_dev *idev;
stephen hemmingerc2e21292010-03-17 20:31:10 +00006505 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006506
Stephen Hemmingere21e8462010-03-20 16:09:01 -07006507 err = ipv6_addr_label_init();
6508 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00006509 pr_crit("%s: cannot initialize default policy table: %d\n",
6510 __func__, err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006511 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006514 err = register_pernet_subsys(&addrconf_ops);
6515 if (err < 0)
6516 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006517
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006518 addrconf_wq = create_workqueue("ipv6_addrconf");
6519 if (!addrconf_wq) {
6520 err = -ENOMEM;
6521 goto out_nowq;
6522 }
6523
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524 /* The addrconf netdev notifier requires that loopback_dev
6525 * has it's ipv6 private information allocated and setup
6526 * before it can bring up and give link-local addresses
6527 * to other devices which are up.
6528 *
6529 * Unfortunately, loopback_dev is not necessarily the first
6530 * entry in the global dev_base list of net devices. In fact,
6531 * it is likely to be the very last entry on that list.
6532 * So this causes the notifier registry below to try and
6533 * give link-local addresses to all devices besides loopback_dev
6534 * first, then loopback_dev, which cases all the non-loopback_dev
6535 * devices to fail to get a link-local address.
6536 *
6537 * So, as a temporary fix, allocate the ipv6 structure for
6538 * loopback_dev first by hand.
6539 * Longer term, all of the dependencies ipv6 has upon the loopback
6540 * device and it being up should be removed.
6541 */
6542 rtnl_lock();
WANG Conga317a2f2014-07-25 15:25:09 -07006543 idev = ipv6_add_dev(init_net.loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544 rtnl_unlock();
WANG Conga317a2f2014-07-25 15:25:09 -07006545 if (IS_ERR(idev)) {
6546 err = PTR_ERR(idev);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006547 goto errlo;
WANG Conga317a2f2014-07-25 15:25:09 -07006548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549
stephen hemmingerc2e21292010-03-17 20:31:10 +00006550 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6551 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6552
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553 register_netdevice_notifier(&ipv6_dev_notf);
6554
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006555 addrconf_verify();
Thomas Grafc127ea22007-03-22 11:58:32 -07006556
stephen hemminger3678a9d2013-12-30 10:41:32 -08006557 rtnl_af_register(&inet6_ops);
Thomas Grafb382b192010-11-16 04:33:57 +00006558
Greg Rosec7ac8672011-06-10 01:27:09 +00006559 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6560 NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006561 if (err < 0)
6562 goto errout;
6563
6564 /* Only the first call to __rtnl_register can fail */
Greg Rosec7ac8672011-06-10 01:27:09 +00006565 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
6566 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
6567 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6568 inet6_dump_ifaddr, NULL);
6569 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6570 inet6_dump_ifmcaddr, NULL);
6571 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6572 inet6_dump_ifacaddr, NULL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +00006573 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
Nicolas Dichtel7a674202013-03-05 23:42:06 +00006574 inet6_netconf_dump_devconf, NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006575
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006576 ipv6_addr_label_rtnl_register();
6577
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07006579errout:
Thomas Grafb382b192010-11-16 04:33:57 +00006580 rtnl_af_unregister(&inet6_ops);
Thomas Grafc127ea22007-03-22 11:58:32 -07006581 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006582errlo:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006583 destroy_workqueue(addrconf_wq);
6584out_nowq:
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006585 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006586out_addrlabel:
6587 ipv6_addr_label_cleanup();
6588out:
Thomas Grafc127ea22007-03-22 11:58:32 -07006589 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590}
6591
Daniel Lezcano09f77092007-12-13 05:34:58 -08006592void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006594 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595 int i;
6596
6597 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006598 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006599 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600
6601 rtnl_lock();
6602
Thomas Grafb382b192010-11-16 04:33:57 +00006603 __rtnl_af_unregister(&inet6_ops);
6604
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006605 /* clean dev list */
6606 for_each_netdev(&init_net, dev) {
6607 if (__in6_dev_get(dev) == NULL)
6608 continue;
6609 addrconf_ifdown(dev, 1);
6610 }
6611 addrconf_ifdown(init_net.loopback_dev, 2);
6612
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614 * Check hash table.
6615 */
stephen hemminger5c578aed2010-03-17 20:31:11 +00006616 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00006617 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6618 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578aed2010-03-17 20:31:11 +00006619 spin_unlock_bh(&addrconf_hash_lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006620 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621 rtnl_unlock();
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006622
6623 destroy_workqueue(addrconf_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006624}