blob: 53e3a9d756b0d804e873c80a820383b756a0624b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
15/*
16 * Changes:
17 *
18 * Janos Farkas : delete timer on ifdown
19 * <chexum@bankinf.banki.hu>
20 * Andi Kleen : kill double kfree on module
21 * unload.
22 * Maciej W. Rozycki : FDDI support
23 * sekiya@USAGI : Don't send too many RS
24 * packets.
25 * yoshfuji@USAGI : Fixed interval between DAD
26 * packets.
27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
28 * address validation timer.
29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
30 * support.
31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
32 * address on a same interface.
33 * YOSHIFUJI Hideaki @USAGI : ARCnet support
34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
35 * seq_file.
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -080036 * YOSHIFUJI Hideaki @USAGI : improved source address
37 * selection; consider scope,
38 * status etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
40
Joe Perchesf3213832012-05-15 14:11:53 +000041#define pr_fmt(fmt) "IPv6: " fmt
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/errno.h>
44#include <linux/types.h>
Ilpo Järvinena0bffff2009-03-21 13:36:17 -070045#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/socket.h>
47#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/net.h>
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +010049#include <linux/inet.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/in6.h>
51#include <linux/netdevice.h>
Thomas Graf18237302006-08-04 23:04:54 -070052#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/if_arp.h>
54#include <linux/if_arcnet.h>
55#include <linux/if_infiniband.h>
56#include <linux/route.h>
57#include <linux/inetdevice.h>
58#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090059#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifdef CONFIG_SYSCTL
61#include <linux/sysctl.h>
62#endif
Randy Dunlap4fc268d2006-01-11 12:17:47 -080063#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/delay.h>
65#include <linux/notifier.h>
Paulo Marques543537b2005-06-23 00:09:02 -070066#include <linux/string.h>
Eric Dumazetddbe5032012-07-18 08:11:12 +000067#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020069#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <net/sock.h>
71#include <net/snmp.h>
72
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +000073#include <net/af_ieee802154.h>
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +000074#include <net/firewire.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <net/ipv6.h>
76#include <net/protocol.h>
77#include <net/ndisc.h>
78#include <net/ip6_route.h>
79#include <net/addrconf.h>
80#include <net/tcp.h>
81#include <net/ip.h>
Thomas Graf5d620262006-08-15 00:35:02 -070082#include <net/netlink.h>
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -070083#include <net/pkt_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <linux/if_tunnel.h>
85#include <linux/rtnetlink.h>
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +000086#include <linux/netconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#include <linux/random.h>
Stephen Hemmingere21e8462010-03-20 16:09:01 -070088#include <linux/uaccess.h>
Herbert Xu7f7d9a62007-04-24 21:54:09 -070089#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91#include <linux/proc_fs.h>
92#include <linux/seq_file.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040093#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/* Set to 3 to get tracing... */
96#define ACONF_DEBUG 2
97
98#if ACONF_DEBUG >= 3
dingtianhongba3542e2013-08-17 10:27:04 +080099#define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#else
dingtianhongba3542e2013-08-17 10:27:04 +0800101#define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#endif
103
104#define INFINITY_LIFE_TIME 0xFFFFFFFF
Thomas Graf18a31e12010-11-17 04:12:02 +0000105
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100106#define IPV6_MAX_STRLEN \
107 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
108
Thomas Graf18a31e12010-11-17 04:12:02 +0000109static inline u32 cstamp_delta(unsigned long cstamp)
110{
111 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
112}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114#ifdef CONFIG_SYSCTL
WANG Conga317a2f2014-07-25 15:25:09 -0700115static int addrconf_sysctl_register(struct inet6_dev *idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800116static void addrconf_sysctl_unregister(struct inet6_dev *idev);
117#else
WANG Conga317a2f2014-07-25 15:25:09 -0700118static inline int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800119{
WANG Conga317a2f2014-07-25 15:25:09 -0700120 return 0;
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800121}
122
123static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
124{
125}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#endif
127
Sorin Dumitrueb7e0572012-08-30 02:01:45 +0000128static void __ipv6_regen_rndid(struct inet6_dev *idev);
129static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static void ipv6_regen_rndid(unsigned long data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900132static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static int ipv6_count_addresses(struct inet6_dev *idev);
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +0100134static int ipv6_generate_stable_address(struct in6_addr *addr,
135 u8 dad_count,
136 const struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/*
139 * Configured unicast address hash table
140 */
stephen hemmingerc2e21292010-03-17 20:31:10 +0000141static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
stephen hemminger5c578ae2010-03-17 20:31:11 +0000142static DEFINE_SPINLOCK(addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100144static void addrconf_verify(void);
145static void addrconf_verify_rtnl(void);
146static void addrconf_verify_work(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100148static struct workqueue_struct *addrconf_wq;
149static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
152static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
153
Jiri Pirko93d9b7d2010-03-10 10:28:56 +0000154static void addrconf_type_change(struct net_device *dev,
155 unsigned long event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static int addrconf_ifdown(struct net_device *dev, int how);
157
Romain Kuntz21caa662013-01-09 21:06:03 +0000158static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
159 int plen,
160 const struct net_device *dev,
161 u32 flags, u32 noflags);
162
David S. Millercf22f9a2012-04-14 21:37:40 -0400163static void addrconf_dad_start(struct inet6_ifaddr *ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100164static void addrconf_dad_work(struct work_struct *w);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900166static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static void addrconf_rs_timer(unsigned long data);
168static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
169static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
170
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900171static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct prefix_info *pinfo);
David S. Miller3e81c6d2010-03-20 16:18:00 -0700173static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
174 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Adrian Bunk888c8482008-07-22 14:21:58 -0700176static struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 .forwarding = 0,
178 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
179 .mtu6 = IPV6_MIN_MTU,
180 .accept_ra = 1,
181 .accept_redirects = 1,
182 .autoconf = 1,
183 .force_mld_version = 0,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200184 .mldv1_unsolicited_report_interval = 10 * HZ,
185 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 .dad_transmits = 1,
187 .rtr_solicits = MAX_RTR_SOLICITATIONS,
188 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
189 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Ian Morris67ba4152014-08-24 21:53:10 +0100190 .use_tempaddr = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 .temp_valid_lft = TEMP_VALID_LIFETIME,
192 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
193 .regen_max_retry = REGEN_MAX_RETRY,
194 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800196 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700197 .accept_ra_from_local = 0,
Hangbin Liu8013d1d2015-07-30 14:28:42 +0800198 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800199 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800200#ifdef CONFIG_IPV6_ROUTER_PREF
201 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800202 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800203#ifdef CONFIG_IPV6_ROUTE_INFO
204 .accept_ra_rt_info_max_plen = 0,
205#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800206#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700207 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700208 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900209 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900210 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200211 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700212 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100213 .stable_secret = {
214 .initialized = false,
Erik Kline3985e8a2015-07-22 16:38:25 +0900215 },
216 .use_oif_addrs_only = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217};
218
Brian Haleyab32ea52006-09-22 14:15:41 -0700219static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 .forwarding = 0,
221 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
222 .mtu6 = IPV6_MIN_MTU,
223 .accept_ra = 1,
224 .accept_redirects = 1,
225 .autoconf = 1,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200226 .force_mld_version = 0,
227 .mldv1_unsolicited_report_interval = 10 * HZ,
228 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 .dad_transmits = 1,
230 .rtr_solicits = MAX_RTR_SOLICITATIONS,
231 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
232 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 .use_tempaddr = 0,
234 .temp_valid_lft = TEMP_VALID_LIFETIME,
235 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
236 .regen_max_retry = REGEN_MAX_RETRY,
237 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800239 .accept_ra_defrtr = 1,
Ben Greeard9333192014-06-25 14:44:53 -0700240 .accept_ra_from_local = 0,
Hangbin Liu8013d1d2015-07-30 14:28:42 +0800241 .accept_ra_min_hop_limit= 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800242 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800243#ifdef CONFIG_IPV6_ROUTER_PREF
244 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800245 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800246#ifdef CONFIG_IPV6_ROUTE_INFO
247 .accept_ra_rt_info_max_plen = 0,
248#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800249#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700250 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700251 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900252 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900253 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200254 .suppress_frag_ndisc = 1,
Harout Hedeshianc2943f12015-01-20 10:06:05 -0700255 .accept_ra_mtu = 1,
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +0100256 .stable_secret = {
257 .initialized = false,
258 },
Erik Kline3985e8a2015-07-22 16:38:25 +0900259 .use_oif_addrs_only = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260};
261
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700262/* Check if a valid qdisc is available */
David S. Miller05297942008-07-08 23:01:27 -0700263static inline bool addrconf_qdisc_ok(const struct net_device *dev)
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700264{
David S. Miller05297942008-07-08 23:01:27 -0700265 return !qdisc_tx_is_noop(dev);
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700266}
267
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200268static void addrconf_del_rs_timer(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200270 if (del_timer(&idev->rs_timer))
271 __in6_dev_put(idev);
272}
273
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100274static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200275{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100276 if (cancel_delayed_work(&ifp->dad_work))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 __in6_ifa_put(ifp);
278}
279
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200280static void addrconf_mod_rs_timer(struct inet6_dev *idev,
281 unsigned long when)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200283 if (!timer_pending(&idev->rs_timer))
284 in6_dev_hold(idev);
285 mod_timer(&idev->rs_timer, jiffies + when);
286}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100288static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
289 unsigned long delay)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200290{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100291 if (!delayed_work_pending(&ifp->dad_work))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200292 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100293 mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700296static int snmp6_alloc_dev(struct inet6_dev *idev)
297{
John Stultz827da442013-10-07 15:51:58 -0700298 int i;
299
WANG Cong698365f2014-05-05 15:55:55 -0700300 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
301 if (!idev->stats.ipv6)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700302 goto err_ip;
John Stultz827da442013-10-07 15:51:58 -0700303
304 for_each_possible_cpu(i) {
305 struct ipstats_mib *addrconf_stats;
WANG Cong698365f2014-05-05 15:55:55 -0700306 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
John Stultz827da442013-10-07 15:51:58 -0700307 u64_stats_init(&addrconf_stats->syncp);
John Stultz827da442013-10-07 15:51:58 -0700308 }
309
310
Eric Dumazetbe281e52011-05-19 01:14:23 +0000311 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
312 GFP_KERNEL);
313 if (!idev->stats.icmpv6dev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700314 goto err_icmp;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000315 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
316 GFP_KERNEL);
317 if (!idev->stats.icmpv6msgdev)
David L Stevens14878f72007-09-16 16:52:35 -0700318 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700319
320 return 0;
321
David L Stevens14878f72007-09-16 16:52:35 -0700322err_icmpmsg:
Eric Dumazetbe281e52011-05-19 01:14:23 +0000323 kfree(idev->stats.icmpv6dev);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700324err_icmp:
WANG Cong698365f2014-05-05 15:55:55 -0700325 free_percpu(idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700326err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700327 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700328}
329
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000330static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 struct inet6_dev *ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700333 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 ASSERT_RTNL();
336
337 if (dev->mtu < IPV6_MIN_MTU)
WANG Conga317a2f2014-07-25 15:25:09 -0700338 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900340 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +0100341 if (!ndev)
WANG Conga317a2f2014-07-25 15:25:09 -0700342 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Ingo Oeser322f74a2006-03-20 23:01:47 -0800344 rwlock_init(&ndev->lock);
345 ndev->dev = dev;
stephen hemminger502a2ff2010-03-17 20:31:13 +0000346 INIT_LIST_HEAD(&ndev->addr_list);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200347 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
348 (unsigned long)ndev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900349 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800350 ndev->cnf.mtu6 = dev->mtu;
351 ndev->cnf.sysctl = NULL;
352 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
Ian Morris63159f22015-03-29 14:00:04 +0100353 if (!ndev->nd_parms) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800354 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700355 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800356 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700357 if (ndev->cnf.forwarding)
358 dev_disable_lro(dev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800359 /* We refer to the device */
360 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Ingo Oeser322f74a2006-03-20 23:01:47 -0800362 if (snmp6_alloc_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800363 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000364 "%s: cannot allocate memory for statistics; dev=%s.\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800365 __func__, dev->name);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800366 neigh_parms_release(&nd_tbl, ndev->nd_parms);
Roy Li8603e332011-09-20 15:10:16 -0400367 dev_put(dev);
368 kfree(ndev);
WANG Conga317a2f2014-07-25 15:25:09 -0700369 return ERR_PTR(err);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Ingo Oeser322f74a2006-03-20 23:01:47 -0800372 if (snmp6_register_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800373 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000374 "%s: cannot create /proc/net/dev_snmp6/%s\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800375 __func__, dev->name);
WANG Conga317a2f2014-07-25 15:25:09 -0700376 goto err_release;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Ingo Oeser322f74a2006-03-20 23:01:47 -0800379 /* One reference from device. We must do this before
380 * we invoke __ipv6_regen_rndid().
381 */
382 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900384 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
385 ndev->cnf.accept_dad = -1;
386
Amerigo Wang07a93622012-10-29 16:23:10 +0000387#if IS_ENABLED(CONFIG_IPV6_SIT)
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700388 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
Joe Perchesf3213832012-05-15 14:11:53 +0000389 pr_info("%s: Disabled Multicast RS\n", dev->name);
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700390 ndev->cnf.rtr_solicits = 0;
391 }
392#endif
393
stephen hemminger372e6c82010-03-17 20:31:09 +0000394 INIT_LIST_HEAD(&ndev->tempaddr_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800395 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800396 if ((dev->flags&IFF_LOOPBACK) ||
397 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700398 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700399 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700400 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800401 ndev->cnf.use_tempaddr = -1;
402 } else {
403 in6_dev_hold(ndev);
404 ipv6_regen_rndid((unsigned long) ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
David S. Miller5d9efa72013-10-28 20:07:50 -0400406
Daniel Borkmann914faa12013-04-09 03:47:14 +0000407 ndev->token = in6addr_any;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800408
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700409 if (netif_running(dev) && addrconf_qdisc_ok(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700410 ndev->if_flags |= IF_READY;
411
Ingo Oeser322f74a2006-03-20 23:01:47 -0800412 ipv6_mc_init_dev(ndev);
413 ndev->tstamp = jiffies;
WANG Conga317a2f2014-07-25 15:25:09 -0700414 err = addrconf_sysctl_register(ndev);
415 if (err) {
416 ipv6_mc_destroy_dev(ndev);
417 del_timer(&ndev->regen_timer);
418 goto err_release;
419 }
David L Stevens30c4cf52007-01-04 12:31:14 -0800420 /* protected by rtnl_lock */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000421 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800422
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000423 /* Join interface-local all-node multicast group */
424 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
425
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800426 /* Join all-node multicast group */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900427 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800428
Li Weid6ddef92012-03-05 14:45:17 +0000429 /* Join all-router multicast group if forwarding is set */
Li Wei8b2aaed2012-03-07 14:58:07 +0000430 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
Li Weid6ddef92012-03-05 14:45:17 +0000431 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return ndev;
WANG Conga317a2f2014-07-25 15:25:09 -0700434
435err_release:
436 neigh_parms_release(&nd_tbl, ndev->nd_parms);
437 ndev->dead = 1;
438 in6_dev_finish_destroy(ndev);
439 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000442static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 struct inet6_dev *idev;
445
446 ASSERT_RTNL();
447
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700448 idev = __in6_dev_get(dev);
449 if (!idev) {
450 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -0700451 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return NULL;
453 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (dev->flags&IFF_UP)
456 ipv6_mc_up(idev);
457 return idev;
458}
459
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000460static int inet6_netconf_msgsize_devconf(int type)
461{
462 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
463 + nla_total_size(4); /* NETCONFA_IFINDEX */
464
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000465 /* type -1 is used for ALL */
466 if (type == -1 || type == NETCONFA_FORWARDING)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000467 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500468#ifdef CONFIG_IPV6_MROUTE
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000469 if (type == -1 || type == NETCONFA_MC_FORWARDING)
470 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500471#endif
stephen hemmingerc92d5492013-12-17 22:37:14 -0800472 if (type == -1 || type == NETCONFA_PROXY_NEIGH)
473 size += nla_total_size(4);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000474
475 return size;
476}
477
478static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
479 struct ipv6_devconf *devconf, u32 portid,
480 u32 seq, int event, unsigned int flags,
481 int type)
482{
483 struct nlmsghdr *nlh;
484 struct netconfmsg *ncm;
485
486 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
487 flags);
Ian Morris63159f22015-03-29 14:00:04 +0100488 if (!nlh)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000489 return -EMSGSIZE;
490
491 ncm = nlmsg_data(nlh);
492 ncm->ncm_family = AF_INET6;
493
494 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
495 goto nla_put_failure;
496
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000497 /* type -1 is used for ALL */
498 if ((type == -1 || type == NETCONFA_FORWARDING) &&
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000499 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
500 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500501#ifdef CONFIG_IPV6_MROUTE
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000502 if ((type == -1 || type == NETCONFA_MC_FORWARDING) &&
503 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
504 devconf->mc_forwarding) < 0)
505 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500506#endif
stephen hemmingerc92d5492013-12-17 22:37:14 -0800507 if ((type == -1 || type == NETCONFA_PROXY_NEIGH) &&
508 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
509 goto nla_put_failure;
510
Johannes Berg053c0952015-01-16 22:09:00 +0100511 nlmsg_end(skb, nlh);
512 return 0;
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000513
514nla_put_failure:
515 nlmsg_cancel(skb, nlh);
516 return -EMSGSIZE;
517}
518
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000519void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
520 struct ipv6_devconf *devconf)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000521{
522 struct sk_buff *skb;
523 int err = -ENOBUFS;
524
525 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +0100526 if (!skb)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000527 goto errout;
528
529 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
530 RTM_NEWNETCONF, 0, type);
531 if (err < 0) {
532 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
533 WARN_ON(err == -EMSGSIZE);
534 kfree_skb(skb);
535 goto errout;
536 }
537 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC);
538 return;
539errout:
Cong Dingbd779022012-12-18 12:08:56 +0000540 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000541}
542
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000543static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
544 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
545 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
stephen hemmingerc92d5492013-12-17 22:37:14 -0800546 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000547};
548
549static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
Thomas Graf661d2962013-03-21 07:45:29 +0000550 struct nlmsghdr *nlh)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000551{
552 struct net *net = sock_net(in_skb->sk);
553 struct nlattr *tb[NETCONFA_MAX+1];
554 struct netconfmsg *ncm;
555 struct sk_buff *skb;
556 struct ipv6_devconf *devconf;
557 struct inet6_dev *in6_dev;
558 struct net_device *dev;
559 int ifindex;
560 int err;
561
562 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
563 devconf_ipv6_policy);
564 if (err < 0)
565 goto errout;
566
567 err = EINVAL;
568 if (!tb[NETCONFA_IFINDEX])
569 goto errout;
570
571 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
572 switch (ifindex) {
573 case NETCONFA_IFINDEX_ALL:
574 devconf = net->ipv6.devconf_all;
575 break;
576 case NETCONFA_IFINDEX_DEFAULT:
577 devconf = net->ipv6.devconf_dflt;
578 break;
579 default:
580 dev = __dev_get_by_index(net, ifindex);
Ian Morris63159f22015-03-29 14:00:04 +0100581 if (!dev)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000582 goto errout;
583 in6_dev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +0100584 if (!in6_dev)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000585 goto errout;
586 devconf = &in6_dev->cnf;
587 break;
588 }
589
590 err = -ENOBUFS;
591 skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +0100592 if (!skb)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000593 goto errout;
594
595 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
596 NETLINK_CB(in_skb).portid,
597 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
598 -1);
599 if (err < 0) {
600 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
601 WARN_ON(err == -EMSGSIZE);
602 kfree_skb(skb);
603 goto errout;
604 }
605 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
606errout:
607 return err;
608}
609
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000610static int inet6_netconf_dump_devconf(struct sk_buff *skb,
611 struct netlink_callback *cb)
612{
613 struct net *net = sock_net(skb->sk);
614 int h, s_h;
615 int idx, s_idx;
616 struct net_device *dev;
617 struct inet6_dev *idev;
618 struct hlist_head *head;
619
620 s_h = cb->args[0];
621 s_idx = idx = cb->args[1];
622
623 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
624 idx = 0;
625 head = &net->dev_index_head[h];
626 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000627 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
628 net->dev_base_seq;
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000629 hlist_for_each_entry_rcu(dev, head, index_hlist) {
630 if (idx < s_idx)
631 goto cont;
632 idev = __in6_dev_get(dev);
633 if (!idev)
634 goto cont;
635
636 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
637 &idev->cnf,
638 NETLINK_CB(cb->skb).portid,
639 cb->nlh->nlmsg_seq,
640 RTM_NEWNETCONF,
641 NLM_F_MULTI,
Johannes Berg053c0952015-01-16 22:09:00 +0100642 -1) < 0) {
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000643 rcu_read_unlock();
644 goto done;
645 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000646 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000647cont:
648 idx++;
649 }
650 rcu_read_unlock();
651 }
652 if (h == NETDEV_HASHENTRIES) {
653 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
654 net->ipv6.devconf_all,
655 NETLINK_CB(cb->skb).portid,
656 cb->nlh->nlmsg_seq,
657 RTM_NEWNETCONF, NLM_F_MULTI,
Johannes Berg053c0952015-01-16 22:09:00 +0100658 -1) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000659 goto done;
660 else
661 h++;
662 }
663 if (h == NETDEV_HASHENTRIES + 1) {
664 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
665 net->ipv6.devconf_dflt,
666 NETLINK_CB(cb->skb).portid,
667 cb->nlh->nlmsg_seq,
668 RTM_NEWNETCONF, NLM_F_MULTI,
Johannes Berg053c0952015-01-16 22:09:00 +0100669 -1) < 0)
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000670 goto done;
671 else
672 h++;
673 }
674done:
675 cb->args[0] = h;
676 cb->args[1] = idx;
677
678 return skb->len;
679}
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681#ifdef CONFIG_SYSCTL
682static void dev_forward_change(struct inet6_dev *idev)
683{
684 struct net_device *dev;
685 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 if (!idev)
688 return;
689 dev = idev->dev;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700690 if (idev->cnf.forwarding)
691 dev_disable_lro(dev);
Amerigo Wang1a940832012-10-28 17:43:53 +0000692 if (dev->flags & IFF_MULTICAST) {
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000693 if (idev->cnf.forwarding) {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900694 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000695 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
696 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
697 } else {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900698 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000699 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
700 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
stephen hemminger502a2ff2010-03-17 20:31:13 +0000703
704 list_for_each_entry(ifa, &idev->addr_list, if_list) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800705 if (ifa->flags&IFA_F_TENTATIVE)
706 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (idev->cnf.forwarding)
708 addrconf_join_anycast(ifa);
709 else
710 addrconf_leave_anycast(ifa);
711 }
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000712 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
713 dev->ifindex, &idev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716
Pavel Emelyanove1869322008-01-10 17:43:50 -0800717static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719 struct net_device *dev;
720 struct inet6_dev *idev;
721
Ben Hutchings4acd4942012-08-14 08:54:51 +0000722 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 idev = __in6_dev_get(dev);
724 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800725 int changed = (!idev->cnf.forwarding) ^ (!newf);
726 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (changed)
728 dev_forward_change(idev);
729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800732
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000733static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800734{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800735 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000736 int old;
737
738 if (!rtnl_trylock())
739 return restart_syscall();
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800740
741 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000742 old = *p;
743 *p = newf;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800744
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000745 if (p == &net->ipv6.devconf_dflt->forwarding) {
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000746 if ((!newf) ^ (!old))
747 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
748 NETCONFA_IFINDEX_DEFAULT,
749 net->ipv6.devconf_dflt);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000750 rtnl_unlock();
751 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +0000752 }
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000753
Pavel Emelyanove1869322008-01-10 17:43:50 -0800754 if (p == &net->ipv6.devconf_all->forwarding) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800755 net->ipv6.devconf_dflt->forwarding = newf;
756 addrconf_forward_change(net, newf);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000757 if ((!newf) ^ (!old))
758 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
759 NETCONFA_IFINDEX_ALL,
760 net->ipv6.devconf_all);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000761 } else if ((!newf) ^ (!old))
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800762 dev_forward_change((struct inet6_dev *)table->extra1);
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700763 rtnl_unlock();
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800764
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000765 if (newf)
Daniel Lezcano7b4da532008-03-04 13:47:14 -0800766 rt6_purge_dflt_routers(net);
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000767 return 1;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800768}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769#endif
770
stephen hemminger5c578ae2010-03-17 20:31:11 +0000771/* Nobody refers to this ifaddr, destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
773{
stephen hemmingerc2e21292010-03-17 20:31:10 +0000774 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776#ifdef NET_REFCNT_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +0000777 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778#endif
779
780 in6_dev_put(ifp->idev);
781
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100782 if (cancel_delayed_work(&ifp->dad_work))
783 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
784 ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Herbert Xue9d3e082010-05-18 15:36:06 -0700786 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
Joe Perchesf3213832012-05-15 14:11:53 +0000787 pr_warn("Freeing alive inet6 address %p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return;
789 }
Amerigo Wang94e187c2012-10-29 00:13:19 +0000790 ip6_rt_put(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Lai Jiangshane5785982011-03-15 18:00:14 +0800792 kfree_rcu(ifp, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
Brian Haleye55ffac2006-07-10 15:25:51 -0700795static void
796ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
797{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000798 struct list_head *p;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700799 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700800
801 /*
802 * Each device address list is sorted in order of scope -
803 * global before linklocal.
804 */
stephen hemminger502a2ff2010-03-17 20:31:13 +0000805 list_for_each(p, &idev->addr_list) {
806 struct inet6_ifaddr *ifa
807 = list_entry(p, struct inet6_ifaddr, if_list);
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700808 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700809 break;
810 }
811
David S. Millerb54c9b92010-03-25 21:25:30 -0700812 list_add_tail(&ifp->if_list, p);
Brian Haleye55ffac2006-07-10 15:25:51 -0700813}
814
Eric Dumazetddbe5032012-07-18 08:11:12 +0000815static u32 inet6_addr_hash(const struct in6_addr *addr)
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900816{
Eric Dumazetddbe5032012-07-18 08:11:12 +0000817 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900818}
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820/* On success it returns ifp with increased reference count */
821
822static struct inet6_ifaddr *
Jiri Pirko3f8f5292013-08-01 10:41:27 +0200823ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
824 const struct in6_addr *peer_addr, int pfxlen,
Jiri Benc8a226b22013-08-01 10:41:28 +0200825 int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 struct inet6_ifaddr *ifa = NULL;
828 struct rt6_info *rt;
stephen hemminger3a88a812010-03-17 20:31:12 +0000829 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 int err = 0;
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +0900831 int addr_type = ipv6_addr_type(addr);
832
833 if (addr_type == IPV6_ADDR_ANY ||
834 addr_type & IPV6_ADDR_MULTICAST ||
835 (!(idev->dev->flags & IFF_LOOPBACK) &&
836 addr_type & IPV6_ADDR_LOOPBACK))
837 return ERR_PTR(-EADDRNOTAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700839 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (idev->dead) {
841 err = -ENODEV; /*XXX*/
842 goto out2;
843 }
844
Brian Haley56d417b2009-06-01 03:07:33 -0700845 if (idev->cnf.disable_ipv6) {
Brian Haley9bdd8d42009-03-18 18:22:48 -0700846 err = -EACCES;
847 goto out2;
848 }
849
stephen hemminger5c578ae2010-03-17 20:31:11 +0000850 spin_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /* Ignore adding duplicate addresses on an interface */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900853 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
dingtianhongba3542e2013-08-17 10:27:04 +0800854 ADBG("ipv6_add_addr: already assigned\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 err = -EEXIST;
856 goto out;
857 }
858
Ingo Oeser322f74a2006-03-20 23:01:47 -0800859 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Ian Morris63159f22015-03-29 14:00:04 +0100861 if (!ifa) {
dingtianhongba3542e2013-08-17 10:27:04 +0800862 ADBG("ipv6_add_addr: malloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 err = -ENOBUFS;
864 goto out;
865 }
866
David S. Miller8f031512011-12-06 16:48:14 -0500867 rt = addrconf_dst_alloc(idev, addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (IS_ERR(rt)) {
869 err = PTR_ERR(rt);
870 goto out;
871 }
872
Jiri Pirkobba24892013-12-07 19:26:57 +0100873 neigh_parms_data_state_setall(idev->nd_parms);
874
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000875 ifa->addr = *addr;
Jiri Pirko3f8f5292013-08-01 10:41:27 +0200876 if (peer_addr)
877 ifa->peer_addr = *peer_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 spin_lock_init(&ifa->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100880 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
stephen hemmingerc2e21292010-03-17 20:31:10 +0000881 INIT_HLIST_NODE(&ifa->addr_lst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 ifa->scope = scope;
883 ifa->prefix_len = pfxlen;
884 ifa->flags = flags | IFA_F_TENTATIVE;
Jiri Benc8a226b22013-08-01 10:41:28 +0200885 ifa->valid_lft = valid_lft;
886 ifa->prefered_lft = prefered_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 ifa->cstamp = ifa->tstamp = jiffies;
Daniel Borkmann617fe292013-04-09 03:47:16 +0000888 ifa->tokenized = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Keir Fraser57f5f542006-08-29 02:43:49 -0700890 ifa->rt = rt;
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 ifa->idev = idev;
893 in6_dev_hold(idev);
894 /* For caller */
895 in6_ifa_hold(ifa);
896
897 /* Add to big hash table */
Eric Dumazetddbe5032012-07-18 08:11:12 +0000898 hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
stephen hemminger5c578ae2010-03-17 20:31:11 +0000900 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
stephen hemminger5c578ae2010-03-17 20:31:11 +0000901 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 write_lock(&idev->lock);
904 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -0700905 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (ifa->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +0000908 list_add(&ifa->tmp_list, &idev->tempaddr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 in6_ifa_hold(ifa);
910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 in6_ifa_hold(ifa);
913 write_unlock(&idev->lock);
914out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700915 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -0700917 if (likely(err == 0))
Cong Wangf88c91d2013-04-14 23:18:43 +0800918 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 else {
920 kfree(ifa);
921 ifa = ERR_PTR(err);
922 }
923
924 return ifa;
925out:
stephen hemminger5c578ae2010-03-17 20:31:11 +0000926 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 goto out2;
928}
929
Thomas Haller5b84efe2014-01-15 15:36:59 +0100930enum cleanup_prefix_rt_t {
931 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
932 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
933 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
934};
935
936/*
937 * Check, whether the prefix for ifp would still need a prefix route
938 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
939 * constants.
940 *
941 * 1) we don't purge prefix if address was not permanent.
942 * prefix is managed by its own lifetime.
943 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
944 * 3) if there are no addresses, delete prefix.
945 * 4) if there are still other permanent address(es),
946 * corresponding prefix is still permanent.
947 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
948 * don't purge the prefix, assume user space is managing it.
949 * 6) otherwise, update prefix lifetime to the
950 * longest valid lifetime among the corresponding
951 * addresses on the device.
952 * Note: subsequent RA will update lifetime.
953 **/
954static enum cleanup_prefix_rt_t
955check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
956{
957 struct inet6_ifaddr *ifa;
958 struct inet6_dev *idev = ifp->idev;
959 unsigned long lifetime;
960 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
961
962 *expires = jiffies;
963
964 list_for_each_entry(ifa, &idev->addr_list, if_list) {
965 if (ifa == ifp)
966 continue;
967 if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
968 ifp->prefix_len))
969 continue;
970 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
971 return CLEANUP_PREFIX_RT_NOP;
972
973 action = CLEANUP_PREFIX_RT_EXPIRE;
974
975 spin_lock(&ifa->lock);
976
977 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
978 /*
979 * Note: Because this address is
980 * not permanent, lifetime <
981 * LONG_MAX / HZ here.
982 */
983 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
984 *expires = ifa->tstamp + lifetime * HZ;
985 spin_unlock(&ifa->lock);
986 }
987
988 return action;
989}
990
991static void
992cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
993{
994 struct rt6_info *rt;
995
996 rt = addrconf_get_prefix_route(&ifp->addr,
997 ifp->prefix_len,
998 ifp->idev->dev,
999 0, RTF_GATEWAY | RTF_DEFAULT);
1000 if (rt) {
1001 if (del_rt)
1002 ip6_del_rt(rt);
1003 else {
1004 if (!(rt->rt6i_flags & RTF_EXPIRES))
1005 rt6_set_expires(rt, expires);
1006 ip6_rt_put(rt);
1007 }
1008 }
1009}
1010
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012/* This function wants to get referenced ifp and releases it before return */
1013
1014static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1015{
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001016 int state;
Thomas Haller5b84efe2014-01-15 15:36:59 +01001017 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
1018 unsigned long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001020 ASSERT_RTNL();
1021
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001022 spin_lock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001023 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -07001024 ifp->state = INET6_IFADDR_STATE_DEAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001025 spin_unlock_bh(&ifp->lock);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001026
1027 if (state == INET6_IFADDR_STATE_DEAD)
1028 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
stephen hemminger5c578ae2010-03-17 20:31:11 +00001030 spin_lock_bh(&addrconf_hash_lock);
1031 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578ae2010-03-17 20:31:11 +00001032 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Thomas Haller5b84efe2014-01-15 15:36:59 +01001034 write_lock_bh(&ifp->idev->lock);
David S. Miller5d9efa72013-10-28 20:07:50 -04001035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001037 list_del(&ifp->tmp_list);
1038 if (ifp->ifpub) {
1039 in6_ifa_put(ifp->ifpub);
1040 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
stephen hemminger372e6c82010-03-17 20:31:09 +00001042 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Thomas Haller5b84efe2014-01-15 15:36:59 +01001045 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1046 action = check_cleanup_prefix_route(ifp, &expires);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001047
Thomas Haller5b84efe2014-01-15 15:36:59 +01001048 list_del_init(&ifp->if_list);
1049 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Thomas Haller5b84efe2014-01-15 15:36:59 +01001051 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001053 addrconf_del_dad_work(ifp);
Andrey Vaginb2238562008-07-08 15:13:31 -07001054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 ipv6_ifa_notify(RTM_DELADDR, ifp);
1056
Cong Wangf88c91d2013-04-14 23:18:43 +08001057 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Thomas Haller5b84efe2014-01-15 15:36:59 +01001059 if (action != CLEANUP_PREFIX_RT_NOP) {
1060 cleanup_prefix_route(ifp, expires,
1061 action == CLEANUP_PREFIX_RT_DEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063
Daniel Walterc3968a82011-04-13 21:10:57 +00001064 /* clean up prefsrc entries */
1065 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001066out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 in6_ifa_put(ifp);
1068}
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1071{
1072 struct inet6_dev *idev = ifp->idev;
1073 struct in6_addr addr, *tmpaddr;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001074 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001075 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 int tmp_plen;
1077 int ret = 0;
Neil Horman95c385b2007-04-25 17:08:10 -07001078 u32 addr_flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001079 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Jiri Pirko53bd6742013-12-06 09:45:22 +01001081 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (ift) {
1083 spin_lock_bh(&ift->lock);
1084 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1085 spin_unlock_bh(&ift->lock);
1086 tmpaddr = &addr;
1087 } else {
1088 tmpaddr = NULL;
1089 }
1090retry:
1091 in6_dev_hold(idev);
1092 if (idev->cnf.use_tempaddr <= 0) {
Jiri Pirko53bd6742013-12-06 09:45:22 +01001093 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001094 pr_info("%s: use_tempaddr is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 in6_dev_put(idev);
1096 ret = -1;
1097 goto out;
1098 }
1099 spin_lock_bh(&ifp->lock);
1100 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1101 idev->cnf.use_tempaddr = -1; /*XXX*/
1102 spin_unlock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01001103 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001104 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1105 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 in6_dev_put(idev);
1107 ret = -1;
1108 goto out;
1109 }
1110 in6_ifa_hold(ifp);
1111 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001112 __ipv6_try_regen_rndid(idev, tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001114 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 tmp_valid_lft = min_t(__u32,
1116 ifp->valid_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +00001117 idev->cnf.temp_valid_lft + age);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001118 tmp_prefered_lft = min_t(__u32,
1119 ifp->prefered_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +00001120 idev->cnf.temp_prefered_lft + age -
Ben Hutchings784e2712010-06-26 11:42:55 +00001121 idev->cnf.max_desync_factor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 tmp_plen = ifp->prefix_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 tmp_tstamp = ifp->tstamp;
1124 spin_unlock_bh(&ifp->lock);
1125
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001126 regen_advance = idev->cnf.regen_max_retry *
Ian Morris67ba4152014-08-24 21:53:10 +01001127 idev->cnf.dad_transmits *
1128 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
Jiri Pirko53bd6742013-12-06 09:45:22 +01001129 write_unlock_bh(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -07001130
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001131 /* A temporary address is created only if this calculated Preferred
1132 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1133 * an implementation must not create a temporary address with a zero
1134 * Preferred Lifetime.
Heiner Kallweitecab6702014-03-12 22:13:19 +01001135 * Use age calculation as in addrconf_verify to avoid unnecessary
1136 * temporary addresses being generated.
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001137 */
Heiner Kallweitecab6702014-03-12 22:13:19 +01001138 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1139 if (tmp_prefered_lft <= regen_advance + age) {
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001140 in6_ifa_put(ifp);
1141 in6_dev_put(idev);
1142 ret = -1;
1143 goto out;
1144 }
1145
Neil Horman95c385b2007-04-25 17:08:10 -07001146 addr_flags = IFA_F_TEMPORARY;
1147 /* set in addrconf_prefix_rcv() */
1148 if (ifp->flags & IFA_F_OPTIMISTIC)
1149 addr_flags |= IFA_F_OPTIMISTIC;
1150
Hannes Frederic Sowa4b08a8f2013-08-16 13:02:27 +02001151 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1152 ipv6_addr_scope(&addr), addr_flags,
1153 tmp_valid_lft, tmp_prefered_lft);
1154 if (IS_ERR(ift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 in6_ifa_put(ifp);
1156 in6_dev_put(idev);
Joe Perchesf3213832012-05-15 14:11:53 +00001157 pr_info("%s: retry temporary address regeneration\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 tmpaddr = &addr;
Jiri Pirko53bd6742013-12-06 09:45:22 +01001159 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 goto retry;
1161 }
1162
1163 spin_lock_bh(&ift->lock);
1164 ift->ifpub = ifp;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001165 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 ift->tstamp = tmp_tstamp;
1167 spin_unlock_bh(&ift->lock);
1168
David S. Millercf22f9a2012-04-14 21:37:40 -04001169 addrconf_dad_start(ift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 in6_ifa_put(ift);
1171 in6_dev_put(idev);
1172out:
1173 return ret;
1174}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001177 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001179enum {
1180 IPV6_SADDR_RULE_INIT = 0,
1181 IPV6_SADDR_RULE_LOCAL,
1182 IPV6_SADDR_RULE_SCOPE,
1183 IPV6_SADDR_RULE_PREFERRED,
1184#ifdef CONFIG_IPV6_MIP6
1185 IPV6_SADDR_RULE_HOA,
1186#endif
1187 IPV6_SADDR_RULE_OIF,
1188 IPV6_SADDR_RULE_LABEL,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001189 IPV6_SADDR_RULE_PRIVACY,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001190 IPV6_SADDR_RULE_ORCHID,
1191 IPV6_SADDR_RULE_PREFIX,
Erik Kline7fd25612014-10-28 18:11:14 +09001192#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1193 IPV6_SADDR_RULE_NOT_OPTIMISTIC,
1194#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001195 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001196};
1197
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001198struct ipv6_saddr_score {
1199 int rule;
1200 int addr_type;
1201 struct inet6_ifaddr *ifa;
1202 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1203 int scopedist;
1204 int matchlen;
1205};
1206
1207struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001208 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001209 int ifindex;
1210 int scope;
1211 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001212 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001213};
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001214
Dave Jonesb6f99a22007-03-22 12:27:49 -07001215static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Ulrich Weber45bb0062010-02-25 23:28:58 +00001217 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001218 return 1;
1219 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
Erik Kline7fd25612014-10-28 18:11:14 +09001222static inline bool ipv6_use_optimistic_addr(struct inet6_dev *idev)
1223{
1224#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1225 return idev && idev->cnf.optimistic_dad && idev->cnf.use_optimistic;
1226#else
1227 return false;
1228#endif
1229}
1230
Benjamin Thery3de23252008-05-28 14:51:24 +02001231static int ipv6_get_saddr_eval(struct net *net,
1232 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001233 struct ipv6_saddr_dst *dst,
1234 int i)
1235{
1236 int ret;
1237
1238 if (i <= score->rule) {
1239 switch (i) {
1240 case IPV6_SADDR_RULE_SCOPE:
1241 ret = score->scopedist;
1242 break;
1243 case IPV6_SADDR_RULE_PREFIX:
1244 ret = score->matchlen;
1245 break;
1246 default:
1247 ret = !!test_bit(i, score->scorebits);
1248 }
1249 goto out;
1250 }
1251
1252 switch (i) {
1253 case IPV6_SADDR_RULE_INIT:
1254 /* Rule 0: remember if hiscore is not ready yet */
1255 ret = !!score->ifa;
1256 break;
1257 case IPV6_SADDR_RULE_LOCAL:
1258 /* Rule 1: Prefer same address */
1259 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1260 break;
1261 case IPV6_SADDR_RULE_SCOPE:
1262 /* Rule 2: Prefer appropriate scope
1263 *
1264 * ret
1265 * ^
1266 * -1 | d 15
1267 * ---+--+-+---> scope
1268 * |
1269 * | d is scope of the destination.
1270 * B-d | \
1271 * | \ <- smaller scope is better if
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08001272 * B-15 | \ if scope is enough for destination.
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001273 * | ret = B - scope (-1 <= scope >= d <= 15).
1274 * d-C-1 | /
1275 * |/ <- greater is better
1276 * -C / if scope is not enough for destination.
1277 * /| ret = scope - C (-1 <= d < scope <= 15).
1278 *
1279 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1280 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1281 * Assume B = 0 and we get C > 29.
1282 */
1283 ret = __ipv6_addr_src_scope(score->addr_type);
1284 if (ret >= dst->scope)
1285 ret = -ret;
1286 else
1287 ret -= 128; /* 30 is enough */
1288 score->scopedist = ret;
1289 break;
1290 case IPV6_SADDR_RULE_PREFERRED:
Erik Kline7fd25612014-10-28 18:11:14 +09001291 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001292 /* Rule 3: Avoid deprecated and optimistic addresses */
Erik Kline7fd25612014-10-28 18:11:14 +09001293 u8 avoid = IFA_F_DEPRECATED;
1294
1295 if (!ipv6_use_optimistic_addr(score->ifa->idev))
1296 avoid |= IFA_F_OPTIMISTIC;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001297 ret = ipv6_saddr_preferred(score->addr_type) ||
Erik Kline7fd25612014-10-28 18:11:14 +09001298 !(score->ifa->flags & avoid);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001299 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001300 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001301#ifdef CONFIG_IPV6_MIP6
1302 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001303 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001304 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001305 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1306 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001307 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001308 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001309#endif
1310 case IPV6_SADDR_RULE_OIF:
1311 /* Rule 5: Prefer outgoing interface */
1312 ret = (!dst->ifindex ||
1313 dst->ifindex == score->ifa->idev->dev->ifindex);
1314 break;
1315 case IPV6_SADDR_RULE_LABEL:
1316 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001317 ret = ipv6_addr_label(net,
1318 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001319 score->ifa->idev->dev->ifindex) == dst->label;
1320 break;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001321 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001322 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001323 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001324 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001325 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001326 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1327 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1328 score->ifa->idev->cnf.use_tempaddr >= 2;
1329 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001330 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001331 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001332 case IPV6_SADDR_RULE_ORCHID:
1333 /* Rule 8-: Prefer ORCHID vs ORCHID or
1334 * non-ORCHID vs non-ORCHID
1335 */
1336 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1337 ipv6_addr_orchid(dst->addr));
1338 break;
1339 case IPV6_SADDR_RULE_PREFIX:
1340 /* Rule 8: Use longest matching prefix */
YOSHIFUJI Hideaki / 吉藤英明91b4b042012-09-10 18:41:07 +00001341 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1342 if (ret > score->ifa->prefix_len)
1343 ret = score->ifa->prefix_len;
1344 score->matchlen = ret;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001345 break;
Erik Kline7fd25612014-10-28 18:11:14 +09001346#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1347 case IPV6_SADDR_RULE_NOT_OPTIMISTIC:
1348 /* Optimistic addresses still have lower precedence than other
1349 * preferred addresses.
1350 */
1351 ret = !(score->ifa->flags & IFA_F_OPTIMISTIC);
1352 break;
1353#endif
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001354 default:
1355 ret = 0;
1356 }
1357
1358 if (ret)
1359 __set_bit(i, score->scorebits);
1360 score->rule = i;
1361out:
1362 return ret;
1363}
1364
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001365static int __ipv6_dev_get_saddr(struct net *net,
1366 struct ipv6_saddr_dst *dst,
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001367 struct inet6_dev *idev,
1368 struct ipv6_saddr_score *scores,
1369 int hiscore_idx)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001370{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001371 struct ipv6_saddr_score *score = &scores[1 - hiscore_idx], *hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001372
1373 read_lock_bh(&idev->lock);
1374 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
1375 int i;
1376
1377 /*
1378 * - Tentative Address (RFC2462 section 5.4)
1379 * - A tentative address is not considered
1380 * "assigned to an interface" in the traditional
1381 * sense, unless it is also flagged as optimistic.
1382 * - Candidate Source Address (section 4)
1383 * - In any case, anycast addresses, multicast
1384 * addresses, and the unspecified address MUST
1385 * NOT be included in a candidate set.
1386 */
1387 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1388 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
1389 continue;
1390
1391 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1392
1393 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1394 score->addr_type & IPV6_ADDR_MULTICAST)) {
1395 net_dbg_ratelimited("ADDRCONF: unspecified / multicast address assigned as unicast address on %s",
1396 idev->dev->name);
1397 continue;
1398 }
1399
1400 score->rule = -1;
1401 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
1402
1403 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1404 int minihiscore, miniscore;
1405
1406 minihiscore = ipv6_get_saddr_eval(net, hiscore, dst, i);
1407 miniscore = ipv6_get_saddr_eval(net, score, dst, i);
1408
1409 if (minihiscore > miniscore) {
1410 if (i == IPV6_SADDR_RULE_SCOPE &&
1411 score->scopedist > 0) {
1412 /*
1413 * special case:
1414 * each remaining entry
1415 * has too small (not enough)
1416 * scope, because ifa entries
1417 * are sorted by their scope
1418 * values.
1419 */
1420 goto out;
1421 }
1422 break;
1423 } else if (minihiscore < miniscore) {
1424 if (hiscore->ifa)
1425 in6_ifa_put(hiscore->ifa);
1426
1427 in6_ifa_hold(score->ifa);
1428
1429 swap(hiscore, score);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001430 hiscore_idx = 1 - hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001431
1432 /* restore our iterator */
1433 score->ifa = hiscore->ifa;
1434
1435 break;
1436 }
1437 }
1438 }
1439out:
1440 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001441 return hiscore_idx;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001442}
1443
Patrick McHardyb3f644f2012-08-26 19:14:14 +02001444int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001445 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001446 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001448 struct ipv6_saddr_score scores[2], *hiscore;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001449 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001450 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001451 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001452 int dst_type;
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001453 bool use_oif_addr = false;
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001454 int hiscore_idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001456 dst_type = __ipv6_addr_type(daddr);
1457 dst.addr = daddr;
1458 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1459 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001460 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001461 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001462
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001463 scores[hiscore_idx].rule = -1;
1464 scores[hiscore_idx].ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001466 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001467
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001468 /* Candidate Source Address (section 4)
1469 * - multicast and link-local destination address,
1470 * the set of candidate source address MUST only
1471 * include addresses assigned to interfaces
1472 * belonging to the same link as the outgoing
1473 * interface.
1474 * (- For site-local destination addresses, the
1475 * set of candidate source addresses MUST only
1476 * include addresses assigned to interfaces
1477 * belonging to the same site as the outgoing
1478 * interface.)
Erik Kline3985e8a2015-07-22 16:38:25 +09001479 * - "It is RECOMMENDED that the candidate source addresses
1480 * be the set of unicast addresses assigned to the
1481 * interface that will be used to send to the destination
1482 * (the 'outgoing' interface)." (RFC 6724)
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001483 */
1484 if (dst_dev) {
Erik Kline3985e8a2015-07-22 16:38:25 +09001485 idev = __in6_dev_get(dst_dev);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001486 if ((dst_type & IPV6_ADDR_MULTICAST) ||
Erik Kline3985e8a2015-07-22 16:38:25 +09001487 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL ||
1488 (idev && idev->cnf.use_oif_addrs_only)) {
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001489 use_oif_addr = true;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001490 }
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001491 }
1492
1493 if (use_oif_addr) {
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001494 if (idev)
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001495 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001496 } else {
1497 for_each_netdev_rcu(net, dev) {
1498 idev = __in6_dev_get(dev);
1499 if (!idev)
1500 continue;
YOSHIFUJI Hideakic15df302015-07-16 16:51:30 +09001501 hiscore_idx = __ipv6_dev_get_saddr(net, &dst, idev, scores, hiscore_idx);
YOSHIFUJI Hideaki/吉藤英明9131f3d2015-07-10 16:58:31 +09001502 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001503 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001504 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
YOSHIFUJI Hideaki/吉藤英明c0b8da12015-07-13 23:28:10 +09001506 hiscore = &scores[hiscore_idx];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001507 if (!hiscore->ifa)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001508 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001509
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001510 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001511 in6_ifa_put(hiscore->ifa);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001514EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Amerigo Wang89657792013-06-29 21:30:49 +08001516int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001517 u32 banned_flags)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001518{
1519 struct inet6_ifaddr *ifp;
1520 int err = -EADDRNOTAVAIL;
1521
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01001522 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1523 if (ifp->scope > IFA_LINK)
1524 break;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001525 if (ifp->scope == IFA_LINK &&
1526 !(ifp->flags & banned_flags)) {
1527 *addr = ifp->addr;
1528 err = 0;
1529 break;
1530 }
1531 }
1532 return err;
1533}
1534
Neil Horman95c385b2007-04-25 17:08:10 -07001535int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001536 u32 banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
1538 struct inet6_dev *idev;
1539 int err = -EADDRNOTAVAIL;
1540
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001541 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001542 idev = __in6_dev_get(dev);
1543 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 read_lock_bh(&idev->lock);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001545 err = __ipv6_get_lladdr(idev, addr, banned_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 read_unlock_bh(&idev->lock);
1547 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001548 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 return err;
1550}
1551
1552static int ipv6_count_addresses(struct inet6_dev *idev)
1553{
1554 int cnt = 0;
1555 struct inet6_ifaddr *ifp;
1556
1557 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001558 list_for_each_entry(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 cnt++;
1560 read_unlock_bh(&idev->lock);
1561 return cnt;
1562}
1563
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001564int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Florian Westphal2a7851b2013-05-17 03:56:10 +00001565 const struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
Erik Klinec58da4c2015-02-04 20:01:23 +09001567 return ipv6_chk_addr_and_flags(net, addr, dev, strict, IFA_F_TENTATIVE);
1568}
1569EXPORT_SYMBOL(ipv6_chk_addr);
1570
1571int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1572 const struct net_device *dev, int strict,
1573 u32 banned_flags)
1574{
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001575 struct inet6_ifaddr *ifp;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001576 unsigned int hash = inet6_addr_hash(addr);
Erik Klinec58da4c2015-02-04 20:01:23 +09001577 u32 ifp_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
stephen hemminger5c578ae2010-03-17 20:31:11 +00001579 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001580 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001581 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001582 continue;
Erik Klinec58da4c2015-02-04 20:01:23 +09001583 /* Decouple optimistic from tentative for evaluation here.
1584 * Ban optimistic addresses explicitly, when required.
1585 */
1586 ifp_flags = (ifp->flags&IFA_F_OPTIMISTIC)
1587 ? (ifp->flags&~IFA_F_TENTATIVE)
1588 : ifp->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (ipv6_addr_equal(&ifp->addr, addr) &&
Erik Klinec58da4c2015-02-04 20:01:23 +09001590 !(ifp_flags&banned_flags) &&
Ian Morris63159f22015-03-29 14:00:04 +01001591 (!dev || ifp->idev->dev == dev ||
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001592 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1593 rcu_read_unlock_bh();
1594 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 }
1596 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001597
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001598 rcu_read_unlock_bh();
1599 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600}
Erik Klinec58da4c2015-02-04 20:01:23 +09001601EXPORT_SYMBOL(ipv6_chk_addr_and_flags);
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001602
David S. Miller3e81c6d2010-03-20 16:18:00 -07001603static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1604 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
Eric Dumazetddbe5032012-07-18 08:11:12 +00001606 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001607 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Sasha Levinb67bfe02013-02-27 17:06:00 -08001609 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001610 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001611 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001613 if (!dev || ifp->idev->dev == dev)
David S. Miller3e81c6d2010-03-20 16:18:00 -07001614 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
1616 }
David S. Miller3e81c6d2010-03-20 16:18:00 -07001617 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001620/* Compares an address/prefix_len with addresses on device @dev.
1621 * If one is found it returns true.
1622 */
1623bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1624 const unsigned int prefix_len, struct net_device *dev)
1625{
1626 struct inet6_dev *idev;
1627 struct inet6_ifaddr *ifa;
1628 bool ret = false;
1629
1630 rcu_read_lock();
1631 idev = __in6_dev_get(dev);
1632 if (idev) {
1633 read_lock_bh(&idev->lock);
1634 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1635 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1636 if (ret)
1637 break;
1638 }
1639 read_unlock_bh(&idev->lock);
1640 }
1641 rcu_read_unlock();
1642
1643 return ret;
1644}
1645EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1646
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001647int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001648{
1649 struct inet6_dev *idev;
1650 struct inet6_ifaddr *ifa;
1651 int onlink;
1652
1653 onlink = 0;
1654 rcu_read_lock();
1655 idev = __in6_dev_get(dev);
1656 if (idev) {
1657 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001658 list_for_each_entry(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001659 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1660 ifa->prefix_len);
1661 if (onlink)
1662 break;
1663 }
1664 read_unlock_bh(&idev->lock);
1665 }
1666 rcu_read_unlock();
1667 return onlink;
1668}
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001669EXPORT_SYMBOL(ipv6_chk_prefix);
1670
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001671struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001672 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
David S. Millerb79d1d52010-03-25 21:39:21 -07001674 struct inet6_ifaddr *ifp, *result = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001675 unsigned int hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
stephen hemminger5c578ae2010-03-17 20:31:11 +00001677 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001678 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001679 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001680 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 if (ipv6_addr_equal(&ifp->addr, addr)) {
Ian Morris63159f22015-03-29 14:00:04 +01001682 if (!dev || ifp->idev->dev == dev ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07001684 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 in6_ifa_hold(ifp);
1686 break;
1687 }
1688 }
1689 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001690 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
David S. Millerb79d1d52010-03-25 21:39:21 -07001692 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695/* Gets referenced address, destroys ifaddr */
1696
Brian Haleycc411d02009-09-09 14:41:32 +00001697static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (ifp->flags&IFA_F_PERMANENT) {
1700 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001701 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 ifp->flags |= IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00001703 if (dad_failed)
1704 ifp->flags |= IFA_F_DADFAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 spin_unlock_bh(&ifp->lock);
Herbert Xue2577a02010-03-13 12:23:29 -08001706 if (dad_failed)
1707 ipv6_ifa_notify(0, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 } else if (ifp->flags&IFA_F_TEMPORARY) {
1710 struct inet6_ifaddr *ifpub;
1711 spin_lock_bh(&ifp->lock);
1712 ifpub = ifp->ifpub;
1713 if (ifpub) {
1714 in6_ifa_hold(ifpub);
1715 spin_unlock_bh(&ifp->lock);
1716 ipv6_create_tempaddr(ifpub, ifp);
1717 in6_ifa_put(ifpub);
1718 } else {
1719 spin_unlock_bh(&ifp->lock);
1720 }
1721 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001722 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
Herbert Xuf2344a12010-05-18 15:55:27 -07001727static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1728{
1729 int err = -ENOENT;
1730
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001731 spin_lock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001732 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1733 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1734 err = 0;
1735 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001736 spin_unlock_bh(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001737
1738 return err;
1739}
1740
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001741void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1742{
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001743 struct in6_addr addr;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001744 struct inet6_dev *idev = ifp->idev;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001745 struct net *net = dev_net(ifp->idev->dev);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001746
Ursula Braun853dc2e2010-10-24 23:06:43 +00001747 if (addrconf_dad_end(ifp)) {
1748 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07001749 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00001750 }
Herbert Xuf2344a12010-05-18 15:55:27 -07001751
Joe Perchese87cc472012-05-13 21:56:26 +00001752 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1753 ifp->idev->dev->name, &ifp->addr);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001754
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001755 spin_lock_bh(&ifp->lock);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001756
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001757 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
1758 int scope = ifp->scope;
1759 u32 flags = ifp->flags;
1760 struct in6_addr new_addr;
1761 struct inet6_ifaddr *ifp2;
1762 u32 valid_lft, preferred_lft;
1763 int pfxlen = ifp->prefix_len;
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001764 int retries = ifp->stable_privacy_retry + 1;
1765
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001766 if (retries > net->ipv6.sysctl.idgen_retries) {
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001767 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
1768 ifp->idev->dev->name);
1769 goto errdad;
1770 }
1771
1772 new_addr = ifp->addr;
1773 if (ipv6_generate_stable_address(&new_addr, retries,
1774 idev))
1775 goto errdad;
1776
1777 valid_lft = ifp->valid_lft;
1778 preferred_lft = ifp->prefered_lft;
1779
1780 spin_unlock_bh(&ifp->lock);
1781
1782 if (idev->cnf.max_addresses &&
1783 ipv6_count_addresses(idev) >=
1784 idev->cnf.max_addresses)
1785 goto lock_errdad;
1786
1787 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
1788 ifp->idev->dev->name);
1789
1790 ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
1791 scope, flags, valid_lft,
1792 preferred_lft);
1793 if (IS_ERR(ifp2))
1794 goto lock_errdad;
1795
1796 spin_lock_bh(&ifp2->lock);
1797 ifp2->stable_privacy_retry = retries;
1798 ifp2->state = INET6_IFADDR_STATE_PREDAD;
1799 spin_unlock_bh(&ifp2->lock);
1800
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01001801 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001802 in6_ifa_put(ifp2);
1803lock_errdad:
1804 spin_lock_bh(&ifp->lock);
1805 } else if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001806 addr.s6_addr32[0] = htonl(0xfe800000);
1807 addr.s6_addr32[1] = 0;
1808
1809 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
1810 ipv6_addr_equal(&ifp->addr, &addr)) {
1811 /* DAD failed for link-local based on MAC address */
1812 idev->cnf.disable_ipv6 = 1;
Brian Haley9bdd8d42009-03-18 18:22:48 -07001813
Joe Perchesf3213832012-05-15 14:11:53 +00001814 pr_info("%s: IPv6 being disabled!\n",
Brian Haley9bdd8d42009-03-18 18:22:48 -07001815 ifp->idev->dev->name);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001816 }
1817 }
1818
Hannes Frederic Sowa5f40ef72015-03-23 23:36:04 +01001819errdad:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001820 /* transition from _POSTDAD to _ERRDAD */
1821 ifp->state = INET6_IFADDR_STATE_ERRDAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01001822 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001823
1824 addrconf_mod_dad_work(ifp, 0);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001825}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02001827/* Join to solicited addr multicast group.
1828 * caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001829void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
1831 struct in6_addr maddr;
1832
1833 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1834 return;
1835
1836 addrconf_addr_solict_mult(addr, &maddr);
1837 ipv6_dev_mc_inc(dev, &maddr);
1838}
1839
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02001840/* caller must hold RTNL */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001841void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
1843 struct in6_addr maddr;
1844
1845 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1846 return;
1847
1848 addrconf_addr_solict_mult(addr, &maddr);
1849 __ipv6_dev_mc_dec(idev, &maddr);
1850}
1851
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02001852/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001853static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
1855 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001856
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01001857 if (ifp->prefix_len >= 127) /* RFC 6164 */
Bjørn Mork2bda8a02011-07-05 23:04:13 +00001858 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1860 if (ipv6_addr_any(&addr))
1861 return;
WANG Cong013b4d92014-09-11 15:35:11 -07001862 __ipv6_dev_ac_inc(ifp->idev, &addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
Sabrina Dubrocaa9ed4a22014-09-02 10:29:29 +02001865/* caller must hold RTNL */
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001866static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
1868 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001869
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01001870 if (ifp->prefix_len >= 127) /* RFC 6164 */
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00001871 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1873 if (ipv6_addr_any(&addr))
1874 return;
1875 __ipv6_dev_ac_dec(ifp->idev, &addr);
1876}
1877
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001878static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1879{
1880 if (dev->addr_len != ETH_ALEN)
1881 return -1;
1882 memcpy(eui, dev->dev_addr, 3);
1883 memcpy(eui + 5, dev->dev_addr + 3, 3);
1884
1885 /*
1886 * The zSeries OSA network cards can be shared among various
1887 * OS instances, but the OSA cards have only one MAC address.
1888 * This leads to duplicate address conflicts in conjunction
1889 * with IPv6 if more than one instance uses the same card.
1890 *
1891 * The driver for these cards can deliver a unique 16-bit
1892 * identifier for each instance sharing the same card. It is
1893 * placed instead of 0xFFFE in the interface identifier. The
1894 * "u" bit of the interface identifier is not inverted in this
1895 * case. Hence the resulting interface identifier has local
1896 * scope according to RFC2373.
1897 */
1898 if (dev->dev_id) {
1899 eui[3] = (dev->dev_id >> 8) & 0xFF;
1900 eui[4] = dev->dev_id & 0xFF;
1901 } else {
1902 eui[3] = 0xFF;
1903 eui[4] = 0xFE;
1904 eui[0] ^= 2;
1905 }
1906 return 0;
1907}
1908
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00001909static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
1910{
1911 if (dev->addr_len != IEEE802154_ADDR_LEN)
1912 return -1;
1913 memcpy(eui, dev->dev_addr, 8);
YOSHIFUJI Hideaki / 吉藤英明5e98a362013-01-28 10:44:29 +00001914 eui[0] ^= 2;
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00001915 return 0;
1916}
1917
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00001918static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
1919{
1920 union fwnet_hwaddr *ha;
1921
1922 if (dev->addr_len != FWNET_ALEN)
1923 return -1;
1924
1925 ha = (union fwnet_hwaddr *)dev->dev_addr;
1926
1927 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
1928 eui[0] ^= 2;
1929 return 0;
1930}
1931
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001932static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1933{
1934 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1935 if (dev->addr_len != ARCNET_ALEN)
1936 return -1;
1937 memset(eui, 0, 7);
Eldad Zack8e5e8f32012-04-01 07:49:08 +00001938 eui[7] = *(u8 *)dev->dev_addr;
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001939 return 0;
1940}
1941
1942static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1943{
1944 if (dev->addr_len != INFINIBAND_ALEN)
1945 return -1;
1946 memcpy(eui, dev->dev_addr + 12, 8);
1947 eui[0] |= 2;
1948 return 0;
1949}
1950
stephen hemmingerc61393e2010-10-04 20:17:53 +00001951static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001952{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00001953 if (addr == 0)
1954 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001955 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1956 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1957 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1958 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1959 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1960 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1961 eui[1] = 0;
1962 eui[2] = 0x5E;
1963 eui[3] = 0xFE;
1964 memcpy(eui + 4, &addr, 4);
1965 return 0;
1966}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001967
1968static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1969{
1970 if (dev->priv_flags & IFF_ISATAP)
1971 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1972 return -1;
1973}
1974
stephen hemmingeraee80b52011-06-08 10:44:30 +00001975static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1976{
1977 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1978}
1979
Nicolas Dichtele8377352013-08-20 12:16:06 +02001980static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
1981{
1982 memcpy(eui, dev->perm_addr, 3);
1983 memcpy(eui + 5, dev->perm_addr + 3, 3);
1984 eui[3] = 0xFF;
1985 eui[4] = 0xFE;
1986 eui[0] ^= 2;
1987 return 0;
1988}
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1991{
1992 switch (dev->type) {
1993 case ARPHRD_ETHER:
1994 case ARPHRD_FDDI:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001995 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001997 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001999 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002000 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09002001 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002002 case ARPHRD_IPGRE:
2003 return addrconf_ifid_gre(eui, dev);
Jukka Rissanene74bccb2013-12-11 17:05:36 +02002004 case ARPHRD_6LOWPAN:
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002005 case ARPHRD_IEEE802154:
2006 return addrconf_ifid_eui64(eui, dev);
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002007 case ARPHRD_IEEE1394:
2008 return addrconf_ifid_ieee1394(eui, dev);
Nicolas Dichtele8377352013-08-20 12:16:06 +02002009 case ARPHRD_TUNNEL6:
2010 return addrconf_ifid_ip6tnl(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
2012 return -1;
2013}
2014
2015static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
2016{
2017 int err = -1;
2018 struct inet6_ifaddr *ifp;
2019
2020 read_lock_bh(&idev->lock);
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01002021 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
2022 if (ifp->scope > IFA_LINK)
2023 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
2025 memcpy(eui, ifp->addr.s6_addr+8, 8);
2026 err = 0;
2027 break;
2028 }
2029 }
2030 read_unlock_bh(&idev->lock);
2031 return err;
2032}
2033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00002035static void __ipv6_regen_rndid(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08002038 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
2041 /*
2042 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
2043 * check if generated address is not inappropriate
2044 *
2045 * - Reserved subnet anycast (RFC 2526)
2046 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002047 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 * 00-00-5E-FE-xx-xx-xx-xx
2049 * - value 0
2050 * - XXX: already assigned to an address on the device
2051 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002052 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
2054 (idev->rndid[7]&0x80))
2055 goto regen;
2056 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
2057 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
2058 goto regen;
2059 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
2060 goto regen;
2061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
2063
2064static void ipv6_regen_rndid(unsigned long data)
2065{
2066 struct inet6_dev *idev = (struct inet6_dev *) data;
2067 unsigned long expires;
2068
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002069 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 write_lock_bh(&idev->lock);
2071
2072 if (idev->dead)
2073 goto out;
2074
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00002075 __ipv6_regen_rndid(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 expires = jiffies +
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002078 idev->cnf.temp_prefered_lft * HZ -
Jiri Pirko1f9248e52013-12-07 19:26:53 +01002079 idev->cnf.regen_max_retry * idev->cnf.dad_transmits *
2080 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) -
Ben Hutchings784e2712010-06-26 11:42:55 +00002081 idev->cnf.max_desync_factor * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 if (time_before(expires, jiffies)) {
Joe Perchesf3213832012-05-15 14:11:53 +00002083 pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
2084 __func__, idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 goto out;
2086 }
2087
2088 if (!mod_timer(&idev->regen_timer, expires))
2089 in6_dev_hold(idev);
2090
2091out:
2092 write_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002093 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 in6_dev_put(idev);
2095}
2096
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00002097static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002098{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00002100 __ipv6_regen_rndid(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103/*
2104 * Add prefix route.
2105 */
2106
2107static void
2108addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002109 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110{
Thomas Graf86872cb2006-08-22 00:01:08 -07002111 struct fib6_config cfg = {
2112 .fc_table = RT6_TABLE_PREFIX,
2113 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2114 .fc_ifindex = dev->ifindex,
2115 .fc_expires = expires,
2116 .fc_dst_len = plen,
2117 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002118 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07002119 .fc_protocol = RTPROT_KERNEL,
Thomas Graf86872cb2006-08-22 00:01:08 -07002120 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00002122 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 /* Prevent useless cloning on PtP SIT.
2125 This thing is done here expecting that the whole
2126 class of non-broadcast devices need not cloning.
2127 */
Amerigo Wang07a93622012-10-29 16:23:10 +00002128#if IS_ENABLED(CONFIG_IPV6_SIT)
Thomas Graf86872cb2006-08-22 00:01:08 -07002129 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
2130 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002131#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Thomas Graf86872cb2006-08-22 00:01:08 -07002133 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002136
2137static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2138 int plen,
2139 const struct net_device *dev,
2140 u32 flags, u32 noflags)
2141{
2142 struct fib6_node *fn;
2143 struct rt6_info *rt = NULL;
2144 struct fib6_table *table;
2145
2146 table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
Ian Morris63159f22015-03-29 14:00:04 +01002147 if (!table)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002148 return NULL;
2149
Li RongQing5744dd92012-09-11 21:59:01 +00002150 read_lock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002151 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2152 if (!fn)
2153 goto out;
Martin KaFai Lau1f56a012015-04-28 13:03:03 -07002154
2155 noflags |= RTF_CACHE;
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002156 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002157 if (rt->dst.dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002158 continue;
2159 if ((rt->rt6i_flags & flags) != flags)
2160 continue;
Romain Kuntz85da53b2013-01-09 15:02:26 +01002161 if ((rt->rt6i_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002162 continue;
2163 dst_hold(&rt->dst);
2164 break;
2165 }
2166out:
Li RongQing5744dd92012-09-11 21:59:01 +00002167 read_unlock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002168 return rt;
2169}
2170
2171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172/* Create "default" multicast route to the interface */
2173
2174static void addrconf_add_mroute(struct net_device *dev)
2175{
Thomas Graf86872cb2006-08-22 00:01:08 -07002176 struct fib6_config cfg = {
2177 .fc_table = RT6_TABLE_LOCAL,
2178 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2179 .fc_ifindex = dev->ifindex,
2180 .fc_dst_len = 8,
2181 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002182 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002183 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Thomas Graf86872cb2006-08-22 00:01:08 -07002185 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2186
2187 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2191{
2192 struct inet6_dev *idev;
2193
2194 ASSERT_RTNL();
2195
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002196 idev = ipv6_find_idev(dev);
2197 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00002198 return ERR_PTR(-ENOBUFS);
2199
2200 if (idev->cnf.disable_ipv6)
2201 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 /* Add default multicast route */
Li Wei4af04ab2011-12-06 21:23:45 +00002204 if (!(dev->flags & IFF_LOOPBACK))
2205 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 return idev;
2208}
2209
Jiri Pirko53bd6742013-12-06 09:45:22 +01002210static void manage_tempaddrs(struct inet6_dev *idev,
2211 struct inet6_ifaddr *ifp,
2212 __u32 valid_lft, __u32 prefered_lft,
2213 bool create, unsigned long now)
2214{
2215 u32 flags;
2216 struct inet6_ifaddr *ift;
2217
2218 read_lock_bh(&idev->lock);
2219 /* update all temporary addresses in the list */
2220 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2221 int age, max_valid, max_prefered;
2222
2223 if (ifp != ift->ifpub)
2224 continue;
2225
2226 /* RFC 4941 section 3.3:
2227 * If a received option will extend the lifetime of a public
2228 * address, the lifetimes of temporary addresses should
2229 * be extended, subject to the overall constraint that no
2230 * temporary addresses should ever remain "valid" or "preferred"
2231 * for a time longer than (TEMP_VALID_LIFETIME) or
2232 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2233 */
2234 age = (now - ift->cstamp) / HZ;
2235 max_valid = idev->cnf.temp_valid_lft - age;
2236 if (max_valid < 0)
2237 max_valid = 0;
2238
2239 max_prefered = idev->cnf.temp_prefered_lft -
2240 idev->cnf.max_desync_factor - age;
2241 if (max_prefered < 0)
2242 max_prefered = 0;
2243
2244 if (valid_lft > max_valid)
2245 valid_lft = max_valid;
2246
2247 if (prefered_lft > max_prefered)
2248 prefered_lft = max_prefered;
2249
2250 spin_lock(&ift->lock);
2251 flags = ift->flags;
2252 ift->valid_lft = valid_lft;
2253 ift->prefered_lft = prefered_lft;
2254 ift->tstamp = now;
2255 if (prefered_lft > 0)
2256 ift->flags &= ~IFA_F_DEPRECATED;
2257
2258 spin_unlock(&ift->lock);
2259 if (!(flags&IFA_F_TENTATIVE))
2260 ipv6_ifa_notify(0, ift);
2261 }
2262
2263 if ((create || list_empty(&idev->tempaddr_list)) &&
2264 idev->cnf.use_tempaddr > 0) {
2265 /* When a new public address is created as described
2266 * in [ADDRCONF], also create a new temporary address.
2267 * Also create a temporary address if it's enabled but
2268 * no temporary address currently exists.
2269 */
2270 read_unlock_bh(&idev->lock);
2271 ipv6_create_tempaddr(ifp, NULL);
2272 } else {
2273 read_unlock_bh(&idev->lock);
2274 }
2275}
2276
Neil Hormane6bff992012-01-04 10:49:15 +00002277void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
2279 struct prefix_info *pinfo;
2280 __u32 valid_lft;
2281 __u32 prefered_lft;
2282 int addr_type;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002283 u32 addr_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07002285 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
2287 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 if (len < sizeof(struct prefix_info)) {
dingtianhongba3542e2013-08-17 10:27:04 +08002290 ADBG("addrconf: prefix option too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 return;
2292 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 /*
2295 * Validation checks ([ADDRCONF], page 19)
2296 */
2297
2298 addr_type = ipv6_addr_type(&pinfo->prefix);
2299
2300 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2301 return;
2302
2303 valid_lft = ntohl(pinfo->valid);
2304 prefered_lft = ntohl(pinfo->prefered);
2305
2306 if (prefered_lft > valid_lft) {
Joe Perchese87cc472012-05-13 21:56:26 +00002307 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return;
2309 }
2310
2311 in6_dev = in6_dev_get(dev);
2312
Ian Morris63159f22015-03-29 14:00:04 +01002313 if (!in6_dev) {
Joe Perchese87cc472012-05-13 21:56:26 +00002314 net_dbg_ratelimited("addrconf: device %s not configured\n",
2315 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 return;
2317 }
2318
2319 /*
2320 * Two things going on here:
2321 * 1) Add routes for on-link prefixes
2322 * 2) Configure prefixes with the auto flag set
2323 */
2324
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002325 if (pinfo->onlink) {
2326 struct rt6_info *rt;
2327 unsigned long rt_expires;
2328
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002329 /* Avoid arithmetic overflow. Really, we could
2330 * save rt_expires in seconds, likely valid_lft,
2331 * but it would require division in fib gc, that it
2332 * not good.
2333 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002334 if (HZ > USER_HZ)
2335 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2336 else
2337 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08002338
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002339 if (addrconf_finite_timeout(rt_expires))
2340 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002342 rt = addrconf_get_prefix_route(&pinfo->prefix,
2343 pinfo->prefix_len,
2344 dev,
2345 RTF_ADDRCONF | RTF_PREFIX_RT,
2346 RTF_GATEWAY | RTF_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002348 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002349 /* Autoconf prefix route */
2350 if (valid_lft == 0) {
2351 ip6_del_rt(rt);
2352 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002353 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002354 /* not infinity */
Gao feng1716a962012-04-06 00:13:10 +00002355 rt6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002356 } else {
Gao feng1716a962012-04-06 00:13:10 +00002357 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 }
2359 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002360 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002361 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2362 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002363 /* not infinity */
2364 flags |= RTF_EXPIRES;
2365 expires = jiffies_to_clock_t(rt_expires);
2366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002368 dev, expires, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00002370 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 }
2372
2373 /* Try to figure out our local address for this prefix */
2374
2375 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002376 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 struct in6_addr addr;
2378 int create = 0, update_lft = 0;
Daniel Borkmann617fe292013-04-09 03:47:16 +00002379 bool tokenized = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381 if (pinfo->prefix_len == 64) {
2382 memcpy(&addr, &pinfo->prefix, 8);
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002383
2384 if (!ipv6_addr_any(&in6_dev->token)) {
2385 read_lock_bh(&in6_dev->lock);
2386 memcpy(addr.s6_addr + 8,
2387 in6_dev->token.s6_addr + 8, 8);
2388 read_unlock_bh(&in6_dev->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00002389 tokenized = true;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002390 } else if (in6_dev->addr_gen_mode ==
2391 IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
2392 !ipv6_generate_stable_address(&addr, 0,
2393 in6_dev)) {
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002394 addr_flags |= IFA_F_STABLE_PRIVACY;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002395 goto ok;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002396 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2397 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 in6_dev_put(in6_dev);
2399 return;
2400 }
2401 goto ok;
2402 }
Joe Perchese87cc472012-05-13 21:56:26 +00002403 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2404 pinfo->prefix_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 in6_dev_put(in6_dev);
2406 return;
2407
2408ok:
2409
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07002410 ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Ian Morris63159f22015-03-29 14:00:04 +01002412 if (!ifp && valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 int max_addresses = in6_dev->cnf.max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -07002414
2415#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2416 if (in6_dev->cnf.optimistic_dad &&
Neil Hormane6bff992012-01-04 10:49:15 +00002417 !net->ipv6.devconf_all->forwarding && sllao)
Neil Horman95c385b2007-04-25 17:08:10 -07002418 addr_flags = IFA_F_OPTIMISTIC;
2419#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
2421 /* Do not allow to create too much of autoconfigured
2422 * addresses; this would be too easy way to crash kernel.
2423 */
2424 if (!max_addresses ||
2425 ipv6_count_addresses(in6_dev) < max_addresses)
Jiri Pirko3f8f5292013-08-01 10:41:27 +02002426 ifp = ipv6_add_addr(in6_dev, &addr, NULL,
2427 pinfo->prefix_len,
Neil Horman95c385b2007-04-25 17:08:10 -07002428 addr_type&IPV6_ADDR_SCOPE_MASK,
Jiri Benc8a226b22013-08-01 10:41:28 +02002429 addr_flags, valid_lft,
2430 prefered_lft);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
YOSHIFUJI Hideaki / 吉藤英明3f0d2ba2013-01-22 06:32:54 +00002432 if (IS_ERR_OR_NULL(ifp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 in6_dev_put(in6_dev);
2434 return;
2435 }
2436
Jiri Benc8a226b22013-08-01 10:41:28 +02002437 update_lft = 0;
2438 create = 1;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002439 spin_lock_bh(&ifp->lock);
2440 ifp->flags |= IFA_F_MANAGETEMPADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 ifp->cstamp = jiffies;
Daniel Borkmann617fe292013-04-09 03:47:16 +00002442 ifp->tokenized = tokenized;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002443 spin_unlock_bh(&ifp->lock);
David S. Millercf22f9a2012-04-14 21:37:40 -04002444 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
2446
2447 if (ifp) {
Jiri Pirko479840f2013-12-06 09:45:21 +01002448 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 u32 stored_lft;
2451
2452 /* update lifetime (RFC2462 5.5.3 e) */
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01002453 spin_lock_bh(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 now = jiffies;
2455 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2456 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2457 else
2458 stored_lft = 0;
Jiri Benc8a226b22013-08-01 10:41:28 +02002459 if (!update_lft && !create && stored_lft) {
Fabian Frederick9451a302014-10-27 19:11:56 +01002460 const u32 minimum_lft = min_t(u32,
2461 stored_lft, MIN_VALID_LIFETIME);
Paul Marksc9d55d52013-09-25 15:12:55 -07002462 valid_lft = max(valid_lft, minimum_lft);
2463
2464 /* RFC4862 Section 5.5.3e:
2465 * "Note that the preferred lifetime of the
2466 * corresponding address is always reset to
2467 * the Preferred Lifetime in the received
2468 * Prefix Information option, regardless of
2469 * whether the valid lifetime is also reset or
2470 * ignored."
2471 *
2472 * So we should always update prefered_lft here.
2473 */
2474 update_lft = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476
2477 if (update_lft) {
2478 ifp->valid_lft = valid_lft;
2479 ifp->prefered_lft = prefered_lft;
2480 ifp->tstamp = now;
2481 flags = ifp->flags;
2482 ifp->flags &= ~IFA_F_DEPRECATED;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01002483 spin_unlock_bh(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
2485 if (!(flags&IFA_F_TENTATIVE))
2486 ipv6_ifa_notify(0, ifp);
2487 } else
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01002488 spin_unlock_bh(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Jiri Pirko53bd6742013-12-06 09:45:22 +01002490 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2491 create, now);
David S. Miller5d9efa72013-10-28 20:07:50 -04002492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002494 addrconf_verify();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 }
2496 }
2497 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2498 in6_dev_put(in6_dev);
2499}
2500
2501/*
2502 * Set destination address.
2503 * Special case for SIT interfaces where we create a new "virtual"
2504 * device.
2505 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002506int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
2508 struct in6_ifreq ireq;
2509 struct net_device *dev;
2510 int err = -EINVAL;
2511
2512 rtnl_lock();
2513
2514 err = -EFAULT;
2515 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2516 goto err_exit;
2517
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002518 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
2520 err = -ENODEV;
Ian Morris63159f22015-03-29 14:00:04 +01002521 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 goto err_exit;
2523
Amerigo Wang07a93622012-10-29 16:23:10 +00002524#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002526 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 struct ip_tunnel_parm p;
2529
2530 err = -EADDRNOTAVAIL;
2531 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2532 goto err_exit;
2533
2534 memset(&p, 0, sizeof(p));
2535 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2536 p.iph.saddr = 0;
2537 p.iph.version = 4;
2538 p.iph.ihl = 5;
2539 p.iph.protocol = IPPROTO_IPV6;
2540 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002541 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002543 if (ops->ndo_do_ioctl) {
2544 mm_segment_t oldfs = get_fs();
2545
2546 set_fs(KERNEL_DS);
2547 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2548 set_fs(oldfs);
2549 } else
2550 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
2552 if (err == 0) {
2553 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002554 dev = __dev_get_by_name(net, p.name);
2555 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 goto err_exit;
2557 err = dev_open(dev);
2558 }
2559 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002560#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
2562err_exit:
2563 rtnl_unlock();
2564 return err;
2565}
2566
Madhu Challa93a714d2015-02-25 09:58:35 -08002567static int ipv6_mc_config(struct sock *sk, bool join,
2568 const struct in6_addr *addr, int ifindex)
2569{
2570 int ret;
2571
2572 ASSERT_RTNL();
2573
2574 lock_sock(sk);
2575 if (join)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002576 ret = ipv6_sock_mc_join(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002577 else
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002578 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
Madhu Challa93a714d2015-02-25 09:58:35 -08002579 release_sock(sk);
2580
2581 return ret;
2582}
2583
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584/*
2585 * Manual configuration of address on an interface
2586 */
Jiri Pirko479840f2013-12-06 09:45:21 +01002587static int inet6_addr_add(struct net *net, int ifindex,
2588 const struct in6_addr *pfx,
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002589 const struct in6_addr *peer_pfx,
Jiri Pirko479840f2013-12-06 09:45:21 +01002590 unsigned int plen, __u32 ifa_flags,
2591 __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
2593 struct inet6_ifaddr *ifp;
2594 struct inet6_dev *idev;
2595 struct net_device *dev;
Madhu Challa93a714d2015-02-25 09:58:35 -08002596 unsigned long timeout;
2597 clock_t expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002599 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
2601 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002602
Thomas Graf24ef0da2008-05-28 16:54:22 +02002603 if (plen > 128)
2604 return -EINVAL;
2605
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002606 /* check the lifetime */
2607 if (!valid_lft || prefered_lft > valid_lft)
2608 return -EINVAL;
2609
Jiri Pirko53bd6742013-12-06 09:45:22 +01002610 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2611 return -EINVAL;
2612
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002613 dev = __dev_get_by_index(net, ifindex);
2614 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002616
Brian Haley64e724f2010-07-20 10:34:30 +00002617 idev = addrconf_add_dev(dev);
2618 if (IS_ERR(idev))
2619 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
Madhu Challa93a714d2015-02-25 09:58:35 -08002621 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2622 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2623 true, pfx, ifindex);
2624
2625 if (ret < 0)
2626 return ret;
2627 }
2628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 scope = ipv6_addr_scope(pfx);
2630
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002631 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2632 if (addrconf_finite_timeout(timeout)) {
2633 expires = jiffies_to_clock_t(timeout * HZ);
2634 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002635 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002636 } else {
2637 expires = 0;
2638 flags = 0;
2639 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002640 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002641
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002642 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2643 if (addrconf_finite_timeout(timeout)) {
2644 if (timeout == 0)
2645 ifa_flags |= IFA_F_DEPRECATED;
2646 prefered_lft = timeout;
2647 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002648
Jiri Benc8a226b22013-08-01 10:41:28 +02002649 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2650 valid_lft, prefered_lft);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 if (!IS_ERR(ifp)) {
Thomas Haller761aac72014-01-15 15:36:58 +01002653 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2654 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2655 expires, flags);
2656 }
2657
Neil Horman95c385b2007-04-25 17:08:10 -07002658 /*
2659 * Note that section 3.1 of RFC 4429 indicates
2660 * that the Optimistic flag should not be set for
2661 * manually configured addresses
2662 */
David S. Millercf22f9a2012-04-14 21:37:40 -04002663 addrconf_dad_start(ifp);
Jiri Pirko53bd6742013-12-06 09:45:22 +01002664 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2665 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2666 true, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002668 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 return 0;
Madhu Challa93a714d2015-02-25 09:58:35 -08002670 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2671 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2672 false, pfx, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 }
2674
2675 return PTR_ERR(ifp);
2676}
2677
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002678static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2679 const struct in6_addr *pfx, unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680{
2681 struct inet6_ifaddr *ifp;
2682 struct inet6_dev *idev;
2683 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002684
Thomas Graf24ef0da2008-05-28 16:54:22 +02002685 if (plen > 128)
2686 return -EINVAL;
2687
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002688 dev = __dev_get_by_index(net, ifindex);
2689 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 return -ENODEV;
2691
Ian Morrise5d08d72014-11-23 21:28:43 +00002692 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01002693 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 return -ENXIO;
2695
2696 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002697 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 if (ifp->prefix_len == plen &&
2699 ipv6_addr_equal(pfx, &ifp->addr)) {
2700 in6_ifa_hold(ifp);
2701 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002702
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002703 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2704 (ifa_flags & IFA_F_MANAGETEMPADDR))
2705 manage_tempaddrs(idev, ifp, 0, 0, false,
2706 jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 ipv6_del_addr(ifp);
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002708 addrconf_verify_rtnl();
Madhu Challa93a714d2015-02-25 09:58:35 -08002709 if (ipv6_addr_is_multicast(pfx)) {
2710 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2711 false, pfx, dev->ifindex);
2712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 return 0;
2714 }
2715 }
2716 read_unlock_bh(&idev->lock);
2717 return -EADDRNOTAVAIL;
2718}
2719
2720
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002721int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722{
2723 struct in6_ifreq ireq;
2724 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002725
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002726 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2730 return -EFAULT;
2731
2732 rtnl_lock();
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002733 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002734 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2735 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 rtnl_unlock();
2737 return err;
2738}
2739
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002740int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741{
2742 struct in6_ifreq ireq;
2743 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002744
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002745 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 return -EPERM;
2747
2748 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2749 return -EFAULT;
2750
2751 rtnl_lock();
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002752 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002753 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 rtnl_unlock();
2755 return err;
2756}
2757
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002758static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2759 int plen, int scope)
2760{
2761 struct inet6_ifaddr *ifp;
2762
Jiri Benc8a226b22013-08-01 10:41:28 +02002763 ifp = ipv6_add_addr(idev, addr, NULL, plen,
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01002764 scope, IFA_F_PERMANENT,
2765 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002766 if (!IS_ERR(ifp)) {
2767 spin_lock_bh(&ifp->lock);
2768 ifp->flags &= ~IFA_F_TENTATIVE;
2769 spin_unlock_bh(&ifp->lock);
2770 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2771 in6_ifa_put(ifp);
2772 }
2773}
2774
Amerigo Wang07a93622012-10-29 16:23:10 +00002775#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776static void sit_add_v4_addrs(struct inet6_dev *idev)
2777{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 struct in6_addr addr;
2779 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002780 struct net *net = dev_net(idev->dev);
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002781 int scope, plen;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002782 u32 pflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
2784 ASSERT_RTNL();
2785
2786 memset(&addr, 0, sizeof(struct in6_addr));
2787 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2788
2789 if (idev->dev->flags&IFF_POINTOPOINT) {
2790 addr.s6_addr32[0] = htonl(0xfe800000);
2791 scope = IFA_LINK;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002792 plen = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 } else {
2794 scope = IPV6_ADDR_COMPATv4;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002795 plen = 96;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002796 pflags |= RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 }
2798
2799 if (addr.s6_addr32[3]) {
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002800 add_addr(idev, &addr, plen, scope);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002801 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 return;
2803 }
2804
Benjamin Thery6fda7352008-03-05 10:47:47 -08002805 for_each_netdev(net, dev) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002806 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 if (in_dev && (dev->flags & IFF_UP)) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002808 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
2810 int flag = scope;
2811
2812 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
2814 addr.s6_addr32[3] = ifa->ifa_local;
2815
2816 if (ifa->ifa_scope == RT_SCOPE_LINK)
2817 continue;
2818 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2819 if (idev->dev->flags&IFF_POINTOPOINT)
2820 continue;
2821 flag |= IFA_HOST;
2822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002824 add_addr(idev, &addr, plen, flag);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002825 addrconf_prefix_route(&addr, plen, idev->dev, 0,
2826 pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 }
2828 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002831#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833static void init_loopback(struct net_device *dev)
2834{
2835 struct inet6_dev *idev;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05302836 struct net_device *sp_dev;
2837 struct inet6_ifaddr *sp_ifa;
2838 struct rt6_info *sp_rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
2840 /* ::1 */
2841
2842 ASSERT_RTNL();
2843
Ian Morrise5d08d72014-11-23 21:28:43 +00002844 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01002845 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00002846 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return;
2848 }
2849
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002850 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05302851
2852 /* Add routes to other interface's IPv6 addresses */
2853 for_each_netdev(dev_net(dev), sp_dev) {
2854 if (!strcmp(sp_dev->name, dev->name))
2855 continue;
2856
2857 idev = __in6_dev_get(sp_dev);
2858 if (!idev)
2859 continue;
2860
2861 read_lock_bh(&idev->lock);
2862 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
2863
2864 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
2865 continue;
2866
Gao feng33d99112014-01-24 16:29:11 +08002867 if (sp_ifa->rt) {
2868 /* This dst has been added to garbage list when
2869 * lo device down, release this obsolete dst and
2870 * reallocate a new router for ifa.
2871 */
2872 if (sp_ifa->rt->dst.obsolete > 0) {
2873 ip6_rt_put(sp_ifa->rt);
2874 sp_ifa->rt = NULL;
2875 } else {
2876 continue;
2877 }
2878 }
Gao fenga881ae12013-06-16 11:14:30 +08002879
François-Xavier Le Bail57ec0af2013-12-02 11:28:49 +01002880 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05302881
2882 /* Failure cases are ignored */
Gao feng534c8772013-06-02 22:16:21 +00002883 if (!IS_ERR(sp_rt)) {
2884 sp_ifa->rt = sp_rt;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05302885 ip6_ins_rt(sp_rt);
Gao feng534c8772013-06-02 22:16:21 +00002886 }
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05302887 }
2888 read_unlock_bh(&idev->lock);
2889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890}
2891
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002892static void addrconf_add_linklocal(struct inet6_dev *idev,
2893 const struct in6_addr *addr, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002895 struct inet6_ifaddr *ifp;
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002896 u32 addr_flags = flags | IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
Neil Horman95c385b2007-04-25 17:08:10 -07002898#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2899 if (idev->cnf.optimistic_dad &&
David Miller702beb82008-07-20 18:17:02 -07002900 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
Neil Horman95c385b2007-04-25 17:08:10 -07002901 addr_flags |= IFA_F_OPTIMISTIC;
2902#endif
2903
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01002904 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
2905 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002907 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
David S. Millercf22f9a2012-04-14 21:37:40 -04002908 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 in6_ifa_put(ifp);
2910 }
2911}
2912
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002913static bool ipv6_reserved_interfaceid(struct in6_addr address)
2914{
2915 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
2916 return true;
2917
2918 if (address.s6_addr32[2] == htonl(0x02005eff) &&
2919 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
2920 return true;
2921
2922 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
2923 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
2924 return true;
2925
2926 return false;
2927}
2928
2929static int ipv6_generate_stable_address(struct in6_addr *address,
2930 u8 dad_count,
2931 const struct inet6_dev *idev)
2932{
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002933 static DEFINE_SPINLOCK(lock);
2934 static __u32 digest[SHA_DIGEST_WORDS];
2935 static __u32 workspace[SHA_WORKSPACE_WORDS];
2936
2937 static union {
2938 char __data[SHA_MESSAGE_BYTES];
2939 struct {
2940 struct in6_addr secret;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01002941 __be32 prefix[2];
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002942 unsigned char hwaddr[MAX_ADDR_LEN];
2943 u8 dad_count;
2944 } __packed;
2945 } data;
2946
2947 struct in6_addr secret;
2948 struct in6_addr temp;
2949 struct net *net = dev_net(idev->dev);
2950
2951 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
2952
2953 if (idev->cnf.stable_secret.initialized)
2954 secret = idev->cnf.stable_secret.secret;
2955 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
2956 secret = net->ipv6.devconf_dflt->stable_secret.secret;
2957 else
2958 return -1;
2959
2960retry:
2961 spin_lock_bh(&lock);
2962
2963 sha_init(digest);
2964 memset(&data, 0, sizeof(data));
2965 memset(workspace, 0, sizeof(workspace));
2966 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01002967 data.prefix[0] = address->s6_addr32[0];
2968 data.prefix[1] = address->s6_addr32[1];
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002969 data.secret = secret;
2970 data.dad_count = dad_count;
2971
2972 sha_transform(digest, data.__data, workspace);
2973
2974 temp = *address;
Hannes Frederic Sowaff402172015-03-24 11:05:28 +01002975 temp.s6_addr32[2] = (__force __be32)digest[0];
2976 temp.s6_addr32[3] = (__force __be32)digest[1];
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002977
2978 spin_unlock_bh(&lock);
2979
2980 if (ipv6_reserved_interfaceid(temp)) {
2981 dad_count++;
Hannes Frederic Sowa1855b7c2015-03-23 23:36:05 +01002982 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002983 return -1;
2984 goto retry;
2985 }
2986
2987 *address = temp;
2988 return 0;
2989}
2990
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02002991static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
2992{
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002993 struct in6_addr addr;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02002994
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01002995 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2996
2997 if (idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY) {
2998 if (!ipv6_generate_stable_address(&addr, 0, idev))
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01002999 addrconf_add_linklocal(idev, &addr,
3000 IFA_F_STABLE_PRIVACY);
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01003001 else if (prefix_route)
3002 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3003 } else if (idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) {
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003004 /* addrconf_add_linklocal also adds a prefix_route and we
3005 * only need to care about prefix routes if ipv6_generate_eui64
3006 * couldn't generate one.
3007 */
3008 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
Hannes Frederic Sowa64236f32015-03-23 23:36:02 +01003009 addrconf_add_linklocal(idev, &addr, 0);
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003010 else if (prefix_route)
3011 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
3012 }
3013}
3014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015static void addrconf_dev_config(struct net_device *dev)
3016{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003017 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
3019 ASSERT_RTNL();
3020
Herbert Xu74235a22007-06-14 13:02:55 -07003021 if ((dev->type != ARPHRD_ETHER) &&
3022 (dev->type != ARPHRD_FDDI) &&
Herbert Xu74235a22007-06-14 13:02:55 -07003023 (dev->type != ARPHRD_ARCNET) &&
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00003024 (dev->type != ARPHRD_INFINIBAND) &&
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00003025 (dev->type != ARPHRD_IEEE802154) &&
Nicolas Dichtele8377352013-08-20 12:16:06 +02003026 (dev->type != ARPHRD_IEEE1394) &&
Jukka Rissanene74bccb2013-12-11 17:05:36 +02003027 (dev->type != ARPHRD_TUNNEL6) &&
3028 (dev->type != ARPHRD_6LOWPAN)) {
Herbert Xu74235a22007-06-14 13:02:55 -07003029 /* Alas, we support only Ethernet autoconfiguration. */
3030 return;
3031 }
3032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00003034 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 return;
3036
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003037 addrconf_addr_gen(idev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038}
3039
Amerigo Wang07a93622012-10-29 16:23:10 +00003040#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041static void addrconf_sit_config(struct net_device *dev)
3042{
3043 struct inet6_dev *idev;
3044
3045 ASSERT_RTNL();
3046
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003047 /*
3048 * Configure the tunnel with one of our IPv4
3049 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 * our v4 addrs in the tunnel
3051 */
3052
Ian Morrise5d08d72014-11-23 21:28:43 +00003053 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003054 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003055 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 return;
3057 }
3058
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003059 if (dev->priv_flags & IFF_ISATAP) {
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003060 addrconf_addr_gen(idev, false);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11003061 return;
3062 }
3063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 sit_add_v4_addrs(idev);
3065
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00003066 if (dev->flags&IFF_POINTOPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068}
Joerg Roedel0be669b2006-10-10 14:49:53 -07003069#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Amerigo Wang07a93622012-10-29 16:23:10 +00003071#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003072static void addrconf_gre_config(struct net_device *dev)
3073{
3074 struct inet6_dev *idev;
stephen hemmingeraee80b52011-06-08 10:44:30 +00003075
stephen hemmingeraee80b52011-06-08 10:44:30 +00003076 ASSERT_RTNL();
3077
Ian Morrise5d08d72014-11-23 21:28:43 +00003078 idev = ipv6_find_idev(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003079 if (!idev) {
Joe Perches91df42b2012-05-15 14:11:54 +00003080 pr_debug("%s: add_dev failed\n", __func__);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003081 return;
3082 }
3083
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02003084 addrconf_addr_gen(idev, true);
stephen hemmingeraee80b52011-06-08 10:44:30 +00003085}
3086#endif
3087
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003088static int addrconf_notify(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00003089 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090{
Jiri Pirko351638e2013-05-28 01:30:21 +00003091 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric Dumazet748e2d92012-08-22 21:50:59 +00003092 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003093 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07003094 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003096 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003097 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07003098 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003099 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -07003100 if (IS_ERR(idev))
3101 return notifier_from_errno(PTR_ERR(idev));
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09003102 }
3103 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003106 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07003107 if (dev->flags & IFF_SLAVE)
3108 break;
3109
WANG Cong3ce62a82014-09-11 15:07:16 -07003110 if (idev && idev->cnf.disable_ipv6)
3111 break;
3112
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003113 if (event == NETDEV_UP) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07003114 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003115 /* device is not ready yet. */
Joe Perchesf3213832012-05-15 14:11:53 +00003116 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003117 dev->name);
3118 break;
3119 }
Kristian Slavov99081042006-02-08 16:10:53 -08003120
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11003121 if (!idev && dev->mtu >= IPV6_MIN_MTU)
3122 idev = ipv6_add_dev(dev);
3123
WANG Conga317a2f2014-07-25 15:25:09 -07003124 if (!IS_ERR_OR_NULL(idev)) {
Kristian Slavov99081042006-02-08 16:10:53 -08003125 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08003126 run_pending = 1;
3127 }
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003128 } else {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07003129 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003130 /* device is still not ready. */
3131 break;
3132 }
3133
3134 if (idev) {
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003135 if (idev->if_flags & IF_READY)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003136 /* device is already configured. */
3137 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003138 idev->if_flags |= IF_READY;
3139 }
3140
Joe Perchesf3213832012-05-15 14:11:53 +00003141 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
3142 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003143
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003144 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003145 }
3146
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003147 switch (dev->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +00003148#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 case ARPHRD_SIT:
3150 addrconf_sit_config(dev);
3151 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07003152#endif
Amerigo Wang07a93622012-10-29 16:23:10 +00003153#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00003154 case ARPHRD_IPGRE:
3155 addrconf_gre_config(dev);
3156 break;
3157#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 case ARPHRD_LOOPBACK:
3159 init_loopback(dev);
3160 break;
3161
3162 default:
3163 addrconf_dev_config(dev);
3164 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003165 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003166
WANG Conga317a2f2014-07-25 15:25:09 -07003167 if (!IS_ERR_OR_NULL(idev)) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003168 if (run_pending)
3169 addrconf_dad_run(idev);
3170
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003171 /*
3172 * If the MTU changed during the interface down,
3173 * when the interface up, the changed MTU must be
3174 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003176 if (idev->cnf.mtu6 != dev->mtu &&
3177 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 rt6_mtu_change(dev, dev->mtu);
3179 idev->cnf.mtu6 = dev->mtu;
3180 }
3181 idev->tstamp = jiffies;
3182 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003183
3184 /*
3185 * If the changed mtu during down is lower than
3186 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 */
3188 if (dev->mtu < IPV6_MIN_MTU)
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003189 addrconf_ifdown(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 }
3191 break;
3192
3193 case NETDEV_CHANGEMTU:
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11003194 if (idev && dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 rt6_mtu_change(dev, dev->mtu);
3196 idev->cnf.mtu6 = dev->mtu;
3197 break;
3198 }
3199
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11003200 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
3201 idev = ipv6_add_dev(dev);
WANG Conga317a2f2014-07-25 15:25:09 -07003202 if (!IS_ERR(idev))
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11003203 break;
3204 }
3205
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003206 /*
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08003207 * if MTU under IPV6_MIN_MTU.
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003208 * Stop IPv6 on this interface.
3209 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
3211 case NETDEV_DOWN:
3212 case NETDEV_UNREGISTER:
3213 /*
3214 * Remove all addresses from this interface.
3215 */
3216 addrconf_ifdown(dev, event != NETDEV_DOWN);
3217 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003218
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07003221 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003222 addrconf_sysctl_unregister(idev);
WANG Conga317a2f2014-07-25 15:25:09 -07003223 err = addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07003224 if (err)
3225 return notifier_from_errno(err);
WANG Conga317a2f2014-07-25 15:25:09 -07003226 err = snmp6_register_dev(idev);
3227 if (err) {
3228 addrconf_sysctl_unregister(idev);
3229 return notifier_from_errno(err);
3230 }
Stephen Hemminger5632c512007-04-28 21:16:39 -07003231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003233
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003234 case NETDEV_PRE_TYPE_CHANGE:
3235 case NETDEV_POST_TYPE_CHANGE:
3236 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07003237 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07003238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
3240 return NOTIFY_OK;
3241}
3242
3243/*
3244 * addrconf module should be notified of a device going up
3245 */
3246static struct notifier_block ipv6_dev_notf = {
3247 .notifier_call = addrconf_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248};
3249
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003250static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07003251{
3252 struct inet6_dev *idev;
3253 ASSERT_RTNL();
3254
3255 idev = __in6_dev_get(dev);
3256
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003257 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003258 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00003259 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07003260 ipv6_mc_unmap(idev);
3261}
3262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263static int addrconf_ifdown(struct net_device *dev, int how)
3264{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003265 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003266 struct inet6_dev *idev;
3267 struct inet6_ifaddr *ifa;
David S. Miller73a8bd72011-01-23 23:27:15 -08003268 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
3270 ASSERT_RTNL();
3271
David S. Miller73a8bd72011-01-23 23:27:15 -08003272 rt6_ifdown(net, dev);
3273 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
3275 idev = __in6_dev_get(dev);
Ian Morris63159f22015-03-29 14:00:04 +01003276 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 return -ENODEV;
3278
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003279 /*
3280 * Step 1: remove reference to ipv6 device from parent device.
3281 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003283 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003285
3286 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003287 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
3289 /* Step 1.5: remove snmp6 entry */
3290 snmp6_unregister_dev(idev);
3291
3292 }
3293
David S. Miller73a8bd72011-01-23 23:27:15 -08003294 /* Step 2: clear hash table */
3295 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3296 struct hlist_head *h = &inet6_addr_lst[i];
David S. Miller73a8bd72011-01-23 23:27:15 -08003297
3298 spin_lock_bh(&addrconf_hash_lock);
Ian Morris67ba4152014-08-24 21:53:10 +01003299restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08003300 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
David S. Miller73a8bd72011-01-23 23:27:15 -08003301 if (ifa->idev == idev) {
3302 hlist_del_init_rcu(&ifa->addr_lst);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003303 addrconf_del_dad_work(ifa);
David S. Miller73a8bd72011-01-23 23:27:15 -08003304 goto restart;
3305 }
3306 }
3307 spin_unlock_bh(&addrconf_hash_lock);
3308 }
3309
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 write_lock_bh(&idev->lock);
3311
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003312 addrconf_del_rs_timer(idev);
3313
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003314 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003315 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003316 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
Denis V. Lunev439e2382008-04-03 13:30:17 -07003318 if (how && del_timer(&idev->regen_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 in6_dev_put(idev);
3320
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003321 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00003322 while (!list_empty(&idev->tempaddr_list)) {
3323 ifa = list_first_entry(&idev->tempaddr_list,
3324 struct inet6_ifaddr, tmp_list);
3325 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 write_unlock_bh(&idev->lock);
3327 spin_lock_bh(&ifa->lock);
3328
3329 if (ifa->ifpub) {
3330 in6_ifa_put(ifa->ifpub);
3331 ifa->ifpub = NULL;
3332 }
3333 spin_unlock_bh(&ifa->lock);
3334 in6_ifa_put(ifa);
3335 write_lock_bh(&idev->lock);
3336 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003337
stephen hemminger502a2ff2010-03-17 20:31:13 +00003338 while (!list_empty(&idev->addr_list)) {
3339 ifa = list_first_entry(&idev->addr_list,
3340 struct inet6_ifaddr, if_list);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003341 addrconf_del_dad_work(ifa);
stephen hemminger84e8b802010-03-02 13:32:46 +00003342
David S. Miller73a8bd72011-01-23 23:27:15 -08003343 list_del(&ifa->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
David S. Miller73a8bd72011-01-23 23:27:15 -08003345 write_unlock_bh(&idev->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003346
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003347 spin_lock_bh(&ifa->lock);
David S. Miller73a8bd72011-01-23 23:27:15 -08003348 state = ifa->state;
3349 ifa->state = INET6_IFADDR_STATE_DEAD;
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003350 spin_unlock_bh(&ifa->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003351
David S. Miller73a8bd72011-01-23 23:27:15 -08003352 if (state != INET6_IFADDR_STATE_DEAD) {
3353 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Cong Wangf88c91d2013-04-14 23:18:43 +08003354 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00003355 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003356 in6_ifa_put(ifa);
stephen hemminger8f37ada2010-03-03 08:19:59 +00003357
David S. Miller73a8bd72011-01-23 23:27:15 -08003358 write_lock_bh(&idev->lock);
3359 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003360
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 write_unlock_bh(&idev->lock);
3362
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003363 /* Step 5: Discard anycast and multicast list */
3364 if (how) {
3365 ipv6_ac_destroy_dev(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 ipv6_mc_destroy_dev(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003367 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 ipv6_mc_down(idev);
Sabrina Dubroca381f4dc2014-09-10 23:23:02 +02003369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003372
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003373 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003374 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003375 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 neigh_parms_release(&nd_tbl, idev->nd_parms);
3377 neigh_ifdown(&nd_tbl, dev);
3378 in6_dev_put(idev);
3379 }
3380 return 0;
3381}
3382
3383static void addrconf_rs_timer(unsigned long data)
3384{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003385 struct inet6_dev *idev = (struct inet6_dev *)data;
Cong Wangcaf92bc2013-08-31 13:44:32 +08003386 struct net_device *dev = idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003387 struct in6_addr lladdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003389 write_lock(&idev->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00003390 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 goto out;
3392
Shmulik Ladkani9ba2add2012-12-02 01:44:53 +00003393 if (!ipv6_accept_ra(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003395
3396 /* Announcement received after solicitation was sent */
3397 if (idev->if_flags & IF_RA_RCVD)
3398 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003400 if (idev->rs_probes++ < idev->cnf.rtr_solicits) {
Cong Wangcaf92bc2013-08-31 13:44:32 +08003401 write_unlock(&idev->lock);
3402 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3403 ndisc_send_rs(dev, &lladdr,
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003404 &in6addr_linklocal_allrouters);
3405 else
Cong Wangcaf92bc2013-08-31 13:44:32 +08003406 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
Cong Wangcaf92bc2013-08-31 13:44:32 +08003408 write_lock(&idev->lock);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003409 /* The wait after the last probe can be shorter */
3410 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3411 idev->cnf.rtr_solicits) ?
3412 idev->cnf.rtr_solicit_delay :
3413 idev->cnf.rtr_solicit_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 /*
3416 * Note: we do not support deprecated "all on-link"
3417 * assumption any longer.
3418 */
Joe Perches91df42b2012-05-15 14:11:54 +00003419 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 }
3421
3422out:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003423 write_unlock(&idev->lock);
Cong Wangcaf92bc2013-08-31 13:44:32 +08003424put:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003425 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426}
3427
3428/*
3429 * Duplicate Address Detection
3430 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003431static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3432{
3433 unsigned long rand_num;
3434 struct inet6_dev *idev = ifp->idev;
3435
Neil Horman95c385b2007-04-25 17:08:10 -07003436 if (ifp->flags & IFA_F_OPTIMISTIC)
3437 rand_num = 0;
3438 else
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003439 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
Neil Horman95c385b2007-04-25 17:08:10 -07003440
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003441 ifp->dad_probes = idev->cnf.dad_transmits;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003442 addrconf_mod_dad_work(ifp, rand_num);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003443}
3444
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003445static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446{
3447 struct inet6_dev *idev = ifp->idev;
3448 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
3450 addrconf_join_solict(dev, &ifp->addr);
3451
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003452 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
3454 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003455 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003456 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
3459 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09003460 idev->cnf.accept_dad < 1 ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003461 !(ifp->flags&IFA_F_TENTATIVE) ||
3462 ifp->flags & IFA_F_NODAD) {
Brian Haleycc411d02009-09-09 14:41:32 +00003463 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003464 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 read_unlock_bh(&idev->lock);
3466
3467 addrconf_dad_completed(ifp);
3468 return;
3469 }
3470
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003471 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003472 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003473 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003474 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003475 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003476 * - keep it tentative if it is a permanent address.
3477 * - otherwise, kill it.
3478 */
3479 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003480 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003481 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003482 }
Neil Horman95c385b2007-04-25 17:08:10 -07003483
3484 /*
3485 * Optimistic nodes can start receiving
3486 * Frames right away
3487 */
Erik Kline7fd25612014-10-28 18:11:14 +09003488 if (ifp->flags & IFA_F_OPTIMISTIC) {
Neil Horman95c385b2007-04-25 17:08:10 -07003489 ip6_ins_rt(ifp->rt);
Erik Kline7fd25612014-10-28 18:11:14 +09003490 if (ipv6_use_optimistic_addr(idev)) {
3491 /* Because optimistic nodes can use this address,
3492 * notify listeners. If DAD fails, RTM_DELADDR is sent.
3493 */
3494 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3495 }
3496 }
Neil Horman95c385b2007-04-25 17:08:10 -07003497
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003498 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003500 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 read_unlock_bh(&idev->lock);
3502}
3503
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003504static void addrconf_dad_start(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003506 bool begin_dad = false;
3507
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003508 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003509 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3510 ifp->state = INET6_IFADDR_STATE_PREDAD;
3511 begin_dad = true;
3512 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003513 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003514
3515 if (begin_dad)
3516 addrconf_mod_dad_work(ifp, 0);
3517}
3518
3519static void addrconf_dad_work(struct work_struct *w)
3520{
3521 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3522 struct inet6_ifaddr,
3523 dad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 struct inet6_dev *idev = ifp->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 struct in6_addr mcaddr;
3526
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003527 enum {
3528 DAD_PROCESS,
3529 DAD_BEGIN,
3530 DAD_ABORT,
3531 } action = DAD_PROCESS;
3532
3533 rtnl_lock();
3534
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003535 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003536 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3537 action = DAD_BEGIN;
3538 ifp->state = INET6_IFADDR_STATE_DAD;
3539 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3540 action = DAD_ABORT;
3541 ifp->state = INET6_IFADDR_STATE_POSTDAD;
3542 }
Hannes Frederic Sowa8e8e6762015-03-23 23:36:03 +01003543 spin_unlock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003544
3545 if (action == DAD_BEGIN) {
3546 addrconf_dad_begin(ifp);
3547 goto out;
3548 } else if (action == DAD_ABORT) {
3549 addrconf_dad_stop(ifp, 1);
3550 goto out;
3551 }
3552
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003553 if (!ifp->dad_probes && addrconf_dad_end(ifp))
Herbert Xuf2344a12010-05-18 15:55:27 -07003554 goto out;
3555
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003556 write_lock_bh(&idev->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003557 if (idev->dead || !(idev->if_flags & IF_READY)) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003558 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 goto out;
3560 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003561
3562 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003563 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3564 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003565 write_unlock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003566 goto out;
3567 }
3568
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003569 if (ifp->dad_probes == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 /*
3571 * DAD was successful
3572 */
3573
Brian Haleycc411d02009-09-09 14:41:32 +00003574 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003575 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003576 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
3578 addrconf_dad_completed(ifp);
3579
3580 goto out;
3581 }
3582
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003583 ifp->dad_probes--;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003584 addrconf_mod_dad_work(ifp,
3585 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
stephen hemminger21809fa2010-02-08 19:48:52 +00003586 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003587 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588
3589 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09003591 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592out:
3593 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003594 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595}
3596
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003597/* ifp->idev must be at least read locked */
3598static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3599{
3600 struct inet6_ifaddr *ifpiter;
3601 struct inet6_dev *idev = ifp->idev;
3602
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01003603 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3604 if (ifpiter->scope > IFA_LINK)
3605 break;
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003606 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3607 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3608 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
3609 IFA_F_PERMANENT)
3610 return false;
3611 }
3612 return true;
3613}
3614
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3616{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003617 struct net_device *dev = ifp->idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003618 struct in6_addr lladdr;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003619 bool send_rs, send_mld;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003620
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003621 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
3623 /*
3624 * Configure the address for reception. Now it is valid.
3625 */
3626
3627 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3628
Tore Anderson026359b2011-08-28 23:47:33 +00003629 /* If added prefix is link local and we are prepared to process
3630 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 */
3632
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003633 read_lock_bh(&ifp->idev->lock);
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003634 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003635 send_rs = send_mld &&
3636 ipv6_accept_ra(ifp->idev) &&
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003637 ifp->idev->cnf.rtr_solicits > 0 &&
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003638 (dev->flags&IFF_LOOPBACK) == 0;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003639 read_unlock_bh(&ifp->idev->lock);
3640
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003641 /* While dad is in progress mld report's source address is in6_addrany.
3642 * Resend with proper ll now.
3643 */
3644 if (send_mld)
3645 ipv6_mc_dad_complete(ifp->idev);
3646
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003647 if (send_rs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 /*
3649 * If a host as already performed a random delay
3650 * [...] as part of DAD [...] there is no need
3651 * to delay again before sending the first RS
3652 */
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003653 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003654 return;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003655 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003657 write_lock_bh(&ifp->idev->lock);
3658 spin_lock(&ifp->lock);
3659 ifp->idev->rs_probes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 ifp->idev->if_flags |= IF_RS_SENT;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003661 addrconf_mod_rs_timer(ifp->idev,
3662 ifp->idev->cnf.rtr_solicit_interval);
3663 spin_unlock(&ifp->lock);
3664 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 }
3666}
3667
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003668static void addrconf_dad_run(struct inet6_dev *idev)
3669{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003670 struct inet6_ifaddr *ifp;
3671
3672 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003673 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003674 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07003675 if (ifp->flags & IFA_F_TENTATIVE &&
3676 ifp->state == INET6_IFADDR_STATE_DAD)
3677 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00003678 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003679 }
3680 read_unlock_bh(&idev->lock);
3681}
3682
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683#ifdef CONFIG_PROC_FS
3684struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08003685 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003687 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688};
3689
Mihai Maruseac1d578302012-01-03 23:31:35 +00003690static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691{
3692 struct inet6_ifaddr *ifa = NULL;
3693 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003694 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00003695 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696
Mihai Maruseac1d578302012-01-03 23:31:35 +00003697 /* initial bucket if pos is 0 */
3698 if (pos == 0) {
3699 state->bucket = 0;
3700 state->offset = 0;
3701 }
3702
3703 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08003704 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00003705 addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003706 if (!net_eq(dev_net(ifa->idev->dev), net))
3707 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003708 /* sync with offset */
3709 if (p < state->offset) {
3710 p++;
3711 continue;
3712 }
3713 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003714 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003715 }
3716
3717 /* prepare for next bucket */
3718 state->offset = 0;
3719 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00003721 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722}
3723
stephen hemmingerc2e21292010-03-17 20:31:10 +00003724static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3725 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726{
3727 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003728 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
Sasha Levinb67bfe02013-02-27 17:06:00 -08003730 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003731 if (!net_eq(dev_net(ifa->idev->dev), net))
3732 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003733 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003734 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003735 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00003736
3737 while (++state->bucket < IN6_ADDR_HSIZE) {
Mihai Maruseac1d578302012-01-03 23:31:35 +00003738 state->offset = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003739 hlist_for_each_entry_rcu_bh(ifa,
stephen hemmingerc2e21292010-03-17 20:31:10 +00003740 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003741 if (!net_eq(dev_net(ifa->idev->dev), net))
3742 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003743 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003744 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08003745 }
3746 }
3747
stephen hemmingerc2e21292010-03-17 20:31:10 +00003748 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749}
3750
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003752 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753{
stephen hemminger5c578ae2010-03-17 20:31:11 +00003754 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00003755 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756}
3757
3758static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3759{
3760 struct inet6_ifaddr *ifa;
3761
3762 ifa = if6_get_next(seq, v);
3763 ++*pos;
3764 return ifa;
3765}
3766
3767static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003768 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769{
stephen hemminger5c578ae2010-03-17 20:31:11 +00003770 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771}
3772
3773static int if6_seq_show(struct seq_file *seq, void *v)
3774{
3775 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Jiri Pirko971a3512013-12-10 13:56:29 +01003776 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07003777 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 ifp->idev->dev->ifindex,
3779 ifp->prefix_len,
3780 ifp->scope,
Jiri Pirko971a3512013-12-10 13:56:29 +01003781 (u8) ifp->flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 ifp->idev->dev->name);
3783 return 0;
3784}
3785
Philippe De Muyter56b3d972007-07-10 23:07:31 -07003786static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 .start = if6_seq_start,
3788 .next = if6_seq_next,
3789 .show = if6_seq_show,
3790 .stop = if6_seq_stop,
3791};
3792
3793static int if6_seq_open(struct inode *inode, struct file *file)
3794{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003795 return seq_open_net(inode, file, &if6_seq_ops,
3796 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797}
3798
Arjan van de Ven9a321442007-02-12 00:55:35 -08003799static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 .owner = THIS_MODULE,
3801 .open = if6_seq_open,
3802 .read = seq_read,
3803 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08003804 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805};
3806
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003807static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808{
Gao fengd4beaa62013-02-18 01:34:54 +00003809 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 return -ENOMEM;
3811 return 0;
3812}
3813
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003814static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08003815{
Gao fengece31ff2013-02-18 01:34:56 +00003816 remove_proc_entry("if_inet6", net->proc_net);
Daniel Lezcano3c400902008-01-10 22:42:49 -08003817}
3818
3819static struct pernet_operations if6_proc_net_ops = {
Ian Morris67ba4152014-08-24 21:53:10 +01003820 .init = if6_proc_net_init,
3821 .exit = if6_proc_net_exit,
Daniel Lezcano3c400902008-01-10 22:42:49 -08003822};
3823
3824int __init if6_proc_init(void)
3825{
3826 return register_pernet_subsys(&if6_proc_net_ops);
3827}
3828
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829void if6_proc_exit(void)
3830{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003831 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832}
3833#endif /* CONFIG_PROC_FS */
3834
Amerigo Wang07a93622012-10-29 16:23:10 +00003835#if IS_ENABLED(CONFIG_IPV6_MIP6)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003836/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003837int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003838{
3839 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00003840 struct inet6_ifaddr *ifp = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00003841 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00003842
stephen hemminger5c578ae2010-03-17 20:31:11 +00003843 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08003844 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09003845 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08003846 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09003847 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003848 (ifp->flags & IFA_F_HOMEADDRESS)) {
3849 ret = 1;
3850 break;
3851 }
3852 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00003853 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003854 return ret;
3855}
3856#endif
3857
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858/*
3859 * Periodic address status verification
3860 */
3861
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003862static void addrconf_verify_rtnl(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003864 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 int i;
3867
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003868 ASSERT_RTNL();
3869
stephen hemminger5c578ae2010-03-17 20:31:11 +00003870 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003872 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003874 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003876 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877restart:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003878 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880
Yasushi Asanofad8da32013-12-31 12:04:19 +09003881 /* When setting preferred_lft to a value not zero or
3882 * infinity, while valid_lft is infinity
3883 * IFA_F_PERMANENT has a non-infinity life time.
3884 */
3885 if ((ifp->flags & IFA_F_PERMANENT) &&
3886 (ifp->prefered_lft == INFINITY_LIFE_TIME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 continue;
3888
3889 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003890 /* We try to batch several events at once. */
3891 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09003893 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
3894 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 spin_unlock(&ifp->lock);
3896 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 ipv6_del_addr(ifp);
3898 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09003899 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
3900 spin_unlock(&ifp->lock);
3901 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00003903 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 int deprecate = 0;
3905
3906 if (!(ifp->flags&IFA_F_DEPRECATED)) {
3907 deprecate = 1;
3908 ifp->flags |= IFA_F_DEPRECATED;
3909 }
3910
Yasushi Asanofad8da32013-12-31 12:04:19 +09003911 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
3912 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 next = ifp->tstamp + ifp->valid_lft * HZ;
3914
3915 spin_unlock(&ifp->lock);
3916
3917 if (deprecate) {
3918 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919
3920 ipv6_ifa_notify(0, ifp);
3921 in6_ifa_put(ifp);
3922 goto restart;
3923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3925 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00003926 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
3927 ifp->idev->cnf.dad_transmits *
Jiri Pirko1f9248e52013-12-07 19:26:53 +01003928 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
stephen hemminger88949cf2010-03-17 20:31:17 +00003929
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 if (age >= ifp->prefered_lft - regen_advance) {
3931 struct inet6_ifaddr *ifpub = ifp->ifpub;
3932 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3933 next = ifp->tstamp + ifp->prefered_lft * HZ;
3934 if (!ifp->regen_count && ifpub) {
3935 ifp->regen_count++;
3936 in6_ifa_hold(ifp);
3937 in6_ifa_hold(ifpub);
3938 spin_unlock(&ifp->lock);
stephen hemminger5c578ae2010-03-17 20:31:11 +00003939
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08003940 spin_lock(&ifpub->lock);
3941 ifpub->regen_count = 0;
3942 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 ipv6_create_tempaddr(ifpub, ifp);
3944 in6_ifa_put(ifpub);
3945 in6_ifa_put(ifp);
3946 goto restart;
3947 }
3948 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3949 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3950 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 } else {
3952 /* ifp->prefered_lft <= ifp->valid_lft */
3953 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3954 next = ifp->tstamp + ifp->prefered_lft * HZ;
3955 spin_unlock(&ifp->lock);
3956 }
3957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 }
3959
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003960 next_sec = round_jiffies_up(next);
3961 next_sched = next;
3962
3963 /* If rounded timeout is accurate enough, accept it. */
3964 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3965 next_sched = next_sec;
3966
3967 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3968 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3969 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3970
dingtianhongba3542e2013-08-17 10:27:04 +08003971 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3972 now, next, next_sec, next_sched);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003973 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
stephen hemminger5c578ae2010-03-17 20:31:11 +00003974 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975}
3976
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003977static void addrconf_verify_work(struct work_struct *w)
3978{
3979 rtnl_lock();
3980 addrconf_verify_rtnl();
3981 rtnl_unlock();
3982}
3983
3984static void addrconf_verify(void)
3985{
3986 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
3987}
3988
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00003989static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
3990 struct in6_addr **peer_pfx)
Thomas Graf461d8832006-09-18 00:09:49 -07003991{
3992 struct in6_addr *pfx = NULL;
3993
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00003994 *peer_pfx = NULL;
3995
Thomas Graf461d8832006-09-18 00:09:49 -07003996 if (addr)
3997 pfx = nla_data(addr);
3998
3999 if (local) {
4000 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004001 *peer_pfx = pfx;
4002 pfx = nla_data(local);
Thomas Graf461d8832006-09-18 00:09:49 -07004003 }
4004
4005 return pfx;
4006}
4007
Patrick McHardyef7c79e2007-06-05 12:38:30 -07004008static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07004009 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
4010 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
4011 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
Jiri Pirko479840f2013-12-06 09:45:21 +01004012 [IFA_FLAGS] = { .len = sizeof(u32) },
Thomas Graf461d8832006-09-18 00:09:49 -07004013};
4014
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015static int
Thomas Graf661d2962013-03-21 07:45:29 +00004016inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004018 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07004019 struct ifaddrmsg *ifm;
4020 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004021 struct in6_addr *pfx, *peer_pfx;
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004022 u32 ifa_flags;
Thomas Grafb933f712006-09-18 00:10:19 -07004023 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024
Thomas Grafb933f712006-09-18 00:10:19 -07004025 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4026 if (err < 0)
4027 return err;
4028
4029 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004030 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004031 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 return -EINVAL;
4033
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02004034 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4035
4036 /* We ignore other flags so far. */
4037 ifa_flags &= IFA_F_MANAGETEMPADDR;
4038
4039 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
4040 ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041}
4042
Jiri Pirko479840f2013-12-06 09:45:21 +01004043static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004044 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004045{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004046 u32 flags;
4047 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004048 unsigned long timeout;
Jiri Pirko53bd6742013-12-06 09:45:22 +01004049 bool was_managetempaddr;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004050 bool had_prefixroute;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09004051
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004052 ASSERT_RTNL();
4053
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004054 if (!valid_lft || (prefered_lft > valid_lft))
4055 return -EINVAL;
4056
Jiri Pirko53bd6742013-12-06 09:45:22 +01004057 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
4058 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
4059 return -EINVAL;
4060
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004061 timeout = addrconf_timeout_fixup(valid_lft, HZ);
4062 if (addrconf_finite_timeout(timeout)) {
4063 expires = jiffies_to_clock_t(timeout * HZ);
4064 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004065 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004066 } else {
4067 expires = 0;
4068 flags = 0;
4069 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07004070 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004071
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09004072 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
4073 if (addrconf_finite_timeout(timeout)) {
4074 if (timeout == 0)
4075 ifa_flags |= IFA_F_DEPRECATED;
4076 prefered_lft = timeout;
4077 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004078
4079 spin_lock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004080 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
Thomas Haller5b84efe2014-01-15 15:36:59 +01004081 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
4082 !(ifp->flags & IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004083 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
Thomas Haller761aac72014-01-15 15:36:58 +01004084 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
4085 IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01004086 ifp->flags |= ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004087 ifp->tstamp = jiffies;
4088 ifp->valid_lft = valid_lft;
4089 ifp->prefered_lft = prefered_lft;
4090
4091 spin_unlock_bh(&ifp->lock);
4092 if (!(ifp->flags&IFA_F_TENTATIVE))
4093 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004094
Thomas Haller761aac72014-01-15 15:36:58 +01004095 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
4096 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
4097 expires, flags);
Thomas Haller5b84efe2014-01-15 15:36:59 +01004098 } else if (had_prefixroute) {
4099 enum cleanup_prefix_rt_t action;
4100 unsigned long rt_expires;
4101
4102 write_lock_bh(&ifp->idev->lock);
4103 action = check_cleanup_prefix_route(ifp, &rt_expires);
4104 write_unlock_bh(&ifp->idev->lock);
4105
4106 if (action != CLEANUP_PREFIX_RT_NOP) {
4107 cleanup_prefix_route(ifp, rt_expires,
4108 action == CLEANUP_PREFIX_RT_DEL);
4109 }
Thomas Haller761aac72014-01-15 15:36:58 +01004110 }
Jiri Pirko53bd6742013-12-06 09:45:22 +01004111
4112 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
4113 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
4114 valid_lft = prefered_lft = 0;
4115 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
4116 !was_managetempaddr, jiffies);
4117 }
4118
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004119 addrconf_verify_rtnl();
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004120
4121 return 0;
4122}
4123
4124static int
Thomas Graf661d2962013-03-21 07:45:29 +00004125inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004127 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07004128 struct ifaddrmsg *ifm;
4129 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004130 struct in6_addr *pfx, *peer_pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07004131 struct inet6_ifaddr *ifa;
4132 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004133 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
Jiri Pirko479840f2013-12-06 09:45:21 +01004134 u32 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07004135 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136
Thomas Graf461d8832006-09-18 00:09:49 -07004137 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4138 if (err < 0)
4139 return err;
4140
4141 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004142 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Ian Morris63159f22015-03-29 14:00:04 +01004143 if (!pfx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 return -EINVAL;
4145
Thomas Graf461d8832006-09-18 00:09:49 -07004146 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004147 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07004148
4149 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004150 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07004151 preferred_lft = ci->ifa_prefered;
4152 } else {
4153 preferred_lft = INFINITY_LIFE_TIME;
4154 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09004155 }
4156
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004157 dev = __dev_get_by_index(net, ifm->ifa_index);
Ian Morris63159f22015-03-29 14:00:04 +01004158 if (!dev)
Thomas Graf7198f8c2006-09-18 00:13:46 -07004159 return -ENODEV;
4160
Jiri Pirko479840f2013-12-06 09:45:21 +01004161 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
4162
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004163 /* We ignore other flags so far. */
Thomas Haller761aac72014-01-15 15:36:58 +01004164 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
Madhu Challa93a714d2015-02-25 09:58:35 -08004165 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004166
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08004167 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Ian Morris63159f22015-03-29 14:00:04 +01004168 if (!ifa) {
Thomas Graf7198f8c2006-09-18 00:13:46 -07004169 /*
4170 * It would be best to check for !NLM_F_CREATE here but
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08004171 * userspace already relies on not having to provide this.
Thomas Graf7198f8c2006-09-18 00:13:46 -07004172 */
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004173 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08004174 ifm->ifa_prefixlen, ifa_flags,
4175 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09004176 }
4177
Thomas Graf7198f8c2006-09-18 00:13:46 -07004178 if (nlh->nlmsg_flags & NLM_F_EXCL ||
4179 !(nlh->nlmsg_flags & NLM_F_REPLACE))
4180 err = -EEXIST;
4181 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07004182 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07004183
4184 in6_ifa_put(ifa);
4185
4186 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187}
4188
Jiri Pirko479840f2013-12-06 09:45:21 +01004189static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
Thomas Graf101bb222006-09-18 00:11:52 -07004190 u8 scope, int ifindex)
4191{
4192 struct ifaddrmsg *ifm;
4193
4194 ifm = nlmsg_data(nlh);
4195 ifm->ifa_family = AF_INET6;
4196 ifm->ifa_prefixlen = prefixlen;
4197 ifm->ifa_flags = flags;
4198 ifm->ifa_scope = scope;
4199 ifm->ifa_index = ifindex;
4200}
4201
Thomas Graf85486af2006-09-18 00:11:24 -07004202static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
4203 unsigned long tstamp, u32 preferred, u32 valid)
4204{
4205 struct ifa_cacheinfo ci;
4206
Thomas Graf18a31e12010-11-17 04:12:02 +00004207 ci.cstamp = cstamp_delta(cstamp);
4208 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07004209 ci.ifa_prefered = preferred;
4210 ci.ifa_valid = valid;
4211
4212 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
4213}
4214
Thomas Graf101bb222006-09-18 00:11:52 -07004215static inline int rt_scope(int ifa_scope)
4216{
4217 if (ifa_scope & IFA_HOST)
4218 return RT_SCOPE_HOST;
4219 else if (ifa_scope & IFA_LINK)
4220 return RT_SCOPE_LINK;
4221 else if (ifa_scope & IFA_SITE)
4222 return RT_SCOPE_SITE;
4223 else
4224 return RT_SCOPE_UNIVERSE;
4225}
4226
Thomas Graf0ab68032006-09-18 00:12:35 -07004227static inline int inet6_ifaddr_msgsize(void)
4228{
Thomas Graf339bf982006-11-10 14:10:15 -08004229 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004230 + nla_total_size(16) /* IFA_LOCAL */
Thomas Graf339bf982006-11-10 14:10:15 -08004231 + nla_total_size(16) /* IFA_ADDRESS */
Jiri Pirko479840f2013-12-06 09:45:21 +01004232 + nla_total_size(sizeof(struct ifa_cacheinfo))
4233 + nla_total_size(4) /* IFA_FLAGS */;
Thomas Graf0ab68032006-09-18 00:12:35 -07004234}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004235
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004237 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07004240 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
Eric W. Biederman15e47302012-09-07 20:12:54 +00004242 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004243 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004244 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004245
Thomas Graf101bb222006-09-18 00:11:52 -07004246 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
4247 ifa->idev->dev->ifindex);
4248
Yasushi Asanofad8da32013-12-31 12:04:19 +09004249 if (!((ifa->flags&IFA_F_PERMANENT) &&
4250 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
Thomas Graf85486af2006-09-18 00:11:24 -07004251 preferred = ifa->prefered_lft;
4252 valid = ifa->valid_lft;
4253 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00004255 if (preferred > tval)
4256 preferred -= tval;
4257 else
4258 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00004259 if (valid != INFINITY_LIFE_TIME) {
4260 if (valid > tval)
4261 valid -= tval;
4262 else
4263 valid = 0;
4264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 }
4266 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07004267 preferred = INFINITY_LIFE_TIME;
4268 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 }
Thomas Graf85486af2006-09-18 00:11:24 -07004270
Cong Wang7996c792013-05-22 05:41:06 +00004271 if (!ipv6_addr_any(&ifa->peer_addr)) {
Jiri Benc930345e2015-03-29 16:59:25 +02004272 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4273 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004274 goto error;
4275 } else
Jiri Benc930345e2015-03-29 16:59:25 +02004276 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004277 goto error;
4278
4279 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
4280 goto error;
Thomas Graf85486af2006-09-18 00:11:24 -07004281
Jiri Pirko479840f2013-12-06 09:45:21 +01004282 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
4283 goto error;
4284
Johannes Berg053c0952015-01-16 22:09:00 +01004285 nlmsg_end(skb, nlh);
4286 return 0;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004287
4288error:
4289 nlmsg_cancel(skb, nlh);
4290 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291}
4292
4293static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004294 u32 portid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004297 u8 scope = RT_SCOPE_UNIVERSE;
4298 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
Thomas Graf101bb222006-09-18 00:11:52 -07004300 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4301 scope = RT_SCOPE_SITE;
4302
Eric W. Biederman15e47302012-09-07 20:12:54 +00004303 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004304 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004305 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004306
Thomas Graf101bb222006-09-18 00:11:52 -07004307 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004308 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004309 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004310 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4311 nlmsg_cancel(skb, nlh);
4312 return -EMSGSIZE;
4313 }
Thomas Graf85486af2006-09-18 00:11:24 -07004314
Johannes Berg053c0952015-01-16 22:09:00 +01004315 nlmsg_end(skb, nlh);
4316 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317}
4318
4319static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004320 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004323 u8 scope = RT_SCOPE_UNIVERSE;
4324 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325
Thomas Graf101bb222006-09-18 00:11:52 -07004326 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4327 scope = RT_SCOPE_SITE;
4328
Eric W. Biederman15e47302012-09-07 20:12:54 +00004329 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004330 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004331 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004332
Thomas Graf101bb222006-09-18 00:11:52 -07004333 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Jiri Benc930345e2015-03-29 16:59:25 +02004334 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
Thomas Graf0ab68032006-09-18 00:12:35 -07004335 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004336 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4337 nlmsg_cancel(skb, nlh);
4338 return -EMSGSIZE;
4339 }
Thomas Graf85486af2006-09-18 00:11:24 -07004340
Johannes Berg053c0952015-01-16 22:09:00 +01004341 nlmsg_end(skb, nlh);
4342 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343}
4344
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004345enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 UNICAST_ADDR,
4347 MULTICAST_ADDR,
4348 ANYCAST_ADDR,
4349};
4350
Eric Dumazet234b27c2009-11-12 04:11:50 +00004351/* called with rcu_read_lock() */
4352static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4353 struct netlink_callback *cb, enum addr_type_t type,
4354 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 struct ifmcaddr6 *ifmca;
4357 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004358 int err = 1;
4359 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360
Eric Dumazet234b27c2009-11-12 04:11:50 +00004361 read_lock_bh(&idev->lock);
4362 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00004363 case UNICAST_ADDR: {
4364 struct inet6_ifaddr *ifa;
4365
Eric Dumazet234b27c2009-11-12 04:11:50 +00004366 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00004367 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4368 if (++ip_idx < s_ip_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004369 continue;
4370 err = inet6_fill_ifaddr(skb, ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004371 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004372 cb->nlh->nlmsg_seq,
4373 RTM_NEWADDR,
4374 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004375 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004376 break;
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004377 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004379 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00004380 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004381 case MULTICAST_ADDR:
4382 /* multicast address */
4383 for (ifmca = idev->mc_list; ifmca;
4384 ifmca = ifmca->next, ip_idx++) {
4385 if (ip_idx < s_ip_idx)
4386 continue;
4387 err = inet6_fill_ifmcaddr(skb, ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004388 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004389 cb->nlh->nlmsg_seq,
4390 RTM_GETMULTICAST,
4391 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004392 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004393 break;
4394 }
4395 break;
4396 case ANYCAST_ADDR:
4397 /* anycast address */
4398 for (ifaca = idev->ac_list; ifaca;
4399 ifaca = ifaca->aca_next, ip_idx++) {
4400 if (ip_idx < s_ip_idx)
4401 continue;
4402 err = inet6_fill_ifacaddr(skb, ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004403 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004404 cb->nlh->nlmsg_seq,
4405 RTM_GETANYCAST,
4406 NLM_F_MULTI);
Johannes Berg053c0952015-01-16 22:09:00 +01004407 if (err < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004408 break;
4409 }
4410 break;
4411 default:
4412 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004414 read_unlock_bh(&idev->lock);
4415 *p_ip_idx = ip_idx;
4416 return err;
4417}
4418
4419static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4420 enum addr_type_t type)
4421{
4422 struct net *net = sock_net(skb->sk);
4423 int h, s_h;
4424 int idx, ip_idx;
4425 int s_idx, s_ip_idx;
4426 struct net_device *dev;
4427 struct inet6_dev *idev;
4428 struct hlist_head *head;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004429
4430 s_h = cb->args[0];
4431 s_idx = idx = cb->args[1];
4432 s_ip_idx = ip_idx = cb->args[2];
4433
4434 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004435 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004436 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4437 idx = 0;
4438 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08004439 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet234b27c2009-11-12 04:11:50 +00004440 if (idx < s_idx)
4441 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07004442 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004443 s_ip_idx = 0;
4444 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004445 idev = __in6_dev_get(dev);
4446 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004447 goto cont;
4448
4449 if (in6_dump_addrs(idev, skb, cb, type,
David S. Miller7b46a642015-01-18 23:36:08 -05004450 s_ip_idx, &ip_idx) < 0)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004451 goto done;
4452cont:
4453 idx++;
4454 }
4455 }
4456done:
4457 rcu_read_unlock();
4458 cb->args[0] = h;
4459 cb->args[1] = idx;
4460 cb->args[2] = ip_idx;
4461
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 return skb->len;
4463}
4464
4465static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4466{
4467 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004468
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 return inet6_dump_addr(skb, cb, type);
4470}
4471
4472static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4473{
4474 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004475
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 return inet6_dump_addr(skb, cb, type);
4477}
4478
4479
4480static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4481{
4482 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004483
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 return inet6_dump_addr(skb, cb, type);
4485}
4486
Thomas Graf661d2962013-03-21 07:45:29 +00004487static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004488{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004489 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004490 struct ifaddrmsg *ifm;
4491 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004492 struct in6_addr *addr = NULL, *peer;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004493 struct net_device *dev = NULL;
4494 struct inet6_ifaddr *ifa;
4495 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004496 int err;
4497
Thomas Graf1b29fc22006-09-18 00:10:50 -07004498 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4499 if (err < 0)
4500 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004501
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004502 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
Ian Morris63159f22015-03-29 14:00:04 +01004503 if (!addr) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004504 err = -EINVAL;
4505 goto errout;
4506 }
4507
4508 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004509 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004510 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004511
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004512 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4513 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004514 err = -EADDRNOTAVAIL;
4515 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004516 }
4517
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004518 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4519 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004520 err = -ENOBUFS;
4521 goto errout_ifa;
4522 }
4523
Eric W. Biederman15e47302012-09-07 20:12:54 +00004524 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004525 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004526 if (err < 0) {
4527 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4528 WARN_ON(err == -EMSGSIZE);
4529 kfree_skb(skb);
4530 goto errout_ifa;
4531 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00004532 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004533errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004534 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004535errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004536 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004537}
4538
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4540{
4541 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004542 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07004543 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
Thomas Graf0ab68032006-09-18 00:12:35 -07004545 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01004546 if (!skb)
Thomas Graf5d620262006-08-15 00:35:02 -07004547 goto errout;
4548
4549 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004550 if (err < 0) {
4551 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4552 WARN_ON(err == -EMSGSIZE);
4553 kfree_skb(skb);
4554 goto errout;
4555 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004556 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4557 return;
Thomas Graf5d620262006-08-15 00:35:02 -07004558errout:
4559 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004560 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561}
4562
Dave Jonesb6f99a22007-03-22 12:27:49 -07004563static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 __s32 *array, int bytes)
4565{
Thomas Graf04561c12006-11-14 19:53:58 -08004566 BUG_ON(bytes < (DEVCONF_MAX * 4));
4567
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 memset(array, 0, bytes);
4569 array[DEVCONF_FORWARDING] = cnf->forwarding;
4570 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4571 array[DEVCONF_MTU6] = cnf->mtu6;
4572 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4573 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4574 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4575 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4576 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00004577 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4578 jiffies_to_msecs(cnf->rtr_solicit_interval);
4579 array[DEVCONF_RTR_SOLICIT_DELAY] =
4580 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02004582 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4583 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4584 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4585 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4587 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4588 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4589 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4590 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004592 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
Hangbin Liu8013d1d2015-07-30 14:28:42 +08004593 array[DEVCONF_ACCEPT_RA_MIN_HOP_LIMIT] = cnf->accept_ra_min_hop_limit;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004594 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004595#ifdef CONFIG_IPV6_ROUTER_PREF
4596 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00004597 array[DEVCONF_RTR_PROBE_INTERVAL] =
4598 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08004599#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004600 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4601#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004602#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004603 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004604 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07004605#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4606 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
Erik Kline7fd25612014-10-28 18:11:14 +09004607 array[DEVCONF_USE_OPTIMISTIC] = cnf->use_optimistic;
Neil Horman95c385b2007-04-25 17:08:10 -07004608#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004609#ifdef CONFIG_IPV6_MROUTE
4610 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4611#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09004612 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09004613 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00004614 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00004615 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02004616 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
Ben Greeard9333192014-06-25 14:44:53 -07004617 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
Harout Hedeshianc2943f12015-01-20 10:06:05 -07004618 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01004619 /* we omit DEVCONF_STABLE_SECRET for now */
Erik Kline3985e8a2015-07-22 16:38:25 +09004620 array[DEVCONF_USE_OIF_ADDRS_ONLY] = cnf->use_oif_addrs_only;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621}
4622
Thomas Grafb382b192010-11-16 04:33:57 +00004623static inline size_t inet6_ifla6_size(void)
4624{
4625 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4626 + nla_total_size(sizeof(struct ifla_cacheinfo))
4627 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4628 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004629 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4630 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
Thomas Grafb382b192010-11-16 04:33:57 +00004631}
4632
Thomas Graf339bf982006-11-10 14:10:15 -08004633static inline size_t inet6_if_nlmsg_size(void)
4634{
4635 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4636 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4637 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4638 + nla_total_size(4) /* IFLA_MTU */
4639 + nla_total_size(4) /* IFLA_LINK */
Thomas Grafb382b192010-11-16 04:33:57 +00004640 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08004641}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004642
Eric Dumazetbe281e52011-05-19 01:14:23 +00004643static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Tejun Heo7d720c32010-02-16 15:20:26 +00004644 int items, int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004645{
4646 int i;
4647 int pad = bytes - sizeof(u64) * items;
4648 BUG_ON(pad < 0);
4649
4650 /* Use put_unaligned() because stats may not be aligned for u64. */
4651 put_unaligned(items, &stats[0]);
4652 for (i = 1; i < items; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00004653 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004654
4655 memset(&stats[items], 0, pad);
4656}
4657
WANG Cong698365f2014-05-05 15:55:55 -07004658static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
Eric Dumazet4ce3c182010-06-30 13:31:19 -07004659 int items, int bytes, size_t syncpoff)
4660{
4661 int i;
4662 int pad = bytes - sizeof(u64) * items;
4663 BUG_ON(pad < 0);
4664
4665 /* Use put_unaligned() because stats may not be aligned for u64. */
4666 put_unaligned(items, &stats[0]);
4667 for (i = 1; i < items; i++)
4668 put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
4669
4670 memset(&stats[items], 0, pad);
4671}
4672
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004673static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
4674 int bytes)
4675{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004676 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004677 case IFLA_INET6_STATS:
WANG Cong698365f2014-05-05 15:55:55 -07004678 __snmp6_fill_stats64(stats, idev->stats.ipv6,
Eric Dumazet4ce3c182010-06-30 13:31:19 -07004679 IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004680 break;
4681 case IFLA_INET6_ICMP6STATS:
Eric Dumazetbe281e52011-05-19 01:14:23 +00004682 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004683 break;
4684 }
4685}
4686
Thomas Grafb382b192010-11-16 04:33:57 +00004687static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004689 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08004690 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691
David S. Millerc78679e2012-04-01 20:27:33 -04004692 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
4693 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08004695 ci.tstamp = cstamp_delta(idev->tstamp);
4696 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
Jiri Pirko1f9248e52013-12-07 19:26:53 +01004697 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
David S. Millerc78679e2012-04-01 20:27:33 -04004698 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
4699 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004700 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
Ian Morris63159f22015-03-29 14:00:04 +01004701 if (!nla)
Thomas Graf04561c12006-11-14 19:53:58 -08004702 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004703 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004705 /* XXX - MC not implemented */
4706
4707 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01004708 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004709 goto nla_put_failure;
4710 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4711
4712 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
Ian Morris63159f22015-03-29 14:00:04 +01004713 if (!nla)
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004714 goto nla_put_failure;
4715 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004717 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
Ian Morris63159f22015-03-29 14:00:04 +01004718 if (!nla)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004719 goto nla_put_failure;
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02004720
4721 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode))
4722 goto nla_put_failure;
4723
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004724 read_lock_bh(&idev->lock);
4725 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
4726 read_unlock_bh(&idev->lock);
4727
Thomas Grafb382b192010-11-16 04:33:57 +00004728 return 0;
4729
4730nla_put_failure:
4731 return -EMSGSIZE;
4732}
4733
4734static size_t inet6_get_link_af_size(const struct net_device *dev)
4735{
4736 if (!__in6_dev_get(dev))
4737 return 0;
4738
4739 return inet6_ifla6_size();
4740}
4741
4742static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4743{
4744 struct inet6_dev *idev = __in6_dev_get(dev);
4745
4746 if (!idev)
4747 return -ENODATA;
4748
4749 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4750 return -EMSGSIZE;
4751
4752 return 0;
4753}
4754
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004755static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
4756{
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004757 struct inet6_ifaddr *ifp;
4758 struct net_device *dev = idev->dev;
Daniel Borkmannfc403832013-04-09 03:47:15 +00004759 bool update_rs = false;
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02004760 struct in6_addr ll_addr;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004761
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004762 ASSERT_RTNL();
4763
Ian Morris63159f22015-03-29 14:00:04 +01004764 if (!token)
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004765 return -EINVAL;
4766 if (ipv6_addr_any(token))
4767 return -EINVAL;
4768 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
4769 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004770 if (!ipv6_accept_ra(idev))
4771 return -EINVAL;
4772 if (idev->cnf.rtr_solicits <= 0)
4773 return -EINVAL;
4774
4775 write_lock_bh(&idev->lock);
4776
4777 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
4778 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
4779
4780 write_unlock_bh(&idev->lock);
4781
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02004782 if (!idev->dead && (idev->if_flags & IF_READY) &&
4783 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
4784 IFA_F_OPTIMISTIC)) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00004785
4786 /* If we're not ready, then normal ifup will take care
4787 * of this. Otherwise, we need to request our rs here.
4788 */
4789 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
4790 update_rs = true;
4791 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004792
4793 write_lock_bh(&idev->lock);
Daniel Borkmannfc403832013-04-09 03:47:15 +00004794
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02004795 if (update_rs) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00004796 idev->if_flags |= IF_RS_SENT;
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02004797 idev->rs_probes = 1;
4798 addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval);
4799 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004800
4801 /* Well, that's kinda nasty ... */
4802 list_for_each_entry(ifp, &idev->addr_list, if_list) {
4803 spin_lock(&ifp->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00004804 if (ifp->tokenized) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004805 ifp->valid_lft = 0;
4806 ifp->prefered_lft = 0;
4807 }
4808 spin_unlock(&ifp->lock);
4809 }
4810
4811 write_unlock_bh(&idev->lock);
Lubomir Rintelb2ed64a2014-10-27 17:39:16 +01004812 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004813 addrconf_verify_rtnl();
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004814 return 0;
4815}
4816
Daniel Borkmann11b1f822015-02-05 14:39:11 +01004817static const struct nla_policy inet6_af_policy[IFLA_INET6_MAX + 1] = {
4818 [IFLA_INET6_ADDR_GEN_MODE] = { .type = NLA_U8 },
4819 [IFLA_INET6_TOKEN] = { .len = sizeof(struct in6_addr) },
4820};
4821
4822static int inet6_validate_link_af(const struct net_device *dev,
4823 const struct nlattr *nla)
4824{
4825 struct nlattr *tb[IFLA_INET6_MAX + 1];
4826
4827 if (dev && !__in6_dev_get(dev))
4828 return -EAFNOSUPPORT;
4829
4830 return nla_parse_nested(tb, IFLA_INET6_MAX, nla, inet6_af_policy);
4831}
4832
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004833static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
4834{
4835 int err = -EINVAL;
4836 struct inet6_dev *idev = __in6_dev_get(dev);
4837 struct nlattr *tb[IFLA_INET6_MAX + 1];
4838
4839 if (!idev)
4840 return -EAFNOSUPPORT;
4841
4842 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
4843 BUG();
4844
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02004845 if (tb[IFLA_INET6_TOKEN]) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004846 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02004847 if (err)
4848 return err;
4849 }
4850
4851 if (tb[IFLA_INET6_ADDR_GEN_MODE]) {
4852 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
4853
4854 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01004855 mode != IN6_ADDR_GEN_MODE_NONE &&
4856 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY)
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02004857 return -EINVAL;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01004858
4859 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
4860 !idev->cnf.stable_secret.initialized &&
4861 !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized)
4862 return -EINVAL;
4863
Jiri Pirkobc91b0f2014-07-11 21:10:18 +02004864 idev->addr_gen_mode = mode;
4865 err = 0;
4866 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004867
4868 return err;
4869}
4870
Thomas Grafb382b192010-11-16 04:33:57 +00004871static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004872 u32 portid, u32 seq, int event, unsigned int flags)
Thomas Grafb382b192010-11-16 04:33:57 +00004873{
4874 struct net_device *dev = idev->dev;
4875 struct ifinfomsg *hdr;
4876 struct nlmsghdr *nlh;
4877 void *protoinfo;
4878
Eric W. Biederman15e47302012-09-07 20:12:54 +00004879 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004880 if (!nlh)
Thomas Grafb382b192010-11-16 04:33:57 +00004881 return -EMSGSIZE;
4882
4883 hdr = nlmsg_data(nlh);
4884 hdr->ifi_family = AF_INET6;
4885 hdr->__ifi_pad = 0;
4886 hdr->ifi_type = dev->type;
4887 hdr->ifi_index = dev->ifindex;
4888 hdr->ifi_flags = dev_get_flags(dev);
4889 hdr->ifi_change = 0;
4890
David S. Millerc78679e2012-04-01 20:27:33 -04004891 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4892 (dev->addr_len &&
4893 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4894 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
Nicolas Dichtela54acb32015-04-02 17:07:00 +02004895 (dev->ifindex != dev_get_iflink(dev) &&
4896 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
David S. Millerc78679e2012-04-01 20:27:33 -04004897 goto nla_put_failure;
Thomas Grafb382b192010-11-16 04:33:57 +00004898 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
Ian Morris63159f22015-03-29 14:00:04 +01004899 if (!protoinfo)
Thomas Grafb382b192010-11-16 04:33:57 +00004900 goto nla_put_failure;
4901
4902 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4903 goto nla_put_failure;
4904
Thomas Graf04561c12006-11-14 19:53:58 -08004905 nla_nest_end(skb, protoinfo);
Johannes Berg053c0952015-01-16 22:09:00 +01004906 nlmsg_end(skb, nlh);
4907 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908
Thomas Graf04561c12006-11-14 19:53:58 -08004909nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004910 nlmsg_cancel(skb, nlh);
4911 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912}
4913
4914static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
4915{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004916 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00004917 int h, s_h;
David S. Miller434a8a582009-11-11 18:53:00 -08004918 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 struct net_device *dev;
4920 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00004921 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
Eric Dumazet84d26972009-11-09 12:11:28 +00004923 s_h = cb->args[0];
4924 s_idx = cb->args[1];
4925
4926 rcu_read_lock();
4927 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4928 idx = 0;
4929 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08004930 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet84d26972009-11-09 12:11:28 +00004931 if (idx < s_idx)
4932 goto cont;
4933 idev = __in6_dev_get(dev);
4934 if (!idev)
4935 goto cont;
4936 if (inet6_fill_ifinfo(skb, idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004937 NETLINK_CB(cb->skb).portid,
Eric Dumazet84d26972009-11-09 12:11:28 +00004938 cb->nlh->nlmsg_seq,
Johannes Berg053c0952015-01-16 22:09:00 +01004939 RTM_NEWLINK, NLM_F_MULTI) < 0)
Eric Dumazet84d26972009-11-09 12:11:28 +00004940 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07004941cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00004942 idx++;
4943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 }
Eric Dumazet84d26972009-11-09 12:11:28 +00004945out:
4946 rcu_read_unlock();
4947 cb->args[1] = idx;
4948 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
4950 return skb->len;
4951}
4952
4953void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
4954{
4955 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004956 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004957 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004958
Thomas Graf339bf982006-11-10 14:10:15 -08004959 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01004960 if (!skb)
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004961 goto errout;
4962
4963 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004964 if (err < 0) {
4965 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
4966 WARN_ON(err == -EMSGSIZE);
4967 kfree_skb(skb);
4968 goto errout;
4969 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00004970 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004971 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004972errout:
4973 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00004974 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975}
4976
Thomas Graf339bf982006-11-10 14:10:15 -08004977static inline size_t inet6_prefix_nlmsg_size(void)
4978{
4979 return NLMSG_ALIGN(sizeof(struct prefixmsg))
4980 + nla_total_size(sizeof(struct in6_addr))
4981 + nla_total_size(sizeof(struct prefix_cacheinfo));
4982}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004983
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004985 struct prefix_info *pinfo, u32 portid, u32 seq,
Thomas Graf6051e2f2006-11-14 19:54:19 -08004986 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987{
Thomas Graf6051e2f2006-11-14 19:54:19 -08004988 struct prefixmsg *pmsg;
4989 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 struct prefix_cacheinfo ci;
4991
Eric W. Biederman15e47302012-09-07 20:12:54 +00004992 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
Ian Morris63159f22015-03-29 14:00:04 +01004993 if (!nlh)
Patrick McHardy26932562007-01-31 23:16:40 -08004994 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08004995
4996 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07004998 pmsg->prefix_pad1 = 0;
4999 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 pmsg->prefix_ifindex = idev->dev->ifindex;
5001 pmsg->prefix_len = pinfo->prefix_len;
5002 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07005003 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 pmsg->prefix_flags = 0;
5005 if (pinfo->onlink)
5006 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
5007 if (pinfo->autoconf)
5008 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
5009
David S. Millerc78679e2012-04-01 20:27:33 -04005010 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
5011 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 ci.preferred_time = ntohl(pinfo->prefered);
5013 ci.valid_time = ntohl(pinfo->valid);
David S. Millerc78679e2012-04-01 20:27:33 -04005014 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
5015 goto nla_put_failure;
Johannes Berg053c0952015-01-16 22:09:00 +01005016 nlmsg_end(skb, nlh);
5017 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018
Thomas Graf6051e2f2006-11-14 19:54:19 -08005019nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08005020 nlmsg_cancel(skb, nlh);
5021 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022}
5023
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005024static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 struct prefix_info *pinfo)
5026{
5027 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005028 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005029 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030
Thomas Graf339bf982006-11-10 14:10:15 -08005031 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Ian Morris63159f22015-03-29 14:00:04 +01005032 if (!skb)
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005033 goto errout;
5034
5035 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08005036 if (err < 0) {
5037 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
5038 WARN_ON(err == -EMSGSIZE);
5039 kfree_skb(skb);
5040 goto errout;
5041 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08005042 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
5043 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07005044errout:
5045 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08005046 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047}
5048
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5050{
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005051 struct net *net = dev_net(ifp->idev->dev);
5052
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005053 if (event)
5054 ASSERT_RTNL();
5055
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
5057
5058 switch (event) {
5059 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07005060 /*
5061 * If the address was optimistic
5062 * we inserted the route at the start of
5063 * our DAD process, so we don't need
5064 * to do it again
5065 */
5066 if (!(ifp->rt->rt6i_node))
5067 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 if (ifp->idev->cnf.forwarding)
5069 addrconf_join_anycast(ifp);
Cong Wang7996c792013-05-22 05:41:06 +00005070 if (!ipv6_addr_any(&ifp->peer_addr))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005071 addrconf_prefix_route(&ifp->peer_addr, 128,
5072 ifp->idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 break;
5074 case RTM_DELADDR:
5075 if (ifp->idev->cnf.forwarding)
5076 addrconf_leave_anycast(ifp);
5077 addrconf_leave_solict(ifp->idev, &ifp->addr);
Cong Wang7996c792013-05-22 05:41:06 +00005078 if (!ipv6_addr_any(&ifp->peer_addr)) {
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005079 struct rt6_info *rt;
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005080
Nicolas Dichtele7478df2014-09-03 23:59:22 +02005081 rt = addrconf_get_prefix_route(&ifp->peer_addr, 128,
5082 ifp->idev->dev, 0, 0);
Nicolas Dichtelf24062b2014-09-03 23:59:21 +02005083 if (rt && ip6_del_rt(rt))
5084 dst_free(&rt->dst);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00005085 }
Changli Gaod8d1f302010-06-10 23:31:35 -07005086 dst_hold(&ifp->rt->dst);
stephen hemminger93fa1592010-04-12 05:41:31 +00005087
David S. Miller73a8bd72011-01-23 23:27:15 -08005088 if (ip6_del_rt(ifp->rt))
Changli Gaod8d1f302010-06-10 23:31:35 -07005089 dst_free(&ifp->rt->dst);
Hannes Frederic Sowa705f1c82014-09-28 00:46:06 +02005090
5091 rt_genid_bump_ipv6(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 break;
5093 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00005094 atomic_inc(&net->ipv6.dev_addr_genid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095}
5096
5097static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
5098{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005099 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 if (likely(ifp->idev->dead == 0))
5101 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07005102 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103}
5104
5105#ifdef CONFIG_SYSCTL
5106
5107static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005108int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 void __user *buffer, size_t *lenp, loff_t *ppos)
5110{
5111 int *valp = ctl->data;
5112 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005113 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005114 struct ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 int ret;
5116
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005117 /*
5118 * ctl->data points to idev->cnf.forwarding, we should
5119 * not modify it until we get the rtnl lock.
5120 */
5121 lctl = *ctl;
5122 lctl.data = &val;
5123
5124 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08005126 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00005127 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005128 if (ret)
5129 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005130 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131}
5132
Marcelo Leitner77751422015-02-23 11:17:13 -03005133static
5134int addrconf_sysctl_mtu(struct ctl_table *ctl, int write,
5135 void __user *buffer, size_t *lenp, loff_t *ppos)
5136{
5137 struct inet6_dev *idev = ctl->extra1;
5138 int min_mtu = IPV6_MIN_MTU;
5139 struct ctl_table lctl;
5140
5141 lctl = *ctl;
5142 lctl.extra1 = &min_mtu;
5143 lctl.extra2 = idev ? &idev->dev->mtu : NULL;
5144
5145 return proc_dointvec_minmax(&lctl, write, buffer, lenp, ppos);
5146}
5147
Brian Haley56d417b2009-06-01 03:07:33 -07005148static void dev_disable_change(struct inet6_dev *idev)
5149{
Cong Wang75538c22013-05-29 11:30:50 +08005150 struct netdev_notifier_info info;
5151
Brian Haley56d417b2009-06-01 03:07:33 -07005152 if (!idev || !idev->dev)
5153 return;
5154
Cong Wang75538c22013-05-29 11:30:50 +08005155 netdev_notifier_info_init(&info, idev->dev);
Brian Haley56d417b2009-06-01 03:07:33 -07005156 if (idev->cnf.disable_ipv6)
Cong Wang75538c22013-05-29 11:30:50 +08005157 addrconf_notify(NULL, NETDEV_DOWN, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005158 else
Cong Wang75538c22013-05-29 11:30:50 +08005159 addrconf_notify(NULL, NETDEV_UP, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07005160}
5161
5162static void addrconf_disable_change(struct net *net, __s32 newf)
5163{
5164 struct net_device *dev;
5165 struct inet6_dev *idev;
5166
Eric Dumazetc6d14c82009-11-04 05:43:23 -08005167 rcu_read_lock();
5168 for_each_netdev_rcu(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07005169 idev = __in6_dev_get(dev);
5170 if (idev) {
5171 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
5172 idev->cnf.disable_ipv6 = newf;
5173 if (changed)
5174 dev_disable_change(idev);
5175 }
Brian Haley56d417b2009-06-01 03:07:33 -07005176 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -08005177 rcu_read_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07005178}
5179
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005180static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07005181{
5182 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005183 int old;
5184
5185 if (!rtnl_trylock())
5186 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07005187
5188 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005189 old = *p;
5190 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07005191
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005192 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
5193 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07005194 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005195 }
Brian Haley56d417b2009-06-01 03:07:33 -07005196
5197 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07005198 net->ipv6.devconf_dflt->disable_ipv6 = newf;
5199 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005200 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07005201 dev_disable_change((struct inet6_dev *)table->extra1);
5202
5203 rtnl_unlock();
5204 return 0;
5205}
5206
5207static
Joe Perchesfe2c6332013-06-11 23:04:25 -07005208int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07005209 void __user *buffer, size_t *lenp, loff_t *ppos)
5210{
5211 int *valp = ctl->data;
5212 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00005213 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005214 struct ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07005215 int ret;
5216
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00005217 /*
5218 * ctl->data points to idev->cnf.disable_ipv6, we should
5219 * not modify it until we get the rtnl lock.
5220 */
5221 lctl = *ctl;
5222 lctl.data = &val;
5223
5224 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07005225
5226 if (write)
5227 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00005228 if (ret)
5229 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07005230 return ret;
5231}
5232
stephen hemmingerc92d5492013-12-17 22:37:14 -08005233static
5234int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5235 void __user *buffer, size_t *lenp, loff_t *ppos)
5236{
5237 int *valp = ctl->data;
5238 int ret;
5239 int old, new;
5240
5241 old = *valp;
5242 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
5243 new = *valp;
5244
5245 if (write && old != new) {
5246 struct net *net = ctl->extra2;
5247
5248 if (!rtnl_trylock())
5249 return restart_syscall();
5250
5251 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
5252 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5253 NETCONFA_IFINDEX_DEFAULT,
5254 net->ipv6.devconf_dflt);
5255 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
5256 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5257 NETCONFA_IFINDEX_ALL,
5258 net->ipv6.devconf_all);
5259 else {
5260 struct inet6_dev *idev = ctl->extra1;
5261
5262 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
5263 idev->dev->ifindex,
5264 &idev->cnf);
5265 }
5266 rtnl_unlock();
5267 }
5268
5269 return ret;
5270}
5271
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005272static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5273 void __user *buffer, size_t *lenp,
5274 loff_t *ppos)
5275{
5276 int err;
5277 struct in6_addr addr;
5278 char str[IPV6_MAX_STRLEN];
5279 struct ctl_table lctl = *ctl;
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005280 struct net *net = ctl->extra2;
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005281 struct ipv6_stable_secret *secret = ctl->data;
5282
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005283 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5284 return -EIO;
5285
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005286 lctl.maxlen = IPV6_MAX_STRLEN;
5287 lctl.data = str;
5288
5289 if (!rtnl_trylock())
5290 return restart_syscall();
5291
5292 if (!write && !secret->initialized) {
5293 err = -EIO;
5294 goto out;
5295 }
5296
5297 if (!write) {
5298 err = snprintf(str, sizeof(str), "%pI6",
5299 &secret->secret);
5300 if (err >= sizeof(str)) {
5301 err = -EIO;
5302 goto out;
5303 }
5304 }
5305
5306 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5307 if (err || !write)
5308 goto out;
5309
5310 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5311 err = -EIO;
5312 goto out;
5313 }
5314
5315 secret->initialized = true;
5316 secret->secret = addr;
5317
Hannes Frederic Sowa622c81d2015-03-23 23:36:01 +01005318 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5319 struct net_device *dev;
5320
5321 for_each_netdev(net, dev) {
5322 struct inet6_dev *idev = __in6_dev_get(dev);
5323
5324 if (idev) {
5325 idev->addr_gen_mode =
5326 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5327 }
5328 }
5329 } else {
5330 struct inet6_dev *idev = ctl->extra1;
5331
5332 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5333 }
5334
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005335out:
5336 rtnl_unlock();
5337
5338 return err;
5339}
stephen hemmingerc92d5492013-12-17 22:37:14 -08005340
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341static struct addrconf_sysctl_table
5342{
5343 struct ctl_table_header *sysctl_header;
Joe Perchesfe2c6332013-06-11 23:04:25 -07005344 struct ctl_table addrconf_vars[DEVCONF_MAX+1];
Brian Haleyab32ea52006-09-22 14:15:41 -07005345} addrconf_sysctl __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 .sysctl_header = NULL,
5347 .addrconf_vars = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09005348 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005349 .procname = "forwarding",
5350 .data = &ipv6_devconf.forwarding,
5351 .maxlen = sizeof(int),
5352 .mode = 0644,
5353 .proc_handler = addrconf_sysctl_forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 },
5355 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005356 .procname = "hop_limit",
5357 .data = &ipv6_devconf.hop_limit,
5358 .maxlen = sizeof(int),
5359 .mode = 0644,
5360 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 },
5362 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005363 .procname = "mtu",
5364 .data = &ipv6_devconf.mtu6,
5365 .maxlen = sizeof(int),
5366 .mode = 0644,
Marcelo Leitner77751422015-02-23 11:17:13 -03005367 .proc_handler = addrconf_sysctl_mtu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 },
5369 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005370 .procname = "accept_ra",
5371 .data = &ipv6_devconf.accept_ra,
5372 .maxlen = sizeof(int),
5373 .mode = 0644,
5374 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 },
5376 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005377 .procname = "accept_redirects",
5378 .data = &ipv6_devconf.accept_redirects,
5379 .maxlen = sizeof(int),
5380 .mode = 0644,
5381 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 },
5383 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005384 .procname = "autoconf",
5385 .data = &ipv6_devconf.autoconf,
5386 .maxlen = sizeof(int),
5387 .mode = 0644,
5388 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 },
5390 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005391 .procname = "dad_transmits",
5392 .data = &ipv6_devconf.dad_transmits,
5393 .maxlen = sizeof(int),
5394 .mode = 0644,
5395 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 },
5397 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005398 .procname = "router_solicitations",
5399 .data = &ipv6_devconf.rtr_solicits,
5400 .maxlen = sizeof(int),
5401 .mode = 0644,
5402 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 },
5404 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005405 .procname = "router_solicitation_interval",
5406 .data = &ipv6_devconf.rtr_solicit_interval,
5407 .maxlen = sizeof(int),
5408 .mode = 0644,
5409 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 },
5411 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005412 .procname = "router_solicitation_delay",
5413 .data = &ipv6_devconf.rtr_solicit_delay,
5414 .maxlen = sizeof(int),
5415 .mode = 0644,
5416 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 },
5418 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005419 .procname = "force_mld_version",
5420 .data = &ipv6_devconf.force_mld_version,
5421 .maxlen = sizeof(int),
5422 .mode = 0644,
5423 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 },
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02005425 {
5426 .procname = "mldv1_unsolicited_report_interval",
5427 .data =
5428 &ipv6_devconf.mldv1_unsolicited_report_interval,
5429 .maxlen = sizeof(int),
5430 .mode = 0644,
5431 .proc_handler = proc_dointvec_ms_jiffies,
5432 },
5433 {
5434 .procname = "mldv2_unsolicited_report_interval",
5435 .data =
5436 &ipv6_devconf.mldv2_unsolicited_report_interval,
5437 .maxlen = sizeof(int),
5438 .mode = 0644,
5439 .proc_handler = proc_dointvec_ms_jiffies,
5440 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005442 .procname = "use_tempaddr",
5443 .data = &ipv6_devconf.use_tempaddr,
5444 .maxlen = sizeof(int),
5445 .mode = 0644,
5446 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 },
5448 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005449 .procname = "temp_valid_lft",
5450 .data = &ipv6_devconf.temp_valid_lft,
5451 .maxlen = sizeof(int),
5452 .mode = 0644,
5453 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 },
5455 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005456 .procname = "temp_prefered_lft",
5457 .data = &ipv6_devconf.temp_prefered_lft,
5458 .maxlen = sizeof(int),
5459 .mode = 0644,
5460 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 },
5462 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005463 .procname = "regen_max_retry",
5464 .data = &ipv6_devconf.regen_max_retry,
5465 .maxlen = sizeof(int),
5466 .mode = 0644,
5467 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 },
5469 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005470 .procname = "max_desync_factor",
5471 .data = &ipv6_devconf.max_desync_factor,
5472 .maxlen = sizeof(int),
5473 .mode = 0644,
5474 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005477 .procname = "max_addresses",
5478 .data = &ipv6_devconf.max_addresses,
5479 .maxlen = sizeof(int),
5480 .mode = 0644,
5481 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 },
5483 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005484 .procname = "accept_ra_defrtr",
5485 .data = &ipv6_devconf.accept_ra_defrtr,
5486 .maxlen = sizeof(int),
5487 .mode = 0644,
5488 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08005489 },
5490 {
Hangbin Liu8013d1d2015-07-30 14:28:42 +08005491 .procname = "accept_ra_min_hop_limit",
5492 .data = &ipv6_devconf.accept_ra_min_hop_limit,
5493 .maxlen = sizeof(int),
5494 .mode = 0644,
5495 .proc_handler = proc_dointvec,
5496 },
5497 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005498 .procname = "accept_ra_pinfo",
5499 .data = &ipv6_devconf.accept_ra_pinfo,
5500 .maxlen = sizeof(int),
5501 .mode = 0644,
5502 .proc_handler = proc_dointvec,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08005503 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005504#ifdef CONFIG_IPV6_ROUTER_PREF
5505 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005506 .procname = "accept_ra_rtr_pref",
5507 .data = &ipv6_devconf.accept_ra_rtr_pref,
5508 .maxlen = sizeof(int),
5509 .mode = 0644,
5510 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005511 },
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08005512 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005513 .procname = "router_probe_interval",
5514 .data = &ipv6_devconf.rtr_probe_interval,
5515 .maxlen = sizeof(int),
5516 .mode = 0644,
5517 .proc_handler = proc_dointvec_jiffies,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08005518 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08005519#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08005520 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005521 .procname = "accept_ra_rt_info_max_plen",
5522 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
5523 .maxlen = sizeof(int),
5524 .mode = 0644,
5525 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08005526 },
5527#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005528#endif
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08005529 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005530 .procname = "proxy_ndp",
5531 .data = &ipv6_devconf.proxy_ndp,
5532 .maxlen = sizeof(int),
5533 .mode = 0644,
stephen hemmingerc92d5492013-12-17 22:37:14 -08005534 .proc_handler = addrconf_sysctl_proxy_ndp,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07005535 },
5536 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005537 .procname = "accept_source_route",
5538 .data = &ipv6_devconf.accept_source_route,
5539 .maxlen = sizeof(int),
5540 .mode = 0644,
5541 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07005542 },
Neil Horman95c385b2007-04-25 17:08:10 -07005543#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5544 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005545 .procname = "optimistic_dad",
5546 .data = &ipv6_devconf.optimistic_dad,
5547 .maxlen = sizeof(int),
5548 .mode = 0644,
5549 .proc_handler = proc_dointvec,
Neil Horman95c385b2007-04-25 17:08:10 -07005550
5551 },
Erik Kline7fd25612014-10-28 18:11:14 +09005552 {
5553 .procname = "use_optimistic",
5554 .data = &ipv6_devconf.use_optimistic,
5555 .maxlen = sizeof(int),
5556 .mode = 0644,
5557 .proc_handler = proc_dointvec,
5558
5559 },
Neil Horman95c385b2007-04-25 17:08:10 -07005560#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005561#ifdef CONFIG_IPV6_MROUTE
5562 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005563 .procname = "mc_forwarding",
5564 .data = &ipv6_devconf.mc_forwarding,
5565 .maxlen = sizeof(int),
5566 .mode = 0444,
5567 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005568 },
5569#endif
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07005570 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005571 .procname = "disable_ipv6",
5572 .data = &ipv6_devconf.disable_ipv6,
5573 .maxlen = sizeof(int),
5574 .mode = 0644,
5575 .proc_handler = addrconf_sysctl_disable,
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09005576 },
5577 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005578 .procname = "accept_dad",
5579 .data = &ipv6_devconf.accept_dad,
5580 .maxlen = sizeof(int),
5581 .mode = 0644,
5582 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09005583 },
5584 {
Octavian Purdilaf7734fd2009-10-02 11:39:15 +00005585 .procname = "force_tllao",
5586 .data = &ipv6_devconf.force_tllao,
5587 .maxlen = sizeof(int),
5588 .mode = 0644,
5589 .proc_handler = proc_dointvec
5590 },
5591 {
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00005592 .procname = "ndisc_notify",
5593 .data = &ipv6_devconf.ndisc_notify,
5594 .maxlen = sizeof(int),
5595 .mode = 0644,
5596 .proc_handler = proc_dointvec
5597 },
5598 {
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02005599 .procname = "suppress_frag_ndisc",
5600 .data = &ipv6_devconf.suppress_frag_ndisc,
5601 .maxlen = sizeof(int),
5602 .mode = 0644,
5603 .proc_handler = proc_dointvec
5604 },
5605 {
Ben Greeard9333192014-06-25 14:44:53 -07005606 .procname = "accept_ra_from_local",
5607 .data = &ipv6_devconf.accept_ra_from_local,
5608 .maxlen = sizeof(int),
5609 .mode = 0644,
5610 .proc_handler = proc_dointvec,
5611 },
5612 {
Harout Hedeshianc2943f12015-01-20 10:06:05 -07005613 .procname = "accept_ra_mtu",
5614 .data = &ipv6_devconf.accept_ra_mtu,
5615 .maxlen = sizeof(int),
5616 .mode = 0644,
5617 .proc_handler = proc_dointvec,
5618 },
5619 {
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005620 .procname = "stable_secret",
5621 .data = &ipv6_devconf.stable_secret,
5622 .maxlen = IPV6_MAX_STRLEN,
5623 .mode = 0600,
5624 .proc_handler = addrconf_sysctl_stable_secret,
5625 },
5626 {
Erik Kline3985e8a2015-07-22 16:38:25 +09005627 .procname = "use_oif_addrs_only",
5628 .data = &ipv6_devconf.use_oif_addrs_only,
5629 .maxlen = sizeof(int),
5630 .mode = 0644,
5631 .proc_handler = proc_dointvec,
5632
5633 },
5634 {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08005635 /* sentinel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 }
5637 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638};
5639
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08005640static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08005641 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642{
5643 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 struct addrconf_sysctl_table *t;
Eric W. Biederman6105e292012-04-19 13:41:24 +00005645 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11005646
Arnaldo Carvalho de Meloaf879cc2006-11-17 12:14:37 -02005647 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01005648 if (!t)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11005649 goto out;
5650
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07005651 for (i = 0; t->addrconf_vars[i].data; i++) {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005652 t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08005654 t->addrconf_vars[i].extra2 = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656
Eric W. Biederman6105e292012-04-19 13:41:24 +00005657 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658
Eric W. Biederman6105e292012-04-19 13:41:24 +00005659 t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars);
Ian Morris63159f22015-03-29 14:00:04 +01005660 if (!t->sysctl_header)
Eric W. Biederman6105e292012-04-19 13:41:24 +00005661 goto free;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11005662
5663 p->sysctl = t;
Pavel Emelyanov95897312008-01-10 17:41:45 -08005664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11005666free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 kfree(t);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11005668out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08005669 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670}
5671
Pavel Emelyanov408c4762008-01-10 17:41:21 -08005672static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
5673{
5674 struct addrconf_sysctl_table *t;
5675
Ian Morris63159f22015-03-29 14:00:04 +01005676 if (!p->sysctl)
Pavel Emelyanov408c4762008-01-10 17:41:21 -08005677 return;
5678
5679 t = p->sysctl;
5680 p->sysctl = NULL;
Lucian Adrian Grijincuff538812011-05-01 01:44:01 +00005681 unregister_net_sysctl_table(t->sysctl_header);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08005682 kfree(t);
5683}
5684
WANG Conga317a2f2014-07-25 15:25:09 -07005685static int addrconf_sysctl_register(struct inet6_dev *idev)
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11005686{
WANG Conga317a2f2014-07-25 15:25:09 -07005687 int err;
5688
5689 if (!sysctl_dev_name_is_allowed(idev->dev->name))
5690 return -EINVAL;
5691
5692 err = neigh_sysctl_register(idev->dev, idev->nd_parms,
5693 &ndisc_ifinfo_sysctl_change);
5694 if (err)
5695 return err;
5696 err = __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
5697 idev, &idev->cnf);
5698 if (err)
5699 neigh_sysctl_unregister(idev->nd_parms);
5700
5701 return err;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11005702}
5703
Pavel Emelyanov408c4762008-01-10 17:41:21 -08005704static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08005706 __addrconf_sysctl_unregister(&idev->cnf);
5707 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708}
5709
5710
5711#endif
5712
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005713static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005714{
Hong Zhiguoa79ca222013-03-26 01:52:45 +08005715 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005716 struct ipv6_devconf *all, *dflt;
5717
Hong Zhiguoa79ca222013-03-26 01:52:45 +08005718 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01005719 if (!all)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08005720 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005721
Hong Zhiguoa79ca222013-03-26 01:52:45 +08005722 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
Ian Morris63159f22015-03-29 14:00:04 +01005723 if (!dflt)
Hong Zhiguoa79ca222013-03-26 01:52:45 +08005724 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005725
Hong Zhiguoa79ca222013-03-26 01:52:45 +08005726 /* these will be inherited by all namespaces */
5727 dflt->autoconf = ipv6_defaults.autoconf;
5728 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005729
Hannes Frederic Sowa3d1bec92015-03-23 23:36:00 +01005730 dflt->stable_secret.initialized = false;
5731 all->stable_secret.initialized = false;
5732
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005733 net->ipv6.devconf_all = all;
5734 net->ipv6.devconf_dflt = dflt;
5735
5736#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08005737 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005738 if (err < 0)
5739 goto err_reg_all;
5740
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08005741 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005742 if (err < 0)
5743 goto err_reg_dflt;
5744#endif
5745 return 0;
5746
5747#ifdef CONFIG_SYSCTL
5748err_reg_dflt:
5749 __addrconf_sysctl_unregister(all);
5750err_reg_all:
5751 kfree(dflt);
5752#endif
5753err_alloc_dflt:
5754 kfree(all);
5755err_alloc_all:
5756 return err;
5757}
5758
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005759static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005760{
5761#ifdef CONFIG_SYSCTL
5762 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
5763 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
5764#endif
zhuyj73cf0e92014-11-26 10:25:58 +08005765 kfree(net->ipv6.devconf_dflt);
5766 kfree(net->ipv6.devconf_all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005767}
5768
5769static struct pernet_operations addrconf_ops = {
5770 .init = addrconf_init_net,
5771 .exit = addrconf_exit_net,
5772};
5773
Daniel Borkmann207895f2015-01-29 12:15:03 +01005774static struct rtnl_af_ops inet6_ops __read_mostly = {
Thomas Grafb382b192010-11-16 04:33:57 +00005775 .family = AF_INET6,
5776 .fill_link_af = inet6_fill_link_af,
5777 .get_link_af_size = inet6_get_link_af_size,
Daniel Borkmann11b1f822015-02-05 14:39:11 +01005778 .validate_link_af = inet6_validate_link_af,
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005779 .set_link_af = inet6_set_link_af,
Thomas Grafb382b192010-11-16 04:33:57 +00005780};
5781
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782/*
5783 * Init / cleanup code
5784 */
5785
5786int __init addrconf_init(void)
5787{
WANG Conga317a2f2014-07-25 15:25:09 -07005788 struct inet6_dev *idev;
stephen hemmingerc2e21292010-03-17 20:31:10 +00005789 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09005790
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005791 err = ipv6_addr_label_init();
5792 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00005793 pr_crit("%s: cannot initialize default policy table: %d\n",
5794 __func__, err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00005795 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09005796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797
Neil Horman2cc6d2b2010-09-24 09:55:52 +00005798 err = register_pernet_subsys(&addrconf_ops);
5799 if (err < 0)
5800 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005801
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005802 addrconf_wq = create_workqueue("ipv6_addrconf");
5803 if (!addrconf_wq) {
5804 err = -ENOMEM;
5805 goto out_nowq;
5806 }
5807
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 /* The addrconf netdev notifier requires that loopback_dev
5809 * has it's ipv6 private information allocated and setup
5810 * before it can bring up and give link-local addresses
5811 * to other devices which are up.
5812 *
5813 * Unfortunately, loopback_dev is not necessarily the first
5814 * entry in the global dev_base list of net devices. In fact,
5815 * it is likely to be the very last entry on that list.
5816 * So this causes the notifier registry below to try and
5817 * give link-local addresses to all devices besides loopback_dev
5818 * first, then loopback_dev, which cases all the non-loopback_dev
5819 * devices to fail to get a link-local address.
5820 *
5821 * So, as a temporary fix, allocate the ipv6 structure for
5822 * loopback_dev first by hand.
5823 * Longer term, all of the dependencies ipv6 has upon the loopback
5824 * device and it being up should be removed.
5825 */
5826 rtnl_lock();
WANG Conga317a2f2014-07-25 15:25:09 -07005827 idev = ipv6_add_dev(init_net.loopback_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 rtnl_unlock();
WANG Conga317a2f2014-07-25 15:25:09 -07005829 if (IS_ERR(idev)) {
5830 err = PTR_ERR(idev);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005831 goto errlo;
WANG Conga317a2f2014-07-25 15:25:09 -07005832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
stephen hemmingerc2e21292010-03-17 20:31:10 +00005834 for (i = 0; i < IN6_ADDR_HSIZE; i++)
5835 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
5836
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 register_netdevice_notifier(&ipv6_dev_notf);
5838
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005839 addrconf_verify();
Thomas Grafc127ea22007-03-22 11:58:32 -07005840
stephen hemminger3678a9d2013-12-30 10:41:32 -08005841 rtnl_af_register(&inet6_ops);
Thomas Grafb382b192010-11-16 04:33:57 +00005842
Greg Rosec7ac8672011-06-10 01:27:09 +00005843 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
5844 NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07005845 if (err < 0)
5846 goto errout;
5847
5848 /* Only the first call to __rtnl_register can fail */
Greg Rosec7ac8672011-06-10 01:27:09 +00005849 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
5850 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
5851 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
5852 inet6_dump_ifaddr, NULL);
5853 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
5854 inet6_dump_ifmcaddr, NULL);
5855 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
5856 inet6_dump_ifacaddr, NULL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +00005857 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
Nicolas Dichtel7a674202013-03-05 23:42:06 +00005858 inet6_netconf_dump_devconf, NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07005859
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09005860 ipv6_addr_label_rtnl_register();
5861
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07005863errout:
Thomas Grafb382b192010-11-16 04:33:57 +00005864 rtnl_af_unregister(&inet6_ops);
Thomas Grafc127ea22007-03-22 11:58:32 -07005865 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005866errlo:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005867 destroy_workqueue(addrconf_wq);
5868out_nowq:
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005869 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00005870out_addrlabel:
5871 ipv6_addr_label_cleanup();
5872out:
Thomas Grafc127ea22007-03-22 11:58:32 -07005873 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874}
5875
Daniel Lezcano09f77092007-12-13 05:34:58 -08005876void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08005878 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 int i;
5880
5881 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005882 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00005883 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884
5885 rtnl_lock();
5886
Thomas Grafb382b192010-11-16 04:33:57 +00005887 __rtnl_af_unregister(&inet6_ops);
5888
Daniel Lezcano176c39a2009-03-03 01:06:45 -08005889 /* clean dev list */
5890 for_each_netdev(&init_net, dev) {
5891 if (__in6_dev_get(dev) == NULL)
5892 continue;
5893 addrconf_ifdown(dev, 1);
5894 }
5895 addrconf_ifdown(init_net.loopback_dev, 2);
5896
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 * Check hash table.
5899 */
stephen hemminger5c578ae2010-03-17 20:31:11 +00005900 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00005901 for (i = 0; i < IN6_ADDR_HSIZE; i++)
5902 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578ae2010-03-17 20:31:11 +00005903 spin_unlock_bh(&addrconf_hash_lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005904 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 rtnl_unlock();
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005906
5907 destroy_workqueue(addrconf_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908}