blob: 67ec87ea5fb699eb8ba4634c91815f97d750ddf1 [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 Liu8013d1d2015-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 Liu8013d1d2015-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 Dubroca2a189f92015-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 Hideaki072047e2005-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 Hideaki072047e2005-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 Hideaki072047e2005-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 Hideaki072047e2005-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 Hideaki072047e2005-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 Hideaki072047e2005-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 Hideaki072047e2005-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 Hideaki072047e2005-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 Hideaki072047e2005-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 Hideaki072047e2005-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
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002076static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
2077{
Alexander Aring5241c2d2015-12-14 20:55:22 +01002078 if (dev->addr_len != EUI64_ADDR_LEN)
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002079 return -1;
Alexander Aring5241c2d2015-12-14 20:55:22 +01002080 memcpy(eui, dev->dev_addr, EUI64_ADDR_LEN);
YOSHIFUJI Hideaki / 吉藤英明5e98a362013-01-28 10:44:29 +00002081 eui[0] ^= 2;
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002082 return 0;
2083}
2084
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002085static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
2086{
2087 union fwnet_hwaddr *ha;
2088
2089 if (dev->addr_len != FWNET_ALEN)
2090 return -1;
2091
2092 ha = (union fwnet_hwaddr *)dev->dev_addr;
2093
2094 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
2095 eui[0] ^= 2;
2096 return 0;
2097}
2098
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002099static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
2100{
2101 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
2102 if (dev->addr_len != ARCNET_ALEN)
2103 return -1;
2104 memset(eui, 0, 7);
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002105 eui[7] = *(u8 *)dev->dev_addr;
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002106 return 0;
2107}
2108
2109static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
2110{
2111 if (dev->addr_len != INFINIBAND_ALEN)
2112 return -1;
2113 memcpy(eui, dev->dev_addr + 12, 8);
2114 eui[0] |= 2;
2115 return 0;
2116}
2117
stephen hemmingerc61393e2010-10-04 20:17:53 +00002118static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002119{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00002120 if (addr == 0)
2121 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002122 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
2123 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
2124 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
2125 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
2126 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
2127 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
2128 eui[1] = 0;
2129 eui[2] = 0x5E;
2130 eui[3] = 0xFE;
2131 memcpy(eui + 4, &addr, 4);
2132 return 0;
2133}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002134
2135static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
2136{
2137 if (dev->priv_flags & IFF_ISATAP)
2138 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2139 return -1;
2140}
2141
stephen hemmingeraee80b52011-06-08 10:44:30 +00002142static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
2143{
2144 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
2145}
2146
Nicolas Dichtele8377352013-08-20 12:16:06 +02002147static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
2148{
2149 memcpy(eui, dev->perm_addr, 3);
2150 memcpy(eui + 5, dev->perm_addr + 3, 3);
2151 eui[3] = 0xFF;
2152 eui[4] = 0xFE;
2153 eui[0] ^= 2;
2154 return 0;
2155}
2156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
2158{
2159 switch (dev->type) {
2160 case ARPHRD_ETHER:
2161 case ARPHRD_FDDI:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002162 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002164 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08002166 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002167 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002168 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002169 case ARPHRD_IPGRE:
Felix Jia45ce0fd2017-01-26 16:59:18 +13002170 case ARPHRD_TUNNEL:
stephen hemmingeraee80b52011-06-08 10:44:30 +00002171 return addrconf_ifid_gre(eui, dev);
Jukka Rissanene74bccb82013-12-11 17:05:36 +02002172 case ARPHRD_6LOWPAN:
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002173 return addrconf_ifid_eui64(eui, dev);
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002174 case ARPHRD_IEEE1394:
2175 return addrconf_ifid_ieee1394(eui, dev);
Nicolas Dichtele8377352013-08-20 12:16:06 +02002176 case ARPHRD_TUNNEL6:
Felix Jia45ce0fd2017-01-26 16:59:18 +13002177 case ARPHRD_IP6GRE:
Nicolas Dichtele8377352013-08-20 12:16:06 +02002178 return addrconf_ifid_ip6tnl(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 }
2180 return -1;
2181}
2182
2183static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2184{
2185 int err = -1;
2186 struct inet6_ifaddr *ifp;
2187
2188 read_lock_bh(&idev->lock);
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01002189 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2190 if (ifp->scope > IFA_LINK)
2191 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2193 memcpy(eui, ifp->addr.s6_addr+8, 8);
2194 err = 0;
2195 break;
2196 }
2197 }
2198 read_unlock_bh(&idev->lock);
2199 return err;
2200}
2201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002203static void ipv6_regen_rndid(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08002206 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209 /*
2210 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2211 * check if generated address is not inappropriate
2212 *
2213 * - Reserved subnet anycast (RFC 2526)
2214 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002215 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 * 00-00-5E-FE-xx-xx-xx-xx
2217 * - value 0
2218 * - XXX: already assigned to an address on the device
2219 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002220 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2222 (idev->rndid[7]&0x80))
2223 goto regen;
2224 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2225 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2226 goto regen;
2227 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2228 goto regen;
2229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
2231
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002232static void ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002233{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
Jiri Bohac9d6280d2016-10-13 18:50:02 +02002235 ipv6_regen_rndid(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238/*
2239 * Add prefix route.
2240 */
2241
2242static void
2243addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002244 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Thomas Graf86872cb2006-08-22 00:01:08 -07002246 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002247 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX,
Thomas Graf86872cb2006-08-22 00:01:08 -07002248 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2249 .fc_ifindex = dev->ifindex,
2250 .fc_expires = expires,
2251 .fc_dst_len = plen,
2252 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002253 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07002254 .fc_protocol = RTPROT_KERNEL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002255 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002257 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
2259 /* Prevent useless cloning on PtP SIT.
2260 This thing is done here expecting that the whole
2261 class of non-broadcast devices need not cloning.
2262 */
Amerigo Wang07a93622012-10-29 16:23:10 +00002263#if IS_ENABLED(CONFIG_IPV6_SIT)
Thomas Graf86872cb2006-08-22 00:01:08 -07002264 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2265 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002266#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Thomas Graf86872cb2006-08-22 00:01:08 -07002268 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269}
2270
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002271
2272static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2273 int plen,
2274 const struct net_device *dev,
2275 u32 flags, u32 noflags)
2276{
2277 struct fib6_node *fn;
2278 struct rt6_info *rt = NULL;
2279 struct fib6_table *table;
David Ahernca254492015-10-12 11:47:10 -07002280 u32 tb_id = l3mdev_fib_table(dev) ? : RT6_TABLE_PREFIX;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002281
David Ahernca254492015-10-12 11:47:10 -07002282 table = fib6_get_table(dev_net(dev), tb_id);
Ian Morris63159f22015-03-29 14:00:04 +01002283 if (!table)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002284 return NULL;
2285
Li RongQing5744dd92012-09-11 21:59:01 +00002286 read_lock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002287 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2288 if (!fn)
2289 goto out;
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07002290
2291 noflags |= RTF_CACHE;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002292 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002293 if (rt->dst.dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002294 continue;
2295 if ((rt->rt6i_flags & flags) != flags)
2296 continue;
Romain Kuntz85da53b2013-01-09 15:02:26 +01002297 if ((rt->rt6i_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002298 continue;
2299 dst_hold(&rt->dst);
2300 break;
2301 }
2302out:
Li RongQing5744dd92012-09-11 21:59:01 +00002303 read_unlock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002304 return rt;
2305}
2306
2307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308/* Create "default" multicast route to the interface */
2309
2310static void addrconf_add_mroute(struct net_device *dev)
2311{
Thomas Graf86872cb2006-08-22 00:01:08 -07002312 struct fib6_config cfg = {
David Ahernca254492015-10-12 11:47:10 -07002313 .fc_table = l3mdev_fib_table(dev) ? : RT6_TABLE_LOCAL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002314 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2315 .fc_ifindex = dev->ifindex,
2316 .fc_dst_len = 8,
2317 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002318 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002319 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Thomas Graf86872cb2006-08-22 00:01:08 -07002321 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2322
2323 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324}
2325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2327{
2328 struct inet6_dev *idev;
2329
2330 ASSERT_RTNL();
2331
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002332 idev = ipv6_find_idev(dev);
2333 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00002334 return ERR_PTR(-ENOBUFS);
2335
2336 if (idev->cnf.disable_ipv6)
2337 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339 /* Add default multicast route */
David Ahernba46ee42016-06-13 13:44:18 -07002340 if (!(dev->flags & IFF_LOOPBACK) && !netif_is_l3_master(dev))
Li Wei4af04ab2011-12-06 21:23:45 +00002341 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 return idev;
2344}
2345
Jiri Pirko53bd6742013-12-06 09:45:22 +01002346static void manage_tempaddrs(struct inet6_dev *idev,
2347 struct inet6_ifaddr *ifp,
2348 __u32 valid_lft, __u32 prefered_lft,
2349 bool create, unsigned long now)
2350{
2351 u32 flags;
2352 struct inet6_ifaddr *ift;
2353
2354 read_lock_bh(&idev->lock);
2355 /* update all temporary addresses in the list */
2356 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2357 int age, max_valid, max_prefered;
2358
2359 if (ifp != ift->ifpub)
2360 continue;
2361
2362 /* RFC 4941 section 3.3:
2363 * If a received option will extend the lifetime of a public
2364 * address, the lifetimes of temporary addresses should
2365 * be extended, subject to the overall constraint that no
2366 * temporary addresses should ever remain "valid" or "preferred"
2367 * for a time longer than (TEMP_VALID_LIFETIME) or
2368 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2369 */
2370 age = (now - ift->cstamp) / HZ;
2371 max_valid = idev->cnf.temp_valid_lft - age;
2372 if (max_valid < 0)
2373 max_valid = 0;
2374
2375 max_prefered = idev->cnf.temp_prefered_lft -
Jiri Bohac76506a92016-10-13 18:52:15 +02002376 idev->desync_factor - age;
Jiri Pirko53bd6742013-12-06 09:45:22 +01002377 if (max_prefered < 0)
2378 max_prefered = 0;
2379
2380 if (valid_lft > max_valid)
2381 valid_lft = max_valid;
2382
2383 if (prefered_lft > max_prefered)
2384 prefered_lft = max_prefered;
2385
2386 spin_lock(&ift->lock);
2387 flags = ift->flags;
2388 ift->valid_lft = valid_lft;
2389 ift->prefered_lft = prefered_lft;
2390 ift->tstamp = now;
2391 if (prefered_lft > 0)
2392 ift->flags &= ~IFA_F_DEPRECATED;
2393
2394 spin_unlock(&ift->lock);
2395 if (!(flags&IFA_F_TENTATIVE))
2396 ipv6_ifa_notify(0, ift);
2397 }
2398
2399 if ((create || list_empty(&idev->tempaddr_list)) &&
2400 idev->cnf.use_tempaddr > 0) {
2401 /* When a new public address is created as described
2402 * in [ADDRCONF], also create a new temporary address.
2403 * Also create a temporary address if it's enabled but
2404 * no temporary address currently exists.
2405 */
2406 read_unlock_bh(&idev->lock);
2407 ipv6_create_tempaddr(ifp, NULL);
2408 } else {
2409 read_unlock_bh(&idev->lock);
2410 }
2411}
2412
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002413static bool is_addr_mode_generate_stable(struct inet6_dev *idev)
2414{
Felix Jiad35a00b2017-01-26 16:59:17 +13002415 return idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY ||
2416 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_RANDOM;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002417}
2418
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002419int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
2420 const struct prefix_info *pinfo,
2421 struct inet6_dev *in6_dev,
2422 const struct in6_addr *addr, int addr_type,
2423 u32 addr_flags, bool sllao, bool tokenized,
2424 __u32 valid_lft, u32 prefered_lft)
Alexander Aring4f672232016-06-15 21:20:22 +02002425{
2426 struct inet6_ifaddr *ifp = ipv6_get_ifaddr(net, addr, dev, 1);
2427 int create = 0, update_lft = 0;
2428
2429 if (!ifp && valid_lft) {
2430 int max_addresses = in6_dev->cnf.max_addresses;
2431
2432#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2433 if (in6_dev->cnf.optimistic_dad &&
2434 !net->ipv6.devconf_all->forwarding && sllao)
2435 addr_flags |= IFA_F_OPTIMISTIC;
2436#endif
2437
2438 /* Do not allow to create too much of autoconfigured
2439 * addresses; this would be too easy way to crash kernel.
2440 */
2441 if (!max_addresses ||
2442 ipv6_count_addresses(in6_dev) < max_addresses)
2443 ifp = ipv6_add_addr(in6_dev, addr, NULL,
2444 pinfo->prefix_len,
2445 addr_type&IPV6_ADDR_SCOPE_MASK,
2446 addr_flags, valid_lft,
2447 prefered_lft);
2448
2449 if (IS_ERR_OR_NULL(ifp))
2450 return -1;
2451
2452 update_lft = 0;
2453 create = 1;
2454 spin_lock_bh(&ifp->lock);
2455 ifp->flags |= IFA_F_MANAGETEMPADDR;
2456 ifp->cstamp = jiffies;
2457 ifp->tokenized = tokenized;
2458 spin_unlock_bh(&ifp->lock);
2459 addrconf_dad_start(ifp);
2460 }
2461
2462 if (ifp) {
2463 u32 flags;
2464 unsigned long now;
2465 u32 stored_lft;
2466
2467 /* update lifetime (RFC2462 5.5.3 e) */
2468 spin_lock_bh(&ifp->lock);
2469 now = jiffies;
2470 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2471 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2472 else
2473 stored_lft = 0;
2474 if (!update_lft && !create && stored_lft) {
2475 const u32 minimum_lft = min_t(u32,
2476 stored_lft, MIN_VALID_LIFETIME);
2477 valid_lft = max(valid_lft, minimum_lft);
2478
2479 /* RFC4862 Section 5.5.3e:
2480 * "Note that the preferred lifetime of the
2481 * corresponding address is always reset to
2482 * the Preferred Lifetime in the received
2483 * Prefix Information option, regardless of
2484 * whether the valid lifetime is also reset or
2485 * ignored."
2486 *
2487 * So we should always update prefered_lft here.
2488 */
2489 update_lft = 1;
2490 }
2491
2492 if (update_lft) {
2493 ifp->valid_lft = valid_lft;
2494 ifp->prefered_lft = prefered_lft;
2495 ifp->tstamp = now;
2496 flags = ifp->flags;
2497 ifp->flags &= ~IFA_F_DEPRECATED;
2498 spin_unlock_bh(&ifp->lock);
2499
2500 if (!(flags&IFA_F_TENTATIVE))
2501 ipv6_ifa_notify(0, ifp);
2502 } else
2503 spin_unlock_bh(&ifp->lock);
2504
2505 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2506 create, now);
2507
2508 in6_ifa_put(ifp);
2509 addrconf_verify();
2510 }
2511
2512 return 0;
2513}
Alexander Aringcc84b3c2016-06-15 21:20:24 +02002514EXPORT_SYMBOL_GPL(addrconf_prefix_rcv_add_addr);
Alexander Aring4f672232016-06-15 21:20:22 +02002515
Neil Hormane6bff992012-01-04 10:49:15 +00002516void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517{
2518 struct prefix_info *pinfo;
2519 __u32 valid_lft;
2520 __u32 prefered_lft;
Alexander Aring4f672232016-06-15 21:20:22 +02002521 int addr_type, err;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002522 u32 addr_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07002524 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
2526 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002527
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 if (len < sizeof(struct prefix_info)) {
dingtianhongba3542e2013-08-17 10:27:04 +08002529 ADBG("addrconf: prefix option too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return;
2531 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 /*
2534 * Validation checks ([ADDRCONF], page 19)
2535 */
2536
2537 addr_type = ipv6_addr_type(&pinfo->prefix);
2538
2539 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2540 return;
2541
2542 valid_lft = ntohl(pinfo->valid);
2543 prefered_lft = ntohl(pinfo->prefered);
2544
2545 if (prefered_lft > valid_lft) {
Joe Perchese87cc472012-05-13 21:56:26 +00002546 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 return;
2548 }
2549
2550 in6_dev = in6_dev_get(dev);
2551
Ian Morris63159f22015-03-29 14:00:04 +01002552 if (!in6_dev) {
Joe Perchese87cc472012-05-13 21:56:26 +00002553 net_dbg_ratelimited("addrconf: device %s not configured\n",
2554 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 return;
2556 }
2557
2558 /*
2559 * Two things going on here:
2560 * 1) Add routes for on-link prefixes
2561 * 2) Configure prefixes with the auto flag set
2562 */
2563
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002564 if (pinfo->onlink) {
2565 struct rt6_info *rt;
2566 unsigned long rt_expires;
2567
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002568 /* Avoid arithmetic overflow. Really, we could
2569 * save rt_expires in seconds, likely valid_lft,
2570 * but it would require division in fib gc, that it
2571 * not good.
2572 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002573 if (HZ > USER_HZ)
2574 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2575 else
2576 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08002577
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002578 if (addrconf_finite_timeout(rt_expires))
2579 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002581 rt = addrconf_get_prefix_route(&pinfo->prefix,
2582 pinfo->prefix_len,
2583 dev,
2584 RTF_ADDRCONF | RTF_PREFIX_RT,
2585 RTF_GATEWAY | RTF_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002587 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002588 /* Autoconf prefix route */
2589 if (valid_lft == 0) {
2590 ip6_del_rt(rt);
2591 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002592 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002593 /* not infinity */
Gao feng1716a962012-04-06 00:13:10 +00002594 rt6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002595 } else {
Gao feng1716a962012-04-06 00:13:10 +00002596 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 }
2598 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002599 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002600 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2601 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002602 /* not infinity */
2603 flags |= RTF_EXPIRES;
2604 expires = jiffies_to_clock_t(rt_expires);
2605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002607 dev, expires, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00002609 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 }
2611
2612 /* Try to figure out our local address for this prefix */
2613
2614 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 struct in6_addr addr;
Alexander Aringf997c552016-06-15 21:20:23 +02002616 bool tokenized = false, dev_addr_generated = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
2618 if (pinfo->prefix_len == 64) {
2619 memcpy(&addr, &pinfo->prefix, 8);
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002620
2621 if (!ipv6_addr_any(&in6_dev->token)) {
2622 read_lock_bh(&in6_dev->lock);
2623 memcpy(addr.s6_addr + 8,
2624 in6_dev->token.s6_addr + 8, 8);
2625 read_unlock_bh(&in6_dev->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00002626 tokenized = true;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01002627 } else if (is_addr_mode_generate_stable(in6_dev) &&
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01002628 !ipv6_generate_stable_address(&addr, 0,
2629 in6_dev)) {
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002630 addr_flags |= IFA_F_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01002631 goto ok;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002632 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2633 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
Alexander Aring4f672232016-06-15 21:20:22 +02002634 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002635 } else {
2636 dev_addr_generated = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 }
2638 goto ok;
2639 }
Joe Perchese87cc472012-05-13 21:56:26 +00002640 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2641 pinfo->prefix_len);
Alexander Aring4f672232016-06-15 21:20:22 +02002642 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
2644ok:
Alexander Aring4f672232016-06-15 21:20:22 +02002645 err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
2646 &addr, addr_type,
2647 addr_flags, sllao,
2648 tokenized, valid_lft,
2649 prefered_lft);
2650 if (err)
2651 goto put;
Alexander Aringf997c552016-06-15 21:20:23 +02002652
2653 /* Ignore error case here because previous prefix add addr was
2654 * successful which will be notified.
2655 */
2656 ndisc_ops_prefix_rcv_add_addr(net, dev, pinfo, in6_dev, &addr,
2657 addr_type, addr_flags, sllao,
2658 tokenized, valid_lft,
2659 prefered_lft,
2660 dev_addr_generated);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 }
2662 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
Alexander Aring4f672232016-06-15 21:20:22 +02002663put:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 in6_dev_put(in6_dev);
2665}
2666
2667/*
2668 * Set destination address.
2669 * Special case for SIT interfaces where we create a new "virtual"
2670 * device.
2671 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002672int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
2674 struct in6_ifreq ireq;
2675 struct net_device *dev;
2676 int err = -EINVAL;
2677
2678 rtnl_lock();
2679
2680 err = -EFAULT;
2681 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2682 goto err_exit;
2683
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002684 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
2686 err = -ENODEV;
Ian Morris63159f22015-03-29 14:00:04 +01002687 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 goto err_exit;
2689
Amerigo Wang07a93622012-10-29 16:23:10 +00002690#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002692 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 struct ip_tunnel_parm p;
2695
2696 err = -EADDRNOTAVAIL;
2697 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2698 goto err_exit;
2699
2700 memset(&p, 0, sizeof(p));
2701 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2702 p.iph.saddr = 0;
2703 p.iph.version = 4;
2704 p.iph.ihl = 5;
2705 p.iph.protocol = IPPROTO_IPV6;
2706 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002707 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002709 if (ops->ndo_do_ioctl) {
2710 mm_segment_t oldfs = get_fs();
2711
2712 set_fs(KERNEL_DS);
2713 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2714 set_fs(oldfs);
2715 } else
2716 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718 if (err == 0) {
2719 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002720 dev = __dev_get_by_name(net, p.name);
2721 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 goto err_exit;
2723 err = dev_open(dev);
2724 }
2725 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002726#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
2728err_exit:
2729 rtnl_unlock();
2730 return err;
2731}
2732
Madhu Challa93a714d2015-02-25 09:58:35 -08002733static int ipv6_mc_config(struct sock *sk, bool join,
2734 const struct in6_addr *addr, int ifindex)
2735{
2736 int ret;
2737
2738 ASSERT_RTNL();
2739
2740 lock_sock(sk);
2741 if (join)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002742 ret = ipv6_sock_mc_join(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002743 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002744 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002745 release_sock(sk);
2746
2747 return ret;
2748}
2749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750/*
2751 * Manual configuration of address on an interface
2752 */
Jiri Pirko479840f2013-12-06 09:45:21 +01002753static int inet6_addr_add(struct net *net, int ifindex,
2754 const struct in6_addr *pfx,
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002755 const struct in6_addr *peer_pfx,
Jiri Pirko479840f2013-12-06 09:45:21 +01002756 unsigned int plen, __u32 ifa_flags,
2757 __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
2759 struct inet6_ifaddr *ifp;
2760 struct inet6_dev *idev;
2761 struct net_device *dev;
Madhu Challa93a714d2015-02-25 09:58:35 -08002762 unsigned long timeout;
2763 clock_t expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002765 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
2767 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002768
Thomas Graf24ef0da2008-05-28 16:54:22 +02002769 if (plen > 128)
2770 return -EINVAL;
2771
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002772 /* check the lifetime */
2773 if (!valid_lft || prefered_lft > valid_lft)
2774 return -EINVAL;
2775
Jiri Pirko53bd6742013-12-06 09:45:22 +01002776 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2777 return -EINVAL;
2778
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002779 dev = __dev_get_by_index(net, ifindex);
2780 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002782
Brian Haley64e724f2010-07-20 10:34:30 +00002783 idev = addrconf_add_dev(dev);
2784 if (IS_ERR(idev))
2785 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Madhu Challa93a714d2015-02-25 09:58:35 -08002787 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2788 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2789 true, pfx, ifindex);
2790
2791 if (ret < 0)
2792 return ret;
2793 }
2794
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 scope = ipv6_addr_scope(pfx);
2796
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002797 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2798 if (addrconf_finite_timeout(timeout)) {
2799 expires = jiffies_to_clock_t(timeout * HZ);
2800 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002801 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002802 } else {
2803 expires = 0;
2804 flags = 0;
2805 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002806 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002807
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002808 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2809 if (addrconf_finite_timeout(timeout)) {
2810 if (timeout == 0)
2811 ifa_flags |= IFA_F_DEPRECATED;
2812 prefered_lft = timeout;
2813 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002814
Jiri Benc8a226b22013-08-01 10:41:28 +02002815 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2816 valid_lft, prefered_lft);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002817
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 if (!IS_ERR(ifp)) {
Thomas Haller761aac72014-01-15 15:36:58 +01002819 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2820 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2821 expires, flags);
2822 }
2823
Neil Horman95c385b2007-04-25 17:08:10 -07002824 /*
2825 * Note that section 3.1 of RFC 4429 indicates
2826 * that the Optimistic flag should not be set for
2827 * manually configured addresses
2828 */
David S. Millercf22f9a2012-04-14 21:37:40 -04002829 addrconf_dad_start(ifp);
Jiri Pirko53bd6742013-12-06 09:45:22 +01002830 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2831 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2832 true, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002834 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 return 0;
Madhu Challa93a714d2015-02-25 09:58:35 -08002836 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2837 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2838 false, pfx, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 }
2840
2841 return PTR_ERR(ifp);
2842}
2843
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002844static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2845 const struct in6_addr *pfx, unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846{
2847 struct inet6_ifaddr *ifp;
2848 struct inet6_dev *idev;
2849 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002850
Thomas Graf24ef0da2008-05-28 16:54:22 +02002851 if (plen > 128)
2852 return -EINVAL;
2853
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002854 dev = __dev_get_by_index(net, ifindex);
2855 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 return -ENODEV;
2857
Ian Morrise5d08d72014-11-23 21:28:43 +00002858 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01002859 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 return -ENXIO;
2861
2862 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002863 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 if (ifp->prefix_len == plen &&
2865 ipv6_addr_equal(pfx, &ifp->addr)) {
2866 in6_ifa_hold(ifp);
2867 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002868
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002869 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2870 (ifa_flags & IFA_F_MANAGETEMPADDR))
2871 manage_tempaddrs(idev, ifp, 0, 0, false,
2872 jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 ipv6_del_addr(ifp);
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002874 addrconf_verify_rtnl();
Madhu Challa93a714d2015-02-25 09:58:35 -08002875 if (ipv6_addr_is_multicast(pfx)) {
2876 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2877 false, pfx, dev->ifindex);
2878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 return 0;
2880 }
2881 }
2882 read_unlock_bh(&idev->lock);
2883 return -EADDRNOTAVAIL;
2884}
2885
2886
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002887int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888{
2889 struct in6_ifreq ireq;
2890 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002891
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002892 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002894
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2896 return -EFAULT;
2897
2898 rtnl_lock();
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002899 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002900 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2901 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 rtnl_unlock();
2903 return err;
2904}
2905
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002906int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
2908 struct in6_ifreq ireq;
2909 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002910
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002911 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 return -EPERM;
2913
2914 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2915 return -EFAULT;
2916
2917 rtnl_lock();
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002918 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002919 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 rtnl_unlock();
2921 return err;
2922}
2923
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002924static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2925 int plen, int scope)
2926{
2927 struct inet6_ifaddr *ifp;
2928
Jiri Benc8a226b22013-08-01 10:41:28 +02002929 ifp = ipv6_add_addr(idev, addr, NULL, plen,
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01002930 scope, IFA_F_PERMANENT,
2931 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002932 if (!IS_ERR(ifp)) {
2933 spin_lock_bh(&ifp->lock);
2934 ifp->flags &= ~IFA_F_TENTATIVE;
2935 spin_unlock_bh(&ifp->lock);
Paolo Abeni764d3be2016-11-22 16:57:40 +01002936 rt_genid_bump_ipv6(dev_net(idev->dev));
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002937 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2938 in6_ifa_put(ifp);
2939 }
2940}
2941
Amerigo Wang07a93622012-10-29 16:23:10 +00002942#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943static void sit_add_v4_addrs(struct inet6_dev *idev)
2944{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 struct in6_addr addr;
2946 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002947 struct net *net = dev_net(idev->dev);
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002948 int scope, plen;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002949 u32 pflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
2951 ASSERT_RTNL();
2952
2953 memset(&addr, 0, sizeof(struct in6_addr));
2954 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2955
2956 if (idev->dev->flags&IFF_POINTOPOINT) {
2957 addr.s6_addr32[0] = htonl(0xfe800000);
2958 scope = IFA_LINK;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002959 plen = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 } else {
2961 scope = IPV6_ADDR_COMPATv4;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002962 plen = 96;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002963 pflags |= RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 }
2965
2966 if (addr.s6_addr32[3]) {
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002967 add_addr(idev, &addr, plen, scope);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002968 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 return;
2970 }
2971
Benjamin Thery6fda7352008-03-05 10:47:47 -08002972 for_each_netdev(net, dev) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002973 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 if (in_dev && (dev->flags & IFF_UP)) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002975 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
2977 int flag = scope;
2978
2979 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
2981 addr.s6_addr32[3] = ifa->ifa_local;
2982
2983 if (ifa->ifa_scope == RT_SCOPE_LINK)
2984 continue;
2985 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2986 if (idev->dev->flags&IFF_POINTOPOINT)
2987 continue;
2988 flag |= IFA_HOST;
2989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002991 add_addr(idev, &addr, plen, flag);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002992 addrconf_prefix_route(&addr, plen, idev->dev, 0,
2993 pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 }
2995 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002998#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
3000static void init_loopback(struct net_device *dev)
3001{
3002 struct inet6_dev *idev;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303003 struct net_device *sp_dev;
3004 struct inet6_ifaddr *sp_ifa;
3005 struct rt6_info *sp_rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
3007 /* ::1 */
3008
3009 ASSERT_RTNL();
3010
Ian Morrise5d08d72014-11-23 21:28:43 +00003011 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003012 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003013 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 return;
3015 }
3016
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08003017 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303018
3019 /* Add routes to other interface's IPv6 addresses */
3020 for_each_netdev(dev_net(dev), sp_dev) {
3021 if (!strcmp(sp_dev->name, dev->name))
3022 continue;
3023
3024 idev = __in6_dev_get(sp_dev);
3025 if (!idev)
3026 continue;
3027
3028 read_lock_bh(&idev->lock);
3029 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
3030
3031 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
3032 continue;
3033
Gao feng33d99112014-01-24 16:29:11 +08003034 if (sp_ifa->rt) {
3035 /* This dst has been added to garbage list when
3036 * lo device down, release this obsolete dst and
3037 * reallocate a new router for ifa.
3038 */
Nicolas Dichtela2204452016-10-12 10:10:40 +02003039 if (!atomic_read(&sp_ifa->rt->rt6i_ref)) {
Gao feng33d99112014-01-24 16:29:11 +08003040 ip6_rt_put(sp_ifa->rt);
3041 sp_ifa->rt = NULL;
3042 } else {
3043 continue;
3044 }
3045 }
Gao fenga881ae12013-06-16 11:14:30 +08003046
François-Xavier Le Bail57ec0af2013-12-02 11:28:49 +01003047 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303048
3049 /* Failure cases are ignored */
Gao feng534c8772013-06-02 22:16:21 +00003050 if (!IS_ERR(sp_rt)) {
3051 sp_ifa->rt = sp_rt;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303052 ip6_ins_rt(sp_rt);
Gao feng534c8772013-06-02 22:16:21 +00003053 }
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05303054 }
3055 read_unlock_bh(&idev->lock);
3056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057}
3058
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003059void addrconf_add_linklocal(struct inet6_dev *idev,
3060 const struct in6_addr *addr, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003062 struct inet6_ifaddr *ifp;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003063 u32 addr_flags = flags | IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
Neil Horman95c385b2007-04-25 17:08:10 -07003065#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3066 if (idev->cnf.optimistic_dad &&
David Miller702beb82008-07-20 18:17:02 -07003067 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
Neil Horman95c385b2007-04-25 17:08:10 -07003068 addr_flags |= IFA_F_OPTIMISTIC;
3069#endif
3070
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01003071 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
3072 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003074 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
David S. Millercf22f9a2012-04-14 21:37:40 -04003075 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 in6_ifa_put(ifp);
3077 }
3078}
Alexander Aring2ad3ed52016-06-15 21:20:17 +02003079EXPORT_SYMBOL_GPL(addrconf_add_linklocal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003081static bool ipv6_reserved_interfaceid(struct in6_addr address)
3082{
3083 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
3084 return true;
3085
3086 if (address.s6_addr32[2] == htonl(0x02005eff) &&
3087 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
3088 return true;
3089
3090 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
3091 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
3092 return true;
3093
3094 return false;
3095}
3096
3097static int ipv6_generate_stable_address(struct in6_addr *address,
3098 u8 dad_count,
3099 const struct inet6_dev *idev)
3100{
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003101 static DEFINE_SPINLOCK(lock);
3102 static __u32 digest[SHA_DIGEST_WORDS];
3103 static __u32 workspace[SHA_WORKSPACE_WORDS];
3104
3105 static union {
3106 char __data[SHA_MESSAGE_BYTES];
3107 struct {
3108 struct in6_addr secret;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003109 __be32 prefix[2];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003110 unsigned char hwaddr[MAX_ADDR_LEN];
3111 u8 dad_count;
3112 } __packed;
3113 } data;
3114
3115 struct in6_addr secret;
3116 struct in6_addr temp;
3117 struct net *net = dev_net(idev->dev);
3118
3119 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
3120
3121 if (idev->cnf.stable_secret.initialized)
3122 secret = idev->cnf.stable_secret.secret;
3123 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
3124 secret = net->ipv6.devconf_dflt->stable_secret.secret;
3125 else
3126 return -1;
3127
3128retry:
3129 spin_lock_bh(&lock);
3130
3131 sha_init(digest);
3132 memset(&data, 0, sizeof(data));
3133 memset(workspace, 0, sizeof(workspace));
3134 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003135 data.prefix[0] = address->s6_addr32[0];
3136 data.prefix[1] = address->s6_addr32[1];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003137 data.secret = secret;
3138 data.dad_count = dad_count;
3139
3140 sha_transform(digest, data.__data, workspace);
3141
3142 temp = *address;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01003143 temp.s6_addr32[2] = (__force __be32)digest[0];
3144 temp.s6_addr32[3] = (__force __be32)digest[1];
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003145
3146 spin_unlock_bh(&lock);
3147
3148 if (ipv6_reserved_interfaceid(temp)) {
3149 dad_count++;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01003150 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003151 return -1;
3152 goto retry;
3153 }
3154
3155 *address = temp;
3156 return 0;
3157}
3158
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003159static void ipv6_gen_mode_random_init(struct inet6_dev *idev)
3160{
3161 struct ipv6_stable_secret *s = &idev->cnf.stable_secret;
3162
3163 if (s->initialized)
3164 return;
3165 s = &idev->cnf.stable_secret;
3166 get_random_bytes(&s->secret, sizeof(s->secret));
3167 s->initialized = true;
3168}
3169
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003170static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
3171{
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003172 struct in6_addr addr;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003173
David Ahernca254492015-10-12 11:47:10 -07003174 /* no link local addresses on L3 master devices */
3175 if (netif_is_l3_master(idev->dev))
3176 return;
3177
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003178 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
3179
Felix Jiad35a00b2017-01-26 16:59:17 +13003180 switch (idev->cnf.addr_gen_mode) {
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003181 case IN6_ADDR_GEN_MODE_RANDOM:
3182 ipv6_gen_mode_random_init(idev);
3183 /* fallthrough */
3184 case IN6_ADDR_GEN_MODE_STABLE_PRIVACY:
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003185 if (!ipv6_generate_stable_address(&addr, 0, idev))
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003186 addrconf_add_linklocal(idev, &addr,
3187 IFA_F_STABLE_PRIVACY);
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01003188 else if (prefix_route)
3189 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003190 break;
3191 case IN6_ADDR_GEN_MODE_EUI64:
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003192 /* addrconf_add_linklocal also adds a prefix_route and we
3193 * only need to care about prefix routes if ipv6_generate_eui64
3194 * couldn't generate one.
3195 */
3196 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003197 addrconf_add_linklocal(idev, &addr, 0);
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003198 else if (prefix_route)
3199 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003200 break;
3201 case IN6_ADDR_GEN_MODE_NONE:
3202 default:
3203 /* will not add any link local address */
3204 break;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003205 }
3206}
3207
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208static void addrconf_dev_config(struct net_device *dev)
3209{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003210 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
3212 ASSERT_RTNL();
3213
Herbert Xu74235a22007-06-14 13:02:55 -07003214 if ((dev->type != ARPHRD_ETHER) &&
3215 (dev->type != ARPHRD_FDDI) &&
Herbert Xu74235a22007-06-14 13:02:55 -07003216 (dev->type != ARPHRD_ARCNET) &&
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00003217 (dev->type != ARPHRD_INFINIBAND) &&
Nicolas Dichtele8377352013-08-20 12:16:06 +02003218 (dev->type != ARPHRD_IEEE1394) &&
Jukka Rissanene74bccb82013-12-11 17:05:36 +02003219 (dev->type != ARPHRD_TUNNEL6) &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003220 (dev->type != ARPHRD_6LOWPAN) &&
Felix Jia45ce0fd2017-01-26 16:59:18 +13003221 (dev->type != ARPHRD_IP6GRE) &&
3222 (dev->type != ARPHRD_IPGRE) &&
3223 (dev->type != ARPHRD_TUNNEL) &&
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003224 (dev->type != ARPHRD_NONE)) {
Herbert Xu74235a22007-06-14 13:02:55 -07003225 /* Alas, we support only Ethernet autoconfiguration. */
3226 return;
3227 }
3228
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00003230 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 return;
3232
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003233 /* this device type has no EUI support */
3234 if (dev->type == ARPHRD_NONE &&
Felix Jiad35a00b2017-01-26 16:59:17 +13003235 idev->cnf.addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64)
3236 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_RANDOM;
Bjørn Morkcc9da6c2015-12-16 16:44:38 +01003237
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003238 addrconf_addr_gen(idev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239}
3240
Amerigo Wang07a93622012-10-29 16:23:10 +00003241#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242static void addrconf_sit_config(struct net_device *dev)
3243{
3244 struct inet6_dev *idev;
3245
3246 ASSERT_RTNL();
3247
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003248 /*
3249 * Configure the tunnel with one of our IPv4
3250 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 * our v4 addrs in the tunnel
3252 */
3253
Ian Morrise5d08d72014-11-23 21:28:43 +00003254 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003255 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003256 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 return;
3258 }
3259
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003260 if (dev->priv_flags & IFF_ISATAP) {
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003261 addrconf_addr_gen(idev, false);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003262 return;
3263 }
3264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 sit_add_v4_addrs(idev);
3266
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00003267 if (dev->flags&IFF_POINTOPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003270#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
Amerigo Wang07a93622012-10-29 16:23:10 +00003272#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003273static void addrconf_gre_config(struct net_device *dev)
3274{
3275 struct inet6_dev *idev;
stephen hemmingeraee80b52011-06-08 10:44:30 +00003276
stephen hemmingeraee80b52011-06-08 10:44:30 +00003277 ASSERT_RTNL();
3278
Ian Morrise5d08d72014-11-23 21:28:43 +00003279 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003280 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003281 pr_debug("%s: add_dev failed\n", __func__);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003282 return;
3283 }
3284
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003285 addrconf_addr_gen(idev, true);
Hannes Frederic Sowad9e4ce62015-10-08 18:19:39 +02003286 if (dev->flags & IFF_POINTOPOINT)
3287 addrconf_add_mroute(dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003288}
3289#endif
3290
David Ahernf1705ec2016-02-24 09:25:37 -08003291static int fixup_permanent_addr(struct inet6_dev *idev,
3292 struct inet6_ifaddr *ifp)
3293{
3294 if (!ifp->rt) {
3295 struct rt6_info *rt;
3296
3297 rt = addrconf_dst_alloc(idev, &ifp->addr, false);
3298 if (unlikely(IS_ERR(rt)))
3299 return PTR_ERR(rt);
3300
3301 ifp->rt = rt;
3302 }
3303
3304 if (!(ifp->flags & IFA_F_NOPREFIXROUTE)) {
3305 addrconf_prefix_route(&ifp->addr, ifp->prefix_len,
3306 idev->dev, 0, 0);
3307 }
3308
3309 addrconf_dad_start(ifp);
3310
3311 return 0;
3312}
3313
3314static void addrconf_permanent_addr(struct net_device *dev)
3315{
3316 struct inet6_ifaddr *ifp, *tmp;
3317 struct inet6_dev *idev;
3318
3319 idev = __in6_dev_get(dev);
3320 if (!idev)
3321 return;
3322
3323 write_lock_bh(&idev->lock);
3324
3325 list_for_each_entry_safe(ifp, tmp, &idev->addr_list, if_list) {
3326 if ((ifp->flags & IFA_F_PERMANENT) &&
3327 fixup_permanent_addr(idev, ifp) < 0) {
3328 write_unlock_bh(&idev->lock);
3329 ipv6_del_addr(ifp);
3330 write_lock_bh(&idev->lock);
3331
3332 net_info_ratelimited("%s: Failed to add prefix route for address %pI6c; dropping\n",
3333 idev->dev->name, &ifp->addr);
3334 }
3335 }
3336
3337 write_unlock_bh(&idev->lock);
3338}
3339
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003340static int addrconf_notify(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00003341 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342{
Jiri Pirko351638e2013-05-28 01:30:21 +00003343 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
David Ahern4f7f34e2016-04-07 11:10:41 -07003344 struct netdev_notifier_changeupper_info *info;
Eric Dumazet748e2d92012-08-22 21:50:59 +00003345 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003346 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07003347 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003349 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003350 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07003351 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003352 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -07003353 if (IS_ERR(idev))
3354 return notifier_from_errno(PTR_ERR(idev));
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003355 }
3356 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003357
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003358 case NETDEV_CHANGEMTU:
3359 /* if MTU under IPV6_MIN_MTU stop IPv6 on this interface. */
3360 if (dev->mtu < IPV6_MIN_MTU) {
3361 addrconf_ifdown(dev, 1);
3362 break;
3363 }
3364
3365 if (idev) {
3366 rt6_mtu_change(dev, dev->mtu);
3367 idev->cnf.mtu6 = dev->mtu;
3368 break;
3369 }
3370
3371 /* allocate new idev */
3372 idev = ipv6_add_dev(dev);
3373 if (IS_ERR(idev))
3374 break;
3375
3376 /* device is still not ready */
3377 if (!(idev->if_flags & IF_READY))
3378 break;
3379
3380 run_pending = 1;
3381
3382 /* fall through */
3383
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003385 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07003386 if (dev->flags & IFF_SLAVE)
3387 break;
3388
WANG Cong3ce62a82014-09-11 15:07:16 -07003389 if (idev && idev->cnf.disable_ipv6)
3390 break;
3391
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003392 if (event == NETDEV_UP) {
David Ahern38bd10c2016-04-21 20:56:12 -07003393 /* restore routes for permanent addresses */
3394 addrconf_permanent_addr(dev);
3395
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07003396 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003397 /* device is not ready yet. */
Joe Perchesf3213832012-05-15 14:11:53 +00003398 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003399 dev->name);
3400 break;
3401 }
Kristian Slavov99081042006-02-08 16:10:53 -08003402
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11003403 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3404 idev = ipv6_add_dev(dev);
3405
WANG Conga317a2f2014-07-25 15:25:09 -07003406 if (!IS_ERR_OR_NULL(idev)) {
Kristian Slavov99081042006-02-08 16:10:53 -08003407 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08003408 run_pending = 1;
3409 }
Alexander Duyckb7b0b1d2015-10-26 11:06:33 -07003410 } else if (event == NETDEV_CHANGE) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07003411 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003412 /* device is still not ready. */
3413 break;
3414 }
3415
3416 if (idev) {
Linus Lüssinga088d1d2017-02-03 08:11:03 +01003417 if (idev->if_flags & IF_READY) {
3418 /* device is already configured -
3419 * but resend MLD reports, we might
3420 * have roamed and need to update
3421 * multicast snooping switches
3422 */
3423 ipv6_mc_up(idev);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003424 break;
Linus Lüssinga088d1d2017-02-03 08:11:03 +01003425 }
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003426 idev->if_flags |= IF_READY;
3427 }
3428
Joe Perchesf3213832012-05-15 14:11:53 +00003429 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3430 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003431
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003432 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003433 }
3434
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003435 switch (dev->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +00003436#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 case ARPHRD_SIT:
3438 addrconf_sit_config(dev);
3439 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07003440#endif
Amerigo Wang07a93622012-10-29 16:23:10 +00003441#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003442 case ARPHRD_IPGRE:
3443 addrconf_gre_config(dev);
3444 break;
3445#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 case ARPHRD_LOOPBACK:
3447 init_loopback(dev);
3448 break;
3449
3450 default:
3451 addrconf_dev_config(dev);
3452 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003453 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003454
WANG Conga317a2f2014-07-25 15:25:09 -07003455 if (!IS_ERR_OR_NULL(idev)) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003456 if (run_pending)
3457 addrconf_dad_run(idev);
3458
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003459 /*
3460 * If the MTU changed during the interface down,
3461 * when the interface up, the changed MTU must be
3462 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003464 if (idev->cnf.mtu6 != dev->mtu &&
3465 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 rt6_mtu_change(dev, dev->mtu);
3467 idev->cnf.mtu6 = dev->mtu;
3468 }
3469 idev->tstamp = jiffies;
3470 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003471
3472 /*
3473 * If the changed mtu during down is lower than
3474 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 */
3476 if (dev->mtu < IPV6_MIN_MTU)
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003477 addrconf_ifdown(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 }
3479 break;
3480
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 case NETDEV_DOWN:
3482 case NETDEV_UNREGISTER:
3483 /*
3484 * Remove all addresses from this interface.
3485 */
3486 addrconf_ifdown(dev, event != NETDEV_DOWN);
3487 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003488
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07003491 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003492 addrconf_sysctl_unregister(idev);
WANG Conga317a2f2014-07-25 15:25:09 -07003493 err = addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07003494 if (err)
3495 return notifier_from_errno(err);
WANG Conga317a2f2014-07-25 15:25:09 -07003496 err = snmp6_register_dev(idev);
3497 if (err) {
3498 addrconf_sysctl_unregister(idev);
3499 return notifier_from_errno(err);
3500 }
Stephen Hemminger5632c512007-04-28 21:16:39 -07003501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003503
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003504 case NETDEV_PRE_TYPE_CHANGE:
3505 case NETDEV_POST_TYPE_CHANGE:
Andrew Lunn3ef09522015-12-03 21:12:32 +01003506 if (idev)
3507 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07003508 break;
David Ahern4f7f34e2016-04-07 11:10:41 -07003509
3510 case NETDEV_CHANGEUPPER:
3511 info = ptr;
3512
3513 /* flush all routes if dev is linked to or unlinked from
3514 * an L3 master device (e.g., VRF)
3515 */
3516 if (info->upper_dev && netif_is_l3_master(info->upper_dev))
3517 addrconf_ifdown(dev, 0);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519
3520 return NOTIFY_OK;
3521}
3522
3523/*
3524 * addrconf module should be notified of a device going up
3525 */
3526static struct notifier_block ipv6_dev_notf = {
3527 .notifier_call = addrconf_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528};
3529
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003530static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07003531{
3532 struct inet6_dev *idev;
3533 ASSERT_RTNL();
3534
3535 idev = __in6_dev_get(dev);
3536
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003537 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003538 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003539 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003540 ipv6_mc_unmap(idev);
3541}
3542
David Ahern70af9212016-04-08 12:01:21 -07003543static bool addr_is_local(const struct in6_addr *addr)
3544{
3545 return ipv6_addr_type(addr) &
3546 (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
3547}
3548
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549static int addrconf_ifdown(struct net_device *dev, int how)
3550{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003551 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003552 struct inet6_dev *idev;
David Ahernf1705ec2016-02-24 09:25:37 -08003553 struct inet6_ifaddr *ifa, *tmp;
3554 struct list_head del_list;
3555 int _keep_addr;
3556 bool keep_addr;
David S. Miller73a8bd72011-01-23 23:27:15 -08003557 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
3559 ASSERT_RTNL();
3560
David S. Miller73a8bd72011-01-23 23:27:15 -08003561 rt6_ifdown(net, dev);
3562 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
3564 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003565 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 return -ENODEV;
3567
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003568 /*
3569 * Step 1: remove reference to ipv6 device from parent device.
3570 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003572 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003574
3575 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003576 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
3578 /* Step 1.5: remove snmp6 entry */
3579 snmp6_unregister_dev(idev);
3580
3581 }
3582
David Ahernf1705ec2016-02-24 09:25:37 -08003583 /* aggregate the system setting and interface setting */
3584 _keep_addr = net->ipv6.devconf_all->keep_addr_on_down;
3585 if (!_keep_addr)
3586 _keep_addr = idev->cnf.keep_addr_on_down;
3587
3588 /* combine the user config with event to determine if permanent
3589 * addresses are to be removed from address hash table
3590 */
Mike Manningbc561632016-08-12 12:02:38 +01003591 keep_addr = !(how || _keep_addr <= 0 || idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003592
David S. Miller73a8bd72011-01-23 23:27:15 -08003593 /* Step 2: clear hash table */
3594 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3595 struct hlist_head *h = &inet6_addr_lst[i];
David S. Miller73a8bd72011-01-23 23:27:15 -08003596
3597 spin_lock_bh(&addrconf_hash_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01003598restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08003599 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
David S. Miller73a8bd72011-01-23 23:27:15 -08003600 if (ifa->idev == idev) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003601 addrconf_del_dad_work(ifa);
David Ahernf1705ec2016-02-24 09:25:37 -08003602 /* combined flag + permanent flag decide if
3603 * address is retained on a down event
3604 */
3605 if (!keep_addr ||
David Ahern70af9212016-04-08 12:01:21 -07003606 !(ifa->flags & IFA_F_PERMANENT) ||
3607 addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003608 hlist_del_init_rcu(&ifa->addr_lst);
3609 goto restart;
3610 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003611 }
3612 }
3613 spin_unlock_bh(&addrconf_hash_lock);
3614 }
3615
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 write_lock_bh(&idev->lock);
3617
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003618 addrconf_del_rs_timer(idev);
3619
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003620 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003621 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003622 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003624 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00003625 while (!list_empty(&idev->tempaddr_list)) {
3626 ifa = list_first_entry(&idev->tempaddr_list,
3627 struct inet6_ifaddr, tmp_list);
3628 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 write_unlock_bh(&idev->lock);
3630 spin_lock_bh(&ifa->lock);
3631
3632 if (ifa->ifpub) {
3633 in6_ifa_put(ifa->ifpub);
3634 ifa->ifpub = NULL;
3635 }
3636 spin_unlock_bh(&ifa->lock);
3637 in6_ifa_put(ifa);
3638 write_lock_bh(&idev->lock);
3639 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003640
David Ahernf1705ec2016-02-24 09:25:37 -08003641 /* re-combine the user config with event to determine if permanent
3642 * addresses are to be removed from the interface list
3643 */
Mike Manningbc561632016-08-12 12:02:38 +01003644 keep_addr = (!how && _keep_addr > 0 && !idev->cnf.disable_ipv6);
David Ahernf1705ec2016-02-24 09:25:37 -08003645
3646 INIT_LIST_HEAD(&del_list);
3647 list_for_each_entry_safe(ifa, tmp, &idev->addr_list, if_list) {
David Ahern38bd10c2016-04-21 20:56:12 -07003648 struct rt6_info *rt = NULL;
3649
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003650 addrconf_del_dad_work(ifa);
stephen hemminger84e8b802010-03-02 13:32:46 +00003651
David S. Miller73a8bd72011-01-23 23:27:15 -08003652 write_unlock_bh(&idev->lock);
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003653 spin_lock_bh(&ifa->lock);
David Ahernf1705ec2016-02-24 09:25:37 -08003654
David Ahern70af9212016-04-08 12:01:21 -07003655 if (keep_addr && (ifa->flags & IFA_F_PERMANENT) &&
3656 !addr_is_local(&ifa->addr)) {
David Ahernf1705ec2016-02-24 09:25:37 -08003657 /* set state to skip the notifier below */
3658 state = INET6_IFADDR_STATE_DEAD;
3659 ifa->state = 0;
3660 if (!(ifa->flags & IFA_F_NODAD))
3661 ifa->flags |= IFA_F_TENTATIVE;
David Ahern38bd10c2016-04-21 20:56:12 -07003662
3663 rt = ifa->rt;
3664 ifa->rt = NULL;
David Ahernf1705ec2016-02-24 09:25:37 -08003665 } else {
3666 state = ifa->state;
3667 ifa->state = INET6_IFADDR_STATE_DEAD;
3668
Wei Yongjunc8822192016-07-28 16:19:03 +00003669 list_move(&ifa->if_list, &del_list);
David Ahernf1705ec2016-02-24 09:25:37 -08003670 }
3671
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003672 spin_unlock_bh(&ifa->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003673
David Ahern38bd10c2016-04-21 20:56:12 -07003674 if (rt)
3675 ip6_del_rt(rt);
3676
David S. Miller73a8bd72011-01-23 23:27:15 -08003677 if (state != INET6_IFADDR_STATE_DEAD) {
3678 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Cong Wangf88c91d2013-04-14 23:18:43 +08003679 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
Mike Manningea06f712016-07-22 18:32:11 +01003680 } else {
3681 if (idev->cnf.forwarding)
3682 addrconf_leave_anycast(ifa);
3683 addrconf_leave_solict(ifa->idev, &ifa->addr);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00003684 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003685
David S. Miller73a8bd72011-01-23 23:27:15 -08003686 write_lock_bh(&idev->lock);
3687 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003688
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 write_unlock_bh(&idev->lock);
3690
David Ahernf1705ec2016-02-24 09:25:37 -08003691 /* now clean up addresses to be removed */
3692 while (!list_empty(&del_list)) {
3693 ifa = list_first_entry(&del_list,
3694 struct inet6_ifaddr, if_list);
3695 list_del(&ifa->if_list);
3696
3697 in6_ifa_put(ifa);
3698 }
3699
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003700 /* Step 5: Discard anycast and multicast list */
3701 if (how) {
3702 ipv6_ac_destroy_dev(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 ipv6_mc_destroy_dev(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003704 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 ipv6_mc_down(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003709
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003710 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003711 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003712 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 neigh_parms_release(&nd_tbl, idev->nd_parms);
3714 neigh_ifdown(&nd_tbl, dev);
3715 in6_dev_put(idev);
3716 }
3717 return 0;
3718}
3719
3720static void addrconf_rs_timer(unsigned long data)
3721{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003722 struct inet6_dev *idev = (struct inet6_dev *)data;
Cong Wangcaf92bc2013-08-31 13:44:32 +08003723 struct net_device *dev = idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003724 struct in6_addr lladdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003726 write_lock(&idev->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00003727 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 goto out;
3729
Shmulik Ladkani9ba2add2012-12-02 01:44:53 +00003730 if (!ipv6_accept_ra(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003732
3733 /* Announcement received after solicitation was sent */
3734 if (idev->if_flags & IF_RA_RCVD)
3735 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003737 if (idev->rs_probes++ < idev->cnf.rtr_solicits || idev->cnf.rtr_solicits < 0) {
Cong Wangcaf92bc2013-08-31 13:44:32 +08003738 write_unlock(&idev->lock);
3739 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3740 ndisc_send_rs(dev, &lladdr,
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003741 &in6addr_linklocal_allrouters);
3742 else
Cong Wangcaf92bc2013-08-31 13:44:32 +08003743 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
Cong Wangcaf92bc2013-08-31 13:44:32 +08003745 write_lock(&idev->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003746 idev->rs_interval = rfc3315_s14_backoff_update(
3747 idev->rs_interval, idev->cnf.rtr_solicit_max_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003748 /* The wait after the last probe can be shorter */
3749 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3750 idev->cnf.rtr_solicits) ?
3751 idev->cnf.rtr_solicit_delay :
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07003752 idev->rs_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 /*
3755 * Note: we do not support deprecated "all on-link"
3756 * assumption any longer.
3757 */
Joe Perches91df42b2012-05-15 14:11:54 +00003758 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 }
3760
3761out:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003762 write_unlock(&idev->lock);
Cong Wangcaf92bc2013-08-31 13:44:32 +08003763put:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003764 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765}
3766
3767/*
3768 * Duplicate Address Detection
3769 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003770static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3771{
3772 unsigned long rand_num;
3773 struct inet6_dev *idev = ifp->idev;
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003774 u64 nonce;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003775
Neil Horman95c385b2007-04-25 17:08:10 -07003776 if (ifp->flags & IFA_F_OPTIMISTIC)
3777 rand_num = 0;
3778 else
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003779 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
Neil Horman95c385b2007-04-25 17:08:10 -07003780
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003781 nonce = 0;
3782 if (idev->cnf.enhanced_dad ||
3783 dev_net(idev->dev)->ipv6.devconf_all->enhanced_dad) {
3784 do
3785 get_random_bytes(&nonce, 6);
3786 while (nonce == 0);
3787 }
3788 ifp->dad_nonce = nonce;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003789 ifp->dad_probes = idev->cnf.dad_transmits;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003790 addrconf_mod_dad_work(ifp, rand_num);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003791}
3792
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003793static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794{
3795 struct inet6_dev *idev = ifp->idev;
3796 struct net_device *dev = idev->dev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003797 bool bump_id, notify = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
3799 addrconf_join_solict(dev, &ifp->addr);
3800
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003801 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
3803 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003804 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003805 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807
3808 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09003809 idev->cnf.accept_dad < 1 ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003810 !(ifp->flags&IFA_F_TENTATIVE) ||
3811 ifp->flags & IFA_F_NODAD) {
Paolo Abeni764d3be2016-11-22 16:57:40 +01003812 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003813 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003814 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 read_unlock_bh(&idev->lock);
3816
Paolo Abeni764d3be2016-11-22 16:57:40 +01003817 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 return;
3819 }
3820
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003821 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003822 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003823 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003824 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003825 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003826 * - keep it tentative if it is a permanent address.
3827 * - otherwise, kill it.
3828 */
3829 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003830 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003831 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003832 }
Neil Horman95c385b2007-04-25 17:08:10 -07003833
3834 /*
3835 * Optimistic nodes can start receiving
3836 * Frames right away
3837 */
Erik Kline7fd25612014-10-28 18:11:14 +09003838 if (ifp->flags & IFA_F_OPTIMISTIC) {
Neil Horman95c385b2007-04-25 17:08:10 -07003839 ip6_ins_rt(ifp->rt);
Erik Kline7fd25612014-10-28 18:11:14 +09003840 if (ipv6_use_optimistic_addr(idev)) {
3841 /* Because optimistic nodes can use this address,
3842 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3843 */
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003844 notify = true;
Erik Kline7fd25612014-10-28 18:11:14 +09003845 }
3846 }
Neil Horman95c385b2007-04-25 17:08:10 -07003847
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003848 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003850 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 read_unlock_bh(&idev->lock);
subashab@codeaurora.org16186a82016-02-02 02:11:10 +00003852 if (notify)
3853 ipv6_ifa_notify(RTM_NEWADDR, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854}
3855
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003856static void addrconf_dad_start(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003858 bool begin_dad = false;
3859
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003860 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003861 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3862 ifp->state = INET6_IFADDR_STATE_PREDAD;
3863 begin_dad = true;
3864 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003865 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003866
3867 if (begin_dad)
3868 addrconf_mod_dad_work(ifp, 0);
3869}
3870
3871static void addrconf_dad_work(struct work_struct *w)
3872{
3873 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3874 struct inet6_ifaddr,
3875 dad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 struct inet6_dev *idev = ifp->idev;
Paolo Abeni764d3be2016-11-22 16:57:40 +01003877 bool bump_id, disable_ipv6 = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 struct in6_addr mcaddr;
3879
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003880 enum {
3881 DAD_PROCESS,
3882 DAD_BEGIN,
3883 DAD_ABORT,
3884 } action = DAD_PROCESS;
3885
3886 rtnl_lock();
3887
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003888 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003889 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3890 action = DAD_BEGIN;
3891 ifp->state = INET6_IFADDR_STATE_DAD;
3892 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3893 action = DAD_ABORT;
3894 ifp->state = INET6_IFADDR_STATE_POSTDAD;
Mike Manning85b51b12016-08-18 14:39:40 +01003895
3896 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6 &&
3897 !(ifp->flags & IFA_F_STABLE_PRIVACY)) {
3898 struct in6_addr addr;
3899
3900 addr.s6_addr32[0] = htonl(0xfe800000);
3901 addr.s6_addr32[1] = 0;
3902
3903 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
3904 ipv6_addr_equal(&ifp->addr, &addr)) {
3905 /* DAD failed for link-local based on MAC */
3906 idev->cnf.disable_ipv6 = 1;
3907
3908 pr_info("%s: IPv6 being disabled!\n",
3909 ifp->idev->dev->name);
3910 disable_ipv6 = true;
3911 }
3912 }
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003913 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003914 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003915
3916 if (action == DAD_BEGIN) {
3917 addrconf_dad_begin(ifp);
3918 goto out;
3919 } else if (action == DAD_ABORT) {
Wei Yongjun751eb6b2016-09-05 16:06:31 +08003920 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003921 addrconf_dad_stop(ifp, 1);
Mike Manning85b51b12016-08-18 14:39:40 +01003922 if (disable_ipv6)
3923 addrconf_ifdown(idev->dev, 0);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003924 goto out;
3925 }
3926
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003927 if (!ifp->dad_probes && addrconf_dad_end(ifp))
Herbert Xuf2344a12010-05-18 15:55:27 -07003928 goto out;
3929
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003930 write_lock_bh(&idev->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003931 if (idev->dead || !(idev->if_flags & IF_READY)) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003932 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 goto out;
3934 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003935
3936 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003937 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3938 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003939 write_unlock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003940 goto out;
3941 }
3942
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003943 if (ifp->dad_probes == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 /*
3945 * DAD was successful
3946 */
3947
Paolo Abeni764d3be2016-11-22 16:57:40 +01003948 bump_id = ifp->flags & IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00003949 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003950 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003951 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952
Paolo Abeni764d3be2016-11-22 16:57:40 +01003953 addrconf_dad_completed(ifp, bump_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954
3955 goto out;
3956 }
3957
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003958 ifp->dad_probes--;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003959 addrconf_mod_dad_work(ifp,
3960 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
stephen hemminger21809fa2010-02-08 19:48:52 +00003961 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003962 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
3964 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
Erik Nordmarkadc176c2016-12-02 14:00:08 -08003966 ndisc_send_ns(ifp->idev->dev, &ifp->addr, &mcaddr, &in6addr_any,
3967 ifp->dad_nonce);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968out:
3969 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003970 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971}
3972
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003973/* ifp->idev must be at least read locked */
3974static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3975{
3976 struct inet6_ifaddr *ifpiter;
3977 struct inet6_dev *idev = ifp->idev;
3978
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01003979 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3980 if (ifpiter->scope > IFA_LINK)
3981 break;
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003982 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3983 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3984 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
3985 IFA_F_PERMANENT)
3986 return false;
3987 }
3988 return true;
3989}
3990
Paolo Abeni764d3be2016-11-22 16:57:40 +01003991static void addrconf_dad_completed(struct inet6_ifaddr *ifp, bool bump_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003993 struct net_device *dev = ifp->idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003994 struct in6_addr lladdr;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003995 bool send_rs, send_mld;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003996
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003997 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
3999 /*
4000 * Configure the address for reception. Now it is valid.
4001 */
4002
4003 ipv6_ifa_notify(RTM_NEWADDR, ifp);
4004
Tore Anderson026359b2011-08-28 23:47:33 +00004005 /* If added prefix is link local and we are prepared to process
4006 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 */
4008
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004009 read_lock_bh(&ifp->idev->lock);
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01004010 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004011 send_rs = send_mld &&
4012 ipv6_accept_ra(ifp->idev) &&
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004013 ifp->idev->cnf.rtr_solicits != 0 &&
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004014 (dev->flags&IFF_LOOPBACK) == 0;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004015 read_unlock_bh(&ifp->idev->lock);
4016
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02004017 /* While dad is in progress mld report's source address is in6_addrany.
4018 * Resend with proper ll now.
4019 */
4020 if (send_mld)
4021 ipv6_mc_dad_complete(ifp->idev);
4022
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004023 if (send_rs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 /*
4025 * If a host as already performed a random delay
4026 * [...] as part of DAD [...] there is no need
4027 * to delay again before sending the first RS
4028 */
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004029 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004030 return;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02004031 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004033 write_lock_bh(&ifp->idev->lock);
4034 spin_lock(&ifp->lock);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004035 ifp->idev->rs_interval = rfc3315_s14_backoff_init(
4036 ifp->idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004037 ifp->idev->rs_probes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 ifp->idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004039 addrconf_mod_rs_timer(ifp->idev, ifp->idev->rs_interval);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02004040 spin_unlock(&ifp->lock);
4041 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 }
Paolo Abeni764d3be2016-11-22 16:57:40 +01004043
4044 if (bump_id)
4045 rt_genid_bump_ipv6(dev_net(dev));
Marcus Huewea11a7f72017-02-06 18:34:56 +01004046
4047 /* Make sure that a new temporary address will be created
4048 * before this temporary address becomes deprecated.
4049 */
4050 if (ifp->flags & IFA_F_TEMPORARY)
4051 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052}
4053
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004054static void addrconf_dad_run(struct inet6_dev *idev)
4055{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004056 struct inet6_ifaddr *ifp;
4057
4058 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00004059 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00004060 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07004061 if (ifp->flags & IFA_F_TENTATIVE &&
4062 ifp->state == INET6_IFADDR_STATE_DAD)
4063 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00004064 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09004065 }
4066 read_unlock_bh(&idev->lock);
4067}
4068
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069#ifdef CONFIG_PROC_FS
4070struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08004071 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004073 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074};
4075
Mihai Maruseac1d578302012-01-03 23:31:35 +00004076static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077{
4078 struct inet6_ifaddr *ifa = NULL;
4079 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004080 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00004081 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
Mihai Maruseac1d578302012-01-03 23:31:35 +00004083 /* initial bucket if pos is 0 */
4084 if (pos == 0) {
4085 state->bucket = 0;
4086 state->offset = 0;
4087 }
4088
4089 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08004090 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00004091 addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004092 if (!net_eq(dev_net(ifa->idev->dev), net))
4093 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004094 /* sync with offset */
4095 if (p < state->offset) {
4096 p++;
4097 continue;
4098 }
4099 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004100 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004101 }
4102
4103 /* prepare for next bucket */
4104 state->offset = 0;
4105 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004107 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108}
4109
stephen hemmingerc2e21292010-03-17 20:31:10 +00004110static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
4111 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112{
4113 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09004114 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
Sasha Levinb67bfe02013-02-27 17:06:00 -08004116 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004117 if (!net_eq(dev_net(ifa->idev->dev), net))
4118 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004119 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004120 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004121 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00004122
4123 while (++state->bucket < IN6_ADDR_HSIZE) {
Mihai Maruseac1d578302012-01-03 23:31:35 +00004124 state->offset = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004125 hlist_for_each_entry_rcu_bh(ifa,
stephen hemmingerc2e21292010-03-17 20:31:10 +00004126 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004127 if (!net_eq(dev_net(ifa->idev->dev), net))
4128 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00004129 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00004130 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08004131 }
4132 }
4133
stephen hemmingerc2e21292010-03-17 20:31:10 +00004134 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135}
4136
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004138 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139{
stephen hemminger5c578aed2010-03-17 20:31:11 +00004140 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00004141 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142}
4143
4144static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4145{
4146 struct inet6_ifaddr *ifa;
4147
4148 ifa = if6_get_next(seq, v);
4149 ++*pos;
4150 return ifa;
4151}
4152
4153static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00004154 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155{
stephen hemminger5c578aed2010-03-17 20:31:11 +00004156 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157}
4158
4159static int if6_seq_show(struct seq_file *seq, void *v)
4160{
4161 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Jiri Pirko971a3512013-12-10 13:56:29 +01004162 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07004163 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 ifp->idev->dev->ifindex,
4165 ifp->prefix_len,
4166 ifp->scope,
Jiri Pirko971a3512013-12-10 13:56:29 +01004167 (u8) ifp->flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 ifp->idev->dev->name);
4169 return 0;
4170}
4171
Philippe De Muyter56b3d972007-07-10 23:07:31 -07004172static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 .start = if6_seq_start,
4174 .next = if6_seq_next,
4175 .show = if6_seq_show,
4176 .stop = if6_seq_stop,
4177};
4178
4179static int if6_seq_open(struct inode *inode, struct file *file)
4180{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004181 return seq_open_net(inode, file, &if6_seq_ops,
4182 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183}
4184
Arjan van de Ven9a321442007-02-12 00:55:35 -08004185static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 .owner = THIS_MODULE,
4187 .open = if6_seq_open,
4188 .read = seq_read,
4189 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004190 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191};
4192
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004193static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194{
Gao fengd4beaa62013-02-18 01:34:54 +00004195 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 return -ENOMEM;
4197 return 0;
4198}
4199
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004200static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08004201{
Gao fengece31ff2013-02-18 01:34:56 +00004202 remove_proc_entry("if_inet6", net->proc_net);
Daniel Lezcano3c400902008-01-10 22:42:49 -08004203}
4204
4205static struct pernet_operations if6_proc_net_ops = {
Ian Morris67ba4152014-08-24 21:53:10 +01004206 .init = if6_proc_net_init,
4207 .exit = if6_proc_net_exit,
Daniel Lezcano3c400902008-01-10 22:42:49 -08004208};
4209
4210int __init if6_proc_init(void)
4211{
4212 return register_pernet_subsys(&if6_proc_net_ops);
4213}
4214
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215void if6_proc_exit(void)
4216{
Daniel Lezcano3c400902008-01-10 22:42:49 -08004217 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218}
4219#endif /* CONFIG_PROC_FS */
4220
Amerigo Wang07a93622012-10-29 16:23:10 +00004221#if IS_ENABLED(CONFIG_IPV6_MIP6)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004222/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00004223int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004224{
4225 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00004226 struct inet6_ifaddr *ifp = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00004227 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004228
stephen hemminger5c578aed2010-03-17 20:31:11 +00004229 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08004230 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09004231 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08004232 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09004233 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004234 (ifp->flags & IFA_F_HOMEADDRESS)) {
4235 ret = 1;
4236 break;
4237 }
4238 }
stephen hemminger5c578aed2010-03-17 20:31:11 +00004239 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07004240 return ret;
4241}
4242#endif
4243
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244/*
4245 * Periodic address status verification
4246 */
4247
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004248static void addrconf_verify_rtnl(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004250 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 int i;
4253
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004254 ASSERT_RTNL();
4255
stephen hemminger5c578aed2010-03-17 20:31:11 +00004256 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004258 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004260 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004262 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263restart:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004264 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
Yasushi Asanofad8da32013-12-31 12:04:19 +09004267 /* When setting preferred_lft to a value not zero or
4268 * infinity, while valid_lft is infinity
4269 * IFA_F_PERMANENT has a non-infinity life time.
4270 */
4271 if ((ifp->flags & IFA_F_PERMANENT) &&
4272 (ifp->prefered_lft == INFINITY_LIFE_TIME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 continue;
4274
4275 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004276 /* We try to batch several events at once. */
4277 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004279 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
4280 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 spin_unlock(&ifp->lock);
4282 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 ipv6_del_addr(ifp);
4284 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09004285 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
4286 spin_unlock(&ifp->lock);
4287 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00004289 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 int deprecate = 0;
4291
4292 if (!(ifp->flags&IFA_F_DEPRECATED)) {
4293 deprecate = 1;
4294 ifp->flags |= IFA_F_DEPRECATED;
4295 }
4296
Yasushi Asanofad8da32013-12-31 12:04:19 +09004297 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
4298 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 next = ifp->tstamp + ifp->valid_lft * HZ;
4300
4301 spin_unlock(&ifp->lock);
4302
4303 if (deprecate) {
4304 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
4306 ipv6_ifa_notify(0, ifp);
4307 in6_ifa_put(ifp);
4308 goto restart;
4309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
4311 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00004312 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
4313 ifp->idev->cnf.dad_transmits *
Jiri Pirko1f9248e2013-12-07 19:26:53 +01004314 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
stephen hemminger88949cf2010-03-17 20:31:17 +00004315
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 if (age >= ifp->prefered_lft - regen_advance) {
4317 struct inet6_ifaddr *ifpub = ifp->ifpub;
4318 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4319 next = ifp->tstamp + ifp->prefered_lft * HZ;
4320 if (!ifp->regen_count && ifpub) {
4321 ifp->regen_count++;
4322 in6_ifa_hold(ifp);
4323 in6_ifa_hold(ifpub);
4324 spin_unlock(&ifp->lock);
stephen hemminger5c578aed2010-03-17 20:31:11 +00004325
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08004326 spin_lock(&ifpub->lock);
4327 ifpub->regen_count = 0;
4328 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 ipv6_create_tempaddr(ifpub, ifp);
4330 in6_ifa_put(ifpub);
4331 in6_ifa_put(ifp);
4332 goto restart;
4333 }
4334 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
4335 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
4336 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 } else {
4338 /* ifp->prefered_lft <= ifp->valid_lft */
4339 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
4340 next = ifp->tstamp + ifp->prefered_lft * HZ;
4341 spin_unlock(&ifp->lock);
4342 }
4343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 }
4345
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07004346 next_sec = round_jiffies_up(next);
4347 next_sched = next;
4348
4349 /* If rounded timeout is accurate enough, accept it. */
4350 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
4351 next_sched = next_sec;
4352
4353 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
4354 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
4355 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
4356
dingtianhongba3542e2013-08-17 10:27:04 +08004357 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
4358 now, next, next_sec, next_sched);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004359 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
stephen hemminger5c578aed2010-03-17 20:31:11 +00004360 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361}
4362
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004363static void addrconf_verify_work(struct work_struct *w)
4364{
4365 rtnl_lock();
4366 addrconf_verify_rtnl();
4367 rtnl_unlock();
4368}
4369
4370static void addrconf_verify(void)
4371{
4372 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
4373}
4374
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004375static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
4376 struct in6_addr **peer_pfx)
Thomas Graf461d8832006-09-18 00:09:49 -07004377{
4378 struct in6_addr *pfx = NULL;
4379
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004380 *peer_pfx = NULL;
4381
Thomas Graf461d8832006-09-18 00:09:49 -07004382 if (addr)
4383 pfx = nla_data(addr);
4384
4385 if (local) {
4386 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004387 *peer_pfx = pfx;
4388 pfx = nla_data(local);
Thomas Graf461d8832006-09-18 00:09:49 -07004389 }
4390
4391 return pfx;
4392}
4393
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004394static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07004395 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4396 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4397 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
Jiri Pirko479840f2013-12-06 09:45:21 +01004398 [IFA_FLAGS] = { .len = sizeof(u32) },
Thomas Graf461d8832006-09-18 00:09:49 -07004399};
4400
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401static int
Thomas Graf661d2962013-03-21 07:45:29 +00004402inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004404 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07004405 struct ifaddrmsg *ifm;
4406 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004407 struct in6_addr *pfx, *peer_pfx;
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004408 u32 ifa_flags;
Thomas Grafb933f712006-09-18 00:10:19 -07004409 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
Thomas Grafb933f712006-09-18 00:10:19 -07004411 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4412 if (err < 0)
4413 return err;
4414
4415 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004416 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004417 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 return -EINVAL;
4419
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004420 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4421
4422 /* We ignore other flags so far. */
4423 ifa_flags &= IFA_F_MANAGETEMPADDR;
4424
4425 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4426 ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427}
4428
Jiri Pirko479840f2013-12-06 09:45:21 +01004429static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004430 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004431{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004432 u32 flags;
4433 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004434 unsigned long timeout;
Jiri Pirko53bd6742013-12-06 09:45:22 +01004435 bool was_managetempaddr;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004436 bool had_prefixroute;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09004437
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004438 ASSERT_RTNL();
4439
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004440 if (!valid_lft || (prefered_lft > valid_lft))
4441 return -EINVAL;
4442
Jiri Pirko53bd6742013-12-06 09:45:22 +01004443 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4444 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4445 return -EINVAL;
4446
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004447 timeout = addrconf_timeout_fixup(valid_lft, HZ);
4448 if (addrconf_finite_timeout(timeout)) {
4449 expires = jiffies_to_clock_t(timeout * HZ);
4450 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004451 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004452 } else {
4453 expires = 0;
4454 flags = 0;
4455 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004456 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004457
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004458 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4459 if (addrconf_finite_timeout(timeout)) {
4460 if (timeout == 0)
4461 ifa_flags |= IFA_F_DEPRECATED;
4462 prefered_lft = timeout;
4463 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004464
4465 spin_lock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004466 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004467 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4468 !(ifp->flags & IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004469 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
Thomas Haller761aac72014-01-15 15:36:58 +01004470 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4471 IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004472 ifp->flags |= ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004473 ifp->tstamp = jiffies;
4474 ifp->valid_lft = valid_lft;
4475 ifp->prefered_lft = prefered_lft;
4476
4477 spin_unlock_bh(&ifp->lock);
4478 if (!(ifp->flags&IFA_F_TENTATIVE))
4479 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004480
Thomas Haller761aac72014-01-15 15:36:58 +01004481 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4482 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4483 expires, flags);
Thomas Haller5b84efe2014-01-15 15:36:59 +01004484 } else if (had_prefixroute) {
4485 enum cleanup_prefix_rt_t action;
4486 unsigned long rt_expires;
4487
4488 write_lock_bh(&ifp->idev->lock);
4489 action = check_cleanup_prefix_route(ifp, &rt_expires);
4490 write_unlock_bh(&ifp->idev->lock);
4491
4492 if (action != CLEANUP_PREFIX_RT_NOP) {
4493 cleanup_prefix_route(ifp, rt_expires,
4494 action == CLEANUP_PREFIX_RT_DEL);
4495 }
Thomas Haller761aac72014-01-15 15:36:58 +01004496 }
Jiri Pirko53bd6742013-12-06 09:45:22 +01004497
4498 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4499 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4500 valid_lft = prefered_lft = 0;
4501 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4502 !was_managetempaddr, jiffies);
4503 }
4504
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004505 addrconf_verify_rtnl();
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004506
4507 return 0;
4508}
4509
4510static int
Thomas Graf661d2962013-03-21 07:45:29 +00004511inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004513 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07004514 struct ifaddrmsg *ifm;
4515 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004516 struct in6_addr *pfx, *peer_pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07004517 struct inet6_ifaddr *ifa;
4518 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004519 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
Jiri Pirko479840f2013-12-06 09:45:21 +01004520 u32 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07004521 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522
Thomas Graf461d8832006-09-18 00:09:49 -07004523 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4524 if (err < 0)
4525 return err;
4526
4527 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004528 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004529 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 return -EINVAL;
4531
Thomas Graf461d8832006-09-18 00:09:49 -07004532 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004533 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07004534
4535 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004536 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07004537 preferred_lft = ci->ifa_prefered;
4538 } else {
4539 preferred_lft = INFINITY_LIFE_TIME;
4540 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004541 }
4542
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004543 dev = __dev_get_by_index(net, ifm->ifa_index);
Ian Morris63159f22015-03-29 14:00:04 +01004544 if (!dev)
Thomas Graf7198f8c2006-09-18 00:13:46 -07004545 return -ENODEV;
4546
Jiri Pirko479840f2013-12-06 09:45:21 +01004547 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4548
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004549 /* We ignore other flags so far. */
Thomas Haller761aac72014-01-15 15:36:58 +01004550 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
Madhu Challa93a714d2015-02-25 09:58:35 -08004551 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004552
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08004553 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Ian Morris63159f22015-03-29 14:00:04 +01004554 if (!ifa) {
Thomas Graf7198f8c2006-09-18 00:13:46 -07004555 /*
4556 * It would be best to check for !NLM_F_CREATE here but
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08004557 * userspace already relies on not having to provide this.
Thomas Graf7198f8c2006-09-18 00:13:46 -07004558 */
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004559 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004560 ifm->ifa_prefixlen, ifa_flags,
4561 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004562 }
4563
Thomas Graf7198f8c2006-09-18 00:13:46 -07004564 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4565 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4566 err = -EEXIST;
4567 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004568 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07004569
4570 in6_ifa_put(ifa);
4571
4572 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573}
4574
Jiri Pirko479840f2013-12-06 09:45:21 +01004575static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
Thomas Graf101bb222006-09-18 00:11:52 -07004576 u8 scope, int ifindex)
4577{
4578 struct ifaddrmsg *ifm;
4579
4580 ifm = nlmsg_data(nlh);
4581 ifm->ifa_family = AF_INET6;
4582 ifm->ifa_prefixlen = prefixlen;
4583 ifm->ifa_flags = flags;
4584 ifm->ifa_scope = scope;
4585 ifm->ifa_index = ifindex;
4586}
4587
Thomas Graf85486af2006-09-18 00:11:24 -07004588static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4589 unsigned long tstamp, u32 preferred, u32 valid)
4590{
4591 struct ifa_cacheinfo ci;
4592
Thomas Graf18a31e12010-11-17 04:12:02 +00004593 ci.cstamp = cstamp_delta(cstamp);
4594 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07004595 ci.ifa_prefered = preferred;
4596 ci.ifa_valid = valid;
4597
4598 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4599}
4600
Thomas Graf101bb222006-09-18 00:11:52 -07004601static inline int rt_scope(int ifa_scope)
4602{
4603 if (ifa_scope & IFA_HOST)
4604 return RT_SCOPE_HOST;
4605 else if (ifa_scope & IFA_LINK)
4606 return RT_SCOPE_LINK;
4607 else if (ifa_scope & IFA_SITE)
4608 return RT_SCOPE_SITE;
4609 else
4610 return RT_SCOPE_UNIVERSE;
4611}
4612
Thomas Graf0ab68032006-09-18 00:12:35 -07004613static inline int inet6_ifaddr_msgsize(void)
4614{
Thomas Graf339bf982006-11-10 14:10:15 -08004615 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004616 + nla_total_size(16) /* IFA_LOCAL */
Thomas Graf339bf982006-11-10 14:10:15 -08004617 + nla_total_size(16) /* IFA_ADDRESS */
Jiri Pirko479840f2013-12-06 09:45:21 +01004618 + nla_total_size(sizeof(struct ifa_cacheinfo))
4619 + nla_total_size(4) /* IFA_FLAGS */;
Thomas Graf0ab68032006-09-18 00:12:35 -07004620}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004621
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004623 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07004626 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627
Eric W. Biederman15e47302012-09-07 20:12:54 +00004628 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004629 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004630 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004631
Thomas Graf101bb222006-09-18 00:11:52 -07004632 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4633 ifa->idev->dev->ifindex);
4634
Yasushi Asanofad8da32013-12-31 12:04:19 +09004635 if (!((ifa->flags&IFA_F_PERMANENT) &&
4636 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
Thomas Graf85486af2006-09-18 00:11:24 -07004637 preferred = ifa->prefered_lft;
4638 valid = ifa->valid_lft;
4639 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00004641 if (preferred > tval)
4642 preferred -= tval;
4643 else
4644 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00004645 if (valid != INFINITY_LIFE_TIME) {
4646 if (valid > tval)
4647 valid -= tval;
4648 else
4649 valid = 0;
4650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 }
4652 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07004653 preferred = INFINITY_LIFE_TIME;
4654 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 }
Thomas Graf85486af2006-09-18 00:11:24 -07004656
Cong Wang7996c792013-05-22 05:41:06 +00004657 if (!ipv6_addr_any(&ifa->peer_addr)) {
Jiri Benc930345e2015-03-29 16:59:25 +02004658 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4659 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004660 goto error;
4661 } else
Jiri Benc930345e2015-03-29 16:59:25 +02004662 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004663 goto error;
4664
4665 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4666 goto error;
Thomas Graf85486af2006-09-18 00:11:24 -07004667
Jiri Pirko479840f2013-12-06 09:45:21 +01004668 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4669 goto error;
4670
Johannes Berg053c0952015-01-16 22:09:00 +01004671 nlmsg_end(skb, nlh);
4672 return 0;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004673
4674error:
4675 nlmsg_cancel(skb, nlh);
4676 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677}
4678
4679static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004680 u32 portid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004683 u8 scope = RT_SCOPE_UNIVERSE;
4684 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
Thomas Graf101bb222006-09-18 00:11:52 -07004686 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4687 scope = RT_SCOPE_SITE;
4688
Eric W. Biederman15e47302012-09-07 20:12:54 +00004689 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004690 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004691 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004692
Thomas Graf101bb222006-09-18 00:11:52 -07004693 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004694 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004695 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004696 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4697 nlmsg_cancel(skb, nlh);
4698 return -EMSGSIZE;
4699 }
Thomas Graf85486af2006-09-18 00:11:24 -07004700
Johannes Berg053c0952015-01-16 22:09:00 +01004701 nlmsg_end(skb, nlh);
4702 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703}
4704
4705static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004706 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004709 u8 scope = RT_SCOPE_UNIVERSE;
4710 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711
Thomas Graf101bb222006-09-18 00:11:52 -07004712 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4713 scope = RT_SCOPE_SITE;
4714
Eric W. Biederman15e47302012-09-07 20:12:54 +00004715 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004716 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004717 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004718
Thomas Graf101bb222006-09-18 00:11:52 -07004719 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004720 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004721 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004722 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4723 nlmsg_cancel(skb, nlh);
4724 return -EMSGSIZE;
4725 }
Thomas Graf85486af2006-09-18 00:11:24 -07004726
Johannes Berg053c0952015-01-16 22:09:00 +01004727 nlmsg_end(skb, nlh);
4728 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729}
4730
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004731enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 UNICAST_ADDR,
4733 MULTICAST_ADDR,
4734 ANYCAST_ADDR,
4735};
4736
Eric Dumazet234b27c2009-11-12 04:11:50 +00004737/* called with rcu_read_lock() */
4738static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4739 struct netlink_callback *cb, enum addr_type_t type,
4740 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 struct ifmcaddr6 *ifmca;
4743 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004744 int err = 1;
4745 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746
Eric Dumazet234b27c2009-11-12 04:11:50 +00004747 read_lock_bh(&idev->lock);
4748 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00004749 case UNICAST_ADDR: {
4750 struct inet6_ifaddr *ifa;
4751
Eric Dumazet234b27c2009-11-12 04:11:50 +00004752 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00004753 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4754 if (++ip_idx < s_ip_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004755 continue;
4756 err = inet6_fill_ifaddr(skb, ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004757 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004758 cb->nlh->nlmsg_seq,
4759 RTM_NEWADDR,
4760 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004761 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004762 break;
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004763 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004765 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00004766 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004767 case MULTICAST_ADDR:
4768 /* multicast address */
4769 for (ifmca = idev->mc_list; ifmca;
4770 ifmca = ifmca->next, ip_idx++) {
4771 if (ip_idx < s_ip_idx)
4772 continue;
4773 err = inet6_fill_ifmcaddr(skb, ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004774 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004775 cb->nlh->nlmsg_seq,
4776 RTM_GETMULTICAST,
4777 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004778 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004779 break;
4780 }
4781 break;
4782 case ANYCAST_ADDR:
4783 /* anycast address */
4784 for (ifaca = idev->ac_list; ifaca;
4785 ifaca = ifaca->aca_next, ip_idx++) {
4786 if (ip_idx < s_ip_idx)
4787 continue;
4788 err = inet6_fill_ifacaddr(skb, ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004789 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004790 cb->nlh->nlmsg_seq,
4791 RTM_GETANYCAST,
4792 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004793 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004794 break;
4795 }
4796 break;
4797 default:
4798 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004800 read_unlock_bh(&idev->lock);
4801 *p_ip_idx = ip_idx;
4802 return err;
4803}
4804
4805static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4806 enum addr_type_t type)
4807{
4808 struct net *net = sock_net(skb->sk);
4809 int h, s_h;
4810 int idx, ip_idx;
4811 int s_idx, s_ip_idx;
4812 struct net_device *dev;
4813 struct inet6_dev *idev;
4814 struct hlist_head *head;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004815
4816 s_h = cb->args[0];
4817 s_idx = idx = cb->args[1];
4818 s_ip_idx = ip_idx = cb->args[2];
4819
4820 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004821 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004822 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4823 idx = 0;
4824 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08004825 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet234b27c2009-11-12 04:11:50 +00004826 if (idx < s_idx)
4827 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07004828 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004829 s_ip_idx = 0;
4830 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004831 idev = __in6_dev_get(dev);
4832 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004833 goto cont;
4834
4835 if (in6_dump_addrs(idev, skb, cb, type,
David S. Miller7b46a642015-01-18 23:36:08 -05004836 s_ip_idx, &ip_idx) < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004837 goto done;
4838cont:
4839 idx++;
4840 }
4841 }
4842done:
4843 rcu_read_unlock();
4844 cb->args[0] = h;
4845 cb->args[1] = idx;
4846 cb->args[2] = ip_idx;
4847
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 return skb->len;
4849}
4850
4851static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4852{
4853 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004854
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 return inet6_dump_addr(skb, cb, type);
4856}
4857
4858static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4859{
4860 enum addr_type_t type = MULTICAST_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
4865
4866static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4867{
4868 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004869
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 return inet6_dump_addr(skb, cb, type);
4871}
4872
Thomas Graf661d2962013-03-21 07:45:29 +00004873static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004874{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004875 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004876 struct ifaddrmsg *ifm;
4877 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004878 struct in6_addr *addr = NULL, *peer;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004879 struct net_device *dev = NULL;
4880 struct inet6_ifaddr *ifa;
4881 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004882 int err;
4883
Thomas Graf1b29fc22006-09-18 00:10:50 -07004884 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4885 if (err < 0)
4886 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004887
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004888 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
Ian Morris63159f22015-03-29 14:00:04 +01004889 if (!addr) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004890 err = -EINVAL;
4891 goto errout;
4892 }
4893
4894 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004895 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004896 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004897
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004898 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4899 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004900 err = -EADDRNOTAVAIL;
4901 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004902 }
4903
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004904 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4905 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004906 err = -ENOBUFS;
4907 goto errout_ifa;
4908 }
4909
Eric W. Biederman15e47302012-09-07 20:12:54 +00004910 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004911 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004912 if (err < 0) {
4913 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4914 WARN_ON(err == -EMSGSIZE);
4915 kfree_skb(skb);
4916 goto errout_ifa;
4917 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00004918 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004919errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004920 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004921errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004922 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004923}
4924
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4926{
4927 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004928 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07004929 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930
Mahesh Bandewarf784ad32017-01-04 15:01:01 -08004931 /* Don't send DELADDR notification for TENTATIVE address,
4932 * since NEWADDR notification is sent only after removing
4933 * TENTATIVE flag.
4934 */
4935 if (ifa->flags & IFA_F_TENTATIVE && event == RTM_DELADDR)
4936 return;
4937
Thomas Graf0ab68032006-09-18 00:12:35 -07004938 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01004939 if (!skb)
Thomas Graf5d620262006-08-15 00:35:02 -07004940 goto errout;
4941
4942 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004943 if (err < 0) {
4944 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4945 WARN_ON(err == -EMSGSIZE);
4946 kfree_skb(skb);
4947 goto errout;
4948 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004949 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4950 return;
Thomas Graf5d620262006-08-15 00:35:02 -07004951errout:
4952 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004953 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954}
4955
Dave Jonesb6f99a22007-03-22 12:27:49 -07004956static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 __s32 *array, int bytes)
4958{
Thomas Graf04561c12006-11-14 19:53:58 -08004959 BUG_ON(bytes < (DEVCONF_MAX * 4));
4960
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 memset(array, 0, bytes);
4962 array[DEVCONF_FORWARDING] = cnf->forwarding;
4963 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4964 array[DEVCONF_MTU6] = cnf->mtu6;
4965 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4966 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4967 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4968 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4969 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00004970 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4971 jiffies_to_msecs(cnf->rtr_solicit_interval);
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07004972 array[DEVCONF_RTR_SOLICIT_MAX_INTERVAL] =
4973 jiffies_to_msecs(cnf->rtr_solicit_max_interval);
Thomas Graf93908d12010-11-17 01:44:24 +00004974 array[DEVCONF_RTR_SOLICIT_DELAY] =
4975 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02004977 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4978 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4979 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4980 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4982 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4983 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4984 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4985 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004987 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
Hangbin Liu8013d1d2015-07-30 14:28:42 +08004988 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004989 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004990#ifdef CONFIG_IPV6_ROUTER_PREF
4991 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00004992 array[DEVCONF_RTR_PROBE_INTERVAL] =
4993 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08004994#ifdef CONFIG_IPV6_ROUTE_INFO
Joel Scherpelzbbea1242017-03-22 18:19:04 +09004995 array[DEVCONF_ACCEPT_RA_RT_INFO_MIN_PLEN] = cnf->accept_ra_rt_info_min_plen;
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004996 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4997#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004998#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004999 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07005000 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07005001#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5002 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
Erik Kline7fd25612014-10-28 18:11:14 +09005003 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
Neil Horman95c385b2007-04-25 17:08:10 -07005004#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005005#ifdef CONFIG_IPV6_MROUTE
5006 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
5007#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09005008 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09005009 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00005010 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00005011 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02005012 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
Ben Greeard9333192014-06-25 14:44:53 -07005013 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
Harout Hedeshianc2943f12015-01-20 10:06:05 -07005014 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
Andy Gospodarek35103d12015-08-13 10:39:01 -04005015 array[DEVCONF_IGNORE_ROUTES_WITH_LINKDOWN] = cnf->ignore_routes_with_linkdown;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005016 /* we omit DEVCONF_STABLE_SECRET for now */
Erik Kline3985e8a2015-07-22 16:38:25 +09005017 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
Johannes Bergabbc3042016-02-04 13:31:19 +01005018 array[DEVCONF_DROP_UNICAST_IN_L2_MULTICAST] = cnf->drop_unicast_in_l2_multicast;
Johannes Berg7a02bf82016-02-04 13:31:20 +01005019 array[DEVCONF_DROP_UNSOLICITED_NA] = cnf->drop_unsolicited_na;
David Ahernf1705ec2016-02-24 09:25:37 -08005020 array[DEVCONF_KEEP_ADDR_ON_DOWN] = cnf->keep_addr_on_down;
David Lebrun1ababeb2016-11-08 14:57:39 +01005021 array[DEVCONF_SEG6_ENABLED] = cnf->seg6_enabled;
David Lebrunbf355b82016-11-08 14:57:42 +01005022#ifdef CONFIG_IPV6_SEG6_HMAC
5023 array[DEVCONF_SEG6_REQUIRE_HMAC] = cnf->seg6_require_hmac;
5024#endif
Erik Nordmarkadc176c2016-12-02 14:00:08 -08005025 array[DEVCONF_ENHANCED_DAD] = cnf->enhanced_dad;
Felix Jiad35a00b2017-01-26 16:59:17 +13005026 array[DEVCONF_ADDR_GEN_MODE] = cnf->addr_gen_mode;
David Forsterdf789fe2017-02-23 16:27:18 +00005027 array[DEVCONF_DISABLE_POLICY] = cnf->disable_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028}
5029
Thomas Grafb382b192010-11-16 04:33:57 +00005030static inline size_t inet6_ifla6_size(void)
5031{
5032 return nla_total_size(4) /* IFLA_INET6_FLAGS */
5033 + nla_total_size(sizeof(struct ifla_cacheinfo))
5034 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
5035 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005036 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
5037 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
Thomas Grafb382b192010-11-16 04:33:57 +00005038}
5039
Thomas Graf339bf982006-11-10 14:10:15 -08005040static inline size_t inet6_if_nlmsg_size(void)
5041{
5042 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
5043 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
5044 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
5045 + nla_total_size(4) /* IFLA_MTU */
5046 + nla_total_size(4) /* IFLA_LINK */
Andy Gospodarek03443382015-08-13 15:26:35 -04005047 + nla_total_size(1) /* IFLA_OPERSTATE */
Thomas Grafb382b192010-11-16 04:33:57 +00005048 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08005049}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005050
Eric Dumazetbe281e52011-05-19 01:14:23 +00005051static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Jia Heaca05672016-09-30 11:29:03 +08005052 int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005053{
5054 int i;
Jia Heaca05672016-09-30 11:29:03 +08005055 int pad = bytes - sizeof(u64) * ICMP6_MIB_MAX;
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005056 BUG_ON(pad < 0);
5057
5058 /* Use put_unaligned() because stats may not be aligned for u64. */
Jia Heaca05672016-09-30 11:29:03 +08005059 put_unaligned(ICMP6_MIB_MAX, &stats[0]);
5060 for (i = 1; i < ICMP6_MIB_MAX; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00005061 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005062
Jia Heaca05672016-09-30 11:29:03 +08005063 memset(&stats[ICMP6_MIB_MAX], 0, pad);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005064}
5065
WANG Cong698365f2014-05-05 15:55:55 -07005066static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305067 int bytes, size_t syncpoff)
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005068{
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305069 int i, c;
5070 u64 buff[IPSTATS_MIB_MAX];
5071 int pad = bytes - sizeof(u64) * IPSTATS_MIB_MAX;
5072
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005073 BUG_ON(pad < 0);
5074
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305075 memset(buff, 0, sizeof(buff));
5076 buff[0] = IPSTATS_MIB_MAX;
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005077
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305078 for_each_possible_cpu(c) {
5079 for (i = 1; i < IPSTATS_MIB_MAX; i++)
5080 buff[i] += snmp_get_cpu_field64(mib, c, i, syncpoff);
5081 }
5082
5083 memcpy(stats, buff, IPSTATS_MIB_MAX * sizeof(u64));
5084 memset(&stats[IPSTATS_MIB_MAX], 0, pad);
Eric Dumazet4ce3c182010-06-30 13:31:19 -07005085}
5086
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005087static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
5088 int bytes)
5089{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005090 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005091 case IFLA_INET6_STATS:
Raghavendra K Ta3a77372015-08-30 11:29:42 +05305092 __snmp6_fill_stats64(stats, idev->stats.ipv6, bytes,
5093 offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005094 break;
5095 case IFLA_INET6_ICMP6STATS:
Jia Heaca05672016-09-30 11:29:03 +08005096 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07005097 break;
5098 }
5099}
5100
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005101static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev,
5102 u32 ext_filter_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005104 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08005105 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106
David S. Millerc78679e2012-04-01 20:27:33 -04005107 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
5108 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08005110 ci.tstamp = cstamp_delta(idev->tstamp);
5111 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
Jiri Pirko1f9248e2013-12-07 19:26:53 +01005112 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
David S. Millerc78679e2012-04-01 20:27:33 -04005113 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
5114 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005115 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
Ian Morris63159f22015-03-29 14:00:04 +01005116 if (!nla)
Thomas Graf04561c12006-11-14 19:53:58 -08005117 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005118 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005120 /* XXX - MC not implemented */
5121
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005122 if (ext_filter_mask & RTEXT_FILTER_SKIP_STATS)
5123 return 0;
5124
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005125 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005126 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005127 goto nla_put_failure;
5128 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
5129
5130 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01005131 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07005132 goto nla_put_failure;
5133 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005135 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
Ian Morris63159f22015-03-29 14:00:04 +01005136 if (!nla)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005137 goto nla_put_failure;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005138
Felix Jiad35a00b2017-01-26 16:59:17 +13005139 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->cnf.addr_gen_mode))
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005140 goto nla_put_failure;
5141
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005142 read_lock_bh(&idev->lock);
5143 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
5144 read_unlock_bh(&idev->lock);
5145
Thomas Grafb382b192010-11-16 04:33:57 +00005146 return 0;
5147
5148nla_put_failure:
5149 return -EMSGSIZE;
5150}
5151
Arad, Ronenb1974ed2015-10-19 09:23:28 -07005152static size_t inet6_get_link_af_size(const struct net_device *dev,
5153 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005154{
5155 if (!__in6_dev_get(dev))
5156 return 0;
5157
5158 return inet6_ifla6_size();
5159}
5160
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005161static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev,
5162 u32 ext_filter_mask)
Thomas Grafb382b192010-11-16 04:33:57 +00005163{
5164 struct inet6_dev *idev = __in6_dev_get(dev);
5165
5166 if (!idev)
5167 return -ENODATA;
5168
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005169 if (inet6_fill_ifla6_attrs(skb, idev, ext_filter_mask) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005170 return -EMSGSIZE;
5171
5172 return 0;
5173}
5174
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005175static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
5176{
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005177 struct inet6_ifaddr *ifp;
5178 struct net_device *dev = idev->dev;
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005179 bool clear_token, update_rs = false;
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005180 struct in6_addr ll_addr;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005181
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005182 ASSERT_RTNL();
5183
Ian Morris63159f22015-03-29 14:00:04 +01005184 if (!token)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005185 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005186 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
5187 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005188 if (!ipv6_accept_ra(idev))
5189 return -EINVAL;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005190 if (idev->cnf.rtr_solicits == 0)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005191 return -EINVAL;
5192
5193 write_lock_bh(&idev->lock);
5194
5195 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
5196 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
5197
5198 write_unlock_bh(&idev->lock);
5199
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005200 clear_token = ipv6_addr_any(token);
5201 if (clear_token)
5202 goto update_lft;
5203
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02005204 if (!idev->dead && (idev->if_flags & IF_READY) &&
5205 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
5206 IFA_F_OPTIMISTIC)) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005207 /* If we're not ready, then normal ifup will take care
5208 * of this. Otherwise, we need to request our rs here.
5209 */
5210 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
5211 update_rs = true;
5212 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005213
Daniel Borkmann47e27d52016-04-08 15:55:00 +02005214update_lft:
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005215 write_lock_bh(&idev->lock);
Daniel Borkmannfc403832013-04-09 03:47:15 +00005216
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005217 if (update_rs) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00005218 idev->if_flags |= IF_RS_SENT;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005219 idev->rs_interval = rfc3315_s14_backoff_init(
5220 idev->cnf.rtr_solicit_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005221 idev->rs_probes = 1;
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07005222 addrconf_mod_rs_timer(idev, idev->rs_interval);
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02005223 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005224
5225 /* Well, that's kinda nasty ... */
5226 list_for_each_entry(ifp, &idev->addr_list, if_list) {
5227 spin_lock(&ifp->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00005228 if (ifp->tokenized) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005229 ifp->valid_lft = 0;
5230 ifp->prefered_lft = 0;
5231 }
5232 spin_unlock(&ifp->lock);
5233 }
5234
5235 write_unlock_bh(&idev->lock);
Lubomir Rintelb2ed64a2014-10-27 17:39:16 +01005236 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005237 addrconf_verify_rtnl();
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005238 return 0;
5239}
5240
Daniel Borkmann11b1f822015-02-05 14:39:11 +01005241static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
5242 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
5243 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
5244};
5245
5246static int inet6_validate_link_af(const struct net_device *dev,
5247 const struct nlattr *nla)
5248{
5249 struct nlattr *tb[IFLA_INET6_MAX + 1];
5250
5251 if (dev && !__in6_dev_get(dev))
5252 return -EAFNOSUPPORT;
5253
5254 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
5255}
5256
Felix Jiad35a00b2017-01-26 16:59:17 +13005257static int check_addr_gen_mode(int mode)
5258{
5259 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
5260 mode != IN6_ADDR_GEN_MODE_NONE &&
5261 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5262 mode != IN6_ADDR_GEN_MODE_RANDOM)
5263 return -EINVAL;
5264 return 1;
5265}
5266
5267static int check_stable_privacy(struct inet6_dev *idev, struct net *net,
5268 int mode)
5269{
5270 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
5271 !idev->cnf.stable_secret.initialized &&
5272 !net->ipv6.devconf_dflt->stable_secret.initialized)
5273 return -EINVAL;
5274 return 1;
5275}
5276
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005277static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
5278{
5279 int err = -EINVAL;
5280 struct inet6_dev *idev = __in6_dev_get(dev);
5281 struct nlattr *tb[IFLA_INET6_MAX + 1];
5282
5283 if (!idev)
5284 return -EAFNOSUPPORT;
5285
5286 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
5287 BUG();
5288
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005289 if (tb[IFLA_INET6_TOKEN]) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005290 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005291 if (err)
5292 return err;
5293 }
5294
5295 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
5296 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
5297
Felix Jiad35a00b2017-01-26 16:59:17 +13005298 if (check_addr_gen_mode(mode) < 0 ||
5299 check_stable_privacy(idev, dev_net(dev), mode) < 0)
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005300 return -EINVAL;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005301
Felix Jiad35a00b2017-01-26 16:59:17 +13005302 idev->cnf.addr_gen_mode = mode;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02005303 err = 0;
5304 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005305
5306 return err;
5307}
5308
Thomas Grafb382b192010-11-16 04:33:57 +00005309static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005310 u32 portid, u32 seq, int event, unsigned int flags)
Thomas Grafb382b192010-11-16 04:33:57 +00005311{
5312 struct net_device *dev = idev->dev;
5313 struct ifinfomsg *hdr;
5314 struct nlmsghdr *nlh;
5315 void *protoinfo;
5316
Eric W. Biederman15e47302012-09-07 20:12:54 +00005317 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005318 if (!nlh)
Thomas Grafb382b192010-11-16 04:33:57 +00005319 return -EMSGSIZE;
5320
5321 hdr = nlmsg_data(nlh);
5322 hdr->ifi_family = AF_INET6;
5323 hdr->__ifi_pad = 0;
5324 hdr->ifi_type = dev->type;
5325 hdr->ifi_index = dev->ifindex;
5326 hdr->ifi_flags = dev_get_flags(dev);
5327 hdr->ifi_change = 0;
5328
David S. Millerc78679e2012-04-01 20:27:33 -04005329 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
5330 (dev->addr_len &&
5331 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
5332 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02005333 (dev->ifindex != dev_get_iflink(dev) &&
Andy Gospodarek03443382015-08-13 15:26:35 -04005334 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
5335 nla_put_u8(skb, IFLA_OPERSTATE,
5336 netif_running(dev) ? dev->operstate : IF_OPER_DOWN))
David S. Millerc78679e2012-04-01 20:27:33 -04005337 goto nla_put_failure;
Thomas Grafb382b192010-11-16 04:33:57 +00005338 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
Ian Morris63159f22015-03-29 14:00:04 +01005339 if (!protoinfo)
Thomas Grafb382b192010-11-16 04:33:57 +00005340 goto nla_put_failure;
5341
Sowmini Varadhand5566fd2015-09-11 16:48:48 -04005342 if (inet6_fill_ifla6_attrs(skb, idev, 0) < 0)
Thomas Grafb382b192010-11-16 04:33:57 +00005343 goto nla_put_failure;
5344
Thomas Graf04561c12006-11-14 19:53:58 -08005345 nla_nest_end(skb, protoinfo);
Johannes Berg053c0952015-01-16 22:09:00 +01005346 nlmsg_end(skb, nlh);
5347 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348
Thomas Graf04561c12006-11-14 19:53:58 -08005349nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005350 nlmsg_cancel(skb, nlh);
5351 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352}
5353
5354static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
5355{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09005356 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00005357 int h, s_h;
David S. Miller434a8a52009-11-11 18:53:00 -08005358 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 struct net_device *dev;
5360 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00005361 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362
Eric Dumazet84d26972009-11-09 12:11:28 +00005363 s_h = cb->args[0];
5364 s_idx = cb->args[1];
5365
5366 rcu_read_lock();
5367 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5368 idx = 0;
5369 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08005370 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet84d26972009-11-09 12:11:28 +00005371 if (idx < s_idx)
5372 goto cont;
5373 idev = __in6_dev_get(dev);
5374 if (!idev)
5375 goto cont;
5376 if (inet6_fill_ifinfo(skb, idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005377 NETLINK_CB(cb->skb).portid,
Eric Dumazet84d26972009-11-09 12:11:28 +00005378 cb->nlh->nlmsg_seq,
Johannes Berg053c0952015-01-16 22:09:00 +01005379 RTM_NEWLINK, NLM_F_MULTI) < 0)
Eric Dumazet84d26972009-11-09 12:11:28 +00005380 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07005381cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00005382 idx++;
5383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 }
Eric Dumazet84d26972009-11-09 12:11:28 +00005385out:
5386 rcu_read_unlock();
5387 cb->args[1] = idx;
5388 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389
5390 return skb->len;
5391}
5392
5393void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
5394{
5395 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005396 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005397 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005398
Thomas Graf339bf982006-11-10 14:10:15 -08005399 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005400 if (!skb)
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005401 goto errout;
5402
5403 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005404 if (err < 0) {
5405 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
5406 WARN_ON(err == -EMSGSIZE);
5407 kfree_skb(skb);
5408 goto errout;
5409 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005410 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005411 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07005412errout:
5413 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00005414 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415}
5416
Thomas Graf339bf982006-11-10 14:10:15 -08005417static inline size_t inet6_prefix_nlmsg_size(void)
5418{
5419 return NLMSG_ALIGN(sizeof(struct prefixmsg))
5420 + nla_total_size(sizeof(struct in6_addr))
5421 + nla_total_size(sizeof(struct prefix_cacheinfo));
5422}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07005423
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005425 struct prefix_info *pinfo, u32 portid, u32 seq,
Thomas Graf6051e2f2006-11-14 19:54:19 -08005426 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427{
Thomas Graf6051e2f2006-11-14 19:54:19 -08005428 struct prefixmsg *pmsg;
5429 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 struct prefix_cacheinfo ci;
5431
Eric W. Biederman15e47302012-09-07 20:12:54 +00005432 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01005433 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08005434 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08005435
5436 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07005438 pmsg->prefix_pad1 = 0;
5439 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 pmsg->prefix_ifindex = idev->dev->ifindex;
5441 pmsg->prefix_len = pinfo->prefix_len;
5442 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07005443 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 pmsg->prefix_flags = 0;
5445 if (pinfo->onlink)
5446 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5447 if (pinfo->autoconf)
5448 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5449
David S. Millerc78679e2012-04-01 20:27:33 -04005450 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5451 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 ci.preferred_time = ntohl(pinfo->prefered);
5453 ci.valid_time = ntohl(pinfo->valid);
David S. Millerc78679e2012-04-01 20:27:33 -04005454 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5455 goto nla_put_failure;
Johannes Berg053c0952015-01-16 22:09:00 +01005456 nlmsg_end(skb, nlh);
5457 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
Thomas Graf6051e2f2006-11-14 19:54:19 -08005459nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005460 nlmsg_cancel(skb, nlh);
5461 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462}
5463
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005464static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 struct prefix_info *pinfo)
5466{
5467 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005468 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005469 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470
Thomas Graf339bf982006-11-10 14:10:15 -08005471 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005472 if (!skb)
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005473 goto errout;
5474
5475 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005476 if (err < 0) {
5477 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5478 WARN_ON(err == -EMSGSIZE);
5479 kfree_skb(skb);
5480 goto errout;
5481 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005482 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5483 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005484errout:
5485 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08005486 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487}
5488
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5490{
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005491 struct net *net = dev_net(ifp->idev->dev);
5492
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005493 if (event)
5494 ASSERT_RTNL();
5495
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5497
5498 switch (event) {
5499 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07005500 /*
5501 * If the address was optimistic
5502 * we inserted the route at the start of
5503 * our DAD process, so we don't need
5504 * to do it again
5505 */
5506 if (!(ifp->rt->rt6i_node))
5507 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 if (ifp->idev->cnf.forwarding)
5509 addrconf_join_anycast(ifp);
Cong Wang7996c792013-05-22 05:41:06 +00005510 if (!ipv6_addr_any(&ifp->peer_addr))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005511 addrconf_prefix_route(&ifp->peer_addr, 128,
5512 ifp->idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 break;
5514 case RTM_DELADDR:
5515 if (ifp->idev->cnf.forwarding)
5516 addrconf_leave_anycast(ifp);
5517 addrconf_leave_solict(ifp->idev, &ifp->addr);
Cong Wang7996c792013-05-22 05:41:06 +00005518 if (!ipv6_addr_any(&ifp->peer_addr)) {
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005519 struct rt6_info *rt;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005520
Nicolas Dichtele7478df2014-09-03 23:59:22 +02005521 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5522 ifp->idev->dev, 0, 0);
Martin KaFai Lau8e3d5be2015-09-15 14:30:08 -07005523 if (rt)
5524 ip6_del_rt(rt);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005525 }
David Ahern38bd10c2016-04-21 20:56:12 -07005526 if (ifp->rt) {
5527 dst_hold(&ifp->rt->dst);
5528 ip6_del_rt(ifp->rt);
5529 }
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02005530 rt_genid_bump_ipv6(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 break;
5532 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005533 atomic_inc(&net->ipv6.dev_addr_genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534}
5535
5536static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5537{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005538 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 if (likely(ifp->idev->dead == 0))
5540 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005541 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542}
5543
5544#ifdef CONFIG_SYSCTL
5545
5546static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005547int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 void __user *buffer, size_t *lenp, loff_t *ppos)
5549{
5550 int *valp = ctl->data;
5551 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005552 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005553 struct ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 int ret;
5555
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005556 /*
5557 * ctl->data points to idev->cnf.forwarding, we should
5558 * not modify it until we get the rtnl lock.
5559 */
5560 lctl = *ctl;
5561 lctl.data = &val;
5562
5563 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08005565 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00005566 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005567 if (ret)
5568 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005569 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570}
5571
Marcelo Leitner77751422015-02-23 11:17:13 -03005572static
5573int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5574 void __user *buffer, size_t *lenp, loff_t *ppos)
5575{
5576 struct inet6_dev *idev = ctl->extra1;
5577 int min_mtu = IPV6_MIN_MTU;
5578 struct ctl_table lctl;
5579
5580 lctl = *ctl;
5581 lctl.extra1 = &min_mtu;
5582 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5583
5584 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5585}
5586
Brian Haley56d417b2009-06-01 03:07:33 -07005587static void dev_disable_change(struct inet6_dev *idev)
5588{
Cong Wang75538c22013-05-29 11:30:50 +08005589 struct netdev_notifier_info info;
5590
Brian Haley56d417b2009-06-01 03:07:33 -07005591 if (!idev || !idev->dev)
5592 return;
5593
Cong Wang75538c22013-05-29 11:30:50 +08005594 netdev_notifier_info_init(&info, idev->dev);
Brian Haley56d417b2009-06-01 03:07:33 -07005595 if (idev->cnf.disable_ipv6)
Cong Wang75538c22013-05-29 11:30:50 +08005596 addrconf_notify(NULL, NETDEV_DOWN, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005597 else
Cong Wang75538c22013-05-29 11:30:50 +08005598 addrconf_notify(NULL, NETDEV_UP, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005599}
5600
5601static void addrconf_disable_change(struct net *net, __s32 newf)
5602{
5603 struct net_device *dev;
5604 struct inet6_dev *idev;
5605
Kefeng Wang03e4def2017-01-19 16:26:21 +08005606 for_each_netdev(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07005607 idev = __in6_dev_get(dev);
5608 if (idev) {
5609 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5610 idev->cnf.disable_ipv6 = newf;
5611 if (changed)
5612 dev_disable_change(idev);
5613 }
Brian Haley56d417b2009-06-01 03:07:33 -07005614 }
Brian Haley56d417b2009-06-01 03:07:33 -07005615}
5616
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005617static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07005618{
5619 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005620 int old;
5621
5622 if (!rtnl_trylock())
5623 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07005624
5625 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005626 old = *p;
5627 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07005628
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005629 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5630 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07005631 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005632 }
Brian Haley56d417b2009-06-01 03:07:33 -07005633
5634 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07005635 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5636 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005637 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07005638 dev_disable_change((struct inet6_dev *)table->extra1);
5639
5640 rtnl_unlock();
5641 return 0;
5642}
5643
5644static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005645int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07005646 void __user *buffer, size_t *lenp, loff_t *ppos)
5647{
5648 int *valp = ctl->data;
5649 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005650 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005651 struct ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07005652 int ret;
5653
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005654 /*
5655 * ctl->data points to idev->cnf.disable_ipv6, we should
5656 * not modify it until we get the rtnl lock.
5657 */
5658 lctl = *ctl;
5659 lctl.data = &val;
5660
5661 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07005662
5663 if (write)
5664 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005665 if (ret)
5666 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07005667 return ret;
5668}
5669
stephen hemmingerc92d5492013-12-17 22:37:14 -08005670static
5671int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5672 void __user *buffer, size_t *lenp, loff_t *ppos)
5673{
5674 int *valp = ctl->data;
5675 int ret;
5676 int old, new;
5677
5678 old = *valp;
5679 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5680 new = *valp;
5681
5682 if (write && old != new) {
5683 struct net *net = ctl->extra2;
5684
5685 if (!rtnl_trylock())
5686 return restart_syscall();
5687
5688 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
David Ahern85b3daa2017-03-28 14:28:04 -07005689 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
5690 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08005691 NETCONFA_IFINDEX_DEFAULT,
5692 net->ipv6.devconf_dflt);
5693 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
David Ahern85b3daa2017-03-28 14:28:04 -07005694 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
5695 NETCONFA_PROXY_NEIGH,
stephen hemmingerc92d5492013-12-17 22:37:14 -08005696 NETCONFA_IFINDEX_ALL,
5697 net->ipv6.devconf_all);
5698 else {
5699 struct inet6_dev *idev = ctl->extra1;
5700
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 idev->dev->ifindex,
5704 &idev->cnf);
5705 }
5706 rtnl_unlock();
5707 }
5708
5709 return ret;
5710}
5711
Felix Jiad35a00b2017-01-26 16:59:17 +13005712static int addrconf_sysctl_addr_gen_mode(struct ctl_table *ctl, int write,
5713 void __user *buffer, size_t *lenp,
5714 loff_t *ppos)
5715{
5716 int ret = 0;
5717 int new_val;
5718 struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
5719 struct net *net = (struct net *)ctl->extra2;
5720
Felix Jia8c171d62017-02-27 12:41:23 +13005721 if (!rtnl_trylock())
5722 return restart_syscall();
5723
Felix Jiad35a00b2017-01-26 16:59:17 +13005724 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5725
5726 if (write) {
5727 new_val = *((int *)ctl->data);
5728
Felix Jia8c171d62017-02-27 12:41:23 +13005729 if (check_addr_gen_mode(new_val) < 0) {
5730 ret = -EINVAL;
5731 goto out;
5732 }
Felix Jiad35a00b2017-01-26 16:59:17 +13005733
5734 /* request for default */
5735 if (&net->ipv6.devconf_dflt->addr_gen_mode == ctl->data) {
5736 ipv6_devconf_dflt.addr_gen_mode = new_val;
5737
5738 /* request for individual net device */
5739 } else {
5740 if (!idev)
Felix Jia8c171d62017-02-27 12:41:23 +13005741 goto out;
Felix Jiad35a00b2017-01-26 16:59:17 +13005742
Felix Jia8c171d62017-02-27 12:41:23 +13005743 if (check_stable_privacy(idev, net, new_val) < 0) {
5744 ret = -EINVAL;
5745 goto out;
5746 }
Felix Jiad35a00b2017-01-26 16:59:17 +13005747
5748 if (idev->cnf.addr_gen_mode != new_val) {
5749 idev->cnf.addr_gen_mode = new_val;
Felix Jiad35a00b2017-01-26 16:59:17 +13005750 addrconf_dev_config(idev->dev);
Felix Jiad35a00b2017-01-26 16:59:17 +13005751 }
5752 }
5753 }
5754
Felix Jia8c171d62017-02-27 12:41:23 +13005755out:
5756 rtnl_unlock();
5757
Felix Jiad35a00b2017-01-26 16:59:17 +13005758 return ret;
5759}
5760
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005761static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5762 void __user *buffer, size_t *lenp,
5763 loff_t *ppos)
5764{
5765 int err;
5766 struct in6_addr addr;
5767 char str[IPV6_MAX_STRLEN];
5768 struct ctl_table lctl = *ctl;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005769 struct net *net = ctl->extra2;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005770 struct ipv6_stable_secret *secret = ctl->data;
5771
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005772 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5773 return -EIO;
5774
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005775 lctl.maxlen = IPV6_MAX_STRLEN;
5776 lctl.data = str;
5777
5778 if (!rtnl_trylock())
5779 return restart_syscall();
5780
5781 if (!write && !secret->initialized) {
5782 err = -EIO;
5783 goto out;
5784 }
5785
WANG Cong5449a5c2015-12-21 10:55:45 -08005786 err = snprintf(str, sizeof(str), "%pI6", &secret->secret);
5787 if (err >= sizeof(str)) {
5788 err = -EIO;
5789 goto out;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005790 }
5791
5792 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5793 if (err || !write)
5794 goto out;
5795
5796 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5797 err = -EIO;
5798 goto out;
5799 }
5800
5801 secret->initialized = true;
5802 secret->secret = addr;
5803
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005804 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5805 struct net_device *dev;
5806
5807 for_each_netdev(net, dev) {
5808 struct inet6_dev *idev = __in6_dev_get(dev);
5809
5810 if (idev) {
Felix Jiad35a00b2017-01-26 16:59:17 +13005811 idev->cnf.addr_gen_mode =
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005812 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5813 }
5814 }
5815 } else {
5816 struct inet6_dev *idev = ctl->extra1;
5817
Felix Jiad35a00b2017-01-26 16:59:17 +13005818 idev->cnf.addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d52015-03-23 23:36:01 +01005819 }
5820
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005821out:
5822 rtnl_unlock();
5823
5824 return err;
5825}
stephen hemmingerc92d5492013-12-17 22:37:14 -08005826
Andy Gospodarek35103d12015-08-13 10:39:01 -04005827static
5828int addrconf_sysctl_ignore_routes_with_linkdown(struct ctl_table *ctl,
5829 int write,
5830 void __user *buffer,
5831 size_t *lenp,
5832 loff_t *ppos)
5833{
5834 int *valp = ctl->data;
5835 int val = *valp;
5836 loff_t pos = *ppos;
5837 struct ctl_table lctl;
5838 int ret;
5839
5840 /* ctl->data points to idev->cnf.ignore_routes_when_linkdown
5841 * we should not modify it until we get the rtnl lock.
5842 */
5843 lctl = *ctl;
5844 lctl.data = &val;
5845
5846 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5847
5848 if (write)
5849 ret = addrconf_fixup_linkdown(ctl, valp, val);
5850 if (ret)
5851 *ppos = pos;
5852 return ret;
5853}
5854
David Forsterdf789fe2017-02-23 16:27:18 +00005855static
5856void addrconf_set_nopolicy(struct rt6_info *rt, int action)
5857{
5858 if (rt) {
5859 if (action)
5860 rt->dst.flags |= DST_NOPOLICY;
5861 else
5862 rt->dst.flags &= ~DST_NOPOLICY;
5863 }
5864}
5865
5866static
5867void addrconf_disable_policy_idev(struct inet6_dev *idev, int val)
5868{
5869 struct inet6_ifaddr *ifa;
5870
5871 read_lock_bh(&idev->lock);
5872 list_for_each_entry(ifa, &idev->addr_list, if_list) {
5873 spin_lock(&ifa->lock);
5874 if (ifa->rt) {
5875 struct rt6_info *rt = ifa->rt;
5876 struct fib6_table *table = rt->rt6i_table;
5877 int cpu;
5878
5879 read_lock(&table->tb6_lock);
5880 addrconf_set_nopolicy(ifa->rt, val);
5881 if (rt->rt6i_pcpu) {
5882 for_each_possible_cpu(cpu) {
5883 struct rt6_info **rtp;
5884
5885 rtp = per_cpu_ptr(rt->rt6i_pcpu, cpu);
5886 addrconf_set_nopolicy(*rtp, val);
5887 }
5888 }
5889 read_unlock(&table->tb6_lock);
5890 }
5891 spin_unlock(&ifa->lock);
5892 }
5893 read_unlock_bh(&idev->lock);
5894}
5895
5896static
5897int addrconf_disable_policy(struct ctl_table *ctl, int *valp, int val)
5898{
5899 struct inet6_dev *idev;
5900 struct net *net;
5901
5902 if (!rtnl_trylock())
5903 return restart_syscall();
5904
5905 *valp = val;
5906
5907 net = (struct net *)ctl->extra2;
5908 if (valp == &net->ipv6.devconf_dflt->disable_policy) {
5909 rtnl_unlock();
5910 return 0;
5911 }
5912
5913 if (valp == &net->ipv6.devconf_all->disable_policy) {
5914 struct net_device *dev;
5915
5916 for_each_netdev(net, dev) {
5917 idev = __in6_dev_get(dev);
5918 if (idev)
5919 addrconf_disable_policy_idev(idev, val);
5920 }
5921 } else {
5922 idev = (struct inet6_dev *)ctl->extra1;
5923 addrconf_disable_policy_idev(idev, val);
5924 }
5925
5926 rtnl_unlock();
5927 return 0;
5928}
5929
5930static
5931int addrconf_sysctl_disable_policy(struct ctl_table *ctl, int write,
5932 void __user *buffer, size_t *lenp,
5933 loff_t *ppos)
5934{
5935 int *valp = ctl->data;
5936 int val = *valp;
5937 loff_t pos = *ppos;
5938 struct ctl_table lctl;
5939 int ret;
5940
5941 lctl = *ctl;
5942 lctl.data = &val;
5943 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
5944
5945 if (write && (*valp != val))
5946 ret = addrconf_disable_policy(ctl, valp, val);
5947
5948 if (ret)
5949 *ppos = pos;
5950
5951 return ret;
5952}
5953
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07005954static int minus_one = -1;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07005955static const int one = 1;
5956static const int two_five_five = 255;
5957
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03005958static const struct ctl_table addrconf_sysctl[] = {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005959 {
5960 .procname = "forwarding",
5961 .data = &ipv6_devconf.forwarding,
5962 .maxlen = sizeof(int),
5963 .mode = 0644,
5964 .proc_handler = addrconf_sysctl_forward,
5965 },
5966 {
5967 .procname = "hop_limit",
5968 .data = &ipv6_devconf.hop_limit,
5969 .maxlen = sizeof(int),
5970 .mode = 0644,
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07005971 .proc_handler = proc_dointvec_minmax,
5972 .extra1 = (void *)&one,
5973 .extra2 = (void *)&two_five_five,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03005974 },
5975 {
5976 .procname = "mtu",
5977 .data = &ipv6_devconf.mtu6,
5978 .maxlen = sizeof(int),
5979 .mode = 0644,
5980 .proc_handler = addrconf_sysctl_mtu,
5981 },
5982 {
5983 .procname = "accept_ra",
5984 .data = &ipv6_devconf.accept_ra,
5985 .maxlen = sizeof(int),
5986 .mode = 0644,
5987 .proc_handler = proc_dointvec,
5988 },
5989 {
5990 .procname = "accept_redirects",
5991 .data = &ipv6_devconf.accept_redirects,
5992 .maxlen = sizeof(int),
5993 .mode = 0644,
5994 .proc_handler = proc_dointvec,
5995 },
5996 {
5997 .procname = "autoconf",
5998 .data = &ipv6_devconf.autoconf,
5999 .maxlen = sizeof(int),
6000 .mode = 0644,
6001 .proc_handler = proc_dointvec,
6002 },
6003 {
6004 .procname = "dad_transmits",
6005 .data = &ipv6_devconf.dad_transmits,
6006 .maxlen = sizeof(int),
6007 .mode = 0644,
6008 .proc_handler = proc_dointvec,
6009 },
6010 {
6011 .procname = "router_solicitations",
6012 .data = &ipv6_devconf.rtr_solicits,
6013 .maxlen = sizeof(int),
6014 .mode = 0644,
Maciej Żenczykowskicb4a4c62016-10-07 01:00:49 -07006015 .proc_handler = proc_dointvec_minmax,
6016 .extra1 = &minus_one,
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006017 },
6018 {
6019 .procname = "router_solicitation_interval",
6020 .data = &ipv6_devconf.rtr_solicit_interval,
6021 .maxlen = sizeof(int),
6022 .mode = 0644,
6023 .proc_handler = proc_dointvec_jiffies,
6024 },
6025 {
Maciej Żenczykowskibd11f072016-09-27 23:57:58 -07006026 .procname = "router_solicitation_max_interval",
6027 .data = &ipv6_devconf.rtr_solicit_max_interval,
6028 .maxlen = sizeof(int),
6029 .mode = 0644,
6030 .proc_handler = proc_dointvec_jiffies,
6031 },
6032 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006033 .procname = "router_solicitation_delay",
6034 .data = &ipv6_devconf.rtr_solicit_delay,
6035 .maxlen = sizeof(int),
6036 .mode = 0644,
6037 .proc_handler = proc_dointvec_jiffies,
6038 },
6039 {
6040 .procname = "force_mld_version",
6041 .data = &ipv6_devconf.force_mld_version,
6042 .maxlen = sizeof(int),
6043 .mode = 0644,
6044 .proc_handler = proc_dointvec,
6045 },
6046 {
6047 .procname = "mldv1_unsolicited_report_interval",
6048 .data =
6049 &ipv6_devconf.mldv1_unsolicited_report_interval,
6050 .maxlen = sizeof(int),
6051 .mode = 0644,
6052 .proc_handler = proc_dointvec_ms_jiffies,
6053 },
6054 {
6055 .procname = "mldv2_unsolicited_report_interval",
6056 .data =
6057 &ipv6_devconf.mldv2_unsolicited_report_interval,
6058 .maxlen = sizeof(int),
6059 .mode = 0644,
6060 .proc_handler = proc_dointvec_ms_jiffies,
6061 },
6062 {
6063 .procname = "use_tempaddr",
6064 .data = &ipv6_devconf.use_tempaddr,
6065 .maxlen = sizeof(int),
6066 .mode = 0644,
6067 .proc_handler = proc_dointvec,
6068 },
6069 {
6070 .procname = "temp_valid_lft",
6071 .data = &ipv6_devconf.temp_valid_lft,
6072 .maxlen = sizeof(int),
6073 .mode = 0644,
6074 .proc_handler = proc_dointvec,
6075 },
6076 {
6077 .procname = "temp_prefered_lft",
6078 .data = &ipv6_devconf.temp_prefered_lft,
6079 .maxlen = sizeof(int),
6080 .mode = 0644,
6081 .proc_handler = proc_dointvec,
6082 },
6083 {
6084 .procname = "regen_max_retry",
6085 .data = &ipv6_devconf.regen_max_retry,
6086 .maxlen = sizeof(int),
6087 .mode = 0644,
6088 .proc_handler = proc_dointvec,
6089 },
6090 {
6091 .procname = "max_desync_factor",
6092 .data = &ipv6_devconf.max_desync_factor,
6093 .maxlen = sizeof(int),
6094 .mode = 0644,
6095 .proc_handler = proc_dointvec,
6096 },
6097 {
6098 .procname = "max_addresses",
6099 .data = &ipv6_devconf.max_addresses,
6100 .maxlen = sizeof(int),
6101 .mode = 0644,
6102 .proc_handler = proc_dointvec,
6103 },
6104 {
6105 .procname = "accept_ra_defrtr",
6106 .data = &ipv6_devconf.accept_ra_defrtr,
6107 .maxlen = sizeof(int),
6108 .mode = 0644,
6109 .proc_handler = proc_dointvec,
6110 },
6111 {
6112 .procname = "accept_ra_min_hop_limit",
6113 .data = &ipv6_devconf.accept_ra_min_hop_limit,
6114 .maxlen = sizeof(int),
6115 .mode = 0644,
6116 .proc_handler = proc_dointvec,
6117 },
6118 {
6119 .procname = "accept_ra_pinfo",
6120 .data = &ipv6_devconf.accept_ra_pinfo,
6121 .maxlen = sizeof(int),
6122 .mode = 0644,
6123 .proc_handler = proc_dointvec,
6124 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08006125#ifdef CONFIG_IPV6_ROUTER_PREF
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006126 {
6127 .procname = "accept_ra_rtr_pref",
6128 .data = &ipv6_devconf.accept_ra_rtr_pref,
6129 .maxlen = sizeof(int),
6130 .mode = 0644,
6131 .proc_handler = proc_dointvec,
6132 },
6133 {
6134 .procname = "router_probe_interval",
6135 .data = &ipv6_devconf.rtr_probe_interval,
6136 .maxlen = sizeof(int),
6137 .mode = 0644,
6138 .proc_handler = proc_dointvec_jiffies,
6139 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08006140#ifdef CONFIG_IPV6_ROUTE_INFO
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006141 {
Joel Scherpelzbbea1242017-03-22 18:19:04 +09006142 .procname = "accept_ra_rt_info_min_plen",
6143 .data = &ipv6_devconf.accept_ra_rt_info_min_plen,
6144 .maxlen = sizeof(int),
6145 .mode = 0644,
6146 .proc_handler = proc_dointvec,
6147 },
6148 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006149 .procname = "accept_ra_rt_info_max_plen",
6150 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
6151 .maxlen = sizeof(int),
6152 .mode = 0644,
6153 .proc_handler = proc_dointvec,
6154 },
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08006155#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08006156#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006157 {
6158 .procname = "proxy_ndp",
6159 .data = &ipv6_devconf.proxy_ndp,
6160 .maxlen = sizeof(int),
6161 .mode = 0644,
6162 .proc_handler = addrconf_sysctl_proxy_ndp,
6163 },
6164 {
6165 .procname = "accept_source_route",
6166 .data = &ipv6_devconf.accept_source_route,
6167 .maxlen = sizeof(int),
6168 .mode = 0644,
6169 .proc_handler = proc_dointvec,
6170 },
Neil Horman95c385b2007-04-25 17:08:10 -07006171#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006172 {
6173 .procname = "optimistic_dad",
6174 .data = &ipv6_devconf.optimistic_dad,
6175 .maxlen = sizeof(int),
6176 .mode = 0644,
6177 .proc_handler = proc_dointvec,
6178 },
6179 {
6180 .procname = "use_optimistic",
6181 .data = &ipv6_devconf.use_optimistic,
6182 .maxlen = sizeof(int),
6183 .mode = 0644,
6184 .proc_handler = proc_dointvec,
6185 },
Neil Horman95c385b2007-04-25 17:08:10 -07006186#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006187#ifdef CONFIG_IPV6_MROUTE
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006188 {
6189 .procname = "mc_forwarding",
6190 .data = &ipv6_devconf.mc_forwarding,
6191 .maxlen = sizeof(int),
6192 .mode = 0444,
6193 .proc_handler = proc_dointvec,
6194 },
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09006195#endif
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006196 {
6197 .procname = "disable_ipv6",
6198 .data = &ipv6_devconf.disable_ipv6,
6199 .maxlen = sizeof(int),
6200 .mode = 0644,
6201 .proc_handler = addrconf_sysctl_disable,
6202 },
6203 {
6204 .procname = "accept_dad",
6205 .data = &ipv6_devconf.accept_dad,
6206 .maxlen = sizeof(int),
6207 .mode = 0644,
6208 .proc_handler = proc_dointvec,
6209 },
6210 {
6211 .procname = "force_tllao",
6212 .data = &ipv6_devconf.force_tllao,
6213 .maxlen = sizeof(int),
6214 .mode = 0644,
6215 .proc_handler = proc_dointvec
6216 },
6217 {
6218 .procname = "ndisc_notify",
6219 .data = &ipv6_devconf.ndisc_notify,
6220 .maxlen = sizeof(int),
6221 .mode = 0644,
6222 .proc_handler = proc_dointvec
6223 },
6224 {
6225 .procname = "suppress_frag_ndisc",
6226 .data = &ipv6_devconf.suppress_frag_ndisc,
6227 .maxlen = sizeof(int),
6228 .mode = 0644,
6229 .proc_handler = proc_dointvec
6230 },
6231 {
6232 .procname = "accept_ra_from_local",
6233 .data = &ipv6_devconf.accept_ra_from_local,
6234 .maxlen = sizeof(int),
6235 .mode = 0644,
6236 .proc_handler = proc_dointvec,
6237 },
6238 {
6239 .procname = "accept_ra_mtu",
6240 .data = &ipv6_devconf.accept_ra_mtu,
6241 .maxlen = sizeof(int),
6242 .mode = 0644,
6243 .proc_handler = proc_dointvec,
6244 },
6245 {
6246 .procname = "stable_secret",
6247 .data = &ipv6_devconf.stable_secret,
6248 .maxlen = IPV6_MAX_STRLEN,
6249 .mode = 0600,
6250 .proc_handler = addrconf_sysctl_stable_secret,
6251 },
6252 {
6253 .procname = "use_oif_addrs_only",
6254 .data = &ipv6_devconf.use_oif_addrs_only,
6255 .maxlen = sizeof(int),
6256 .mode = 0644,
6257 .proc_handler = proc_dointvec,
6258 },
6259 {
6260 .procname = "ignore_routes_with_linkdown",
6261 .data = &ipv6_devconf.ignore_routes_with_linkdown,
6262 .maxlen = sizeof(int),
6263 .mode = 0644,
6264 .proc_handler = addrconf_sysctl_ignore_routes_with_linkdown,
6265 },
6266 {
6267 .procname = "drop_unicast_in_l2_multicast",
6268 .data = &ipv6_devconf.drop_unicast_in_l2_multicast,
6269 .maxlen = sizeof(int),
6270 .mode = 0644,
6271 .proc_handler = proc_dointvec,
6272 },
6273 {
6274 .procname = "drop_unsolicited_na",
6275 .data = &ipv6_devconf.drop_unsolicited_na,
6276 .maxlen = sizeof(int),
6277 .mode = 0644,
6278 .proc_handler = proc_dointvec,
6279 },
6280 {
6281 .procname = "keep_addr_on_down",
6282 .data = &ipv6_devconf.keep_addr_on_down,
6283 .maxlen = sizeof(int),
6284 .mode = 0644,
6285 .proc_handler = proc_dointvec,
David Ahernf1705ec2016-02-24 09:25:37 -08006286
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006287 },
6288 {
David Lebrun1ababeb2016-11-08 14:57:39 +01006289 .procname = "seg6_enabled",
6290 .data = &ipv6_devconf.seg6_enabled,
6291 .maxlen = sizeof(int),
6292 .mode = 0644,
6293 .proc_handler = proc_dointvec,
6294 },
David Lebrunbf355b82016-11-08 14:57:42 +01006295#ifdef CONFIG_IPV6_SEG6_HMAC
6296 {
6297 .procname = "seg6_require_hmac",
6298 .data = &ipv6_devconf.seg6_require_hmac,
6299 .maxlen = sizeof(int),
6300 .mode = 0644,
6301 .proc_handler = proc_dointvec,
6302 },
6303#endif
David Lebrun1ababeb2016-11-08 14:57:39 +01006304 {
Erik Nordmarkadc176c2016-12-02 14:00:08 -08006305 .procname = "enhanced_dad",
6306 .data = &ipv6_devconf.enhanced_dad,
6307 .maxlen = sizeof(int),
6308 .mode = 0644,
6309 .proc_handler = proc_dointvec,
6310 },
6311 {
Felix Jiad35a00b2017-01-26 16:59:17 +13006312 .procname = "addr_gen_mode",
6313 .data = &ipv6_devconf.addr_gen_mode,
6314 .maxlen = sizeof(int),
6315 .mode = 0644,
6316 .proc_handler = addrconf_sysctl_addr_gen_mode,
6317 },
6318 {
David Forsterdf789fe2017-02-23 16:27:18 +00006319 .procname = "disable_policy",
6320 .data = &ipv6_devconf.disable_policy,
6321 .maxlen = sizeof(int),
6322 .mode = 0644,
6323 .proc_handler = addrconf_sysctl_disable_policy,
6324 },
6325 {
Konstantin Khlebnikov5df1f772016-04-18 14:41:17 +03006326 /* sentinel */
6327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328};
6329
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08006330static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006331 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332{
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006333 int i, ifindex;
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006334 struct ctl_table *table;
Eric W. Biederman6105e292012-04-19 13:41:24 +00006335 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11006336
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006337 table = kmemdup(addrconf_sysctl, sizeof(addrconf_sysctl), GFP_KERNEL);
6338 if (!table)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006339 goto out;
6340
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006341 for (i = 0; table[i].data; i++) {
6342 table[i].data += (char *)p - (char *)&ipv6_devconf;
Maciej Żenczykowskicb9e6842016-09-29 00:33:43 -07006343 /* If one of these is already set, then it is not safe to
6344 * overwrite either of them: this makes proc_dointvec_minmax
6345 * usable.
6346 */
6347 if (!table[i].extra1 && !table[i].extra2) {
6348 table[i].extra1 = idev; /* embedded; no ref */
6349 table[i].extra2 = net;
6350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352
Eric W. Biederman6105e292012-04-19 13:41:24 +00006353 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006355 p->sysctl_header = register_net_sysctl(net, path, table);
6356 if (!p->sysctl_header)
Eric W. Biederman6105e292012-04-19 13:41:24 +00006357 goto free;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006358
Nicolas Dichtel29c994e2016-08-30 10:09:22 +02006359 if (!strcmp(dev_name, "all"))
6360 ifindex = NETCONFA_IFINDEX_ALL;
6361 else if (!strcmp(dev_name, "default"))
6362 ifindex = NETCONFA_IFINDEX_DEFAULT;
6363 else
6364 ifindex = idev->dev->ifindex;
David Ahern85b3daa2017-03-28 14:28:04 -07006365 inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL,
6366 ifindex, p);
Pavel Emelyanov95897312008-01-10 17:41:45 -08006367 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006369free:
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006370 kfree(table);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11006371out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08006372 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373}
6374
David Ahern23452172017-03-28 14:28:05 -07006375static void __addrconf_sysctl_unregister(struct net *net,
6376 struct ipv6_devconf *p, int ifindex)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006377{
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006378 struct ctl_table *table;
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006379
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006380 if (!p->sysctl_header)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006381 return;
6382
Konstantin Khlebnikov607ea7c2016-04-18 14:41:10 +03006383 table = p->sysctl_header->ctl_table_arg;
6384 unregister_net_sysctl_table(p->sysctl_header);
6385 p->sysctl_header = NULL;
6386 kfree(table);
David Ahern23452172017-03-28 14:28:05 -07006387
6388 inet6_netconf_notify_devconf(net, RTM_DELNETCONF, 0, ifindex, NULL);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006389}
6390
WANG Conga317a2f2014-07-25 15:25:09 -07006391static int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006392{
WANG Conga317a2f2014-07-25 15:25:09 -07006393 int err;
6394
6395 if (!sysctl_dev_name_is_allowed(idev->dev->name))
6396 return -EINVAL;
6397
6398 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
6399 &ndisc_ifinfo_sysctl_change);
6400 if (err)
6401 return err;
6402 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
6403 idev, &idev->cnf);
6404 if (err)
6405 neigh_sysctl_unregister(idev->nd_parms);
6406
6407 return err;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11006408}
6409
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006410static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411{
David Ahern23452172017-03-28 14:28:05 -07006412 __addrconf_sysctl_unregister(dev_net(idev->dev), &idev->cnf,
6413 idev->dev->ifindex);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08006414 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415}
6416
6417
6418#endif
6419
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006420static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006421{
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006422 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006423 struct ipv6_devconf *all, *dflt;
6424
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006425 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006426 if (!all)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006427 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006428
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006429 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01006430 if (!dflt)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006431 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006432
Hong Zhiguoa79ca222013-03-26 01:52:45 +08006433 /* these will be inherited by all namespaces */
6434 dflt->autoconf = ipv6_defaults.autoconf;
6435 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006436
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01006437 dflt->stable_secret.initialized = false;
6438 all->stable_secret.initialized = false;
6439
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006440 net->ipv6.devconf_all = all;
6441 net->ipv6.devconf_dflt = dflt;
6442
6443#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006444 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006445 if (err < 0)
6446 goto err_reg_all;
6447
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08006448 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006449 if (err < 0)
6450 goto err_reg_dflt;
6451#endif
6452 return 0;
6453
6454#ifdef CONFIG_SYSCTL
6455err_reg_dflt:
David Ahern23452172017-03-28 14:28:05 -07006456 __addrconf_sysctl_unregister(net, all, NETCONFA_IFINDEX_ALL);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006457err_reg_all:
6458 kfree(dflt);
6459#endif
6460err_alloc_dflt:
6461 kfree(all);
6462err_alloc_all:
6463 return err;
6464}
6465
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00006466static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006467{
6468#ifdef CONFIG_SYSCTL
David Ahern23452172017-03-28 14:28:05 -07006469 __addrconf_sysctl_unregister(net, net->ipv6.devconf_dflt,
6470 NETCONFA_IFINDEX_DEFAULT);
6471 __addrconf_sysctl_unregister(net, net->ipv6.devconf_all,
6472 NETCONFA_IFINDEX_ALL);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006473#endif
zhuyj73cf0e922014-11-26 10:25:58 +08006474 kfree(net->ipv6.devconf_dflt);
6475 kfree(net->ipv6.devconf_all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006476}
6477
6478static struct pernet_operations addrconf_ops = {
6479 .init = addrconf_init_net,
6480 .exit = addrconf_exit_net,
6481};
6482
Daniel Borkmann207895f2015-01-29 12:15:03 +01006483static struct rtnl_af_ops inet6_ops __read_mostly = {
Thomas Grafb382b192010-11-16 04:33:57 +00006484 .family = AF_INET6,
6485 .fill_link_af = inet6_fill_link_af,
6486 .get_link_af_size = inet6_get_link_af_size,
Daniel Borkmann11b1f822015-02-05 14:39:11 +01006487 .validate_link_af = inet6_validate_link_af,
Daniel Borkmannf53adae2013-04-08 04:01:30 +00006488 .set_link_af = inet6_set_link_af,
Thomas Grafb382b192010-11-16 04:33:57 +00006489};
6490
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491/*
6492 * Init / cleanup code
6493 */
6494
6495int __init addrconf_init(void)
6496{
WANG Conga317a2f2014-07-25 15:25:09 -07006497 struct inet6_dev *idev;
stephen hemmingerc2e21292010-03-17 20:31:10 +00006498 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006499
Stephen Hemmingere21e8462010-03-20 16:09:01 -07006500 err = ipv6_addr_label_init();
6501 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00006502 pr_crit("%s: cannot initialize default policy table: %d\n",
6503 __func__, err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006504 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006507 err = register_pernet_subsys(&addrconf_ops);
6508 if (err < 0)
6509 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006510
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006511 addrconf_wq = create_workqueue("ipv6_addrconf");
6512 if (!addrconf_wq) {
6513 err = -ENOMEM;
6514 goto out_nowq;
6515 }
6516
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517 /* The addrconf netdev notifier requires that loopback_dev
6518 * has it's ipv6 private information allocated and setup
6519 * before it can bring up and give link-local addresses
6520 * to other devices which are up.
6521 *
6522 * Unfortunately, loopback_dev is not necessarily the first
6523 * entry in the global dev_base list of net devices. In fact,
6524 * it is likely to be the very last entry on that list.
6525 * So this causes the notifier registry below to try and
6526 * give link-local addresses to all devices besides loopback_dev
6527 * first, then loopback_dev, which cases all the non-loopback_dev
6528 * devices to fail to get a link-local address.
6529 *
6530 * So, as a temporary fix, allocate the ipv6 structure for
6531 * loopback_dev first by hand.
6532 * Longer term, all of the dependencies ipv6 has upon the loopback
6533 * device and it being up should be removed.
6534 */
6535 rtnl_lock();
WANG Conga317a2f2014-07-25 15:25:09 -07006536 idev = ipv6_add_dev(init_net.loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537 rtnl_unlock();
WANG Conga317a2f2014-07-25 15:25:09 -07006538 if (IS_ERR(idev)) {
6539 err = PTR_ERR(idev);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006540 goto errlo;
WANG Conga317a2f2014-07-25 15:25:09 -07006541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542
stephen hemmingerc2e21292010-03-17 20:31:10 +00006543 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6544 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
6545
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546 register_netdevice_notifier(&ipv6_dev_notf);
6547
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006548 addrconf_verify();
Thomas Grafc127ea22007-03-22 11:58:32 -07006549
stephen hemminger3678a9d2013-12-30 10:41:32 -08006550 rtnl_af_register(&inet6_ops);
Thomas Grafb382b192010-11-16 04:33:57 +00006551
Greg Rosec7ac8672011-06-10 01:27:09 +00006552 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
6553 NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006554 if (err < 0)
6555 goto errout;
6556
6557 /* Only the first call to __rtnl_register can fail */
Greg Rosec7ac8672011-06-10 01:27:09 +00006558 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
6559 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
6560 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
6561 inet6_dump_ifaddr, NULL);
6562 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
6563 inet6_dump_ifmcaddr, NULL);
6564 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
6565 inet6_dump_ifacaddr, NULL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +00006566 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
Nicolas Dichtel7a674202013-03-05 23:42:06 +00006567 inet6_netconf_dump_devconf, NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07006568
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09006569 ipv6_addr_label_rtnl_register();
6570
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07006572errout:
Thomas Grafb382b192010-11-16 04:33:57 +00006573 rtnl_af_unregister(&inet6_ops);
Thomas Grafc127ea22007-03-22 11:58:32 -07006574 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006575errlo:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006576 destroy_workqueue(addrconf_wq);
6577out_nowq:
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006578 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006579out_addrlabel:
6580 ipv6_addr_label_cleanup();
6581out:
Thomas Grafc127ea22007-03-22 11:58:32 -07006582 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583}
6584
Daniel Lezcano09f77092007-12-13 05:34:58 -08006585void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006587 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588 int i;
6589
6590 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08006591 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00006592 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593
6594 rtnl_lock();
6595
Thomas Grafb382b192010-11-16 04:33:57 +00006596 __rtnl_af_unregister(&inet6_ops);
6597
Daniel Lezcano176c39a2009-03-03 01:06:45 -08006598 /* clean dev list */
6599 for_each_netdev(&init_net, dev) {
6600 if (__in6_dev_get(dev) == NULL)
6601 continue;
6602 addrconf_ifdown(dev, 1);
6603 }
6604 addrconf_ifdown(init_net.loopback_dev, 2);
6605
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607 * Check hash table.
6608 */
stephen hemminger5c578aed2010-03-17 20:31:11 +00006609 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00006610 for (i = 0; i < IN6_ADDR_HSIZE; i++)
6611 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578aed2010-03-17 20:31:11 +00006612 spin_unlock_bh(&addrconf_hash_lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006613 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614 rtnl_unlock();
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01006615
6616 destroy_workqueue(addrconf_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617}