blob: 5667b3003af9b51779ff322717e999282113c4b7 [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>
49#include <linux/in6.h>
50#include <linux/netdevice.h>
Thomas Graf18237302006-08-04 23:04:54 -070051#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/if_arp.h>
53#include <linux/if_arcnet.h>
54#include <linux/if_infiniband.h>
55#include <linux/route.h>
56#include <linux/inetdevice.h>
57#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090058#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#ifdef CONFIG_SYSCTL
60#include <linux/sysctl.h>
61#endif
Randy Dunlap4fc268d2006-01-11 12:17:47 -080062#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/delay.h>
64#include <linux/notifier.h>
Paulo Marques543537b2005-06-23 00:09:02 -070065#include <linux/string.h>
Eric Dumazetddbe5032012-07-18 08:11:12 +000066#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020068#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <net/sock.h>
70#include <net/snmp.h>
71
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +000072#include <net/af_ieee802154.h>
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +000073#include <net/firewire.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <net/ipv6.h>
75#include <net/protocol.h>
76#include <net/ndisc.h>
77#include <net/ip6_route.h>
78#include <net/addrconf.h>
79#include <net/tcp.h>
80#include <net/ip.h>
Thomas Graf5d620262006-08-15 00:35:02 -070081#include <net/netlink.h>
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -070082#include <net/pkt_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <linux/if_tunnel.h>
84#include <linux/rtnetlink.h>
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +000085#include <linux/netconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include <linux/random.h>
Stephen Hemmingere21e8462010-03-20 16:09:01 -070087#include <linux/uaccess.h>
Herbert Xu7f7d9a62007-04-24 21:54:09 -070088#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90#include <linux/proc_fs.h>
91#include <linux/seq_file.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040092#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/* Set to 3 to get tracing... */
95#define ACONF_DEBUG 2
96
97#if ACONF_DEBUG >= 3
dingtianhongba3542e2013-08-17 10:27:04 +080098#define ADBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#else
dingtianhongba3542e2013-08-17 10:27:04 +0800100#define ADBG(fmt, ...) do { if (0) printk(fmt, ##__VA_ARGS__); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#endif
102
103#define INFINITY_LIFE_TIME 0xFFFFFFFF
Thomas Graf18a31e12010-11-17 04:12:02 +0000104
105static inline u32 cstamp_delta(unsigned long cstamp)
106{
107 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
108}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110#ifdef CONFIG_SYSCTL
Pavel Emelyanovf52295a2007-12-02 00:58:37 +1100111static void addrconf_sysctl_register(struct inet6_dev *idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800112static void addrconf_sysctl_unregister(struct inet6_dev *idev);
113#else
114static inline void addrconf_sysctl_register(struct inet6_dev *idev)
115{
116}
117
118static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
119{
120}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#endif
122
Sorin Dumitrueb7e0572012-08-30 02:01:45 +0000123static void __ipv6_regen_rndid(struct inet6_dev *idev);
124static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static void ipv6_regen_rndid(unsigned long data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900127static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static int ipv6_count_addresses(struct inet6_dev *idev);
129
130/*
131 * Configured unicast address hash table
132 */
stephen hemmingerc2e21292010-03-17 20:31:10 +0000133static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
stephen hemminger5c578ae2010-03-17 20:31:11 +0000134static DEFINE_SPINLOCK(addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100136static void addrconf_verify(void);
137static void addrconf_verify_rtnl(void);
138static void addrconf_verify_work(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100140static struct workqueue_struct *addrconf_wq;
141static DECLARE_DELAYED_WORK(addr_chk_work, addrconf_verify_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
144static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
145
Jiri Pirko93d9b7d2010-03-10 10:28:56 +0000146static void addrconf_type_change(struct net_device *dev,
147 unsigned long event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static int addrconf_ifdown(struct net_device *dev, int how);
149
Romain Kuntz21caa662013-01-09 21:06:03 +0000150static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
151 int plen,
152 const struct net_device *dev,
153 u32 flags, u32 noflags);
154
David S. Millercf22f9a2012-04-14 21:37:40 -0400155static void addrconf_dad_start(struct inet6_ifaddr *ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100156static void addrconf_dad_work(struct work_struct *w);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900158static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static void addrconf_rs_timer(unsigned long data);
160static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
161static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
162
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900163static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 struct prefix_info *pinfo);
David S. Miller3e81c6d2010-03-20 16:18:00 -0700165static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
166 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Adrian Bunk888c8482008-07-22 14:21:58 -0700168static struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 .forwarding = 0,
170 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
171 .mtu6 = IPV6_MIN_MTU,
172 .accept_ra = 1,
173 .accept_redirects = 1,
174 .autoconf = 1,
175 .force_mld_version = 0,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200176 .mldv1_unsolicited_report_interval = 10 * HZ,
177 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 .dad_transmits = 1,
179 .rtr_solicits = MAX_RTR_SOLICITATIONS,
180 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
181 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 .use_tempaddr = 0,
183 .temp_valid_lft = TEMP_VALID_LIFETIME,
184 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
185 .regen_max_retry = REGEN_MAX_RETRY,
186 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800188 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800189 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800190#ifdef CONFIG_IPV6_ROUTER_PREF
191 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800192 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800193#ifdef CONFIG_IPV6_ROUTE_INFO
194 .accept_ra_rt_info_max_plen = 0,
195#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800196#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700197 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700198 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900199 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900200 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200201 .suppress_frag_ndisc = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202};
203
Brian Haleyab32ea52006-09-22 14:15:41 -0700204static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 .forwarding = 0,
206 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
207 .mtu6 = IPV6_MIN_MTU,
208 .accept_ra = 1,
209 .accept_redirects = 1,
210 .autoconf = 1,
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +0200211 .force_mld_version = 0,
212 .mldv1_unsolicited_report_interval = 10 * HZ,
213 .mldv2_unsolicited_report_interval = HZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 .dad_transmits = 1,
215 .rtr_solicits = MAX_RTR_SOLICITATIONS,
216 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
217 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 .use_tempaddr = 0,
219 .temp_valid_lft = TEMP_VALID_LIFETIME,
220 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
221 .regen_max_retry = REGEN_MAX_RETRY,
222 .max_desync_factor = MAX_DESYNC_FACTOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800224 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800225 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800226#ifdef CONFIG_IPV6_ROUTER_PREF
227 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800228 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800229#ifdef CONFIG_IPV6_ROUTE_INFO
230 .accept_ra_rt_info_max_plen = 0,
231#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800232#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700233 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700234 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900235 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900236 .accept_dad = 1,
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +0200237 .suppress_frag_ndisc = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238};
239
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700240/* Check if a valid qdisc is available */
David S. Miller05297942008-07-08 23:01:27 -0700241static inline bool addrconf_qdisc_ok(const struct net_device *dev)
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700242{
David S. Miller05297942008-07-08 23:01:27 -0700243 return !qdisc_tx_is_noop(dev);
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700244}
245
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200246static void addrconf_del_rs_timer(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200248 if (del_timer(&idev->rs_timer))
249 __in6_dev_put(idev);
250}
251
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100252static void addrconf_del_dad_work(struct inet6_ifaddr *ifp)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200253{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100254 if (cancel_delayed_work(&ifp->dad_work))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 __in6_ifa_put(ifp);
256}
257
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200258static void addrconf_mod_rs_timer(struct inet6_dev *idev,
259 unsigned long when)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200261 if (!timer_pending(&idev->rs_timer))
262 in6_dev_hold(idev);
263 mod_timer(&idev->rs_timer, jiffies + when);
264}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100266static void addrconf_mod_dad_work(struct inet6_ifaddr *ifp,
267 unsigned long delay)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200268{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100269 if (!delayed_work_pending(&ifp->dad_work))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200270 in6_ifa_hold(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100271 mod_delayed_work(addrconf_wq, &ifp->dad_work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700274static int snmp6_alloc_dev(struct inet6_dev *idev)
275{
John Stultz827da442013-10-07 15:51:58 -0700276 int i;
277
WANG Cong698365f2014-05-05 15:55:55 -0700278 idev->stats.ipv6 = alloc_percpu(struct ipstats_mib);
279 if (!idev->stats.ipv6)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700280 goto err_ip;
John Stultz827da442013-10-07 15:51:58 -0700281
282 for_each_possible_cpu(i) {
283 struct ipstats_mib *addrconf_stats;
WANG Cong698365f2014-05-05 15:55:55 -0700284 addrconf_stats = per_cpu_ptr(idev->stats.ipv6, i);
John Stultz827da442013-10-07 15:51:58 -0700285 u64_stats_init(&addrconf_stats->syncp);
John Stultz827da442013-10-07 15:51:58 -0700286 }
287
288
Eric Dumazetbe281e52011-05-19 01:14:23 +0000289 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
290 GFP_KERNEL);
291 if (!idev->stats.icmpv6dev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700292 goto err_icmp;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000293 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
294 GFP_KERNEL);
295 if (!idev->stats.icmpv6msgdev)
David L Stevens14878f72007-09-16 16:52:35 -0700296 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700297
298 return 0;
299
David L Stevens14878f72007-09-16 16:52:35 -0700300err_icmpmsg:
Eric Dumazetbe281e52011-05-19 01:14:23 +0000301 kfree(idev->stats.icmpv6dev);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700302err_icmp:
WANG Cong698365f2014-05-05 15:55:55 -0700303 free_percpu(idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700304err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700305 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700306}
307
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000308static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 struct inet6_dev *ndev;
311
312 ASSERT_RTNL();
313
314 if (dev->mtu < IPV6_MIN_MTU)
315 return NULL;
316
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900317 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900319 if (ndev == NULL)
320 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Ingo Oeser322f74a2006-03-20 23:01:47 -0800322 rwlock_init(&ndev->lock);
323 ndev->dev = dev;
stephen hemminger502a2ff2010-03-17 20:31:13 +0000324 INIT_LIST_HEAD(&ndev->addr_list);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +0200325 setup_timer(&ndev->rs_timer, addrconf_rs_timer,
326 (unsigned long)ndev);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900327 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800328 ndev->cnf.mtu6 = dev->mtu;
329 ndev->cnf.sysctl = NULL;
330 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
331 if (ndev->nd_parms == NULL) {
332 kfree(ndev);
333 return NULL;
334 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700335 if (ndev->cnf.forwarding)
336 dev_disable_lro(dev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800337 /* We refer to the device */
338 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Ingo Oeser322f74a2006-03-20 23:01:47 -0800340 if (snmp6_alloc_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800341 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000342 "%s: cannot allocate memory for statistics; dev=%s.\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800343 __func__, dev->name);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800344 neigh_parms_release(&nd_tbl, ndev->nd_parms);
Roy Li8603e332011-09-20 15:10:16 -0400345 dev_put(dev);
346 kfree(ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800347 return NULL;
348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Ingo Oeser322f74a2006-03-20 23:01:47 -0800350 if (snmp6_register_dev(ndev) < 0) {
dingtianhongba3542e2013-08-17 10:27:04 +0800351 ADBG(KERN_WARNING
Joe Perchesf3213832012-05-15 14:11:53 +0000352 "%s: cannot create /proc/net/dev_snmp6/%s\n",
dingtianhongba3542e2013-08-17 10:27:04 +0800353 __func__, dev->name);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800354 neigh_parms_release(&nd_tbl, ndev->nd_parms);
355 ndev->dead = 1;
356 in6_dev_finish_destroy(ndev);
357 return NULL;
358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Ingo Oeser322f74a2006-03-20 23:01:47 -0800360 /* One reference from device. We must do this before
361 * we invoke __ipv6_regen_rndid().
362 */
363 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900365 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
366 ndev->cnf.accept_dad = -1;
367
Amerigo Wang07a93622012-10-29 16:23:10 +0000368#if IS_ENABLED(CONFIG_IPV6_SIT)
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700369 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
Joe Perchesf3213832012-05-15 14:11:53 +0000370 pr_info("%s: Disabled Multicast RS\n", dev->name);
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700371 ndev->cnf.rtr_solicits = 0;
372 }
373#endif
374
stephen hemminger372e6c82010-03-17 20:31:09 +0000375 INIT_LIST_HEAD(&ndev->tempaddr_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800376 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800377 if ((dev->flags&IFF_LOOPBACK) ||
378 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700379 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700380 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700381 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800382 ndev->cnf.use_tempaddr = -1;
383 } else {
384 in6_dev_hold(ndev);
385 ipv6_regen_rndid((unsigned long) ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
David S. Miller5d9efa72013-10-28 20:07:50 -0400387
Daniel Borkmann914faa12013-04-09 03:47:14 +0000388 ndev->token = in6addr_any;
Ingo Oeser322f74a2006-03-20 23:01:47 -0800389
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700390 if (netif_running(dev) && addrconf_qdisc_ok(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700391 ndev->if_flags |= IF_READY;
392
Ingo Oeser322f74a2006-03-20 23:01:47 -0800393 ipv6_mc_init_dev(ndev);
394 ndev->tstamp = jiffies;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +1100395 addrconf_sysctl_register(ndev);
David L Stevens30c4cf52007-01-04 12:31:14 -0800396 /* protected by rtnl_lock */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000397 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800398
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000399 /* Join interface-local all-node multicast group */
400 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
401
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800402 /* Join all-node multicast group */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900403 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800404
Li Weid6ddef92012-03-05 14:45:17 +0000405 /* Join all-router multicast group if forwarding is set */
Li Wei8b2aaed2012-03-07 14:58:07 +0000406 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
Li Weid6ddef92012-03-05 14:45:17 +0000407 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return ndev;
410}
411
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000412static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 struct inet6_dev *idev;
415
416 ASSERT_RTNL();
417
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700418 idev = __in6_dev_get(dev);
419 if (!idev) {
420 idev = ipv6_add_dev(dev);
421 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return NULL;
423 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (dev->flags&IFF_UP)
426 ipv6_mc_up(idev);
427 return idev;
428}
429
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000430static int inet6_netconf_msgsize_devconf(int type)
431{
432 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
433 + nla_total_size(4); /* NETCONFA_IFINDEX */
434
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000435 /* type -1 is used for ALL */
436 if (type == -1 || type == NETCONFA_FORWARDING)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000437 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500438#ifdef CONFIG_IPV6_MROUTE
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000439 if (type == -1 || type == NETCONFA_MC_FORWARDING)
440 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500441#endif
stephen hemmingerc92d5492013-12-17 22:37:14 -0800442 if (type == -1 || type == NETCONFA_PROXY_NEIGH)
443 size += nla_total_size(4);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000444
445 return size;
446}
447
448static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
449 struct ipv6_devconf *devconf, u32 portid,
450 u32 seq, int event, unsigned int flags,
451 int type)
452{
453 struct nlmsghdr *nlh;
454 struct netconfmsg *ncm;
455
456 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
457 flags);
458 if (nlh == NULL)
459 return -EMSGSIZE;
460
461 ncm = nlmsg_data(nlh);
462 ncm->ncm_family = AF_INET6;
463
464 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
465 goto nla_put_failure;
466
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000467 /* type -1 is used for ALL */
468 if ((type == -1 || type == NETCONFA_FORWARDING) &&
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000469 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
470 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500471#ifdef CONFIG_IPV6_MROUTE
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000472 if ((type == -1 || type == NETCONFA_MC_FORWARDING) &&
473 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
474 devconf->mc_forwarding) < 0)
475 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500476#endif
stephen hemmingerc92d5492013-12-17 22:37:14 -0800477 if ((type == -1 || type == NETCONFA_PROXY_NEIGH) &&
478 nla_put_s32(skb, NETCONFA_PROXY_NEIGH, devconf->proxy_ndp) < 0)
479 goto nla_put_failure;
480
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000481 return nlmsg_end(skb, nlh);
482
483nla_put_failure:
484 nlmsg_cancel(skb, nlh);
485 return -EMSGSIZE;
486}
487
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000488void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
489 struct ipv6_devconf *devconf)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000490{
491 struct sk_buff *skb;
492 int err = -ENOBUFS;
493
494 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
495 if (skb == NULL)
496 goto errout;
497
498 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
499 RTM_NEWNETCONF, 0, type);
500 if (err < 0) {
501 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
502 WARN_ON(err == -EMSGSIZE);
503 kfree_skb(skb);
504 goto errout;
505 }
506 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC);
507 return;
508errout:
Cong Dingbd779022012-12-18 12:08:56 +0000509 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000510}
511
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000512static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
513 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
514 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
stephen hemmingerc92d5492013-12-17 22:37:14 -0800515 [NETCONFA_PROXY_NEIGH] = { .len = sizeof(int) },
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000516};
517
518static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
Thomas Graf661d2962013-03-21 07:45:29 +0000519 struct nlmsghdr *nlh)
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000520{
521 struct net *net = sock_net(in_skb->sk);
522 struct nlattr *tb[NETCONFA_MAX+1];
523 struct netconfmsg *ncm;
524 struct sk_buff *skb;
525 struct ipv6_devconf *devconf;
526 struct inet6_dev *in6_dev;
527 struct net_device *dev;
528 int ifindex;
529 int err;
530
531 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
532 devconf_ipv6_policy);
533 if (err < 0)
534 goto errout;
535
536 err = EINVAL;
537 if (!tb[NETCONFA_IFINDEX])
538 goto errout;
539
540 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
541 switch (ifindex) {
542 case NETCONFA_IFINDEX_ALL:
543 devconf = net->ipv6.devconf_all;
544 break;
545 case NETCONFA_IFINDEX_DEFAULT:
546 devconf = net->ipv6.devconf_dflt;
547 break;
548 default:
549 dev = __dev_get_by_index(net, ifindex);
550 if (dev == NULL)
551 goto errout;
552 in6_dev = __in6_dev_get(dev);
553 if (in6_dev == NULL)
554 goto errout;
555 devconf = &in6_dev->cnf;
556 break;
557 }
558
559 err = -ENOBUFS;
560 skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC);
561 if (skb == NULL)
562 goto errout;
563
564 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
565 NETLINK_CB(in_skb).portid,
566 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
567 -1);
568 if (err < 0) {
569 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
570 WARN_ON(err == -EMSGSIZE);
571 kfree_skb(skb);
572 goto errout;
573 }
574 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
575errout:
576 return err;
577}
578
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000579static int inet6_netconf_dump_devconf(struct sk_buff *skb,
580 struct netlink_callback *cb)
581{
582 struct net *net = sock_net(skb->sk);
583 int h, s_h;
584 int idx, s_idx;
585 struct net_device *dev;
586 struct inet6_dev *idev;
587 struct hlist_head *head;
588
589 s_h = cb->args[0];
590 s_idx = idx = cb->args[1];
591
592 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
593 idx = 0;
594 head = &net->dev_index_head[h];
595 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000596 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^
597 net->dev_base_seq;
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000598 hlist_for_each_entry_rcu(dev, head, index_hlist) {
599 if (idx < s_idx)
600 goto cont;
601 idev = __in6_dev_get(dev);
602 if (!idev)
603 goto cont;
604
605 if (inet6_netconf_fill_devconf(skb, dev->ifindex,
606 &idev->cnf,
607 NETLINK_CB(cb->skb).portid,
608 cb->nlh->nlmsg_seq,
609 RTM_NEWNETCONF,
610 NLM_F_MULTI,
611 -1) <= 0) {
612 rcu_read_unlock();
613 goto done;
614 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +0000615 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Nicolas Dichtel7a674202013-03-05 23:42:06 +0000616cont:
617 idx++;
618 }
619 rcu_read_unlock();
620 }
621 if (h == NETDEV_HASHENTRIES) {
622 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_ALL,
623 net->ipv6.devconf_all,
624 NETLINK_CB(cb->skb).portid,
625 cb->nlh->nlmsg_seq,
626 RTM_NEWNETCONF, NLM_F_MULTI,
627 -1) <= 0)
628 goto done;
629 else
630 h++;
631 }
632 if (h == NETDEV_HASHENTRIES + 1) {
633 if (inet6_netconf_fill_devconf(skb, NETCONFA_IFINDEX_DEFAULT,
634 net->ipv6.devconf_dflt,
635 NETLINK_CB(cb->skb).portid,
636 cb->nlh->nlmsg_seq,
637 RTM_NEWNETCONF, NLM_F_MULTI,
638 -1) <= 0)
639 goto done;
640 else
641 h++;
642 }
643done:
644 cb->args[0] = h;
645 cb->args[1] = idx;
646
647 return skb->len;
648}
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650#ifdef CONFIG_SYSCTL
651static void dev_forward_change(struct inet6_dev *idev)
652{
653 struct net_device *dev;
654 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 if (!idev)
657 return;
658 dev = idev->dev;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700659 if (idev->cnf.forwarding)
660 dev_disable_lro(dev);
Amerigo Wang1a940832012-10-28 17:43:53 +0000661 if (dev->flags & IFF_MULTICAST) {
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000662 if (idev->cnf.forwarding) {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900663 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000664 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
665 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
666 } else {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900667 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000668 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
669 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
stephen hemminger502a2ff2010-03-17 20:31:13 +0000672
673 list_for_each_entry(ifa, &idev->addr_list, if_list) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800674 if (ifa->flags&IFA_F_TENTATIVE)
675 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (idev->cnf.forwarding)
677 addrconf_join_anycast(ifa);
678 else
679 addrconf_leave_anycast(ifa);
680 }
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000681 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
682 dev->ifindex, &idev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685
Pavel Emelyanove1869322008-01-10 17:43:50 -0800686static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 struct net_device *dev;
689 struct inet6_dev *idev;
690
Ben Hutchings4acd4942012-08-14 08:54:51 +0000691 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 idev = __in6_dev_get(dev);
693 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800694 int changed = (!idev->cnf.forwarding) ^ (!newf);
695 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (changed)
697 dev_forward_change(idev);
698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800701
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000702static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800703{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800704 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000705 int old;
706
707 if (!rtnl_trylock())
708 return restart_syscall();
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800709
710 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000711 old = *p;
712 *p = newf;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800713
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000714 if (p == &net->ipv6.devconf_dflt->forwarding) {
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000715 if ((!newf) ^ (!old))
716 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
717 NETCONFA_IFINDEX_DEFAULT,
718 net->ipv6.devconf_dflt);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000719 rtnl_unlock();
720 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +0000721 }
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000722
Pavel Emelyanove1869322008-01-10 17:43:50 -0800723 if (p == &net->ipv6.devconf_all->forwarding) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800724 net->ipv6.devconf_dflt->forwarding = newf;
725 addrconf_forward_change(net, newf);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000726 if ((!newf) ^ (!old))
727 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
728 NETCONFA_IFINDEX_ALL,
729 net->ipv6.devconf_all);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000730 } else if ((!newf) ^ (!old))
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800731 dev_forward_change((struct inet6_dev *)table->extra1);
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700732 rtnl_unlock();
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800733
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000734 if (newf)
Daniel Lezcano7b4da532008-03-04 13:47:14 -0800735 rt6_purge_dflt_routers(net);
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000736 return 1;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800737}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738#endif
739
stephen hemminger5c578ae2010-03-17 20:31:11 +0000740/* Nobody refers to this ifaddr, destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
742{
stephen hemmingerc2e21292010-03-17 20:31:10 +0000743 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745#ifdef NET_REFCNT_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +0000746 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747#endif
748
749 in6_dev_put(ifp->idev);
750
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100751 if (cancel_delayed_work(&ifp->dad_work))
752 pr_notice("delayed DAD work was pending while freeing ifa=%p\n",
753 ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Herbert Xue9d3e082010-05-18 15:36:06 -0700755 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
Joe Perchesf3213832012-05-15 14:11:53 +0000756 pr_warn("Freeing alive inet6 address %p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return;
758 }
Amerigo Wang94e187c2012-10-29 00:13:19 +0000759 ip6_rt_put(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Lai Jiangshane5785982011-03-15 18:00:14 +0800761 kfree_rcu(ifp, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
Brian Haleye55ffac2006-07-10 15:25:51 -0700764static void
765ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
766{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000767 struct list_head *p;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700768 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700769
770 /*
771 * Each device address list is sorted in order of scope -
772 * global before linklocal.
773 */
stephen hemminger502a2ff2010-03-17 20:31:13 +0000774 list_for_each(p, &idev->addr_list) {
775 struct inet6_ifaddr *ifa
776 = list_entry(p, struct inet6_ifaddr, if_list);
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700777 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700778 break;
779 }
780
David S. Millerb54c9b92010-03-25 21:25:30 -0700781 list_add_tail(&ifp->if_list, p);
Brian Haleye55ffac2006-07-10 15:25:51 -0700782}
783
Eric Dumazetddbe5032012-07-18 08:11:12 +0000784static u32 inet6_addr_hash(const struct in6_addr *addr)
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900785{
Eric Dumazetddbe5032012-07-18 08:11:12 +0000786 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900787}
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789/* On success it returns ifp with increased reference count */
790
791static struct inet6_ifaddr *
Jiri Pirko3f8f5292013-08-01 10:41:27 +0200792ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
793 const struct in6_addr *peer_addr, int pfxlen,
Jiri Benc8a226b22013-08-01 10:41:28 +0200794 int scope, u32 flags, u32 valid_lft, u32 prefered_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 struct inet6_ifaddr *ifa = NULL;
797 struct rt6_info *rt;
stephen hemminger3a88a812010-03-17 20:31:12 +0000798 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 int err = 0;
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +0900800 int addr_type = ipv6_addr_type(addr);
801
802 if (addr_type == IPV6_ADDR_ANY ||
803 addr_type & IPV6_ADDR_MULTICAST ||
804 (!(idev->dev->flags & IFF_LOOPBACK) &&
805 addr_type & IPV6_ADDR_LOOPBACK))
806 return ERR_PTR(-EADDRNOTAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700808 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (idev->dead) {
810 err = -ENODEV; /*XXX*/
811 goto out2;
812 }
813
Brian Haley56d417b2009-06-01 03:07:33 -0700814 if (idev->cnf.disable_ipv6) {
Brian Haley9bdd8d42009-03-18 18:22:48 -0700815 err = -EACCES;
816 goto out2;
817 }
818
stephen hemminger5c578ae2010-03-17 20:31:11 +0000819 spin_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 /* Ignore adding duplicate addresses on an interface */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900822 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
dingtianhongba3542e2013-08-17 10:27:04 +0800823 ADBG("ipv6_add_addr: already assigned\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 err = -EEXIST;
825 goto out;
826 }
827
Ingo Oeser322f74a2006-03-20 23:01:47 -0800828 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 if (ifa == NULL) {
dingtianhongba3542e2013-08-17 10:27:04 +0800831 ADBG("ipv6_add_addr: malloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 err = -ENOBUFS;
833 goto out;
834 }
835
David S. Miller8f031512011-12-06 16:48:14 -0500836 rt = addrconf_dst_alloc(idev, addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (IS_ERR(rt)) {
838 err = PTR_ERR(rt);
839 goto out;
840 }
841
Jiri Pirkobba24892013-12-07 19:26:57 +0100842 neigh_parms_data_state_setall(idev->nd_parms);
843
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000844 ifa->addr = *addr;
Jiri Pirko3f8f5292013-08-01 10:41:27 +0200845 if (peer_addr)
846 ifa->peer_addr = *peer_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 spin_lock_init(&ifa->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -0700849 spin_lock_init(&ifa->state_lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100850 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
stephen hemmingerc2e21292010-03-17 20:31:10 +0000851 INIT_HLIST_NODE(&ifa->addr_lst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 ifa->scope = scope;
853 ifa->prefix_len = pfxlen;
854 ifa->flags = flags | IFA_F_TENTATIVE;
Jiri Benc8a226b22013-08-01 10:41:28 +0200855 ifa->valid_lft = valid_lft;
856 ifa->prefered_lft = prefered_lft;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 ifa->cstamp = ifa->tstamp = jiffies;
Daniel Borkmann617fe292013-04-09 03:47:16 +0000858 ifa->tokenized = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Keir Fraser57f5f542006-08-29 02:43:49 -0700860 ifa->rt = rt;
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 ifa->idev = idev;
863 in6_dev_hold(idev);
864 /* For caller */
865 in6_ifa_hold(ifa);
866
867 /* Add to big hash table */
Eric Dumazetddbe5032012-07-18 08:11:12 +0000868 hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
stephen hemminger5c578ae2010-03-17 20:31:11 +0000870 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
stephen hemminger5c578ae2010-03-17 20:31:11 +0000871 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 write_lock(&idev->lock);
874 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -0700875 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (ifa->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +0000878 list_add(&ifa->tmp_list, &idev->tempaddr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 in6_ifa_hold(ifa);
880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 in6_ifa_hold(ifa);
883 write_unlock(&idev->lock);
884out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700885 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -0700887 if (likely(err == 0))
Cong Wangf88c91d2013-04-14 23:18:43 +0800888 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 else {
890 kfree(ifa);
891 ifa = ERR_PTR(err);
892 }
893
894 return ifa;
895out:
stephen hemminger5c578ae2010-03-17 20:31:11 +0000896 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 goto out2;
898}
899
Thomas Haller5b84efe2014-01-15 15:36:59 +0100900enum cleanup_prefix_rt_t {
901 CLEANUP_PREFIX_RT_NOP, /* no cleanup action for prefix route */
902 CLEANUP_PREFIX_RT_DEL, /* delete the prefix route */
903 CLEANUP_PREFIX_RT_EXPIRE, /* update the lifetime of the prefix route */
904};
905
906/*
907 * Check, whether the prefix for ifp would still need a prefix route
908 * after deleting ifp. The function returns one of the CLEANUP_PREFIX_RT_*
909 * constants.
910 *
911 * 1) we don't purge prefix if address was not permanent.
912 * prefix is managed by its own lifetime.
913 * 2) we also don't purge, if the address was IFA_F_NOPREFIXROUTE.
914 * 3) if there are no addresses, delete prefix.
915 * 4) if there are still other permanent address(es),
916 * corresponding prefix is still permanent.
917 * 5) if there are still other addresses with IFA_F_NOPREFIXROUTE,
918 * don't purge the prefix, assume user space is managing it.
919 * 6) otherwise, update prefix lifetime to the
920 * longest valid lifetime among the corresponding
921 * addresses on the device.
922 * Note: subsequent RA will update lifetime.
923 **/
924static enum cleanup_prefix_rt_t
925check_cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long *expires)
926{
927 struct inet6_ifaddr *ifa;
928 struct inet6_dev *idev = ifp->idev;
929 unsigned long lifetime;
930 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_DEL;
931
932 *expires = jiffies;
933
934 list_for_each_entry(ifa, &idev->addr_list, if_list) {
935 if (ifa == ifp)
936 continue;
937 if (!ipv6_prefix_equal(&ifa->addr, &ifp->addr,
938 ifp->prefix_len))
939 continue;
940 if (ifa->flags & (IFA_F_PERMANENT | IFA_F_NOPREFIXROUTE))
941 return CLEANUP_PREFIX_RT_NOP;
942
943 action = CLEANUP_PREFIX_RT_EXPIRE;
944
945 spin_lock(&ifa->lock);
946
947 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
948 /*
949 * Note: Because this address is
950 * not permanent, lifetime <
951 * LONG_MAX / HZ here.
952 */
953 if (time_before(*expires, ifa->tstamp + lifetime * HZ))
954 *expires = ifa->tstamp + lifetime * HZ;
955 spin_unlock(&ifa->lock);
956 }
957
958 return action;
959}
960
961static void
962cleanup_prefix_route(struct inet6_ifaddr *ifp, unsigned long expires, bool del_rt)
963{
964 struct rt6_info *rt;
965
966 rt = addrconf_get_prefix_route(&ifp->addr,
967 ifp->prefix_len,
968 ifp->idev->dev,
969 0, RTF_GATEWAY | RTF_DEFAULT);
970 if (rt) {
971 if (del_rt)
972 ip6_del_rt(rt);
973 else {
974 if (!(rt->rt6i_flags & RTF_EXPIRES))
975 rt6_set_expires(rt, expires);
976 ip6_rt_put(rt);
977 }
978 }
979}
980
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982/* This function wants to get referenced ifp and releases it before return */
983
984static void ipv6_del_addr(struct inet6_ifaddr *ifp)
985{
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700986 int state;
Thomas Haller5b84efe2014-01-15 15:36:59 +0100987 enum cleanup_prefix_rt_t action = CLEANUP_PREFIX_RT_NOP;
988 unsigned long expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +0100990 ASSERT_RTNL();
991
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700992 spin_lock_bh(&ifp->state_lock);
993 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -0700994 ifp->state = INET6_IFADDR_STATE_DEAD;
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700995 spin_unlock_bh(&ifp->state_lock);
996
997 if (state == INET6_IFADDR_STATE_DEAD)
998 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
stephen hemminger5c578ae2010-03-17 20:31:11 +00001000 spin_lock_bh(&addrconf_hash_lock);
1001 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578ae2010-03-17 20:31:11 +00001002 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Thomas Haller5b84efe2014-01-15 15:36:59 +01001004 write_lock_bh(&ifp->idev->lock);
David S. Miller5d9efa72013-10-28 20:07:50 -04001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +00001007 list_del(&ifp->tmp_list);
1008 if (ifp->ifpub) {
1009 in6_ifa_put(ifp->ifpub);
1010 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
stephen hemminger372e6c82010-03-17 20:31:09 +00001012 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Thomas Haller5b84efe2014-01-15 15:36:59 +01001015 if (ifp->flags & IFA_F_PERMANENT && !(ifp->flags & IFA_F_NOPREFIXROUTE))
1016 action = check_cleanup_prefix_route(ifp, &expires);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001017
Thomas Haller5b84efe2014-01-15 15:36:59 +01001018 list_del_init(&ifp->if_list);
1019 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Thomas Haller5b84efe2014-01-15 15:36:59 +01001021 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001023 addrconf_del_dad_work(ifp);
Andrey Vaginb2238562008-07-08 15:13:31 -07001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 ipv6_ifa_notify(RTM_DELADDR, ifp);
1026
Cong Wangf88c91d2013-04-14 23:18:43 +08001027 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Thomas Haller5b84efe2014-01-15 15:36:59 +01001029 if (action != CLEANUP_PREFIX_RT_NOP) {
1030 cleanup_prefix_route(ifp, expires,
1031 action == CLEANUP_PREFIX_RT_DEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033
Daniel Walterc3968a82011-04-13 21:10:57 +00001034 /* clean up prefsrc entries */
1035 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -07001036out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 in6_ifa_put(ifp);
1038}
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1041{
1042 struct inet6_dev *idev = ifp->idev;
1043 struct in6_addr addr, *tmpaddr;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001044 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001045 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 int tmp_plen;
1047 int ret = 0;
Neil Horman95c385b2007-04-25 17:08:10 -07001048 u32 addr_flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001049 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Jiri Pirko53bd6742013-12-06 09:45:22 +01001051 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (ift) {
1053 spin_lock_bh(&ift->lock);
1054 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
1055 spin_unlock_bh(&ift->lock);
1056 tmpaddr = &addr;
1057 } else {
1058 tmpaddr = NULL;
1059 }
1060retry:
1061 in6_dev_hold(idev);
1062 if (idev->cnf.use_tempaddr <= 0) {
Jiri Pirko53bd6742013-12-06 09:45:22 +01001063 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001064 pr_info("%s: use_tempaddr is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 in6_dev_put(idev);
1066 ret = -1;
1067 goto out;
1068 }
1069 spin_lock_bh(&ifp->lock);
1070 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1071 idev->cnf.use_tempaddr = -1; /*XXX*/
1072 spin_unlock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01001073 write_unlock_bh(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001074 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1075 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 in6_dev_put(idev);
1077 ret = -1;
1078 goto out;
1079 }
1080 in6_ifa_hold(ifp);
1081 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001082 __ipv6_try_regen_rndid(idev, tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001084 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 tmp_valid_lft = min_t(__u32,
1086 ifp->valid_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +00001087 idev->cnf.temp_valid_lft + age);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001088 tmp_prefered_lft = min_t(__u32,
1089 ifp->prefered_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +00001090 idev->cnf.temp_prefered_lft + age -
Ben Hutchings784e2712010-06-26 11:42:55 +00001091 idev->cnf.max_desync_factor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 tmp_plen = ifp->prefix_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 tmp_tstamp = ifp->tstamp;
1094 spin_unlock_bh(&ifp->lock);
1095
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001096 regen_advance = idev->cnf.regen_max_retry *
David S. Miller7eaa48a2013-08-20 23:44:39 -07001097 idev->cnf.dad_transmits *
Jiri Pirko1f9248e52013-12-07 19:26:53 +01001098 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) / HZ;
Jiri Pirko53bd6742013-12-06 09:45:22 +01001099 write_unlock_bh(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -07001100
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001101 /* A temporary address is created only if this calculated Preferred
1102 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1103 * an implementation must not create a temporary address with a zero
1104 * Preferred Lifetime.
Heiner Kallweitecab6702014-03-12 22:13:19 +01001105 * Use age calculation as in addrconf_verify to avoid unnecessary
1106 * temporary addresses being generated.
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001107 */
Heiner Kallweitecab6702014-03-12 22:13:19 +01001108 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
1109 if (tmp_prefered_lft <= regen_advance + age) {
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001110 in6_ifa_put(ifp);
1111 in6_dev_put(idev);
1112 ret = -1;
1113 goto out;
1114 }
1115
Neil Horman95c385b2007-04-25 17:08:10 -07001116 addr_flags = IFA_F_TEMPORARY;
1117 /* set in addrconf_prefix_rcv() */
1118 if (ifp->flags & IFA_F_OPTIMISTIC)
1119 addr_flags |= IFA_F_OPTIMISTIC;
1120
Hannes Frederic Sowa4b08a8f2013-08-16 13:02:27 +02001121 ift = ipv6_add_addr(idev, &addr, NULL, tmp_plen,
1122 ipv6_addr_scope(&addr), addr_flags,
1123 tmp_valid_lft, tmp_prefered_lft);
1124 if (IS_ERR(ift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 in6_ifa_put(ifp);
1126 in6_dev_put(idev);
Joe Perchesf3213832012-05-15 14:11:53 +00001127 pr_info("%s: retry temporary address regeneration\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 tmpaddr = &addr;
Jiri Pirko53bd6742013-12-06 09:45:22 +01001129 write_lock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 goto retry;
1131 }
1132
1133 spin_lock_bh(&ift->lock);
1134 ift->ifpub = ifp;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001135 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 ift->tstamp = tmp_tstamp;
1137 spin_unlock_bh(&ift->lock);
1138
David S. Millercf22f9a2012-04-14 21:37:40 -04001139 addrconf_dad_start(ift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 in6_ifa_put(ift);
1141 in6_dev_put(idev);
1142out:
1143 return ret;
1144}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001147 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001149enum {
1150 IPV6_SADDR_RULE_INIT = 0,
1151 IPV6_SADDR_RULE_LOCAL,
1152 IPV6_SADDR_RULE_SCOPE,
1153 IPV6_SADDR_RULE_PREFERRED,
1154#ifdef CONFIG_IPV6_MIP6
1155 IPV6_SADDR_RULE_HOA,
1156#endif
1157 IPV6_SADDR_RULE_OIF,
1158 IPV6_SADDR_RULE_LABEL,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001159 IPV6_SADDR_RULE_PRIVACY,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001160 IPV6_SADDR_RULE_ORCHID,
1161 IPV6_SADDR_RULE_PREFIX,
1162 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001163};
1164
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001165struct ipv6_saddr_score {
1166 int rule;
1167 int addr_type;
1168 struct inet6_ifaddr *ifa;
1169 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1170 int scopedist;
1171 int matchlen;
1172};
1173
1174struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001175 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001176 int ifindex;
1177 int scope;
1178 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001179 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001180};
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001181
Dave Jonesb6f99a22007-03-22 12:27:49 -07001182static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Ulrich Weber45bb0062010-02-25 23:28:58 +00001184 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001185 return 1;
1186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
Benjamin Thery3de23252008-05-28 14:51:24 +02001189static int ipv6_get_saddr_eval(struct net *net,
1190 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001191 struct ipv6_saddr_dst *dst,
1192 int i)
1193{
1194 int ret;
1195
1196 if (i <= score->rule) {
1197 switch (i) {
1198 case IPV6_SADDR_RULE_SCOPE:
1199 ret = score->scopedist;
1200 break;
1201 case IPV6_SADDR_RULE_PREFIX:
1202 ret = score->matchlen;
1203 break;
1204 default:
1205 ret = !!test_bit(i, score->scorebits);
1206 }
1207 goto out;
1208 }
1209
1210 switch (i) {
1211 case IPV6_SADDR_RULE_INIT:
1212 /* Rule 0: remember if hiscore is not ready yet */
1213 ret = !!score->ifa;
1214 break;
1215 case IPV6_SADDR_RULE_LOCAL:
1216 /* Rule 1: Prefer same address */
1217 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1218 break;
1219 case IPV6_SADDR_RULE_SCOPE:
1220 /* Rule 2: Prefer appropriate scope
1221 *
1222 * ret
1223 * ^
1224 * -1 | d 15
1225 * ---+--+-+---> scope
1226 * |
1227 * | d is scope of the destination.
1228 * B-d | \
1229 * | \ <- smaller scope is better if
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08001230 * B-15 | \ if scope is enough for destination.
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001231 * | ret = B - scope (-1 <= scope >= d <= 15).
1232 * d-C-1 | /
1233 * |/ <- greater is better
1234 * -C / if scope is not enough for destination.
1235 * /| ret = scope - C (-1 <= d < scope <= 15).
1236 *
1237 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1238 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1239 * Assume B = 0 and we get C > 29.
1240 */
1241 ret = __ipv6_addr_src_scope(score->addr_type);
1242 if (ret >= dst->scope)
1243 ret = -ret;
1244 else
1245 ret -= 128; /* 30 is enough */
1246 score->scopedist = ret;
1247 break;
1248 case IPV6_SADDR_RULE_PREFERRED:
1249 /* Rule 3: Avoid deprecated and optimistic addresses */
1250 ret = ipv6_saddr_preferred(score->addr_type) ||
1251 !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1252 break;
1253#ifdef CONFIG_IPV6_MIP6
1254 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001255 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001256 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001257 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1258 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001259 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001260 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001261#endif
1262 case IPV6_SADDR_RULE_OIF:
1263 /* Rule 5: Prefer outgoing interface */
1264 ret = (!dst->ifindex ||
1265 dst->ifindex == score->ifa->idev->dev->ifindex);
1266 break;
1267 case IPV6_SADDR_RULE_LABEL:
1268 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001269 ret = ipv6_addr_label(net,
1270 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001271 score->ifa->idev->dev->ifindex) == dst->label;
1272 break;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001273 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001274 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001275 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001276 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001277 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001278 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1279 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1280 score->ifa->idev->cnf.use_tempaddr >= 2;
1281 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001282 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001283 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001284 case IPV6_SADDR_RULE_ORCHID:
1285 /* Rule 8-: Prefer ORCHID vs ORCHID or
1286 * non-ORCHID vs non-ORCHID
1287 */
1288 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1289 ipv6_addr_orchid(dst->addr));
1290 break;
1291 case IPV6_SADDR_RULE_PREFIX:
1292 /* Rule 8: Use longest matching prefix */
YOSHIFUJI Hideaki / 吉藤英明91b4b042012-09-10 18:41:07 +00001293 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1294 if (ret > score->ifa->prefix_len)
1295 ret = score->ifa->prefix_len;
1296 score->matchlen = ret;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001297 break;
1298 default:
1299 ret = 0;
1300 }
1301
1302 if (ret)
1303 __set_bit(i, score->scorebits);
1304 score->rule = i;
1305out:
1306 return ret;
1307}
1308
Patrick McHardyb3f644f2012-08-26 19:14:14 +02001309int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001310 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001311 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001313 struct ipv6_saddr_score scores[2],
1314 *score = &scores[0], *hiscore = &scores[1];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001315 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001316 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001317 int dst_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001319 dst_type = __ipv6_addr_type(daddr);
1320 dst.addr = daddr;
1321 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1322 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001323 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001324 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001325
1326 hiscore->rule = -1;
1327 hiscore->ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001329 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001330
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001331 for_each_netdev_rcu(net, dev) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001332 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001333
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001334 /* Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001335 * - multicast and link-local destination address,
1336 * the set of candidate source address MUST only
1337 * include addresses assigned to interfaces
1338 * belonging to the same link as the outgoing
1339 * interface.
1340 * (- For site-local destination addresses, the
1341 * set of candidate source addresses MUST only
1342 * include addresses assigned to interfaces
1343 * belonging to the same site as the outgoing
1344 * interface.)
1345 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001346 if (((dst_type & IPV6_ADDR_MULTICAST) ||
1347 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1348 dst.ifindex && dev->ifindex != dst.ifindex)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001349 continue;
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 idev = __in6_dev_get(dev);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001352 if (!idev)
1353 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001355 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001356 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001357 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001359 /*
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001360 * - Tentative Address (RFC2462 section 5.4)
1361 * - A tentative address is not considered
1362 * "assigned to an interface" in the traditional
Neil Horman95c385b2007-04-25 17:08:10 -07001363 * sense, unless it is also flagged as optimistic.
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001364 * - Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001365 * - In any case, anycast addresses, multicast
1366 * addresses, and the unspecified address MUST
1367 * NOT be included in a candidate set.
1368 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001369 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1370 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001371 continue;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001372
1373 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1374
1375 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1376 score->addr_type & IPV6_ADDR_MULTICAST)) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001377 LIMIT_NETDEBUG(KERN_DEBUG
Joe Perches3b6d821c2007-11-19 23:47:25 -08001378 "ADDRCONF: unspecified / multicast address "
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001379 "assigned as unicast address on %s",
1380 dev->name);
1381 continue;
1382 }
1383
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001384 score->rule = -1;
1385 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001386
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001387 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1388 int minihiscore, miniscore;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001389
Benjamin Thery3de23252008-05-28 14:51:24 +02001390 minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
1391 miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001392
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001393 if (minihiscore > miniscore) {
1394 if (i == IPV6_SADDR_RULE_SCOPE &&
1395 score->scopedist > 0) {
1396 /*
1397 * special case:
1398 * each remaining entry
1399 * has too small (not enough)
1400 * scope, because ifa entries
1401 * are sorted by their scope
1402 * values.
1403 */
1404 goto try_nextdev;
1405 }
1406 break;
1407 } else if (minihiscore < miniscore) {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001408 if (hiscore->ifa)
1409 in6_ifa_put(hiscore->ifa);
1410
1411 in6_ifa_hold(score->ifa);
1412
Ilpo Järvinena0bffff2009-03-21 13:36:17 -07001413 swap(hiscore, score);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001414
1415 /* restore our iterator */
1416 score->ifa = hiscore->ifa;
1417
1418 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001421 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001422try_nextdev:
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001423 read_unlock_bh(&idev->lock);
1424 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001425 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001427 if (!hiscore->ifa)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001428 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001429
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001430 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001431 in6_ifa_put(hiscore->ifa);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001434EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Amerigo Wang89657792013-06-29 21:30:49 +08001436int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001437 u32 banned_flags)
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001438{
1439 struct inet6_ifaddr *ifp;
1440 int err = -EADDRNOTAVAIL;
1441
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01001442 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1443 if (ifp->scope > IFA_LINK)
1444 break;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001445 if (ifp->scope == IFA_LINK &&
1446 !(ifp->flags & banned_flags)) {
1447 *addr = ifp->addr;
1448 err = 0;
1449 break;
1450 }
1451 }
1452 return err;
1453}
1454
Neil Horman95c385b2007-04-25 17:08:10 -07001455int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
Jiri Pirko479840f2013-12-06 09:45:21 +01001456 u32 banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
1458 struct inet6_dev *idev;
1459 int err = -EADDRNOTAVAIL;
1460
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001461 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001462 idev = __in6_dev_get(dev);
1463 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 read_lock_bh(&idev->lock);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02001465 err = __ipv6_get_lladdr(idev, addr, banned_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 read_unlock_bh(&idev->lock);
1467 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001468 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return err;
1470}
1471
1472static int ipv6_count_addresses(struct inet6_dev *idev)
1473{
1474 int cnt = 0;
1475 struct inet6_ifaddr *ifp;
1476
1477 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001478 list_for_each_entry(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 cnt++;
1480 read_unlock_bh(&idev->lock);
1481 return cnt;
1482}
1483
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001484int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Florian Westphal2a7851b2013-05-17 03:56:10 +00001485 const struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001487 struct inet6_ifaddr *ifp;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001488 unsigned int hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
stephen hemminger5c578ae2010-03-17 20:31:11 +00001490 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001491 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001492 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001493 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (ipv6_addr_equal(&ifp->addr, addr) &&
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001495 !(ifp->flags&IFA_F_TENTATIVE) &&
1496 (dev == NULL || ifp->idev->dev == dev ||
1497 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1498 rcu_read_unlock_bh();
1499 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
1501 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001502
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001503 rcu_read_unlock_bh();
1504 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001506EXPORT_SYMBOL(ipv6_chk_addr);
1507
David S. Miller3e81c6d2010-03-20 16:18:00 -07001508static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1509 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Eric Dumazetddbe5032012-07-18 08:11:12 +00001511 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001512 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Sasha Levinb67bfe02013-02-27 17:06:00 -08001514 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001515 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001516 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 if (ipv6_addr_equal(&ifp->addr, addr)) {
1518 if (dev == NULL || ifp->idev->dev == dev)
David S. Miller3e81c6d2010-03-20 16:18:00 -07001519 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
1521 }
David S. Miller3e81c6d2010-03-20 16:18:00 -07001522 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
Catalin\(ux\) M. BOIE7df37ff2013-09-23 23:04:19 +03001525/* Compares an address/prefix_len with addresses on device @dev.
1526 * If one is found it returns true.
1527 */
1528bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
1529 const unsigned int prefix_len, struct net_device *dev)
1530{
1531 struct inet6_dev *idev;
1532 struct inet6_ifaddr *ifa;
1533 bool ret = false;
1534
1535 rcu_read_lock();
1536 idev = __in6_dev_get(dev);
1537 if (idev) {
1538 read_lock_bh(&idev->lock);
1539 list_for_each_entry(ifa, &idev->addr_list, if_list) {
1540 ret = ipv6_prefix_equal(addr, &ifa->addr, prefix_len);
1541 if (ret)
1542 break;
1543 }
1544 read_unlock_bh(&idev->lock);
1545 }
1546 rcu_read_unlock();
1547
1548 return ret;
1549}
1550EXPORT_SYMBOL(ipv6_chk_custom_prefix);
1551
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001552int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001553{
1554 struct inet6_dev *idev;
1555 struct inet6_ifaddr *ifa;
1556 int onlink;
1557
1558 onlink = 0;
1559 rcu_read_lock();
1560 idev = __in6_dev_get(dev);
1561 if (idev) {
1562 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001563 list_for_each_entry(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001564 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1565 ifa->prefix_len);
1566 if (onlink)
1567 break;
1568 }
1569 read_unlock_bh(&idev->lock);
1570 }
1571 rcu_read_unlock();
1572 return onlink;
1573}
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001574EXPORT_SYMBOL(ipv6_chk_prefix);
1575
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001576struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001577 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
David S. Millerb79d1d52010-03-25 21:39:21 -07001579 struct inet6_ifaddr *ifp, *result = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001580 unsigned int hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
stephen hemminger5c578ae2010-03-17 20:31:11 +00001582 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001583 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001584 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001585 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (ipv6_addr_equal(&ifp->addr, addr)) {
1587 if (dev == NULL || ifp->idev->dev == dev ||
1588 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07001589 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 in6_ifa_hold(ifp);
1591 break;
1592 }
1593 }
1594 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001595 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
David S. Millerb79d1d52010-03-25 21:39:21 -07001597 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600/* Gets referenced address, destroys ifaddr */
1601
Brian Haleycc411d02009-09-09 14:41:32 +00001602static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 if (ifp->flags&IFA_F_PERMANENT) {
1605 spin_lock_bh(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001606 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 ifp->flags |= IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00001608 if (dad_failed)
1609 ifp->flags |= IFA_F_DADFAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 spin_unlock_bh(&ifp->lock);
Herbert Xue2577a02010-03-13 12:23:29 -08001611 if (dad_failed)
1612 ipv6_ifa_notify(0, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 } else if (ifp->flags&IFA_F_TEMPORARY) {
1615 struct inet6_ifaddr *ifpub;
1616 spin_lock_bh(&ifp->lock);
1617 ifpub = ifp->ifpub;
1618 if (ifpub) {
1619 in6_ifa_hold(ifpub);
1620 spin_unlock_bh(&ifp->lock);
1621 ipv6_create_tempaddr(ifpub, ifp);
1622 in6_ifa_put(ifpub);
1623 } else {
1624 spin_unlock_bh(&ifp->lock);
1625 }
1626 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001627 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 ipv6_del_addr(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
1631
Herbert Xuf2344a12010-05-18 15:55:27 -07001632static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1633{
1634 int err = -ENOENT;
1635
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001636 spin_lock_bh(&ifp->state_lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001637 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1638 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1639 err = 0;
1640 }
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001641 spin_unlock_bh(&ifp->state_lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07001642
1643 return err;
1644}
1645
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001646void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1647{
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001648 struct inet6_dev *idev = ifp->idev;
Brian Haley9bdd8d42009-03-18 18:22:48 -07001649
Ursula Braun853dc2e2010-10-24 23:06:43 +00001650 if (addrconf_dad_end(ifp)) {
1651 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07001652 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00001653 }
Herbert Xuf2344a12010-05-18 15:55:27 -07001654
Joe Perchese87cc472012-05-13 21:56:26 +00001655 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1656 ifp->idev->dev->name, &ifp->addr);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001657
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001658 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
1659 struct in6_addr addr;
1660
1661 addr.s6_addr32[0] = htonl(0xfe800000);
1662 addr.s6_addr32[1] = 0;
1663
1664 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
1665 ipv6_addr_equal(&ifp->addr, &addr)) {
1666 /* DAD failed for link-local based on MAC address */
1667 idev->cnf.disable_ipv6 = 1;
Brian Haley9bdd8d42009-03-18 18:22:48 -07001668
Joe Perchesf3213832012-05-15 14:11:53 +00001669 pr_info("%s: IPv6 being disabled!\n",
Brian Haley9bdd8d42009-03-18 18:22:48 -07001670 ifp->idev->dev->name);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001671 }
1672 }
1673
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001674 spin_lock_bh(&ifp->state_lock);
1675 /* transition from _POSTDAD to _ERRDAD */
1676 ifp->state = INET6_IFADDR_STATE_ERRDAD;
1677 spin_unlock_bh(&ifp->state_lock);
1678
1679 addrconf_mod_dad_work(ifp, 0);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001680}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682/* Join to solicited addr multicast group. */
1683
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001684void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
1686 struct in6_addr maddr;
1687
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001688 ASSERT_RTNL();
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1691 return;
1692
1693 addrconf_addr_solict_mult(addr, &maddr);
1694 ipv6_dev_mc_inc(dev, &maddr);
1695}
1696
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001697void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 struct in6_addr maddr;
1700
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001701 ASSERT_RTNL();
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1704 return;
1705
1706 addrconf_addr_solict_mult(addr, &maddr);
1707 __ipv6_dev_mc_dec(idev, &maddr);
1708}
1709
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001710static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
1712 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001713
1714 ASSERT_RTNL();
1715
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01001716 if (ifp->prefix_len >= 127) /* RFC 6164 */
Bjørn Mork2bda8a02011-07-05 23:04:13 +00001717 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1719 if (ipv6_addr_any(&addr))
1720 return;
1721 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1722}
1723
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001724static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
1726 struct in6_addr addr;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01001727
1728 ASSERT_RTNL();
1729
Hannes Frederic Sowa88ad3142014-01-06 17:53:14 +01001730 if (ifp->prefix_len >= 127) /* RFC 6164 */
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00001731 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1733 if (ipv6_addr_any(&addr))
1734 return;
1735 __ipv6_dev_ac_dec(ifp->idev, &addr);
1736}
1737
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001738static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1739{
1740 if (dev->addr_len != ETH_ALEN)
1741 return -1;
1742 memcpy(eui, dev->dev_addr, 3);
1743 memcpy(eui + 5, dev->dev_addr + 3, 3);
1744
1745 /*
1746 * The zSeries OSA network cards can be shared among various
1747 * OS instances, but the OSA cards have only one MAC address.
1748 * This leads to duplicate address conflicts in conjunction
1749 * with IPv6 if more than one instance uses the same card.
1750 *
1751 * The driver for these cards can deliver a unique 16-bit
1752 * identifier for each instance sharing the same card. It is
1753 * placed instead of 0xFFFE in the interface identifier. The
1754 * "u" bit of the interface identifier is not inverted in this
1755 * case. Hence the resulting interface identifier has local
1756 * scope according to RFC2373.
1757 */
1758 if (dev->dev_id) {
1759 eui[3] = (dev->dev_id >> 8) & 0xFF;
1760 eui[4] = dev->dev_id & 0xFF;
1761 } else {
1762 eui[3] = 0xFF;
1763 eui[4] = 0xFE;
1764 eui[0] ^= 2;
1765 }
1766 return 0;
1767}
1768
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00001769static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
1770{
1771 if (dev->addr_len != IEEE802154_ADDR_LEN)
1772 return -1;
1773 memcpy(eui, dev->dev_addr, 8);
YOSHIFUJI Hideaki / 吉藤英明5e98a362013-01-28 10:44:29 +00001774 eui[0] ^= 2;
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00001775 return 0;
1776}
1777
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00001778static int addrconf_ifid_ieee1394(u8 *eui, struct net_device *dev)
1779{
1780 union fwnet_hwaddr *ha;
1781
1782 if (dev->addr_len != FWNET_ALEN)
1783 return -1;
1784
1785 ha = (union fwnet_hwaddr *)dev->dev_addr;
1786
1787 memcpy(eui, &ha->uc.uniq_id, sizeof(ha->uc.uniq_id));
1788 eui[0] ^= 2;
1789 return 0;
1790}
1791
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001792static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1793{
1794 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1795 if (dev->addr_len != ARCNET_ALEN)
1796 return -1;
1797 memset(eui, 0, 7);
Eldad Zack8e5e8f32012-04-01 07:49:08 +00001798 eui[7] = *(u8 *)dev->dev_addr;
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001799 return 0;
1800}
1801
1802static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1803{
1804 if (dev->addr_len != INFINIBAND_ALEN)
1805 return -1;
1806 memcpy(eui, dev->dev_addr + 12, 8);
1807 eui[0] |= 2;
1808 return 0;
1809}
1810
stephen hemmingerc61393e2010-10-04 20:17:53 +00001811static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001812{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00001813 if (addr == 0)
1814 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001815 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1816 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1817 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1818 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1819 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1820 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1821 eui[1] = 0;
1822 eui[2] = 0x5E;
1823 eui[3] = 0xFE;
1824 memcpy(eui + 4, &addr, 4);
1825 return 0;
1826}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001827
1828static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1829{
1830 if (dev->priv_flags & IFF_ISATAP)
1831 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1832 return -1;
1833}
1834
stephen hemmingeraee80b52011-06-08 10:44:30 +00001835static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1836{
1837 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1838}
1839
Nicolas Dichtele8377352013-08-20 12:16:06 +02001840static int addrconf_ifid_ip6tnl(u8 *eui, struct net_device *dev)
1841{
1842 memcpy(eui, dev->perm_addr, 3);
1843 memcpy(eui + 5, dev->perm_addr + 3, 3);
1844 eui[3] = 0xFF;
1845 eui[4] = 0xFE;
1846 eui[0] ^= 2;
1847 return 0;
1848}
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1851{
1852 switch (dev->type) {
1853 case ARPHRD_ETHER:
1854 case ARPHRD_FDDI:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001855 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001857 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001859 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001860 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001861 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00001862 case ARPHRD_IPGRE:
1863 return addrconf_ifid_gre(eui, dev);
Jukka Rissanene74bccb2013-12-11 17:05:36 +02001864 case ARPHRD_6LOWPAN:
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00001865 case ARPHRD_IEEE802154:
1866 return addrconf_ifid_eui64(eui, dev);
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00001867 case ARPHRD_IEEE1394:
1868 return addrconf_ifid_ieee1394(eui, dev);
Nicolas Dichtele8377352013-08-20 12:16:06 +02001869 case ARPHRD_TUNNEL6:
1870 return addrconf_ifid_ip6tnl(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
1872 return -1;
1873}
1874
1875static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1876{
1877 int err = -1;
1878 struct inet6_ifaddr *ifp;
1879
1880 read_lock_bh(&idev->lock);
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01001881 list_for_each_entry_reverse(ifp, &idev->addr_list, if_list) {
1882 if (ifp->scope > IFA_LINK)
1883 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1885 memcpy(eui, ifp->addr.s6_addr+8, 8);
1886 err = 0;
1887 break;
1888 }
1889 }
1890 read_unlock_bh(&idev->lock);
1891 return err;
1892}
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001895static void __ipv6_regen_rndid(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08001898 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 /*
1902 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1903 * check if generated address is not inappropriate
1904 *
1905 * - Reserved subnet anycast (RFC 2526)
1906 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001907 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 * 00-00-5E-FE-xx-xx-xx-xx
1909 * - value 0
1910 * - XXX: already assigned to an address on the device
1911 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001912 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1914 (idev->rndid[7]&0x80))
1915 goto regen;
1916 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1917 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1918 goto regen;
1919 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1920 goto regen;
1921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}
1923
1924static void ipv6_regen_rndid(unsigned long data)
1925{
1926 struct inet6_dev *idev = (struct inet6_dev *) data;
1927 unsigned long expires;
1928
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001929 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 write_lock_bh(&idev->lock);
1931
1932 if (idev->dead)
1933 goto out;
1934
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001935 __ipv6_regen_rndid(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 expires = jiffies +
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001938 idev->cnf.temp_prefered_lft * HZ -
Jiri Pirko1f9248e52013-12-07 19:26:53 +01001939 idev->cnf.regen_max_retry * idev->cnf.dad_transmits *
1940 NEIGH_VAR(idev->nd_parms, RETRANS_TIME) -
Ben Hutchings784e2712010-06-26 11:42:55 +00001941 idev->cnf.max_desync_factor * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (time_before(expires, jiffies)) {
Joe Perchesf3213832012-05-15 14:11:53 +00001943 pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
1944 __func__, idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 goto out;
1946 }
1947
1948 if (!mod_timer(&idev->regen_timer, expires))
1949 in6_dev_hold(idev);
1950
1951out:
1952 write_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001953 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 in6_dev_put(idev);
1955}
1956
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001957static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
Eldad Zack8e5e8f32012-04-01 07:49:08 +00001958{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001960 __ipv6_regen_rndid(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963/*
1964 * Add prefix route.
1965 */
1966
1967static void
1968addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001969 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
Thomas Graf86872cb2006-08-22 00:01:08 -07001971 struct fib6_config cfg = {
1972 .fc_table = RT6_TABLE_PREFIX,
1973 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1974 .fc_ifindex = dev->ifindex,
1975 .fc_expires = expires,
1976 .fc_dst_len = plen,
1977 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001978 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07001979 .fc_protocol = RTPROT_KERNEL,
Thomas Graf86872cb2006-08-22 00:01:08 -07001980 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001982 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 /* Prevent useless cloning on PtP SIT.
1985 This thing is done here expecting that the whole
1986 class of non-broadcast devices need not cloning.
1987 */
Amerigo Wang07a93622012-10-29 16:23:10 +00001988#if IS_ENABLED(CONFIG_IPV6_SIT)
Thomas Graf86872cb2006-08-22 00:01:08 -07001989 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1990 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07001991#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Thomas Graf86872cb2006-08-22 00:01:08 -07001993 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994}
1995
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001996
1997static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
1998 int plen,
1999 const struct net_device *dev,
2000 u32 flags, u32 noflags)
2001{
2002 struct fib6_node *fn;
2003 struct rt6_info *rt = NULL;
2004 struct fib6_table *table;
2005
2006 table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
2007 if (table == NULL)
2008 return NULL;
2009
Li RongQing5744dd92012-09-11 21:59:01 +00002010 read_lock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002011 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
2012 if (!fn)
2013 goto out;
2014 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05002015 if (rt->dst.dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002016 continue;
2017 if ((rt->rt6i_flags & flags) != flags)
2018 continue;
Romain Kuntz85da53b2013-01-09 15:02:26 +01002019 if ((rt->rt6i_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002020 continue;
2021 dst_hold(&rt->dst);
2022 break;
2023 }
2024out:
Li RongQing5744dd92012-09-11 21:59:01 +00002025 read_unlock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002026 return rt;
2027}
2028
2029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030/* Create "default" multicast route to the interface */
2031
2032static void addrconf_add_mroute(struct net_device *dev)
2033{
Thomas Graf86872cb2006-08-22 00:01:08 -07002034 struct fib6_config cfg = {
2035 .fc_table = RT6_TABLE_LOCAL,
2036 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2037 .fc_ifindex = dev->ifindex,
2038 .fc_dst_len = 8,
2039 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002040 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07002041 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Thomas Graf86872cb2006-08-22 00:01:08 -07002043 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
2044
2045 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046}
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2049{
2050 struct inet6_dev *idev;
2051
2052 ASSERT_RTNL();
2053
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002054 idev = ipv6_find_idev(dev);
2055 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00002056 return ERR_PTR(-ENOBUFS);
2057
2058 if (idev->cnf.disable_ipv6)
2059 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 /* Add default multicast route */
Li Wei4af04ab2011-12-06 21:23:45 +00002062 if (!(dev->flags & IFF_LOOPBACK))
2063 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 return idev;
2066}
2067
Jiri Pirko53bd6742013-12-06 09:45:22 +01002068static void manage_tempaddrs(struct inet6_dev *idev,
2069 struct inet6_ifaddr *ifp,
2070 __u32 valid_lft, __u32 prefered_lft,
2071 bool create, unsigned long now)
2072{
2073 u32 flags;
2074 struct inet6_ifaddr *ift;
2075
2076 read_lock_bh(&idev->lock);
2077 /* update all temporary addresses in the list */
2078 list_for_each_entry(ift, &idev->tempaddr_list, tmp_list) {
2079 int age, max_valid, max_prefered;
2080
2081 if (ifp != ift->ifpub)
2082 continue;
2083
2084 /* RFC 4941 section 3.3:
2085 * If a received option will extend the lifetime of a public
2086 * address, the lifetimes of temporary addresses should
2087 * be extended, subject to the overall constraint that no
2088 * temporary addresses should ever remain "valid" or "preferred"
2089 * for a time longer than (TEMP_VALID_LIFETIME) or
2090 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR), respectively.
2091 */
2092 age = (now - ift->cstamp) / HZ;
2093 max_valid = idev->cnf.temp_valid_lft - age;
2094 if (max_valid < 0)
2095 max_valid = 0;
2096
2097 max_prefered = idev->cnf.temp_prefered_lft -
2098 idev->cnf.max_desync_factor - age;
2099 if (max_prefered < 0)
2100 max_prefered = 0;
2101
2102 if (valid_lft > max_valid)
2103 valid_lft = max_valid;
2104
2105 if (prefered_lft > max_prefered)
2106 prefered_lft = max_prefered;
2107
2108 spin_lock(&ift->lock);
2109 flags = ift->flags;
2110 ift->valid_lft = valid_lft;
2111 ift->prefered_lft = prefered_lft;
2112 ift->tstamp = now;
2113 if (prefered_lft > 0)
2114 ift->flags &= ~IFA_F_DEPRECATED;
2115
2116 spin_unlock(&ift->lock);
2117 if (!(flags&IFA_F_TENTATIVE))
2118 ipv6_ifa_notify(0, ift);
2119 }
2120
2121 if ((create || list_empty(&idev->tempaddr_list)) &&
2122 idev->cnf.use_tempaddr > 0) {
2123 /* When a new public address is created as described
2124 * in [ADDRCONF], also create a new temporary address.
2125 * Also create a temporary address if it's enabled but
2126 * no temporary address currently exists.
2127 */
2128 read_unlock_bh(&idev->lock);
2129 ipv6_create_tempaddr(ifp, NULL);
2130 } else {
2131 read_unlock_bh(&idev->lock);
2132 }
2133}
2134
Neil Hormane6bff992012-01-04 10:49:15 +00002135void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
2137 struct prefix_info *pinfo;
2138 __u32 valid_lft;
2139 __u32 prefered_lft;
2140 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07002142 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 if (len < sizeof(struct prefix_info)) {
dingtianhongba3542e2013-08-17 10:27:04 +08002147 ADBG("addrconf: prefix option too short\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 return;
2149 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 /*
2152 * Validation checks ([ADDRCONF], page 19)
2153 */
2154
2155 addr_type = ipv6_addr_type(&pinfo->prefix);
2156
2157 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
2158 return;
2159
2160 valid_lft = ntohl(pinfo->valid);
2161 prefered_lft = ntohl(pinfo->prefered);
2162
2163 if (prefered_lft > valid_lft) {
Joe Perchese87cc472012-05-13 21:56:26 +00002164 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 return;
2166 }
2167
2168 in6_dev = in6_dev_get(dev);
2169
2170 if (in6_dev == NULL) {
Joe Perchese87cc472012-05-13 21:56:26 +00002171 net_dbg_ratelimited("addrconf: device %s not configured\n",
2172 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 return;
2174 }
2175
2176 /*
2177 * Two things going on here:
2178 * 1) Add routes for on-link prefixes
2179 * 2) Configure prefixes with the auto flag set
2180 */
2181
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002182 if (pinfo->onlink) {
2183 struct rt6_info *rt;
2184 unsigned long rt_expires;
2185
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002186 /* Avoid arithmetic overflow. Really, we could
2187 * save rt_expires in seconds, likely valid_lft,
2188 * but it would require division in fib gc, that it
2189 * not good.
2190 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002191 if (HZ > USER_HZ)
2192 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2193 else
2194 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08002195
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002196 if (addrconf_finite_timeout(rt_expires))
2197 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002199 rt = addrconf_get_prefix_route(&pinfo->prefix,
2200 pinfo->prefix_len,
2201 dev,
2202 RTF_ADDRCONF | RTF_PREFIX_RT,
2203 RTF_GATEWAY | RTF_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002205 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002206 /* Autoconf prefix route */
2207 if (valid_lft == 0) {
2208 ip6_del_rt(rt);
2209 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002210 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002211 /* not infinity */
Gao feng1716a962012-04-06 00:13:10 +00002212 rt6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002213 } else {
Gao feng1716a962012-04-06 00:13:10 +00002214 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 }
2216 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002217 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002218 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2219 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002220 /* not infinity */
2221 flags |= RTF_EXPIRES;
2222 expires = jiffies_to_clock_t(rt_expires);
2223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002225 dev, expires, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00002227 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 }
2229
2230 /* Try to figure out our local address for this prefix */
2231
2232 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002233 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 struct in6_addr addr;
2235 int create = 0, update_lft = 0;
Daniel Borkmann617fe292013-04-09 03:47:16 +00002236 bool tokenized = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 if (pinfo->prefix_len == 64) {
2239 memcpy(&addr, &pinfo->prefix, 8);
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002240
2241 if (!ipv6_addr_any(&in6_dev->token)) {
2242 read_lock_bh(&in6_dev->lock);
2243 memcpy(addr.s6_addr + 8,
2244 in6_dev->token.s6_addr + 8, 8);
2245 read_unlock_bh(&in6_dev->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00002246 tokenized = true;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00002247 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2248 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 in6_dev_put(in6_dev);
2250 return;
2251 }
2252 goto ok;
2253 }
Joe Perchese87cc472012-05-13 21:56:26 +00002254 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2255 pinfo->prefix_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 in6_dev_put(in6_dev);
2257 return;
2258
2259ok:
2260
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07002261 ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
2263 if (ifp == NULL && valid_lft) {
2264 int max_addresses = in6_dev->cnf.max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -07002265 u32 addr_flags = 0;
2266
2267#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2268 if (in6_dev->cnf.optimistic_dad &&
Neil Hormane6bff992012-01-04 10:49:15 +00002269 !net->ipv6.devconf_all->forwarding && sllao)
Neil Horman95c385b2007-04-25 17:08:10 -07002270 addr_flags = IFA_F_OPTIMISTIC;
2271#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 /* Do not allow to create too much of autoconfigured
2274 * addresses; this would be too easy way to crash kernel.
2275 */
2276 if (!max_addresses ||
2277 ipv6_count_addresses(in6_dev) < max_addresses)
Jiri Pirko3f8f5292013-08-01 10:41:27 +02002278 ifp = ipv6_add_addr(in6_dev, &addr, NULL,
2279 pinfo->prefix_len,
Neil Horman95c385b2007-04-25 17:08:10 -07002280 addr_type&IPV6_ADDR_SCOPE_MASK,
Jiri Benc8a226b22013-08-01 10:41:28 +02002281 addr_flags, valid_lft,
2282 prefered_lft);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
YOSHIFUJI Hideaki / 吉藤英明3f0d2ba2013-01-22 06:32:54 +00002284 if (IS_ERR_OR_NULL(ifp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 in6_dev_put(in6_dev);
2286 return;
2287 }
2288
Jiri Benc8a226b22013-08-01 10:41:28 +02002289 update_lft = 0;
2290 create = 1;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002291 spin_lock_bh(&ifp->lock);
2292 ifp->flags |= IFA_F_MANAGETEMPADDR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 ifp->cstamp = jiffies;
Daniel Borkmann617fe292013-04-09 03:47:16 +00002294 ifp->tokenized = tokenized;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002295 spin_unlock_bh(&ifp->lock);
David S. Millercf22f9a2012-04-14 21:37:40 -04002296 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 }
2298
2299 if (ifp) {
Jiri Pirko479840f2013-12-06 09:45:21 +01002300 u32 flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 u32 stored_lft;
2303
2304 /* update lifetime (RFC2462 5.5.3 e) */
2305 spin_lock(&ifp->lock);
2306 now = jiffies;
2307 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2308 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2309 else
2310 stored_lft = 0;
Jiri Benc8a226b22013-08-01 10:41:28 +02002311 if (!update_lft && !create && stored_lft) {
Paul Marksc9d55d52013-09-25 15:12:55 -07002312 const u32 minimum_lft = min(
2313 stored_lft, (u32)MIN_VALID_LIFETIME);
2314 valid_lft = max(valid_lft, minimum_lft);
2315
2316 /* RFC4862 Section 5.5.3e:
2317 * "Note that the preferred lifetime of the
2318 * corresponding address is always reset to
2319 * the Preferred Lifetime in the received
2320 * Prefix Information option, regardless of
2321 * whether the valid lifetime is also reset or
2322 * ignored."
2323 *
2324 * So we should always update prefered_lft here.
2325 */
2326 update_lft = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 }
2328
2329 if (update_lft) {
2330 ifp->valid_lft = valid_lft;
2331 ifp->prefered_lft = prefered_lft;
2332 ifp->tstamp = now;
2333 flags = ifp->flags;
2334 ifp->flags &= ~IFA_F_DEPRECATED;
2335 spin_unlock(&ifp->lock);
2336
2337 if (!(flags&IFA_F_TENTATIVE))
2338 ipv6_ifa_notify(0, ifp);
2339 } else
2340 spin_unlock(&ifp->lock);
2341
Jiri Pirko53bd6742013-12-06 09:45:22 +01002342 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2343 create, now);
David S. Miller5d9efa72013-10-28 20:07:50 -04002344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002346 addrconf_verify();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 }
2348 }
2349 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2350 in6_dev_put(in6_dev);
2351}
2352
2353/*
2354 * Set destination address.
2355 * Special case for SIT interfaces where we create a new "virtual"
2356 * device.
2357 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002358int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
2360 struct in6_ifreq ireq;
2361 struct net_device *dev;
2362 int err = -EINVAL;
2363
2364 rtnl_lock();
2365
2366 err = -EFAULT;
2367 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2368 goto err_exit;
2369
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002370 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
2372 err = -ENODEV;
2373 if (dev == NULL)
2374 goto err_exit;
2375
Amerigo Wang07a93622012-10-29 16:23:10 +00002376#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002378 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 struct ip_tunnel_parm p;
2381
2382 err = -EADDRNOTAVAIL;
2383 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2384 goto err_exit;
2385
2386 memset(&p, 0, sizeof(p));
2387 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2388 p.iph.saddr = 0;
2389 p.iph.version = 4;
2390 p.iph.ihl = 5;
2391 p.iph.protocol = IPPROTO_IPV6;
2392 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002393 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002395 if (ops->ndo_do_ioctl) {
2396 mm_segment_t oldfs = get_fs();
2397
2398 set_fs(KERNEL_DS);
2399 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2400 set_fs(oldfs);
2401 } else
2402 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 if (err == 0) {
2405 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002406 dev = __dev_get_by_name(net, p.name);
2407 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 goto err_exit;
2409 err = dev_open(dev);
2410 }
2411 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002412#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
2414err_exit:
2415 rtnl_unlock();
2416 return err;
2417}
2418
2419/*
2420 * Manual configuration of address on an interface
2421 */
Jiri Pirko479840f2013-12-06 09:45:21 +01002422static int inet6_addr_add(struct net *net, int ifindex,
2423 const struct in6_addr *pfx,
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002424 const struct in6_addr *peer_pfx,
Jiri Pirko479840f2013-12-06 09:45:21 +01002425 unsigned int plen, __u32 ifa_flags,
2426 __u32 prefered_lft, __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427{
2428 struct inet6_ifaddr *ifp;
2429 struct inet6_dev *idev;
2430 struct net_device *dev;
2431 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002432 u32 flags;
2433 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002434 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002437
Thomas Graf24ef0da2008-05-28 16:54:22 +02002438 if (plen > 128)
2439 return -EINVAL;
2440
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002441 /* check the lifetime */
2442 if (!valid_lft || prefered_lft > valid_lft)
2443 return -EINVAL;
2444
Jiri Pirko53bd6742013-12-06 09:45:22 +01002445 if (ifa_flags & IFA_F_MANAGETEMPADDR && plen != 64)
2446 return -EINVAL;
2447
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002448 dev = __dev_get_by_index(net, ifindex);
2449 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002451
Brian Haley64e724f2010-07-20 10:34:30 +00002452 idev = addrconf_add_dev(dev);
2453 if (IS_ERR(idev))
2454 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456 scope = ipv6_addr_scope(pfx);
2457
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002458 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2459 if (addrconf_finite_timeout(timeout)) {
2460 expires = jiffies_to_clock_t(timeout * HZ);
2461 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002462 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002463 } else {
2464 expires = 0;
2465 flags = 0;
2466 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002467 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002468
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002469 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2470 if (addrconf_finite_timeout(timeout)) {
2471 if (timeout == 0)
2472 ifa_flags |= IFA_F_DEPRECATED;
2473 prefered_lft = timeout;
2474 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002475
Jiri Benc8a226b22013-08-01 10:41:28 +02002476 ifp = ipv6_add_addr(idev, pfx, peer_pfx, plen, scope, ifa_flags,
2477 valid_lft, prefered_lft);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002478
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 if (!IS_ERR(ifp)) {
Thomas Haller761aac72014-01-15 15:36:58 +01002480 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
2481 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
2482 expires, flags);
2483 }
2484
Neil Horman95c385b2007-04-25 17:08:10 -07002485 /*
2486 * Note that section 3.1 of RFC 4429 indicates
2487 * that the Optimistic flag should not be set for
2488 * manually configured addresses
2489 */
David S. Millercf22f9a2012-04-14 21:37:40 -04002490 addrconf_dad_start(ifp);
Jiri Pirko53bd6742013-12-06 09:45:22 +01002491 if (ifa_flags & IFA_F_MANAGETEMPADDR)
2492 manage_tempaddrs(idev, ifp, valid_lft, prefered_lft,
2493 true, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01002495 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 return 0;
2497 }
2498
2499 return PTR_ERR(ifp);
2500}
2501
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002502static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2503 const struct in6_addr *pfx, unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
2505 struct inet6_ifaddr *ifp;
2506 struct inet6_dev *idev;
2507 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002508
Thomas Graf24ef0da2008-05-28 16:54:22 +02002509 if (plen > 128)
2510 return -EINVAL;
2511
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002512 dev = __dev_get_by_index(net, ifindex);
2513 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 return -ENODEV;
2515
David S. Miller7eaa48a2013-08-20 23:44:39 -07002516 if ((idev = __in6_dev_get(dev)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return -ENXIO;
2518
2519 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002520 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 if (ifp->prefix_len == plen &&
2522 ipv6_addr_equal(pfx, &ifp->addr)) {
2523 in6_ifa_hold(ifp);
2524 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002525
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002526 if (!(ifp->flags & IFA_F_TEMPORARY) &&
2527 (ifa_flags & IFA_F_MANAGETEMPADDR))
2528 manage_tempaddrs(idev, ifp, 0, 0, false,
2529 jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 ipv6_del_addr(ifp);
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002531 addrconf_verify_rtnl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 return 0;
2533 }
2534 }
2535 read_unlock_bh(&idev->lock);
2536 return -EADDRNOTAVAIL;
2537}
2538
2539
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002540int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
2542 struct in6_ifreq ireq;
2543 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002544
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002545 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2549 return -EFAULT;
2550
2551 rtnl_lock();
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00002552 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr, NULL,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002553 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2554 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 rtnl_unlock();
2556 return err;
2557}
2558
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002559int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560{
2561 struct in6_ifreq ireq;
2562 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002563
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002564 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 return -EPERM;
2566
2567 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2568 return -EFAULT;
2569
2570 rtnl_lock();
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02002571 err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002572 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 rtnl_unlock();
2574 return err;
2575}
2576
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002577static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2578 int plen, int scope)
2579{
2580 struct inet6_ifaddr *ifp;
2581
Jiri Benc8a226b22013-08-01 10:41:28 +02002582 ifp = ipv6_add_addr(idev, addr, NULL, plen,
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01002583 scope, IFA_F_PERMANENT,
2584 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002585 if (!IS_ERR(ifp)) {
2586 spin_lock_bh(&ifp->lock);
2587 ifp->flags &= ~IFA_F_TENTATIVE;
2588 spin_unlock_bh(&ifp->lock);
2589 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2590 in6_ifa_put(ifp);
2591 }
2592}
2593
Amerigo Wang07a93622012-10-29 16:23:10 +00002594#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595static void sit_add_v4_addrs(struct inet6_dev *idev)
2596{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 struct in6_addr addr;
2598 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002599 struct net *net = dev_net(idev->dev);
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002600 int scope, plen;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002601 u32 pflags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
2603 ASSERT_RTNL();
2604
2605 memset(&addr, 0, sizeof(struct in6_addr));
2606 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2607
2608 if (idev->dev->flags&IFF_POINTOPOINT) {
2609 addr.s6_addr32[0] = htonl(0xfe800000);
2610 scope = IFA_LINK;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002611 plen = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 } else {
2613 scope = IPV6_ADDR_COMPATv4;
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002614 plen = 96;
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002615 pflags |= RTF_NONEXTHOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 }
2617
2618 if (addr.s6_addr32[3]) {
Nicolas Dichtel929c9cf2013-11-14 13:51:05 +01002619 add_addr(idev, &addr, plen, scope);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002620 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 return;
2622 }
2623
Benjamin Thery6fda7352008-03-05 10:47:47 -08002624 for_each_netdev(net, dev) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002625 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 if (in_dev && (dev->flags & IFF_UP)) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002627 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
2629 int flag = scope;
2630
2631 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
2633 addr.s6_addr32[3] = ifa->ifa_local;
2634
2635 if (ifa->ifa_scope == RT_SCOPE_LINK)
2636 continue;
2637 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2638 if (idev->dev->flags&IFF_POINTOPOINT)
2639 continue;
2640 flag |= IFA_HOST;
2641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002643 add_addr(idev, &addr, plen, flag);
Nicolas Dichtelf0e2acf2013-11-14 13:51:06 +01002644 addrconf_prefix_route(&addr, plen, idev->dev, 0,
2645 pflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 }
2647 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002650#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
2652static void init_loopback(struct net_device *dev)
2653{
2654 struct inet6_dev *idev;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05302655 struct net_device *sp_dev;
2656 struct inet6_ifaddr *sp_ifa;
2657 struct rt6_info *sp_rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
2659 /* ::1 */
2660
2661 ASSERT_RTNL();
2662
David S. Miller7eaa48a2013-08-20 23:44:39 -07002663 if ((idev = ipv6_find_idev(dev)) == NULL) {
Joe Perches91df42b2012-05-15 14:11:54 +00002664 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 return;
2666 }
2667
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002668 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05302669
2670 /* Add routes to other interface's IPv6 addresses */
2671 for_each_netdev(dev_net(dev), sp_dev) {
2672 if (!strcmp(sp_dev->name, dev->name))
2673 continue;
2674
2675 idev = __in6_dev_get(sp_dev);
2676 if (!idev)
2677 continue;
2678
2679 read_lock_bh(&idev->lock);
2680 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
2681
2682 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
2683 continue;
2684
Gao feng33d99112014-01-24 16:29:11 +08002685 if (sp_ifa->rt) {
2686 /* This dst has been added to garbage list when
2687 * lo device down, release this obsolete dst and
2688 * reallocate a new router for ifa.
2689 */
2690 if (sp_ifa->rt->dst.obsolete > 0) {
2691 ip6_rt_put(sp_ifa->rt);
2692 sp_ifa->rt = NULL;
2693 } else {
2694 continue;
2695 }
2696 }
Gao fenga881ae12013-06-16 11:14:30 +08002697
François-Xavier Le Bail57ec0af2013-12-02 11:28:49 +01002698 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05302699
2700 /* Failure cases are ignored */
Gao feng534c8772013-06-02 22:16:21 +00002701 if (!IS_ERR(sp_rt)) {
2702 sp_ifa->rt = sp_rt;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05302703 ip6_ins_rt(sp_rt);
Gao feng534c8772013-06-02 22:16:21 +00002704 }
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05302705 }
2706 read_unlock_bh(&idev->lock);
2707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
2709
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002710static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002712 struct inet6_ifaddr *ifp;
Neil Horman95c385b2007-04-25 17:08:10 -07002713 u32 addr_flags = IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Neil Horman95c385b2007-04-25 17:08:10 -07002715#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2716 if (idev->cnf.optimistic_dad &&
David Miller702beb82008-07-20 18:17:02 -07002717 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
Neil Horman95c385b2007-04-25 17:08:10 -07002718 addr_flags |= IFA_F_OPTIMISTIC;
2719#endif
2720
2721
Hannes Frederic Sowa07edd742014-01-08 15:43:22 +01002722 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
2723 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002725 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
David S. Millercf22f9a2012-04-14 21:37:40 -04002726 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 in6_ifa_put(ifp);
2728 }
2729}
2730
2731static void addrconf_dev_config(struct net_device *dev)
2732{
2733 struct in6_addr addr;
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002734 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
2736 ASSERT_RTNL();
2737
Herbert Xu74235a22007-06-14 13:02:55 -07002738 if ((dev->type != ARPHRD_ETHER) &&
2739 (dev->type != ARPHRD_FDDI) &&
Herbert Xu74235a22007-06-14 13:02:55 -07002740 (dev->type != ARPHRD_ARCNET) &&
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002741 (dev->type != ARPHRD_INFINIBAND) &&
YOSHIFUJI Hideaki / 吉藤英明cb6bf352013-03-25 08:26:24 +00002742 (dev->type != ARPHRD_IEEE802154) &&
Nicolas Dichtele8377352013-08-20 12:16:06 +02002743 (dev->type != ARPHRD_IEEE1394) &&
Jukka Rissanene74bccb2013-12-11 17:05:36 +02002744 (dev->type != ARPHRD_TUNNEL6) &&
2745 (dev->type != ARPHRD_6LOWPAN)) {
Herbert Xu74235a22007-06-14 13:02:55 -07002746 /* Alas, we support only Ethernet autoconfiguration. */
2747 return;
2748 }
2749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00002751 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 return;
2753
2754 memset(&addr, 0, sizeof(struct in6_addr));
2755 addr.s6_addr32[0] = htonl(0xFE800000);
2756
2757 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2758 addrconf_add_linklocal(idev, &addr);
2759}
2760
Amerigo Wang07a93622012-10-29 16:23:10 +00002761#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762static void addrconf_sit_config(struct net_device *dev)
2763{
2764 struct inet6_dev *idev;
2765
2766 ASSERT_RTNL();
2767
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002768 /*
2769 * Configure the tunnel with one of our IPv4
2770 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 * our v4 addrs in the tunnel
2772 */
2773
David S. Miller7eaa48a2013-08-20 23:44:39 -07002774 if ((idev = ipv6_find_idev(dev)) == NULL) {
Joe Perches91df42b2012-05-15 14:11:54 +00002775 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 return;
2777 }
2778
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002779 if (dev->priv_flags & IFF_ISATAP) {
2780 struct in6_addr addr;
2781
2782 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002783 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2784 addrconf_add_linklocal(idev, &addr);
2785 return;
2786 }
2787
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 sit_add_v4_addrs(idev);
2789
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00002790 if (dev->flags&IFF_POINTOPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002793#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Amerigo Wang07a93622012-10-29 16:23:10 +00002795#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00002796static void addrconf_gre_config(struct net_device *dev)
2797{
2798 struct inet6_dev *idev;
2799 struct in6_addr addr;
2800
stephen hemmingeraee80b52011-06-08 10:44:30 +00002801 ASSERT_RTNL();
2802
David S. Miller7eaa48a2013-08-20 23:44:39 -07002803 if ((idev = ipv6_find_idev(dev)) == NULL) {
Joe Perches91df42b2012-05-15 14:11:54 +00002804 pr_debug("%s: add_dev failed\n", __func__);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002805 return;
2806 }
2807
2808 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002809 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2810 addrconf_add_linklocal(idev, &addr);
Nicolas Dichtel08b44652014-02-17 14:22:21 +01002811 else
2812 addrconf_prefix_route(&addr, 64, dev, 0, 0);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002813}
2814#endif
2815
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002816static int addrconf_notify(struct notifier_block *this, unsigned long event,
Jiri Pirko351638e2013-05-28 01:30:21 +00002817 void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818{
Jiri Pirko351638e2013-05-28 01:30:21 +00002819 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Eric Dumazet748e2d92012-08-22 21:50:59 +00002820 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002821 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07002822 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002824 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002825 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07002826 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002827 idev = ipv6_add_dev(dev);
2828 if (!idev)
Herbert Xub217d612007-07-30 17:04:52 -07002829 return notifier_from_errno(-ENOMEM);
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002830 }
2831 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002834 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07002835 if (dev->flags & IFF_SLAVE)
2836 break;
2837
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002838 if (event == NETDEV_UP) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002839 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002840 /* device is not ready yet. */
Joe Perchesf3213832012-05-15 14:11:53 +00002841 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002842 dev->name);
2843 break;
2844 }
Kristian Slavov99081042006-02-08 16:10:53 -08002845
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002846 if (!idev && dev->mtu >= IPV6_MIN_MTU)
2847 idev = ipv6_add_dev(dev);
2848
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08002849 if (idev) {
Kristian Slavov99081042006-02-08 16:10:53 -08002850 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08002851 run_pending = 1;
2852 }
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002853 } else {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002854 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002855 /* device is still not ready. */
2856 break;
2857 }
2858
2859 if (idev) {
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002860 if (idev->if_flags & IF_READY)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002861 /* device is already configured. */
2862 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002863 idev->if_flags |= IF_READY;
2864 }
2865
Joe Perchesf3213832012-05-15 14:11:53 +00002866 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
2867 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002868
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002869 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002870 }
2871
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002872 switch (dev->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +00002873#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 case ARPHRD_SIT:
2875 addrconf_sit_config(dev);
2876 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002877#endif
Amerigo Wang07a93622012-10-29 16:23:10 +00002878#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00002879 case ARPHRD_IPGRE:
2880 addrconf_gre_config(dev);
2881 break;
2882#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 case ARPHRD_LOOPBACK:
2884 init_loopback(dev);
2885 break;
2886
2887 default:
2888 addrconf_dev_config(dev);
2889 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002890 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 if (idev) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002893 if (run_pending)
2894 addrconf_dad_run(idev);
2895
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002896 /*
2897 * If the MTU changed during the interface down,
2898 * when the interface up, the changed MTU must be
2899 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002901 if (idev->cnf.mtu6 != dev->mtu &&
2902 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 rt6_mtu_change(dev, dev->mtu);
2904 idev->cnf.mtu6 = dev->mtu;
2905 }
2906 idev->tstamp = jiffies;
2907 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002908
2909 /*
2910 * If the changed mtu during down is lower than
2911 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 */
2913 if (dev->mtu < IPV6_MIN_MTU)
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002914 addrconf_ifdown(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 }
2916 break;
2917
2918 case NETDEV_CHANGEMTU:
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002919 if (idev && dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 rt6_mtu_change(dev, dev->mtu);
2921 idev->cnf.mtu6 = dev->mtu;
2922 break;
2923 }
2924
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002925 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2926 idev = ipv6_add_dev(dev);
2927 if (idev)
2928 break;
2929 }
2930
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002931 /*
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08002932 * if MTU under IPV6_MIN_MTU.
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002933 * Stop IPv6 on this interface.
2934 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
2936 case NETDEV_DOWN:
2937 case NETDEV_UNREGISTER:
2938 /*
2939 * Remove all addresses from this interface.
2940 */
2941 addrconf_ifdown(dev, event != NETDEV_DOWN);
2942 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07002946 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08002947 addrconf_sysctl_unregister(idev);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11002948 addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07002949 err = snmp6_register_dev(idev);
2950 if (err)
2951 return notifier_from_errno(err);
Stephen Hemminger5632c512007-04-28 21:16:39 -07002952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002954
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002955 case NETDEV_PRE_TYPE_CHANGE:
2956 case NETDEV_POST_TYPE_CHANGE:
2957 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07002958 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
2961 return NOTIFY_OK;
2962}
2963
2964/*
2965 * addrconf module should be notified of a device going up
2966 */
2967static struct notifier_block ipv6_dev_notf = {
2968 .notifier_call = addrconf_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969};
2970
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002971static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07002972{
2973 struct inet6_dev *idev;
2974 ASSERT_RTNL();
2975
2976 idev = __in6_dev_get(dev);
2977
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002978 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07002979 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002980 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07002981 ipv6_mc_unmap(idev);
2982}
2983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984static int addrconf_ifdown(struct net_device *dev, int how)
2985{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002986 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002987 struct inet6_dev *idev;
2988 struct inet6_ifaddr *ifa;
David S. Miller73a8bd72011-01-23 23:27:15 -08002989 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
2991 ASSERT_RTNL();
2992
David S. Miller73a8bd72011-01-23 23:27:15 -08002993 rt6_ifdown(net, dev);
2994 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
2996 idev = __in6_dev_get(dev);
2997 if (idev == NULL)
2998 return -ENODEV;
2999
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003000 /*
3001 * Step 1: remove reference to ipv6 device from parent device.
3002 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003004 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07003006
3007 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00003008 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
3010 /* Step 1.5: remove snmp6 entry */
3011 snmp6_unregister_dev(idev);
3012
3013 }
3014
David S. Miller73a8bd72011-01-23 23:27:15 -08003015 /* Step 2: clear hash table */
3016 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
3017 struct hlist_head *h = &inet6_addr_lst[i];
David S. Miller73a8bd72011-01-23 23:27:15 -08003018
3019 spin_lock_bh(&addrconf_hash_lock);
3020 restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08003021 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
David S. Miller73a8bd72011-01-23 23:27:15 -08003022 if (ifa->idev == idev) {
3023 hlist_del_init_rcu(&ifa->addr_lst);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003024 addrconf_del_dad_work(ifa);
David S. Miller73a8bd72011-01-23 23:27:15 -08003025 goto restart;
3026 }
3027 }
3028 spin_unlock_bh(&addrconf_hash_lock);
3029 }
3030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 write_lock_bh(&idev->lock);
3032
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003033 addrconf_del_rs_timer(idev);
3034
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003035 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003036 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003037 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Denis V. Lunev439e2382008-04-03 13:30:17 -07003039 if (how && del_timer(&idev->regen_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 in6_dev_put(idev);
3041
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003042 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00003043 while (!list_empty(&idev->tempaddr_list)) {
3044 ifa = list_first_entry(&idev->tempaddr_list,
3045 struct inet6_ifaddr, tmp_list);
3046 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 write_unlock_bh(&idev->lock);
3048 spin_lock_bh(&ifa->lock);
3049
3050 if (ifa->ifpub) {
3051 in6_ifa_put(ifa->ifpub);
3052 ifa->ifpub = NULL;
3053 }
3054 spin_unlock_bh(&ifa->lock);
3055 in6_ifa_put(ifa);
3056 write_lock_bh(&idev->lock);
3057 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003058
stephen hemminger502a2ff2010-03-17 20:31:13 +00003059 while (!list_empty(&idev->addr_list)) {
3060 ifa = list_first_entry(&idev->addr_list,
3061 struct inet6_ifaddr, if_list);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003062 addrconf_del_dad_work(ifa);
stephen hemminger84e8b802010-03-02 13:32:46 +00003063
David S. Miller73a8bd72011-01-23 23:27:15 -08003064 list_del(&ifa->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
David S. Miller73a8bd72011-01-23 23:27:15 -08003066 write_unlock_bh(&idev->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003067
David S. Miller73a8bd72011-01-23 23:27:15 -08003068 spin_lock_bh(&ifa->state_lock);
3069 state = ifa->state;
3070 ifa->state = INET6_IFADDR_STATE_DEAD;
3071 spin_unlock_bh(&ifa->state_lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00003072
David S. Miller73a8bd72011-01-23 23:27:15 -08003073 if (state != INET6_IFADDR_STATE_DEAD) {
3074 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Cong Wangf88c91d2013-04-14 23:18:43 +08003075 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00003076 }
David S. Miller73a8bd72011-01-23 23:27:15 -08003077 in6_ifa_put(ifa);
stephen hemminger8f37ada2010-03-03 08:19:59 +00003078
David S. Miller73a8bd72011-01-23 23:27:15 -08003079 write_lock_bh(&idev->lock);
3080 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00003081
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 write_unlock_bh(&idev->lock);
3083
3084 /* Step 5: Discard multicast list */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003085 if (how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 ipv6_mc_destroy_dev(idev);
3087 else
3088 ipv6_mc_down(idev);
3089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003091
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003092 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003093 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003094 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 neigh_parms_release(&nd_tbl, idev->nd_parms);
3096 neigh_ifdown(&nd_tbl, dev);
3097 in6_dev_put(idev);
3098 }
3099 return 0;
3100}
3101
3102static void addrconf_rs_timer(unsigned long data)
3103{
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003104 struct inet6_dev *idev = (struct inet6_dev *)data;
Cong Wangcaf92bc2013-08-31 13:44:32 +08003105 struct net_device *dev = idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003106 struct in6_addr lladdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003108 write_lock(&idev->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00003109 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 goto out;
3111
Shmulik Ladkani9ba2add2012-12-02 01:44:53 +00003112 if (!ipv6_accept_ra(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003114
3115 /* Announcement received after solicitation was sent */
3116 if (idev->if_flags & IF_RA_RCVD)
3117 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003119 if (idev->rs_probes++ < idev->cnf.rtr_solicits) {
Cong Wangcaf92bc2013-08-31 13:44:32 +08003120 write_unlock(&idev->lock);
3121 if (!ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
3122 ndisc_send_rs(dev, &lladdr,
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003123 &in6addr_linklocal_allrouters);
3124 else
Cong Wangcaf92bc2013-08-31 13:44:32 +08003125 goto put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Cong Wangcaf92bc2013-08-31 13:44:32 +08003127 write_lock(&idev->lock);
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003128 /* The wait after the last probe can be shorter */
3129 addrconf_mod_rs_timer(idev, (idev->rs_probes ==
3130 idev->cnf.rtr_solicits) ?
3131 idev->cnf.rtr_solicit_delay :
3132 idev->cnf.rtr_solicit_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 /*
3135 * Note: we do not support deprecated "all on-link"
3136 * assumption any longer.
3137 */
Joe Perches91df42b2012-05-15 14:11:54 +00003138 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 }
3140
3141out:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003142 write_unlock(&idev->lock);
Cong Wangcaf92bc2013-08-31 13:44:32 +08003143put:
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003144 in6_dev_put(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145}
3146
3147/*
3148 * Duplicate Address Detection
3149 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003150static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3151{
3152 unsigned long rand_num;
3153 struct inet6_dev *idev = ifp->idev;
3154
Neil Horman95c385b2007-04-25 17:08:10 -07003155 if (ifp->flags & IFA_F_OPTIMISTIC)
3156 rand_num = 0;
3157 else
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003158 rand_num = prandom_u32() % (idev->cnf.rtr_solicit_delay ? : 1);
Neil Horman95c385b2007-04-25 17:08:10 -07003159
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003160 ifp->dad_probes = idev->cnf.dad_transmits;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003161 addrconf_mod_dad_work(ifp, rand_num);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003162}
3163
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003164static void addrconf_dad_begin(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165{
3166 struct inet6_dev *idev = ifp->idev;
3167 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
3169 addrconf_join_solict(dev, &ifp->addr);
3170
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -05003171 prandom_seed((__force u32) ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
3173 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003174 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003175 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
3178 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09003179 idev->cnf.accept_dad < 1 ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003180 !(ifp->flags&IFA_F_TENTATIVE) ||
3181 ifp->flags & IFA_F_NODAD) {
Brian Haleycc411d02009-09-09 14:41:32 +00003182 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003183 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 read_unlock_bh(&idev->lock);
3185
3186 addrconf_dad_completed(ifp);
3187 return;
3188 }
3189
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003190 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003191 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003192 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003193 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003194 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003195 * - keep it tentative if it is a permanent address.
3196 * - otherwise, kill it.
3197 */
3198 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003199 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003200 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003201 }
Neil Horman95c385b2007-04-25 17:08:10 -07003202
3203 /*
3204 * Optimistic nodes can start receiving
3205 * Frames right away
3206 */
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003207 if (ifp->flags & IFA_F_OPTIMISTIC)
Neil Horman95c385b2007-04-25 17:08:10 -07003208 ip6_ins_rt(ifp->rt);
3209
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003210 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003212 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 read_unlock_bh(&idev->lock);
3214}
3215
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003216static void addrconf_dad_start(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217{
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003218 bool begin_dad = false;
3219
3220 spin_lock_bh(&ifp->state_lock);
3221 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3222 ifp->state = INET6_IFADDR_STATE_PREDAD;
3223 begin_dad = true;
3224 }
3225 spin_unlock_bh(&ifp->state_lock);
3226
3227 if (begin_dad)
3228 addrconf_mod_dad_work(ifp, 0);
3229}
3230
3231static void addrconf_dad_work(struct work_struct *w)
3232{
3233 struct inet6_ifaddr *ifp = container_of(to_delayed_work(w),
3234 struct inet6_ifaddr,
3235 dad_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 struct inet6_dev *idev = ifp->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 struct in6_addr mcaddr;
3238
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003239 enum {
3240 DAD_PROCESS,
3241 DAD_BEGIN,
3242 DAD_ABORT,
3243 } action = DAD_PROCESS;
3244
3245 rtnl_lock();
3246
3247 spin_lock_bh(&ifp->state_lock);
3248 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3249 action = DAD_BEGIN;
3250 ifp->state = INET6_IFADDR_STATE_DAD;
3251 } else if (ifp->state == INET6_IFADDR_STATE_ERRDAD) {
3252 action = DAD_ABORT;
3253 ifp->state = INET6_IFADDR_STATE_POSTDAD;
3254 }
3255 spin_unlock_bh(&ifp->state_lock);
3256
3257 if (action == DAD_BEGIN) {
3258 addrconf_dad_begin(ifp);
3259 goto out;
3260 } else if (action == DAD_ABORT) {
3261 addrconf_dad_stop(ifp, 1);
3262 goto out;
3263 }
3264
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003265 if (!ifp->dad_probes && addrconf_dad_end(ifp))
Herbert Xuf2344a12010-05-18 15:55:27 -07003266 goto out;
3267
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003268 write_lock_bh(&idev->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003269 if (idev->dead || !(idev->if_flags & IF_READY)) {
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003270 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 goto out;
3272 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003273
3274 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003275 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3276 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003277 write_unlock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003278 goto out;
3279 }
3280
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003281 if (ifp->dad_probes == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 /*
3283 * DAD was successful
3284 */
3285
Brian Haleycc411d02009-09-09 14:41:32 +00003286 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003287 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003288 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
3290 addrconf_dad_completed(ifp);
3291
3292 goto out;
3293 }
3294
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003295 ifp->dad_probes--;
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003296 addrconf_mod_dad_work(ifp,
3297 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME));
stephen hemminger21809fa2010-02-08 19:48:52 +00003298 spin_unlock(&ifp->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003299 write_unlock_bh(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
3301 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09003303 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304out:
3305 in6_ifa_put(ifp);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003306 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307}
3308
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003309/* ifp->idev must be at least read locked */
3310static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3311{
3312 struct inet6_ifaddr *ifpiter;
3313 struct inet6_dev *idev = ifp->idev;
3314
Hannes Frederic Sowa602582c2014-01-19 21:58:19 +01003315 list_for_each_entry_reverse(ifpiter, &idev->addr_list, if_list) {
3316 if (ifpiter->scope > IFA_LINK)
3317 break;
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003318 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3319 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3320 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
3321 IFA_F_PERMANENT)
3322 return false;
3323 }
3324 return true;
3325}
3326
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3328{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003329 struct net_device *dev = ifp->idev->dev;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003330 struct in6_addr lladdr;
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003331 bool send_rs, send_mld;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003332
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003333 addrconf_del_dad_work(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334
3335 /*
3336 * Configure the address for reception. Now it is valid.
3337 */
3338
3339 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3340
Tore Anderson026359b2011-08-28 23:47:33 +00003341 /* If added prefix is link local and we are prepared to process
3342 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 */
3344
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003345 read_lock_bh(&ifp->idev->lock);
Hannes Frederic Sowa11ffff72014-01-16 20:13:04 +01003346 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003347 send_rs = send_mld &&
3348 ipv6_accept_ra(ifp->idev) &&
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003349 ifp->idev->cnf.rtr_solicits > 0 &&
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003350 (dev->flags&IFF_LOOPBACK) == 0;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003351 read_unlock_bh(&ifp->idev->lock);
3352
Hannes Frederic Sowab173ee42013-06-27 00:07:01 +02003353 /* While dad is in progress mld report's source address is in6_addrany.
3354 * Resend with proper ll now.
3355 */
3356 if (send_mld)
3357 ipv6_mc_dad_complete(ifp->idev);
3358
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003359 if (send_rs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 /*
3361 * If a host as already performed a random delay
3362 * [...] as part of DAD [...] there is no need
3363 * to delay again before sending the first RS
3364 */
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003365 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003366 return;
Hannes Frederic Sowa1ec047e2013-06-27 00:06:56 +02003367 ndisc_send_rs(dev, &lladdr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003369 write_lock_bh(&ifp->idev->lock);
3370 spin_lock(&ifp->lock);
3371 ifp->idev->rs_probes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 ifp->idev->if_flags |= IF_RS_SENT;
Hannes Frederic Sowab7b1bfc2013-06-23 18:39:01 +02003373 addrconf_mod_rs_timer(ifp->idev,
3374 ifp->idev->cnf.rtr_solicit_interval);
3375 spin_unlock(&ifp->lock);
3376 write_unlock_bh(&ifp->idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 }
3378}
3379
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003380static void addrconf_dad_run(struct inet6_dev *idev)
3381{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003382 struct inet6_ifaddr *ifp;
3383
3384 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003385 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003386 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07003387 if (ifp->flags & IFA_F_TENTATIVE &&
3388 ifp->state == INET6_IFADDR_STATE_DAD)
3389 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00003390 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003391 }
3392 read_unlock_bh(&idev->lock);
3393}
3394
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395#ifdef CONFIG_PROC_FS
3396struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08003397 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003399 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400};
3401
Mihai Maruseac1d578302012-01-03 23:31:35 +00003402static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
3404 struct inet6_ifaddr *ifa = NULL;
3405 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003406 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00003407 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
Mihai Maruseac1d578302012-01-03 23:31:35 +00003409 /* initial bucket if pos is 0 */
3410 if (pos == 0) {
3411 state->bucket = 0;
3412 state->offset = 0;
3413 }
3414
3415 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08003416 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00003417 addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003418 if (!net_eq(dev_net(ifa->idev->dev), net))
3419 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003420 /* sync with offset */
3421 if (p < state->offset) {
3422 p++;
3423 continue;
3424 }
3425 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003426 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003427 }
3428
3429 /* prepare for next bucket */
3430 state->offset = 0;
3431 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00003433 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434}
3435
stephen hemmingerc2e21292010-03-17 20:31:10 +00003436static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3437 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438{
3439 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003440 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
Sasha Levinb67bfe02013-02-27 17:06:00 -08003442 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003443 if (!net_eq(dev_net(ifa->idev->dev), net))
3444 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003445 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003446 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003447 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00003448
3449 while (++state->bucket < IN6_ADDR_HSIZE) {
Mihai Maruseac1d578302012-01-03 23:31:35 +00003450 state->offset = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003451 hlist_for_each_entry_rcu_bh(ifa,
stephen hemmingerc2e21292010-03-17 20:31:10 +00003452 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003453 if (!net_eq(dev_net(ifa->idev->dev), net))
3454 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003455 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003456 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08003457 }
3458 }
3459
stephen hemmingerc2e21292010-03-17 20:31:10 +00003460 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461}
3462
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003464 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465{
stephen hemminger5c578ae2010-03-17 20:31:11 +00003466 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00003467 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468}
3469
3470static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3471{
3472 struct inet6_ifaddr *ifa;
3473
3474 ifa = if6_get_next(seq, v);
3475 ++*pos;
3476 return ifa;
3477}
3478
3479static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003480 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481{
stephen hemminger5c578ae2010-03-17 20:31:11 +00003482 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
3484
3485static int if6_seq_show(struct seq_file *seq, void *v)
3486{
3487 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Jiri Pirko971a3512013-12-10 13:56:29 +01003488 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07003489 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 ifp->idev->dev->ifindex,
3491 ifp->prefix_len,
3492 ifp->scope,
Jiri Pirko971a3512013-12-10 13:56:29 +01003493 (u8) ifp->flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 ifp->idev->dev->name);
3495 return 0;
3496}
3497
Philippe De Muyter56b3d972007-07-10 23:07:31 -07003498static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 .start = if6_seq_start,
3500 .next = if6_seq_next,
3501 .show = if6_seq_show,
3502 .stop = if6_seq_stop,
3503};
3504
3505static int if6_seq_open(struct inode *inode, struct file *file)
3506{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003507 return seq_open_net(inode, file, &if6_seq_ops,
3508 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509}
3510
Arjan van de Ven9a321442007-02-12 00:55:35 -08003511static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 .owner = THIS_MODULE,
3513 .open = if6_seq_open,
3514 .read = seq_read,
3515 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08003516 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517};
3518
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003519static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520{
Gao fengd4beaa62013-02-18 01:34:54 +00003521 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 return -ENOMEM;
3523 return 0;
3524}
3525
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003526static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08003527{
Gao fengece31ff2013-02-18 01:34:56 +00003528 remove_proc_entry("if_inet6", net->proc_net);
Daniel Lezcano3c400902008-01-10 22:42:49 -08003529}
3530
3531static struct pernet_operations if6_proc_net_ops = {
3532 .init = if6_proc_net_init,
3533 .exit = if6_proc_net_exit,
3534};
3535
3536int __init if6_proc_init(void)
3537{
3538 return register_pernet_subsys(&if6_proc_net_ops);
3539}
3540
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541void if6_proc_exit(void)
3542{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003543 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544}
3545#endif /* CONFIG_PROC_FS */
3546
Amerigo Wang07a93622012-10-29 16:23:10 +00003547#if IS_ENABLED(CONFIG_IPV6_MIP6)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003548/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003549int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003550{
3551 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00003552 struct inet6_ifaddr *ifp = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00003553 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00003554
stephen hemminger5c578ae2010-03-17 20:31:11 +00003555 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08003556 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09003557 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08003558 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09003559 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003560 (ifp->flags & IFA_F_HOMEADDRESS)) {
3561 ret = 1;
3562 break;
3563 }
3564 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00003565 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003566 return ret;
3567}
3568#endif
3569
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570/*
3571 * Periodic address status verification
3572 */
3573
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003574static void addrconf_verify_rtnl(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003576 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 int i;
3579
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003580 ASSERT_RTNL();
3581
stephen hemminger5c578ae2010-03-17 20:31:11 +00003582 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003584 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003586 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003588 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589restart:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003590 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592
Yasushi Asanofad8da32013-12-31 12:04:19 +09003593 /* When setting preferred_lft to a value not zero or
3594 * infinity, while valid_lft is infinity
3595 * IFA_F_PERMANENT has a non-infinity life time.
3596 */
3597 if ((ifp->flags & IFA_F_PERMANENT) &&
3598 (ifp->prefered_lft == INFINITY_LIFE_TIME))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 continue;
3600
3601 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003602 /* We try to batch several events at once. */
3603 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09003605 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
3606 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 spin_unlock(&ifp->lock);
3608 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 ipv6_del_addr(ifp);
3610 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09003611 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
3612 spin_unlock(&ifp->lock);
3613 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00003615 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 int deprecate = 0;
3617
3618 if (!(ifp->flags&IFA_F_DEPRECATED)) {
3619 deprecate = 1;
3620 ifp->flags |= IFA_F_DEPRECATED;
3621 }
3622
Yasushi Asanofad8da32013-12-31 12:04:19 +09003623 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
3624 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 next = ifp->tstamp + ifp->valid_lft * HZ;
3626
3627 spin_unlock(&ifp->lock);
3628
3629 if (deprecate) {
3630 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
3632 ipv6_ifa_notify(0, ifp);
3633 in6_ifa_put(ifp);
3634 goto restart;
3635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3637 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00003638 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
3639 ifp->idev->cnf.dad_transmits *
Jiri Pirko1f9248e52013-12-07 19:26:53 +01003640 NEIGH_VAR(ifp->idev->nd_parms, RETRANS_TIME) / HZ;
stephen hemminger88949cf2010-03-17 20:31:17 +00003641
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 if (age >= ifp->prefered_lft - regen_advance) {
3643 struct inet6_ifaddr *ifpub = ifp->ifpub;
3644 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3645 next = ifp->tstamp + ifp->prefered_lft * HZ;
3646 if (!ifp->regen_count && ifpub) {
3647 ifp->regen_count++;
3648 in6_ifa_hold(ifp);
3649 in6_ifa_hold(ifpub);
3650 spin_unlock(&ifp->lock);
stephen hemminger5c578ae2010-03-17 20:31:11 +00003651
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08003652 spin_lock(&ifpub->lock);
3653 ifpub->regen_count = 0;
3654 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 ipv6_create_tempaddr(ifpub, ifp);
3656 in6_ifa_put(ifpub);
3657 in6_ifa_put(ifp);
3658 goto restart;
3659 }
3660 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3661 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3662 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 } else {
3664 /* ifp->prefered_lft <= ifp->valid_lft */
3665 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3666 next = ifp->tstamp + ifp->prefered_lft * HZ;
3667 spin_unlock(&ifp->lock);
3668 }
3669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 }
3671
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003672 next_sec = round_jiffies_up(next);
3673 next_sched = next;
3674
3675 /* If rounded timeout is accurate enough, accept it. */
3676 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3677 next_sched = next_sec;
3678
3679 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3680 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3681 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3682
dingtianhongba3542e2013-08-17 10:27:04 +08003683 ADBG(KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3684 now, next, next_sec, next_sched);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003685 mod_delayed_work(addrconf_wq, &addr_chk_work, next_sched - now);
stephen hemminger5c578ae2010-03-17 20:31:11 +00003686 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687}
3688
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003689static void addrconf_verify_work(struct work_struct *w)
3690{
3691 rtnl_lock();
3692 addrconf_verify_rtnl();
3693 rtnl_unlock();
3694}
3695
3696static void addrconf_verify(void)
3697{
3698 mod_delayed_work(addrconf_wq, &addr_chk_work, 0);
3699}
3700
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00003701static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local,
3702 struct in6_addr **peer_pfx)
Thomas Graf461d8832006-09-18 00:09:49 -07003703{
3704 struct in6_addr *pfx = NULL;
3705
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00003706 *peer_pfx = NULL;
3707
Thomas Graf461d8832006-09-18 00:09:49 -07003708 if (addr)
3709 pfx = nla_data(addr);
3710
3711 if (local) {
3712 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00003713 *peer_pfx = pfx;
3714 pfx = nla_data(local);
Thomas Graf461d8832006-09-18 00:09:49 -07003715 }
3716
3717 return pfx;
3718}
3719
Patrick McHardyef7c79e2007-06-05 12:38:30 -07003720static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07003721 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
3722 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
3723 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
Jiri Pirko479840f2013-12-06 09:45:21 +01003724 [IFA_FLAGS] = { .len = sizeof(u32) },
Thomas Graf461d8832006-09-18 00:09:49 -07003725};
3726
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727static int
Thomas Graf661d2962013-03-21 07:45:29 +00003728inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003730 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07003731 struct ifaddrmsg *ifm;
3732 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00003733 struct in6_addr *pfx, *peer_pfx;
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02003734 u32 ifa_flags;
Thomas Grafb933f712006-09-18 00:10:19 -07003735 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736
Thomas Grafb933f712006-09-18 00:10:19 -07003737 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3738 if (err < 0)
3739 return err;
3740
3741 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00003742 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 if (pfx == NULL)
3744 return -EINVAL;
3745
Heiner Kallweit6046d5b2014-04-20 21:29:36 +02003746 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
3747
3748 /* We ignore other flags so far. */
3749 ifa_flags &= IFA_F_MANAGETEMPADDR;
3750
3751 return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
3752 ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753}
3754
Jiri Pirko479840f2013-12-06 09:45:21 +01003755static int inet6_addr_modify(struct inet6_ifaddr *ifp, u32 ifa_flags,
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003756 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003757{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003758 u32 flags;
3759 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003760 unsigned long timeout;
Jiri Pirko53bd6742013-12-06 09:45:22 +01003761 bool was_managetempaddr;
Thomas Haller5b84efe2014-01-15 15:36:59 +01003762 bool had_prefixroute;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003763
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003764 ASSERT_RTNL();
3765
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003766 if (!valid_lft || (prefered_lft > valid_lft))
3767 return -EINVAL;
3768
Jiri Pirko53bd6742013-12-06 09:45:22 +01003769 if (ifa_flags & IFA_F_MANAGETEMPADDR &&
3770 (ifp->flags & IFA_F_TEMPORARY || ifp->prefix_len != 64))
3771 return -EINVAL;
3772
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003773 timeout = addrconf_timeout_fixup(valid_lft, HZ);
3774 if (addrconf_finite_timeout(timeout)) {
3775 expires = jiffies_to_clock_t(timeout * HZ);
3776 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003777 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003778 } else {
3779 expires = 0;
3780 flags = 0;
3781 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003782 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003783
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003784 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
3785 if (addrconf_finite_timeout(timeout)) {
3786 if (timeout == 0)
3787 ifa_flags |= IFA_F_DEPRECATED;
3788 prefered_lft = timeout;
3789 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003790
3791 spin_lock_bh(&ifp->lock);
Jiri Pirko53bd6742013-12-06 09:45:22 +01003792 was_managetempaddr = ifp->flags & IFA_F_MANAGETEMPADDR;
Thomas Haller5b84efe2014-01-15 15:36:59 +01003793 had_prefixroute = ifp->flags & IFA_F_PERMANENT &&
3794 !(ifp->flags & IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01003795 ifp->flags &= ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD |
Thomas Haller761aac72014-01-15 15:36:58 +01003796 IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
3797 IFA_F_NOPREFIXROUTE);
Jiri Pirko53bd6742013-12-06 09:45:22 +01003798 ifp->flags |= ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003799 ifp->tstamp = jiffies;
3800 ifp->valid_lft = valid_lft;
3801 ifp->prefered_lft = prefered_lft;
3802
3803 spin_unlock_bh(&ifp->lock);
3804 if (!(ifp->flags&IFA_F_TENTATIVE))
3805 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003806
Thomas Haller761aac72014-01-15 15:36:58 +01003807 if (!(ifa_flags & IFA_F_NOPREFIXROUTE)) {
3808 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
3809 expires, flags);
Thomas Haller5b84efe2014-01-15 15:36:59 +01003810 } else if (had_prefixroute) {
3811 enum cleanup_prefix_rt_t action;
3812 unsigned long rt_expires;
3813
3814 write_lock_bh(&ifp->idev->lock);
3815 action = check_cleanup_prefix_route(ifp, &rt_expires);
3816 write_unlock_bh(&ifp->idev->lock);
3817
3818 if (action != CLEANUP_PREFIX_RT_NOP) {
3819 cleanup_prefix_route(ifp, rt_expires,
3820 action == CLEANUP_PREFIX_RT_DEL);
3821 }
Thomas Haller761aac72014-01-15 15:36:58 +01003822 }
Jiri Pirko53bd6742013-12-06 09:45:22 +01003823
3824 if (was_managetempaddr || ifp->flags & IFA_F_MANAGETEMPADDR) {
3825 if (was_managetempaddr && !(ifp->flags & IFA_F_MANAGETEMPADDR))
3826 valid_lft = prefered_lft = 0;
3827 manage_tempaddrs(ifp->idev, ifp, valid_lft, prefered_lft,
3828 !was_managetempaddr, jiffies);
3829 }
3830
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01003831 addrconf_verify_rtnl();
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003832
3833 return 0;
3834}
3835
3836static int
Thomas Graf661d2962013-03-21 07:45:29 +00003837inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003839 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07003840 struct ifaddrmsg *ifm;
3841 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00003842 struct in6_addr *pfx, *peer_pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07003843 struct inet6_ifaddr *ifa;
3844 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003845 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
Jiri Pirko479840f2013-12-06 09:45:21 +01003846 u32 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07003847 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
Thomas Graf461d8832006-09-18 00:09:49 -07003849 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3850 if (err < 0)
3851 return err;
3852
3853 ifm = nlmsg_data(nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00003854 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 if (pfx == NULL)
3856 return -EINVAL;
3857
Thomas Graf461d8832006-09-18 00:09:49 -07003858 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003859 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07003860
3861 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003862 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07003863 preferred_lft = ci->ifa_prefered;
3864 } else {
3865 preferred_lft = INFINITY_LIFE_TIME;
3866 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003867 }
3868
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003869 dev = __dev_get_by_index(net, ifm->ifa_index);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003870 if (dev == NULL)
3871 return -ENODEV;
3872
Jiri Pirko479840f2013-12-06 09:45:21 +01003873 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
3874
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003875 /* We ignore other flags so far. */
Thomas Haller761aac72014-01-15 15:36:58 +01003876 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
3877 IFA_F_NOPREFIXROUTE;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003878
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08003879 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003880 if (ifa == NULL) {
3881 /*
3882 * It would be best to check for !NLM_F_CREATE here but
stephen hemmingerdb9c7c32014-01-12 11:26:32 -08003883 * userspace already relies on not having to provide this.
Thomas Graf7198f8c2006-09-18 00:13:46 -07003884 */
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00003885 return inet6_addr_add(net, ifm->ifa_index, pfx, peer_pfx,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003886 ifm->ifa_prefixlen, ifa_flags,
3887 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003888 }
3889
Thomas Graf7198f8c2006-09-18 00:13:46 -07003890 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3891 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3892 err = -EEXIST;
3893 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003894 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003895
3896 in6_ifa_put(ifa);
3897
3898 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899}
3900
Jiri Pirko479840f2013-12-06 09:45:21 +01003901static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u32 flags,
Thomas Graf101bb222006-09-18 00:11:52 -07003902 u8 scope, int ifindex)
3903{
3904 struct ifaddrmsg *ifm;
3905
3906 ifm = nlmsg_data(nlh);
3907 ifm->ifa_family = AF_INET6;
3908 ifm->ifa_prefixlen = prefixlen;
3909 ifm->ifa_flags = flags;
3910 ifm->ifa_scope = scope;
3911 ifm->ifa_index = ifindex;
3912}
3913
Thomas Graf85486af2006-09-18 00:11:24 -07003914static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3915 unsigned long tstamp, u32 preferred, u32 valid)
3916{
3917 struct ifa_cacheinfo ci;
3918
Thomas Graf18a31e12010-11-17 04:12:02 +00003919 ci.cstamp = cstamp_delta(cstamp);
3920 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07003921 ci.ifa_prefered = preferred;
3922 ci.ifa_valid = valid;
3923
3924 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3925}
3926
Thomas Graf101bb222006-09-18 00:11:52 -07003927static inline int rt_scope(int ifa_scope)
3928{
3929 if (ifa_scope & IFA_HOST)
3930 return RT_SCOPE_HOST;
3931 else if (ifa_scope & IFA_LINK)
3932 return RT_SCOPE_LINK;
3933 else if (ifa_scope & IFA_SITE)
3934 return RT_SCOPE_SITE;
3935 else
3936 return RT_SCOPE_UNIVERSE;
3937}
3938
Thomas Graf0ab68032006-09-18 00:12:35 -07003939static inline int inet6_ifaddr_msgsize(void)
3940{
Thomas Graf339bf982006-11-10 14:10:15 -08003941 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00003942 + nla_total_size(16) /* IFA_LOCAL */
Thomas Graf339bf982006-11-10 14:10:15 -08003943 + nla_total_size(16) /* IFA_ADDRESS */
Jiri Pirko479840f2013-12-06 09:45:21 +01003944 + nla_total_size(sizeof(struct ifa_cacheinfo))
3945 + nla_total_size(4) /* IFA_FLAGS */;
Thomas Graf0ab68032006-09-18 00:12:35 -07003946}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003947
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003949 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07003952 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953
Eric W. Biederman15e47302012-09-07 20:12:54 +00003954 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Thomas Graf0ab68032006-09-18 00:12:35 -07003955 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003956 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003957
Thomas Graf101bb222006-09-18 00:11:52 -07003958 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3959 ifa->idev->dev->ifindex);
3960
Yasushi Asanofad8da32013-12-31 12:04:19 +09003961 if (!((ifa->flags&IFA_F_PERMANENT) &&
3962 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
Thomas Graf85486af2006-09-18 00:11:24 -07003963 preferred = ifa->prefered_lft;
3964 valid = ifa->valid_lft;
3965 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00003967 if (preferred > tval)
3968 preferred -= tval;
3969 else
3970 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00003971 if (valid != INFINITY_LIFE_TIME) {
3972 if (valid > tval)
3973 valid -= tval;
3974 else
3975 valid = 0;
3976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 }
3978 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07003979 preferred = INFINITY_LIFE_TIME;
3980 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 }
Thomas Graf85486af2006-09-18 00:11:24 -07003982
Cong Wang7996c792013-05-22 05:41:06 +00003983 if (!ipv6_addr_any(&ifa->peer_addr)) {
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00003984 if (nla_put(skb, IFA_LOCAL, 16, &ifa->addr) < 0 ||
3985 nla_put(skb, IFA_ADDRESS, 16, &ifa->peer_addr) < 0)
3986 goto error;
3987 } else
3988 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0)
3989 goto error;
3990
3991 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
3992 goto error;
Thomas Graf85486af2006-09-18 00:11:24 -07003993
Jiri Pirko479840f2013-12-06 09:45:21 +01003994 if (nla_put_u32(skb, IFA_FLAGS, ifa->flags) < 0)
3995 goto error;
3996
Thomas Graf0ab68032006-09-18 00:12:35 -07003997 return nlmsg_end(skb, nlh);
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00003998
3999error:
4000 nlmsg_cancel(skb, nlh);
4001 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002}
4003
4004static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004005 u32 portid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004008 u8 scope = RT_SCOPE_UNIVERSE;
4009 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010
Thomas Graf101bb222006-09-18 00:11:52 -07004011 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
4012 scope = RT_SCOPE_SITE;
4013
Eric W. Biederman15e47302012-09-07 20:12:54 +00004014 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Thomas Graf0ab68032006-09-18 00:12:35 -07004015 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08004016 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004017
Thomas Graf101bb222006-09-18 00:11:52 -07004018 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07004019 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
4020 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004021 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4022 nlmsg_cancel(skb, nlh);
4023 return -EMSGSIZE;
4024 }
Thomas Graf85486af2006-09-18 00:11:24 -07004025
Thomas Graf0ab68032006-09-18 00:12:35 -07004026 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027}
4028
4029static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004030 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07004033 u8 scope = RT_SCOPE_UNIVERSE;
4034 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
Thomas Graf101bb222006-09-18 00:11:52 -07004036 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
4037 scope = RT_SCOPE_SITE;
4038
Eric W. Biederman15e47302012-09-07 20:12:54 +00004039 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Thomas Graf0ab68032006-09-18 00:12:35 -07004040 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08004041 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07004042
Thomas Graf101bb222006-09-18 00:11:52 -07004043 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07004044 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
4045 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08004046 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4047 nlmsg_cancel(skb, nlh);
4048 return -EMSGSIZE;
4049 }
Thomas Graf85486af2006-09-18 00:11:24 -07004050
Thomas Graf0ab68032006-09-18 00:12:35 -07004051 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052}
4053
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004054enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 UNICAST_ADDR,
4056 MULTICAST_ADDR,
4057 ANYCAST_ADDR,
4058};
4059
Eric Dumazet234b27c2009-11-12 04:11:50 +00004060/* called with rcu_read_lock() */
4061static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
4062 struct netlink_callback *cb, enum addr_type_t type,
4063 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 struct ifmcaddr6 *ifmca;
4066 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004067 int err = 1;
4068 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069
Eric Dumazet234b27c2009-11-12 04:11:50 +00004070 read_lock_bh(&idev->lock);
4071 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00004072 case UNICAST_ADDR: {
4073 struct inet6_ifaddr *ifa;
4074
Eric Dumazet234b27c2009-11-12 04:11:50 +00004075 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00004076 list_for_each_entry(ifa, &idev->addr_list, if_list) {
4077 if (++ip_idx < s_ip_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004078 continue;
4079 err = inet6_fill_ifaddr(skb, ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004080 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004081 cb->nlh->nlmsg_seq,
4082 RTM_NEWADDR,
4083 NLM_F_MULTI);
4084 if (err <= 0)
4085 break;
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004086 nl_dump_check_consistent(cb, nlmsg_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004088 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00004089 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004090 case MULTICAST_ADDR:
4091 /* multicast address */
4092 for (ifmca = idev->mc_list; ifmca;
4093 ifmca = ifmca->next, ip_idx++) {
4094 if (ip_idx < s_ip_idx)
4095 continue;
4096 err = inet6_fill_ifmcaddr(skb, ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004097 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004098 cb->nlh->nlmsg_seq,
4099 RTM_GETMULTICAST,
4100 NLM_F_MULTI);
4101 if (err <= 0)
4102 break;
4103 }
4104 break;
4105 case ANYCAST_ADDR:
4106 /* anycast address */
4107 for (ifaca = idev->ac_list; ifaca;
4108 ifaca = ifaca->aca_next, ip_idx++) {
4109 if (ip_idx < s_ip_idx)
4110 continue;
4111 err = inet6_fill_ifacaddr(skb, ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004112 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00004113 cb->nlh->nlmsg_seq,
4114 RTM_GETANYCAST,
4115 NLM_F_MULTI);
4116 if (err <= 0)
4117 break;
4118 }
4119 break;
4120 default:
4121 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00004123 read_unlock_bh(&idev->lock);
4124 *p_ip_idx = ip_idx;
4125 return err;
4126}
4127
4128static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
4129 enum addr_type_t type)
4130{
4131 struct net *net = sock_net(skb->sk);
4132 int h, s_h;
4133 int idx, ip_idx;
4134 int s_idx, s_ip_idx;
4135 struct net_device *dev;
4136 struct inet6_dev *idev;
4137 struct hlist_head *head;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004138
4139 s_h = cb->args[0];
4140 s_idx = idx = cb->args[1];
4141 s_ip_idx = ip_idx = cb->args[2];
4142
4143 rcu_read_lock();
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004144 cb->seq = atomic_read(&net->ipv6.dev_addr_genid) ^ net->dev_base_seq;
Eric Dumazet234b27c2009-11-12 04:11:50 +00004145 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4146 idx = 0;
4147 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08004148 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet234b27c2009-11-12 04:11:50 +00004149 if (idx < s_idx)
4150 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07004151 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004152 s_ip_idx = 0;
4153 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004154 idev = __in6_dev_get(dev);
4155 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00004156 goto cont;
4157
4158 if (in6_dump_addrs(idev, skb, cb, type,
4159 s_ip_idx, &ip_idx) <= 0)
4160 goto done;
4161cont:
4162 idx++;
4163 }
4164 }
4165done:
4166 rcu_read_unlock();
4167 cb->args[0] = h;
4168 cb->args[1] = idx;
4169 cb->args[2] = ip_idx;
4170
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 return skb->len;
4172}
4173
4174static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
4175{
4176 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004177
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 return inet6_dump_addr(skb, cb, type);
4179}
4180
4181static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
4182{
4183 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004184
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 return inet6_dump_addr(skb, cb, type);
4186}
4187
4188
4189static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
4190{
4191 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11004192
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 return inet6_dump_addr(skb, cb, type);
4194}
4195
Thomas Graf661d2962013-03-21 07:45:29 +00004196static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004197{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004198 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004199 struct ifaddrmsg *ifm;
4200 struct nlattr *tb[IFA_MAX+1];
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004201 struct in6_addr *addr = NULL, *peer;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004202 struct net_device *dev = NULL;
4203 struct inet6_ifaddr *ifa;
4204 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004205 int err;
4206
Thomas Graf1b29fc22006-09-18 00:10:50 -07004207 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
4208 if (err < 0)
4209 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004210
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004211 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004212 if (addr == NULL) {
4213 err = -EINVAL;
4214 goto errout;
4215 }
4216
4217 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004218 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004219 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004220
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004221 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
4222 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004223 err = -EADDRNOTAVAIL;
4224 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004225 }
4226
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004227 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
4228 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07004229 err = -ENOBUFS;
4230 goto errout_ifa;
4231 }
4232
Eric W. Biederman15e47302012-09-07 20:12:54 +00004233 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004234 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004235 if (err < 0) {
4236 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4237 WARN_ON(err == -EMSGSIZE);
4238 kfree_skb(skb);
4239 goto errout_ifa;
4240 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00004241 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004242errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004243 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07004244errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004245 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09004246}
4247
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4249{
4250 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004251 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07004252 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
Thomas Graf0ab68032006-09-18 00:12:35 -07004254 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07004255 if (skb == NULL)
4256 goto errout;
4257
4258 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004259 if (err < 0) {
4260 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4261 WARN_ON(err == -EMSGSIZE);
4262 kfree_skb(skb);
4263 goto errout;
4264 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004265 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4266 return;
Thomas Graf5d620262006-08-15 00:35:02 -07004267errout:
4268 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004269 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270}
4271
Dave Jonesb6f99a22007-03-22 12:27:49 -07004272static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 __s32 *array, int bytes)
4274{
Thomas Graf04561c12006-11-14 19:53:58 -08004275 BUG_ON(bytes < (DEVCONF_MAX * 4));
4276
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 memset(array, 0, bytes);
4278 array[DEVCONF_FORWARDING] = cnf->forwarding;
4279 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4280 array[DEVCONF_MTU6] = cnf->mtu6;
4281 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4282 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4283 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4284 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4285 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00004286 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4287 jiffies_to_msecs(cnf->rtr_solicit_interval);
4288 array[DEVCONF_RTR_SOLICIT_DELAY] =
4289 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02004291 array[DEVCONF_MLDV1_UNSOLICITED_REPORT_INTERVAL] =
4292 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4293 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4294 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4296 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4297 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4298 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4299 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004301 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004302 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004303#ifdef CONFIG_IPV6_ROUTER_PREF
4304 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00004305 array[DEVCONF_RTR_PROBE_INTERVAL] =
4306 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08004307#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004308 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4309#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004310#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004311 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004312 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07004313#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4314 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
4315#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004316#ifdef CONFIG_IPV6_MROUTE
4317 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4318#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09004319 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09004320 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00004321 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00004322 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02004323 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324}
4325
Thomas Grafb382b192010-11-16 04:33:57 +00004326static inline size_t inet6_ifla6_size(void)
4327{
4328 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4329 + nla_total_size(sizeof(struct ifla_cacheinfo))
4330 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4331 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004332 + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
4333 + nla_total_size(sizeof(struct in6_addr)); /* IFLA_INET6_TOKEN */
Thomas Grafb382b192010-11-16 04:33:57 +00004334}
4335
Thomas Graf339bf982006-11-10 14:10:15 -08004336static inline size_t inet6_if_nlmsg_size(void)
4337{
4338 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4339 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4340 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4341 + nla_total_size(4) /* IFLA_MTU */
4342 + nla_total_size(4) /* IFLA_LINK */
Thomas Grafb382b192010-11-16 04:33:57 +00004343 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08004344}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004345
Eric Dumazetbe281e52011-05-19 01:14:23 +00004346static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Tejun Heo7d720c32010-02-16 15:20:26 +00004347 int items, int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004348{
4349 int i;
4350 int pad = bytes - sizeof(u64) * items;
4351 BUG_ON(pad < 0);
4352
4353 /* Use put_unaligned() because stats may not be aligned for u64. */
4354 put_unaligned(items, &stats[0]);
4355 for (i = 1; i < items; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00004356 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004357
4358 memset(&stats[items], 0, pad);
4359}
4360
WANG Cong698365f2014-05-05 15:55:55 -07004361static inline void __snmp6_fill_stats64(u64 *stats, void __percpu *mib,
Eric Dumazet4ce3c182010-06-30 13:31:19 -07004362 int items, int bytes, size_t syncpoff)
4363{
4364 int i;
4365 int pad = bytes - sizeof(u64) * items;
4366 BUG_ON(pad < 0);
4367
4368 /* Use put_unaligned() because stats may not be aligned for u64. */
4369 put_unaligned(items, &stats[0]);
4370 for (i = 1; i < items; i++)
4371 put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
4372
4373 memset(&stats[items], 0, pad);
4374}
4375
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004376static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
4377 int bytes)
4378{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004379 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004380 case IFLA_INET6_STATS:
WANG Cong698365f2014-05-05 15:55:55 -07004381 __snmp6_fill_stats64(stats, idev->stats.ipv6,
Eric Dumazet4ce3c182010-06-30 13:31:19 -07004382 IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004383 break;
4384 case IFLA_INET6_ICMP6STATS:
Eric Dumazetbe281e52011-05-19 01:14:23 +00004385 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004386 break;
4387 }
4388}
4389
Thomas Grafb382b192010-11-16 04:33:57 +00004390static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004392 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08004393 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394
David S. Millerc78679e2012-04-01 20:27:33 -04004395 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
4396 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08004398 ci.tstamp = cstamp_delta(idev->tstamp);
4399 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
Jiri Pirko1f9248e52013-12-07 19:26:53 +01004400 ci.retrans_time = jiffies_to_msecs(NEIGH_VAR(idev->nd_parms, RETRANS_TIME));
David S. Millerc78679e2012-04-01 20:27:33 -04004401 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
4402 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004403 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
4404 if (nla == NULL)
Thomas Graf04561c12006-11-14 19:53:58 -08004405 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004406 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004408 /* XXX - MC not implemented */
4409
4410 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
4411 if (nla == NULL)
4412 goto nla_put_failure;
4413 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4414
4415 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4416 if (nla == NULL)
4417 goto nla_put_failure;
4418 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004420 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
4421 if (nla == NULL)
4422 goto nla_put_failure;
4423 read_lock_bh(&idev->lock);
4424 memcpy(nla_data(nla), idev->token.s6_addr, nla_len(nla));
4425 read_unlock_bh(&idev->lock);
4426
Thomas Grafb382b192010-11-16 04:33:57 +00004427 return 0;
4428
4429nla_put_failure:
4430 return -EMSGSIZE;
4431}
4432
4433static size_t inet6_get_link_af_size(const struct net_device *dev)
4434{
4435 if (!__in6_dev_get(dev))
4436 return 0;
4437
4438 return inet6_ifla6_size();
4439}
4440
4441static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4442{
4443 struct inet6_dev *idev = __in6_dev_get(dev);
4444
4445 if (!idev)
4446 return -ENODATA;
4447
4448 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4449 return -EMSGSIZE;
4450
4451 return 0;
4452}
4453
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004454static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
4455{
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004456 struct inet6_ifaddr *ifp;
4457 struct net_device *dev = idev->dev;
Daniel Borkmannfc403832013-04-09 03:47:15 +00004458 bool update_rs = false;
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02004459 struct in6_addr ll_addr;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004460
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004461 ASSERT_RTNL();
4462
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004463 if (token == NULL)
4464 return -EINVAL;
4465 if (ipv6_addr_any(token))
4466 return -EINVAL;
4467 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP))
4468 return -EINVAL;
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004469 if (!ipv6_accept_ra(idev))
4470 return -EINVAL;
4471 if (idev->cnf.rtr_solicits <= 0)
4472 return -EINVAL;
4473
4474 write_lock_bh(&idev->lock);
4475
4476 BUILD_BUG_ON(sizeof(token->s6_addr) != 16);
4477 memcpy(idev->token.s6_addr + 8, token->s6_addr + 8, 8);
4478
4479 write_unlock_bh(&idev->lock);
4480
Hannes Frederic Sowadc848292013-06-24 21:42:40 +02004481 if (!idev->dead && (idev->if_flags & IF_READY) &&
4482 !ipv6_get_lladdr(dev, &ll_addr, IFA_F_TENTATIVE |
4483 IFA_F_OPTIMISTIC)) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00004484
4485 /* If we're not ready, then normal ifup will take care
4486 * of this. Otherwise, we need to request our rs here.
4487 */
4488 ndisc_send_rs(dev, &ll_addr, &in6addr_linklocal_allrouters);
4489 update_rs = true;
4490 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004491
4492 write_lock_bh(&idev->lock);
Daniel Borkmannfc403832013-04-09 03:47:15 +00004493
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02004494 if (update_rs) {
Daniel Borkmannfc403832013-04-09 03:47:15 +00004495 idev->if_flags |= IF_RS_SENT;
Hannes Frederic Sowa77ecaac2013-06-26 03:41:49 +02004496 idev->rs_probes = 1;
4497 addrconf_mod_rs_timer(idev, idev->cnf.rtr_solicit_interval);
4498 }
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004499
4500 /* Well, that's kinda nasty ... */
4501 list_for_each_entry(ifp, &idev->addr_list, if_list) {
4502 spin_lock(&ifp->lock);
Daniel Borkmann617fe292013-04-09 03:47:16 +00004503 if (ifp->tokenized) {
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004504 ifp->valid_lft = 0;
4505 ifp->prefered_lft = 0;
4506 }
4507 spin_unlock(&ifp->lock);
4508 }
4509
4510 write_unlock_bh(&idev->lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004511 addrconf_verify_rtnl();
Daniel Borkmannf53adae2013-04-08 04:01:30 +00004512 return 0;
4513}
4514
4515static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
4516{
4517 int err = -EINVAL;
4518 struct inet6_dev *idev = __in6_dev_get(dev);
4519 struct nlattr *tb[IFLA_INET6_MAX + 1];
4520
4521 if (!idev)
4522 return -EAFNOSUPPORT;
4523
4524 if (nla_parse_nested(tb, IFLA_INET6_MAX, nla, NULL) < 0)
4525 BUG();
4526
4527 if (tb[IFLA_INET6_TOKEN])
4528 err = inet6_set_iftoken(idev, nla_data(tb[IFLA_INET6_TOKEN]));
4529
4530 return err;
4531}
4532
Thomas Grafb382b192010-11-16 04:33:57 +00004533static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004534 u32 portid, u32 seq, int event, unsigned int flags)
Thomas Grafb382b192010-11-16 04:33:57 +00004535{
4536 struct net_device *dev = idev->dev;
4537 struct ifinfomsg *hdr;
4538 struct nlmsghdr *nlh;
4539 void *protoinfo;
4540
Eric W. Biederman15e47302012-09-07 20:12:54 +00004541 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
Thomas Grafb382b192010-11-16 04:33:57 +00004542 if (nlh == NULL)
4543 return -EMSGSIZE;
4544
4545 hdr = nlmsg_data(nlh);
4546 hdr->ifi_family = AF_INET6;
4547 hdr->__ifi_pad = 0;
4548 hdr->ifi_type = dev->type;
4549 hdr->ifi_index = dev->ifindex;
4550 hdr->ifi_flags = dev_get_flags(dev);
4551 hdr->ifi_change = 0;
4552
David S. Millerc78679e2012-04-01 20:27:33 -04004553 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4554 (dev->addr_len &&
4555 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4556 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4557 (dev->ifindex != dev->iflink &&
4558 nla_put_u32(skb, IFLA_LINK, dev->iflink)))
4559 goto nla_put_failure;
Thomas Grafb382b192010-11-16 04:33:57 +00004560 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4561 if (protoinfo == NULL)
4562 goto nla_put_failure;
4563
4564 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4565 goto nla_put_failure;
4566
Thomas Graf04561c12006-11-14 19:53:58 -08004567 nla_nest_end(skb, protoinfo);
4568 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
Thomas Graf04561c12006-11-14 19:53:58 -08004570nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004571 nlmsg_cancel(skb, nlh);
4572 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573}
4574
4575static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
4576{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004577 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00004578 int h, s_h;
David S. Miller434a8a582009-11-11 18:53:00 -08004579 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 struct net_device *dev;
4581 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00004582 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
Eric Dumazet84d26972009-11-09 12:11:28 +00004584 s_h = cb->args[0];
4585 s_idx = cb->args[1];
4586
4587 rcu_read_lock();
4588 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4589 idx = 0;
4590 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08004591 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet84d26972009-11-09 12:11:28 +00004592 if (idx < s_idx)
4593 goto cont;
4594 idev = __in6_dev_get(dev);
4595 if (!idev)
4596 goto cont;
4597 if (inet6_fill_ifinfo(skb, idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004598 NETLINK_CB(cb->skb).portid,
Eric Dumazet84d26972009-11-09 12:11:28 +00004599 cb->nlh->nlmsg_seq,
4600 RTM_NEWLINK, NLM_F_MULTI) <= 0)
4601 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07004602cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00004603 idx++;
4604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 }
Eric Dumazet84d26972009-11-09 12:11:28 +00004606out:
4607 rcu_read_unlock();
4608 cb->args[1] = idx;
4609 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610
4611 return skb->len;
4612}
4613
4614void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
4615{
4616 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004617 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004618 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004619
Thomas Graf339bf982006-11-10 14:10:15 -08004620 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004621 if (skb == NULL)
4622 goto errout;
4623
4624 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004625 if (err < 0) {
4626 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
4627 WARN_ON(err == -EMSGSIZE);
4628 kfree_skb(skb);
4629 goto errout;
4630 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00004631 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004632 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004633errout:
4634 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00004635 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636}
4637
Thomas Graf339bf982006-11-10 14:10:15 -08004638static inline size_t inet6_prefix_nlmsg_size(void)
4639{
4640 return NLMSG_ALIGN(sizeof(struct prefixmsg))
4641 + nla_total_size(sizeof(struct in6_addr))
4642 + nla_total_size(sizeof(struct prefix_cacheinfo));
4643}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004644
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004646 struct prefix_info *pinfo, u32 portid, u32 seq,
Thomas Graf6051e2f2006-11-14 19:54:19 -08004647 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648{
Thomas Graf6051e2f2006-11-14 19:54:19 -08004649 struct prefixmsg *pmsg;
4650 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 struct prefix_cacheinfo ci;
4652
Eric W. Biederman15e47302012-09-07 20:12:54 +00004653 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
Thomas Graf6051e2f2006-11-14 19:54:19 -08004654 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08004655 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08004656
4657 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07004659 pmsg->prefix_pad1 = 0;
4660 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 pmsg->prefix_ifindex = idev->dev->ifindex;
4662 pmsg->prefix_len = pinfo->prefix_len;
4663 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07004664 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 pmsg->prefix_flags = 0;
4666 if (pinfo->onlink)
4667 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
4668 if (pinfo->autoconf)
4669 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
4670
David S. Millerc78679e2012-04-01 20:27:33 -04004671 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
4672 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 ci.preferred_time = ntohl(pinfo->prefered);
4674 ci.valid_time = ntohl(pinfo->valid);
David S. Millerc78679e2012-04-01 20:27:33 -04004675 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
4676 goto nla_put_failure;
Thomas Graf6051e2f2006-11-14 19:54:19 -08004677 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
Thomas Graf6051e2f2006-11-14 19:54:19 -08004679nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004680 nlmsg_cancel(skb, nlh);
4681 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682}
4683
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004684static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 struct prefix_info *pinfo)
4686{
4687 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004688 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004689 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
Thomas Graf339bf982006-11-10 14:10:15 -08004691 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004692 if (skb == NULL)
4693 goto errout;
4694
4695 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004696 if (err < 0) {
4697 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
4698 WARN_ON(err == -EMSGSIZE);
4699 kfree_skb(skb);
4700 goto errout;
4701 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004702 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
4703 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004704errout:
4705 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004706 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707}
4708
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4710{
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004711 struct net *net = dev_net(ifp->idev->dev);
4712
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01004713 if (event)
4714 ASSERT_RTNL();
4715
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
4717
4718 switch (event) {
4719 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07004720 /*
4721 * If the address was optimistic
4722 * we inserted the route at the start of
4723 * our DAD process, so we don't need
4724 * to do it again
4725 */
4726 if (!(ifp->rt->rt6i_node))
4727 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 if (ifp->idev->cnf.forwarding)
4729 addrconf_join_anycast(ifp);
Cong Wang7996c792013-05-22 05:41:06 +00004730 if (!ipv6_addr_any(&ifp->peer_addr))
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004731 addrconf_prefix_route(&ifp->peer_addr, 128,
4732 ifp->idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 break;
4734 case RTM_DELADDR:
4735 if (ifp->idev->cnf.forwarding)
4736 addrconf_leave_anycast(ifp);
4737 addrconf_leave_solict(ifp->idev, &ifp->addr);
Cong Wang7996c792013-05-22 05:41:06 +00004738 if (!ipv6_addr_any(&ifp->peer_addr)) {
Nicolas Dichtelcaeaba72013-05-16 22:32:00 +00004739 struct rt6_info *rt;
4740 struct net_device *dev = ifp->idev->dev;
4741
4742 rt = rt6_lookup(dev_net(dev), &ifp->peer_addr, NULL,
4743 dev->ifindex, 1);
4744 if (rt) {
4745 dst_hold(&rt->dst);
4746 if (ip6_del_rt(rt))
4747 dst_free(&rt->dst);
4748 }
4749 }
Changli Gaod8d1f302010-06-10 23:31:35 -07004750 dst_hold(&ifp->rt->dst);
stephen hemminger93fa1592010-04-12 05:41:31 +00004751
David S. Miller73a8bd72011-01-23 23:27:15 -08004752 if (ip6_del_rt(ifp->rt))
Changli Gaod8d1f302010-06-10 23:31:35 -07004753 dst_free(&ifp->rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 break;
4755 }
Nicolas Dichtel63998ac2013-03-22 06:28:43 +00004756 atomic_inc(&net->ipv6.dev_addr_genid);
fan.du439677d2013-08-01 17:44:44 +08004757 rt_genid_bump_ipv6(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758}
4759
4760static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4761{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07004762 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 if (likely(ifp->idev->dead == 0))
4764 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07004765 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766}
4767
4768#ifdef CONFIG_SYSCTL
4769
4770static
Joe Perchesfe2c6332013-06-11 23:04:25 -07004771int addrconf_sysctl_forward(struct ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 void __user *buffer, size_t *lenp, loff_t *ppos)
4773{
4774 int *valp = ctl->data;
4775 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004776 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07004777 struct ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 int ret;
4779
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004780 /*
4781 * ctl->data points to idev->cnf.forwarding, we should
4782 * not modify it until we get the rtnl lock.
4783 */
4784 lctl = *ctl;
4785 lctl.data = &val;
4786
4787 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08004789 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00004790 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00004791 if (ret)
4792 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004793 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794}
4795
Brian Haley56d417b2009-06-01 03:07:33 -07004796static void dev_disable_change(struct inet6_dev *idev)
4797{
Cong Wang75538c22013-05-29 11:30:50 +08004798 struct netdev_notifier_info info;
4799
Brian Haley56d417b2009-06-01 03:07:33 -07004800 if (!idev || !idev->dev)
4801 return;
4802
Cong Wang75538c22013-05-29 11:30:50 +08004803 netdev_notifier_info_init(&info, idev->dev);
Brian Haley56d417b2009-06-01 03:07:33 -07004804 if (idev->cnf.disable_ipv6)
Cong Wang75538c22013-05-29 11:30:50 +08004805 addrconf_notify(NULL, NETDEV_DOWN, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07004806 else
Cong Wang75538c22013-05-29 11:30:50 +08004807 addrconf_notify(NULL, NETDEV_UP, &info);
Brian Haley56d417b2009-06-01 03:07:33 -07004808}
4809
4810static void addrconf_disable_change(struct net *net, __s32 newf)
4811{
4812 struct net_device *dev;
4813 struct inet6_dev *idev;
4814
Eric Dumazetc6d14c82009-11-04 05:43:23 -08004815 rcu_read_lock();
4816 for_each_netdev_rcu(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07004817 idev = __in6_dev_get(dev);
4818 if (idev) {
4819 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
4820 idev->cnf.disable_ipv6 = newf;
4821 if (changed)
4822 dev_disable_change(idev);
4823 }
Brian Haley56d417b2009-06-01 03:07:33 -07004824 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -08004825 rcu_read_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07004826}
4827
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004828static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07004829{
4830 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004831 int old;
4832
4833 if (!rtnl_trylock())
4834 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07004835
4836 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004837 old = *p;
4838 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07004839
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004840 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
4841 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07004842 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004843 }
Brian Haley56d417b2009-06-01 03:07:33 -07004844
4845 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07004846 net->ipv6.devconf_dflt->disable_ipv6 = newf;
4847 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004848 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07004849 dev_disable_change((struct inet6_dev *)table->extra1);
4850
4851 rtnl_unlock();
4852 return 0;
4853}
4854
4855static
Joe Perchesfe2c6332013-06-11 23:04:25 -07004856int addrconf_sysctl_disable(struct ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07004857 void __user *buffer, size_t *lenp, loff_t *ppos)
4858{
4859 int *valp = ctl->data;
4860 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004861 loff_t pos = *ppos;
Joe Perchesfe2c6332013-06-11 23:04:25 -07004862 struct ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07004863 int ret;
4864
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004865 /*
4866 * ctl->data points to idev->cnf.disable_ipv6, we should
4867 * not modify it until we get the rtnl lock.
4868 */
4869 lctl = *ctl;
4870 lctl.data = &val;
4871
4872 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07004873
4874 if (write)
4875 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00004876 if (ret)
4877 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07004878 return ret;
4879}
4880
stephen hemmingerc92d5492013-12-17 22:37:14 -08004881static
4882int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
4883 void __user *buffer, size_t *lenp, loff_t *ppos)
4884{
4885 int *valp = ctl->data;
4886 int ret;
4887 int old, new;
4888
4889 old = *valp;
4890 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
4891 new = *valp;
4892
4893 if (write && old != new) {
4894 struct net *net = ctl->extra2;
4895
4896 if (!rtnl_trylock())
4897 return restart_syscall();
4898
4899 if (valp == &net->ipv6.devconf_dflt->proxy_ndp)
4900 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
4901 NETCONFA_IFINDEX_DEFAULT,
4902 net->ipv6.devconf_dflt);
4903 else if (valp == &net->ipv6.devconf_all->proxy_ndp)
4904 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
4905 NETCONFA_IFINDEX_ALL,
4906 net->ipv6.devconf_all);
4907 else {
4908 struct inet6_dev *idev = ctl->extra1;
4909
4910 inet6_netconf_notify_devconf(net, NETCONFA_PROXY_NEIGH,
4911 idev->dev->ifindex,
4912 &idev->cnf);
4913 }
4914 rtnl_unlock();
4915 }
4916
4917 return ret;
4918}
4919
4920
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921static struct addrconf_sysctl_table
4922{
4923 struct ctl_table_header *sysctl_header;
Joe Perchesfe2c6332013-06-11 23:04:25 -07004924 struct ctl_table addrconf_vars[DEVCONF_MAX+1];
Brian Haleyab32ea52006-09-22 14:15:41 -07004925} addrconf_sysctl __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 .sysctl_header = NULL,
4927 .addrconf_vars = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004928 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004929 .procname = "forwarding",
4930 .data = &ipv6_devconf.forwarding,
4931 .maxlen = sizeof(int),
4932 .mode = 0644,
4933 .proc_handler = addrconf_sysctl_forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 },
4935 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004936 .procname = "hop_limit",
4937 .data = &ipv6_devconf.hop_limit,
4938 .maxlen = sizeof(int),
4939 .mode = 0644,
4940 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 },
4942 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004943 .procname = "mtu",
4944 .data = &ipv6_devconf.mtu6,
4945 .maxlen = sizeof(int),
4946 .mode = 0644,
4947 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 },
4949 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004950 .procname = "accept_ra",
4951 .data = &ipv6_devconf.accept_ra,
4952 .maxlen = sizeof(int),
4953 .mode = 0644,
4954 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 },
4956 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004957 .procname = "accept_redirects",
4958 .data = &ipv6_devconf.accept_redirects,
4959 .maxlen = sizeof(int),
4960 .mode = 0644,
4961 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 },
4963 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004964 .procname = "autoconf",
4965 .data = &ipv6_devconf.autoconf,
4966 .maxlen = sizeof(int),
4967 .mode = 0644,
4968 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 },
4970 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004971 .procname = "dad_transmits",
4972 .data = &ipv6_devconf.dad_transmits,
4973 .maxlen = sizeof(int),
4974 .mode = 0644,
4975 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 },
4977 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004978 .procname = "router_solicitations",
4979 .data = &ipv6_devconf.rtr_solicits,
4980 .maxlen = sizeof(int),
4981 .mode = 0644,
4982 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 },
4984 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004985 .procname = "router_solicitation_interval",
4986 .data = &ipv6_devconf.rtr_solicit_interval,
4987 .maxlen = sizeof(int),
4988 .mode = 0644,
4989 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 },
4991 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004992 .procname = "router_solicitation_delay",
4993 .data = &ipv6_devconf.rtr_solicit_delay,
4994 .maxlen = sizeof(int),
4995 .mode = 0644,
4996 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 },
4998 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004999 .procname = "force_mld_version",
5000 .data = &ipv6_devconf.force_mld_version,
5001 .maxlen = sizeof(int),
5002 .mode = 0644,
5003 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 },
Hannes Frederic Sowafc4eba52013-08-14 01:03:46 +02005005 {
5006 .procname = "mldv1_unsolicited_report_interval",
5007 .data =
5008 &ipv6_devconf.mldv1_unsolicited_report_interval,
5009 .maxlen = sizeof(int),
5010 .mode = 0644,
5011 .proc_handler = proc_dointvec_ms_jiffies,
5012 },
5013 {
5014 .procname = "mldv2_unsolicited_report_interval",
5015 .data =
5016 &ipv6_devconf.mldv2_unsolicited_report_interval,
5017 .maxlen = sizeof(int),
5018 .mode = 0644,
5019 .proc_handler = proc_dointvec_ms_jiffies,
5020 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005022 .procname = "use_tempaddr",
5023 .data = &ipv6_devconf.use_tempaddr,
5024 .maxlen = sizeof(int),
5025 .mode = 0644,
5026 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 },
5028 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005029 .procname = "temp_valid_lft",
5030 .data = &ipv6_devconf.temp_valid_lft,
5031 .maxlen = sizeof(int),
5032 .mode = 0644,
5033 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 },
5035 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005036 .procname = "temp_prefered_lft",
5037 .data = &ipv6_devconf.temp_prefered_lft,
5038 .maxlen = sizeof(int),
5039 .mode = 0644,
5040 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 },
5042 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005043 .procname = "regen_max_retry",
5044 .data = &ipv6_devconf.regen_max_retry,
5045 .maxlen = sizeof(int),
5046 .mode = 0644,
5047 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 },
5049 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005050 .procname = "max_desync_factor",
5051 .data = &ipv6_devconf.max_desync_factor,
5052 .maxlen = sizeof(int),
5053 .mode = 0644,
5054 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005057 .procname = "max_addresses",
5058 .data = &ipv6_devconf.max_addresses,
5059 .maxlen = sizeof(int),
5060 .mode = 0644,
5061 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 },
5063 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005064 .procname = "accept_ra_defrtr",
5065 .data = &ipv6_devconf.accept_ra_defrtr,
5066 .maxlen = sizeof(int),
5067 .mode = 0644,
5068 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08005069 },
5070 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005071 .procname = "accept_ra_pinfo",
5072 .data = &ipv6_devconf.accept_ra_pinfo,
5073 .maxlen = sizeof(int),
5074 .mode = 0644,
5075 .proc_handler = proc_dointvec,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08005076 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005077#ifdef CONFIG_IPV6_ROUTER_PREF
5078 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005079 .procname = "accept_ra_rtr_pref",
5080 .data = &ipv6_devconf.accept_ra_rtr_pref,
5081 .maxlen = sizeof(int),
5082 .mode = 0644,
5083 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005084 },
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08005085 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005086 .procname = "router_probe_interval",
5087 .data = &ipv6_devconf.rtr_probe_interval,
5088 .maxlen = sizeof(int),
5089 .mode = 0644,
5090 .proc_handler = proc_dointvec_jiffies,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08005091 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08005092#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08005093 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005094 .procname = "accept_ra_rt_info_max_plen",
5095 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
5096 .maxlen = sizeof(int),
5097 .mode = 0644,
5098 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08005099 },
5100#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08005101#endif
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08005102 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005103 .procname = "proxy_ndp",
5104 .data = &ipv6_devconf.proxy_ndp,
5105 .maxlen = sizeof(int),
5106 .mode = 0644,
stephen hemmingerc92d5492013-12-17 22:37:14 -08005107 .proc_handler = addrconf_sysctl_proxy_ndp,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07005108 },
5109 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005110 .procname = "accept_source_route",
5111 .data = &ipv6_devconf.accept_source_route,
5112 .maxlen = sizeof(int),
5113 .mode = 0644,
5114 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07005115 },
Neil Horman95c385b2007-04-25 17:08:10 -07005116#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
5117 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005118 .procname = "optimistic_dad",
5119 .data = &ipv6_devconf.optimistic_dad,
5120 .maxlen = sizeof(int),
5121 .mode = 0644,
5122 .proc_handler = proc_dointvec,
Neil Horman95c385b2007-04-25 17:08:10 -07005123
5124 },
5125#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005126#ifdef CONFIG_IPV6_MROUTE
5127 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005128 .procname = "mc_forwarding",
5129 .data = &ipv6_devconf.mc_forwarding,
5130 .maxlen = sizeof(int),
5131 .mode = 0444,
5132 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09005133 },
5134#endif
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07005135 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005136 .procname = "disable_ipv6",
5137 .data = &ipv6_devconf.disable_ipv6,
5138 .maxlen = sizeof(int),
5139 .mode = 0644,
5140 .proc_handler = addrconf_sysctl_disable,
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09005141 },
5142 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005143 .procname = "accept_dad",
5144 .data = &ipv6_devconf.accept_dad,
5145 .maxlen = sizeof(int),
5146 .mode = 0644,
5147 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09005148 },
5149 {
Octavian Purdilaf7734fd2009-10-02 11:39:15 +00005150 .procname = "force_tllao",
5151 .data = &ipv6_devconf.force_tllao,
5152 .maxlen = sizeof(int),
5153 .mode = 0644,
5154 .proc_handler = proc_dointvec
5155 },
5156 {
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00005157 .procname = "ndisc_notify",
5158 .data = &ipv6_devconf.ndisc_notify,
5159 .maxlen = sizeof(int),
5160 .mode = 0644,
5161 .proc_handler = proc_dointvec
5162 },
5163 {
Hannes Frederic Sowab800c3b2013-08-27 01:36:51 +02005164 .procname = "suppress_frag_ndisc",
5165 .data = &ipv6_devconf.suppress_frag_ndisc,
5166 .maxlen = sizeof(int),
5167 .mode = 0644,
5168 .proc_handler = proc_dointvec
5169 },
5170 {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08005171 /* sentinel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 }
5173 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174};
5175
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08005176static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08005177 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178{
5179 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 struct addrconf_sysctl_table *t;
Eric W. Biederman6105e292012-04-19 13:41:24 +00005181 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11005182
Arnaldo Carvalho de Meloaf879cc2006-11-17 12:14:37 -02005183 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 if (t == NULL)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11005185 goto out;
5186
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07005187 for (i = 0; t->addrconf_vars[i].data; i++) {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005188 t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08005190 t->addrconf_vars[i].extra2 = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192
Eric W. Biederman6105e292012-04-19 13:41:24 +00005193 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194
Eric W. Biederman6105e292012-04-19 13:41:24 +00005195 t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 if (t->sysctl_header == NULL)
Eric W. Biederman6105e292012-04-19 13:41:24 +00005197 goto free;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11005198
5199 p->sysctl = t;
Pavel Emelyanov95897312008-01-10 17:41:45 -08005200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11005202free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 kfree(t);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11005204out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08005205 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206}
5207
Pavel Emelyanov408c4762008-01-10 17:41:21 -08005208static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
5209{
5210 struct addrconf_sysctl_table *t;
5211
5212 if (p->sysctl == NULL)
5213 return;
5214
5215 t = p->sysctl;
5216 p->sysctl = NULL;
Lucian Adrian Grijincuff538812011-05-01 01:44:01 +00005217 unregister_net_sysctl_table(t->sysctl_header);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08005218 kfree(t);
5219}
5220
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11005221static void addrconf_sysctl_register(struct inet6_dev *idev)
5222{
Jiri Pirko73af6142013-12-07 19:26:55 +01005223 neigh_sysctl_register(idev->dev, idev->nd_parms,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08005224 &ndisc_ifinfo_sysctl_change);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09005225 __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08005226 idev, &idev->cnf);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11005227}
5228
Pavel Emelyanov408c4762008-01-10 17:41:21 -08005229static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08005231 __addrconf_sysctl_unregister(&idev->cnf);
5232 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233}
5234
5235
5236#endif
5237
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005238static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005239{
Hong Zhiguoa79ca222013-03-26 01:52:45 +08005240 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005241 struct ipv6_devconf *all, *dflt;
5242
Hong Zhiguoa79ca222013-03-26 01:52:45 +08005243 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
5244 if (all == NULL)
5245 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005246
Hong Zhiguoa79ca222013-03-26 01:52:45 +08005247 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
5248 if (dflt == NULL)
5249 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005250
Hong Zhiguoa79ca222013-03-26 01:52:45 +08005251 /* these will be inherited by all namespaces */
5252 dflt->autoconf = ipv6_defaults.autoconf;
5253 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005254
5255 net->ipv6.devconf_all = all;
5256 net->ipv6.devconf_dflt = dflt;
5257
5258#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08005259 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005260 if (err < 0)
5261 goto err_reg_all;
5262
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08005263 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005264 if (err < 0)
5265 goto err_reg_dflt;
5266#endif
5267 return 0;
5268
5269#ifdef CONFIG_SYSCTL
5270err_reg_dflt:
5271 __addrconf_sysctl_unregister(all);
5272err_reg_all:
5273 kfree(dflt);
5274#endif
5275err_alloc_dflt:
5276 kfree(all);
5277err_alloc_all:
5278 return err;
5279}
5280
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00005281static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005282{
5283#ifdef CONFIG_SYSCTL
5284 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
5285 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
5286#endif
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08005287 if (!net_eq(net, &init_net)) {
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005288 kfree(net->ipv6.devconf_dflt);
5289 kfree(net->ipv6.devconf_all);
5290 }
5291}
5292
5293static struct pernet_operations addrconf_ops = {
5294 .init = addrconf_init_net,
5295 .exit = addrconf_exit_net,
5296};
5297
Thomas Grafb382b192010-11-16 04:33:57 +00005298static struct rtnl_af_ops inet6_ops = {
5299 .family = AF_INET6,
5300 .fill_link_af = inet6_fill_link_af,
5301 .get_link_af_size = inet6_get_link_af_size,
Daniel Borkmannf53adae2013-04-08 04:01:30 +00005302 .set_link_af = inet6_set_link_af,
Thomas Grafb382b192010-11-16 04:33:57 +00005303};
5304
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305/*
5306 * Init / cleanup code
5307 */
5308
5309int __init addrconf_init(void)
5310{
stephen hemmingerc2e21292010-03-17 20:31:10 +00005311 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09005312
Stephen Hemmingere21e8462010-03-20 16:09:01 -07005313 err = ipv6_addr_label_init();
5314 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00005315 pr_crit("%s: cannot initialize default policy table: %d\n",
5316 __func__, err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00005317 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09005318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319
Neil Horman2cc6d2b2010-09-24 09:55:52 +00005320 err = register_pernet_subsys(&addrconf_ops);
5321 if (err < 0)
5322 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005323
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005324 addrconf_wq = create_workqueue("ipv6_addrconf");
5325 if (!addrconf_wq) {
5326 err = -ENOMEM;
5327 goto out_nowq;
5328 }
5329
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 /* The addrconf netdev notifier requires that loopback_dev
5331 * has it's ipv6 private information allocated and setup
5332 * before it can bring up and give link-local addresses
5333 * to other devices which are up.
5334 *
5335 * Unfortunately, loopback_dev is not necessarily the first
5336 * entry in the global dev_base list of net devices. In fact,
5337 * it is likely to be the very last entry on that list.
5338 * So this causes the notifier registry below to try and
5339 * give link-local addresses to all devices besides loopback_dev
5340 * first, then loopback_dev, which cases all the non-loopback_dev
5341 * devices to fail to get a link-local address.
5342 *
5343 * So, as a temporary fix, allocate the ipv6 structure for
5344 * loopback_dev first by hand.
5345 * Longer term, all of the dependencies ipv6 has upon the loopback
5346 * device and it being up should be removed.
5347 */
5348 rtnl_lock();
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07005349 if (!ipv6_add_dev(init_net.loopback_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 err = -ENOMEM;
5351 rtnl_unlock();
5352 if (err)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005353 goto errlo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354
stephen hemmingerc2e21292010-03-17 20:31:10 +00005355 for (i = 0; i < IN6_ADDR_HSIZE; i++)
5356 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
5357
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 register_netdevice_notifier(&ipv6_dev_notf);
5359
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005360 addrconf_verify();
Thomas Grafc127ea22007-03-22 11:58:32 -07005361
stephen hemminger3678a9d2013-12-30 10:41:32 -08005362 rtnl_af_register(&inet6_ops);
Thomas Grafb382b192010-11-16 04:33:57 +00005363
Greg Rosec7ac8672011-06-10 01:27:09 +00005364 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
5365 NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07005366 if (err < 0)
5367 goto errout;
5368
5369 /* Only the first call to __rtnl_register can fail */
Greg Rosec7ac8672011-06-10 01:27:09 +00005370 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
5371 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
5372 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
5373 inet6_dump_ifaddr, NULL);
5374 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
5375 inet6_dump_ifmcaddr, NULL);
5376 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
5377 inet6_dump_ifacaddr, NULL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +00005378 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
Nicolas Dichtel7a674202013-03-05 23:42:06 +00005379 inet6_netconf_dump_devconf, NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07005380
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09005381 ipv6_addr_label_rtnl_register();
5382
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07005384errout:
Thomas Grafb382b192010-11-16 04:33:57 +00005385 rtnl_af_unregister(&inet6_ops);
Thomas Grafc127ea22007-03-22 11:58:32 -07005386 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005387errlo:
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005388 destroy_workqueue(addrconf_wq);
5389out_nowq:
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005390 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00005391out_addrlabel:
5392 ipv6_addr_label_cleanup();
5393out:
Thomas Grafc127ea22007-03-22 11:58:32 -07005394 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395}
5396
Daniel Lezcano09f77092007-12-13 05:34:58 -08005397void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08005399 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 int i;
5401
5402 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08005403 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00005404 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405
5406 rtnl_lock();
5407
Thomas Grafb382b192010-11-16 04:33:57 +00005408 __rtnl_af_unregister(&inet6_ops);
5409
Daniel Lezcano176c39a2009-03-03 01:06:45 -08005410 /* clean dev list */
5411 for_each_netdev(&init_net, dev) {
5412 if (__in6_dev_get(dev) == NULL)
5413 continue;
5414 addrconf_ifdown(dev, 1);
5415 }
5416 addrconf_ifdown(init_net.loopback_dev, 2);
5417
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 * Check hash table.
5420 */
stephen hemminger5c578ae2010-03-17 20:31:11 +00005421 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00005422 for (i = 0; i < IN6_ADDR_HSIZE; i++)
5423 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578ae2010-03-17 20:31:11 +00005424 spin_unlock_bh(&addrconf_hash_lock);
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005425 cancel_delayed_work(&addr_chk_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 rtnl_unlock();
Hannes Frederic Sowac15b1cc2014-03-27 18:28:07 +01005427
5428 destroy_workqueue(addrconf_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429}