blob: 6f0125dafc997d3f97d39dbc9dbbf21de8e34c20 [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.
Harout Hedeshian8b2414d2012-12-21 12:38:38 -080039 * Harout S. Hedeshian : procfs flag to toggle automatic
40 * addition of prefix route
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020067#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <net/sock.h>
69#include <net/snmp.h>
70
71#include <net/ipv6.h>
72#include <net/protocol.h>
73#include <net/ndisc.h>
74#include <net/ip6_route.h>
75#include <net/addrconf.h>
76#include <net/tcp.h>
77#include <net/ip.h>
Thomas Graf5d620262006-08-15 00:35:02 -070078#include <net/netlink.h>
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -070079#include <net/pkt_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/if_tunnel.h>
81#include <linux/rtnetlink.h>
82
83#ifdef CONFIG_IPV6_PRIVACY
84#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#endif
86
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
98#define ADBG(x) printk x
99#else
100#define ADBG(x)
101#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
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -0700110#define ADDRCONF_TIMER_FUZZ_MINUS (HZ > 50 ? HZ/50 : 1)
111#define ADDRCONF_TIMER_FUZZ (HZ / 4)
112#define ADDRCONF_TIMER_FUZZ_MAX (HZ)
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#ifdef CONFIG_SYSCTL
Pavel Emelyanovf52295a2007-12-02 00:58:37 +1100115static void addrconf_sysctl_register(struct inet6_dev *idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800116static void addrconf_sysctl_unregister(struct inet6_dev *idev);
117#else
118static inline void addrconf_sysctl_register(struct inet6_dev *idev)
119{
120}
121
122static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
123{
124}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#endif
126
127#ifdef CONFIG_IPV6_PRIVACY
128static int __ipv6_regen_rndid(struct inet6_dev *idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900129static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static void ipv6_regen_rndid(unsigned long data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131#endif
132
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900133static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134static int ipv6_count_addresses(struct inet6_dev *idev);
135
136/*
137 * Configured unicast address hash table
138 */
stephen hemmingerc2e21292010-03-17 20:31:10 +0000139static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
stephen hemminger5c578ae2010-03-17 20:31:11 +0000140static DEFINE_SPINLOCK(addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142static void addrconf_verify(unsigned long);
143
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700144static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static DEFINE_SPINLOCK(addrconf_verify_lock);
146
147static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
148static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
149
Jiri Pirko93d9b7d2010-03-10 10:28:56 +0000150static void addrconf_type_change(struct net_device *dev,
151 unsigned long event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static int addrconf_ifdown(struct net_device *dev, int how);
153
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700154static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static void addrconf_dad_timer(unsigned long data);
156static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900157static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static void addrconf_rs_timer(unsigned long data);
159static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
160static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
161
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900162static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 struct prefix_info *pinfo);
David S. Miller3e81c6d2010-03-20 16:18:00 -0700164static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
165 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Alan Sterne041c682006-03-27 01:16:30 -0800167static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Adrian Bunk888c8482008-07-22 14:21:58 -0700169static struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 .forwarding = 0,
171 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
172 .mtu6 = IPV6_MIN_MTU,
173 .accept_ra = 1,
174 .accept_redirects = 1,
175 .autoconf = 1,
176 .force_mld_version = 0,
177 .dad_transmits = 1,
178 .rtr_solicits = MAX_RTR_SOLICITATIONS,
179 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
180 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
181#ifdef CONFIG_IPV6_PRIVACY
182 .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,
187#endif
188 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800189 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800190 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800191#ifdef CONFIG_IPV6_ROUTER_PREF
192 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800193 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800194#ifdef CONFIG_IPV6_ROUTE_INFO
195 .accept_ra_rt_info_max_plen = 0,
196#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800197#endif
Lorenzo Colitti653be182014-03-26 19:35:41 +0900198 .accept_ra_rt_table = 0,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700199 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700200 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900201 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900202 .accept_dad = 1,
Harout Hedeshian8b2414d2012-12-21 12:38:38 -0800203 .accept_ra_prefix_route = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Brian Haleyab32ea52006-09-22 14:15:41 -0700206static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 .forwarding = 0,
208 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
209 .mtu6 = IPV6_MIN_MTU,
210 .accept_ra = 1,
211 .accept_redirects = 1,
212 .autoconf = 1,
213 .dad_transmits = 1,
214 .rtr_solicits = MAX_RTR_SOLICITATIONS,
215 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
216 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
217#ifdef CONFIG_IPV6_PRIVACY
218 .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,
223#endif
224 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800225 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800226 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800227#ifdef CONFIG_IPV6_ROUTER_PREF
228 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800229 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800230#ifdef CONFIG_IPV6_ROUTE_INFO
231 .accept_ra_rt_info_max_plen = 0,
232#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800233#endif
Lorenzo Colitti653be182014-03-26 19:35:41 +0900234 .accept_ra_rt_table = 0,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700235 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700236 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900237 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900238 .accept_dad = 1,
Harout Hedeshian8b2414d2012-12-21 12:38:38 -0800239 .accept_ra_prefix_route = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240};
241
242/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900245const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
246const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700248/* Check if a valid qdisc is available */
David S. Miller05297942008-07-08 23:01:27 -0700249static inline bool addrconf_qdisc_ok(const struct net_device *dev)
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700250{
David S. Miller05297942008-07-08 23:01:27 -0700251 return !qdisc_tx_is_noop(dev);
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700252}
253
YOSHIFUJI Hideaki2b5ead42008-05-13 01:16:24 +0900254/* Check if a route is valid prefix route */
255static inline int addrconf_is_prefix_route(const struct rt6_info *rt)
256{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000257 return (rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0;
YOSHIFUJI Hideaki2b5ead42008-05-13 01:16:24 +0900258}
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static void addrconf_del_timer(struct inet6_ifaddr *ifp)
261{
262 if (del_timer(&ifp->timer))
263 __in6_ifa_put(ifp);
264}
265
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700266enum addrconf_timer_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 AC_NONE,
268 AC_DAD,
269 AC_RS,
270};
271
272static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
273 enum addrconf_timer_t what,
274 unsigned long when)
275{
276 if (!del_timer(&ifp->timer))
277 in6_ifa_hold(ifp);
278
279 switch (what) {
280 case AC_DAD:
281 ifp->timer.function = addrconf_dad_timer;
282 break;
283 case AC_RS:
284 ifp->timer.function = addrconf_rs_timer;
285 break;
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700286 default:
287 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289 ifp->timer.expires = jiffies + when;
290 add_timer(&ifp->timer);
291}
292
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700293static int snmp6_alloc_dev(struct inet6_dev *idev)
294{
Tejun Heo7d720c32010-02-16 15:20:26 +0000295 if (snmp_mib_init((void __percpu **)idev->stats.ipv6,
Eric Dumazet1823e4c82010-06-22 20:58:41 +0000296 sizeof(struct ipstats_mib),
297 __alignof__(struct ipstats_mib)) < 0)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700298 goto err_ip;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000299 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
300 GFP_KERNEL);
301 if (!idev->stats.icmpv6dev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700302 goto err_icmp;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000303 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
304 GFP_KERNEL);
305 if (!idev->stats.icmpv6msgdev)
David L Stevens14878f72007-09-16 16:52:35 -0700306 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700307
308 return 0;
309
David L Stevens14878f72007-09-16 16:52:35 -0700310err_icmpmsg:
Eric Dumazetbe281e52011-05-19 01:14:23 +0000311 kfree(idev->stats.icmpv6dev);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700312err_icmp:
Tejun Heo7d720c32010-02-16 15:20:26 +0000313 snmp_mib_free((void __percpu **)idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700314err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700315 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700316}
317
Pavel Emelyanov16910b92007-10-17 21:23:43 -0700318static void snmp6_free_dev(struct inet6_dev *idev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700319{
Eric Dumazetbe281e52011-05-19 01:14:23 +0000320 kfree(idev->stats.icmpv6msgdev);
321 kfree(idev->stats.icmpv6dev);
Tejun Heo7d720c32010-02-16 15:20:26 +0000322 snmp_mib_free((void __percpu **)idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/* Nobody refers to this device, we may destroy it. */
326
327void in6_dev_finish_destroy(struct inet6_dev *idev)
328{
329 struct net_device *dev = idev->dev;
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700330
stephen hemminger502a2ff2010-03-17 20:31:13 +0000331 WARN_ON(!list_empty(&idev->addr_list));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700332 WARN_ON(idev->mc_list != NULL);
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334#ifdef NET_REFCNT_DEBUG
335 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
336#endif
337 dev_put(dev);
338 if (!idev->dead) {
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700339 pr_warning("Freeing alive inet6 device %p\n", idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return;
341 }
342 snmp6_free_dev(idev);
Lai Jiangshan38f57d12011-03-15 17:59:14 +0800343 kfree_rcu(idev, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900346EXPORT_SYMBOL(in6_dev_finish_destroy);
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
349{
350 struct inet6_dev *ndev;
351
352 ASSERT_RTNL();
353
354 if (dev->mtu < IPV6_MIN_MTU)
355 return NULL;
356
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900357 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900359 if (ndev == NULL)
360 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Ingo Oeser322f74a2006-03-20 23:01:47 -0800362 rwlock_init(&ndev->lock);
363 ndev->dev = dev;
stephen hemminger502a2ff2010-03-17 20:31:13 +0000364 INIT_LIST_HEAD(&ndev->addr_list);
365
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900366 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800367 ndev->cnf.mtu6 = dev->mtu;
368 ndev->cnf.sysctl = NULL;
369 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
370 if (ndev->nd_parms == NULL) {
371 kfree(ndev);
372 return NULL;
373 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700374 if (ndev->cnf.forwarding)
375 dev_disable_lro(dev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800376 /* We refer to the device */
377 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Ingo Oeser322f74a2006-03-20 23:01:47 -0800379 if (snmp6_alloc_dev(ndev) < 0) {
380 ADBG((KERN_WARNING
381 "%s(): cannot allocate memory for statistics; dev=%s.\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800382 __func__, dev->name));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800383 neigh_parms_release(&nd_tbl, ndev->nd_parms);
Roy Li8603e332011-09-20 15:10:16 -0400384 dev_put(dev);
385 kfree(ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800386 return NULL;
387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Ingo Oeser322f74a2006-03-20 23:01:47 -0800389 if (snmp6_register_dev(ndev) < 0) {
390 ADBG((KERN_WARNING
391 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800392 __func__, dev->name));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800393 neigh_parms_release(&nd_tbl, ndev->nd_parms);
394 ndev->dead = 1;
395 in6_dev_finish_destroy(ndev);
396 return NULL;
397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Ingo Oeser322f74a2006-03-20 23:01:47 -0800399 /* One reference from device. We must do this before
400 * we invoke __ipv6_regen_rndid().
401 */
402 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900404 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
405 ndev->cnf.accept_dad = -1;
406
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700407#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
408 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
409 printk(KERN_INFO
410 "%s: Disabled Multicast RS\n",
411 dev->name);
412 ndev->cnf.rtr_solicits = 0;
413 }
414#endif
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416#ifdef CONFIG_IPV6_PRIVACY
stephen hemminger372e6c82010-03-17 20:31:09 +0000417 INIT_LIST_HEAD(&ndev->tempaddr_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800418 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800419 if ((dev->flags&IFF_LOOPBACK) ||
420 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700421 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700422 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700423 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800424 ndev->cnf.use_tempaddr = -1;
425 } else {
426 in6_dev_hold(ndev);
427 ipv6_regen_rndid((unsigned long) ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
Ingo Oeser322f74a2006-03-20 23:01:47 -0800429#endif
430
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700431 if (netif_running(dev) && addrconf_qdisc_ok(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700432 ndev->if_flags |= IF_READY;
433
Ingo Oeser322f74a2006-03-20 23:01:47 -0800434 ipv6_mc_init_dev(ndev);
435 ndev->tstamp = jiffies;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +1100436 addrconf_sysctl_register(ndev);
David L Stevens30c4cf52007-01-04 12:31:14 -0800437 /* protected by rtnl_lock */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000438 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800439
440 /* Join all-node multicast group */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900441 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800442
Li Weid6ddef92012-03-05 14:45:17 +0000443 /* Join all-router multicast group if forwarding is set */
Li Wei8b2aaed2012-03-07 14:58:07 +0000444 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
Li Weid6ddef92012-03-05 14:45:17 +0000445 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return ndev;
448}
449
YOSHIFUJI Hideakicee89472008-04-13 23:21:16 -0700450static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 struct inet6_dev *idev;
453
454 ASSERT_RTNL();
455
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700456 idev = __in6_dev_get(dev);
457 if (!idev) {
458 idev = ipv6_add_dev(dev);
459 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return NULL;
461 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (dev->flags&IFF_UP)
464 ipv6_mc_up(idev);
465 return idev;
466}
467
468#ifdef CONFIG_SYSCTL
469static void dev_forward_change(struct inet6_dev *idev)
470{
471 struct net_device *dev;
472 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 if (!idev)
475 return;
476 dev = idev->dev;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700477 if (idev->cnf.forwarding)
478 dev_disable_lro(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (dev && (dev->flags & IFF_MULTICAST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (idev->cnf.forwarding)
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900481 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 else
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900483 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
stephen hemminger502a2ff2010-03-17 20:31:13 +0000485
486 list_for_each_entry(ifa, &idev->addr_list, if_list) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800487 if (ifa->flags&IFA_F_TENTATIVE)
488 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (idev->cnf.forwarding)
490 addrconf_join_anycast(ifa);
491 else
492 addrconf_leave_anycast(ifa);
493 }
494}
495
496
Pavel Emelyanove1869322008-01-10 17:43:50 -0800497static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 struct net_device *dev;
500 struct inet6_dev *idev;
501
Ben Hutchings84a2d3c2012-08-14 08:54:51 +0000502 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 idev = __in6_dev_get(dev);
504 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800505 int changed = (!idev->cnf.forwarding) ^ (!newf);
506 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (changed)
508 dev_forward_change(idev);
509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800512
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000513static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800514{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800515 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000516 int old;
517
518 if (!rtnl_trylock())
519 return restart_syscall();
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800520
521 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000522 old = *p;
523 *p = newf;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800524
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000525 if (p == &net->ipv6.devconf_dflt->forwarding) {
526 rtnl_unlock();
527 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +0000528 }
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000529
Pavel Emelyanove1869322008-01-10 17:43:50 -0800530 if (p == &net->ipv6.devconf_all->forwarding) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800531 net->ipv6.devconf_dflt->forwarding = newf;
532 addrconf_forward_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000533 } else if ((!newf) ^ (!old))
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800534 dev_forward_change((struct inet6_dev *)table->extra1);
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700535 rtnl_unlock();
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800536
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000537 if (newf)
Daniel Lezcano7b4da532008-03-04 13:47:14 -0800538 rt6_purge_dflt_routers(net);
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000539 return 1;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800540}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541#endif
542
stephen hemminger5c578ae2010-03-17 20:31:11 +0000543/* Nobody refers to this ifaddr, destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
545{
stephen hemmingerc2e21292010-03-17 20:31:10 +0000546 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548#ifdef NET_REFCNT_DEBUG
549 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
550#endif
551
552 in6_dev_put(ifp->idev);
553
554 if (del_timer(&ifp->timer))
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700555 pr_notice("Timer is still running, when freeing ifa=%p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Herbert Xue9d3e082010-05-18 15:36:06 -0700557 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700558 pr_warning("Freeing alive inet6 address %p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return;
560 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700561 dst_release(&ifp->rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Lai Jiangshane5785982011-03-15 18:00:14 +0800563 kfree_rcu(ifp, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Brian Haleye55ffac2006-07-10 15:25:51 -0700566static void
567ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
568{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000569 struct list_head *p;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700570 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700571
572 /*
573 * Each device address list is sorted in order of scope -
574 * global before linklocal.
575 */
stephen hemminger502a2ff2010-03-17 20:31:13 +0000576 list_for_each(p, &idev->addr_list) {
577 struct inet6_ifaddr *ifa
578 = list_entry(p, struct inet6_ifaddr, if_list);
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700579 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700580 break;
581 }
582
David S. Millerb54c9b92010-03-25 21:25:30 -0700583 list_add_tail(&ifp->if_list, p);
Brian Haleye55ffac2006-07-10 15:25:51 -0700584}
585
stephen hemminger3a88a812010-03-17 20:31:12 +0000586static u32 ipv6_addr_hash(const struct in6_addr *addr)
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900587{
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900588 /*
589 * We perform the hash function over the last 64 bits of the address
590 * This will include the IEEE address token on links that support it.
591 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700592 return jhash_2words((__force u32)addr->s6_addr32[2],
593 (__force u32)addr->s6_addr32[3], 0)
stephen hemminger3a88a812010-03-17 20:31:12 +0000594 & (IN6_ADDR_HSIZE - 1);
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900595}
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597/* On success it returns ifp with increased reference count */
598
599static struct inet6_ifaddr *
600ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700601 int scope, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 struct inet6_ifaddr *ifa = NULL;
604 struct rt6_info *rt;
stephen hemminger3a88a812010-03-17 20:31:12 +0000605 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 int err = 0;
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +0900607 int addr_type = ipv6_addr_type(addr);
608
609 if (addr_type == IPV6_ADDR_ANY ||
610 addr_type & IPV6_ADDR_MULTICAST ||
611 (!(idev->dev->flags & IFF_LOOPBACK) &&
612 addr_type & IPV6_ADDR_LOOPBACK))
613 return ERR_PTR(-EADDRNOTAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700615 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (idev->dead) {
617 err = -ENODEV; /*XXX*/
618 goto out2;
619 }
620
Brian Haley56d417b2009-06-01 03:07:33 -0700621 if (idev->cnf.disable_ipv6) {
Brian Haley9bdd8d42009-03-18 18:22:48 -0700622 err = -EACCES;
623 goto out2;
624 }
625
stephen hemminger5c578ae2010-03-17 20:31:11 +0000626 spin_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 /* Ignore adding duplicate addresses on an interface */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900629 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 ADBG(("ipv6_add_addr: already assigned\n"));
631 err = -EEXIST;
632 goto out;
633 }
634
Ingo Oeser322f74a2006-03-20 23:01:47 -0800635 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 if (ifa == NULL) {
638 ADBG(("ipv6_add_addr: malloc failed\n"));
639 err = -ENOBUFS;
640 goto out;
641 }
642
David S. Miller8f031512011-12-06 16:48:14 -0500643 rt = addrconf_dst_alloc(idev, addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (IS_ERR(rt)) {
645 err = PTR_ERR(rt);
646 goto out;
647 }
648
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000649 ifa->addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 spin_lock_init(&ifa->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -0700652 spin_lock_init(&ifa->state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 init_timer(&ifa->timer);
stephen hemmingerc2e21292010-03-17 20:31:10 +0000654 INIT_HLIST_NODE(&ifa->addr_lst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 ifa->timer.data = (unsigned long) ifa;
656 ifa->scope = scope;
657 ifa->prefix_len = pfxlen;
658 ifa->flags = flags | IFA_F_TENTATIVE;
659 ifa->cstamp = ifa->tstamp = jiffies;
660
Keir Fraser57f5f542006-08-29 02:43:49 -0700661 ifa->rt = rt;
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 ifa->idev = idev;
664 in6_dev_hold(idev);
665 /* For caller */
666 in6_ifa_hold(ifa);
667
668 /* Add to big hash table */
669 hash = ipv6_addr_hash(addr);
670
stephen hemminger5c578ae2010-03-17 20:31:11 +0000671 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
stephen hemminger5c578ae2010-03-17 20:31:11 +0000672 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 write_lock(&idev->lock);
675 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -0700676 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678#ifdef CONFIG_IPV6_PRIVACY
679 if (ifa->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +0000680 list_add(&ifa->tmp_list, &idev->tempaddr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 in6_ifa_hold(ifa);
682 }
683#endif
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 in6_ifa_hold(ifa);
686 write_unlock(&idev->lock);
687out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700688 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -0700690 if (likely(err == 0))
Alan Sterne041c682006-03-27 01:16:30 -0800691 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 else {
693 kfree(ifa);
694 ifa = ERR_PTR(err);
695 }
696
697 return ifa;
698out:
stephen hemminger5c578ae2010-03-17 20:31:11 +0000699 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 goto out2;
701}
702
703/* This function wants to get referenced ifp and releases it before return */
704
705static void ipv6_del_addr(struct inet6_ifaddr *ifp)
706{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000707 struct inet6_ifaddr *ifa, *ifn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 struct inet6_dev *idev = ifp->idev;
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700709 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 int deleted = 0, onlink = 0;
711 unsigned long expires = jiffies;
712
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700713 spin_lock_bh(&ifp->state_lock);
714 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -0700715 ifp->state = INET6_IFADDR_STATE_DEAD;
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700716 spin_unlock_bh(&ifp->state_lock);
717
718 if (state == INET6_IFADDR_STATE_DEAD)
719 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
stephen hemminger5c578ae2010-03-17 20:31:11 +0000721 spin_lock_bh(&addrconf_hash_lock);
722 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578ae2010-03-17 20:31:11 +0000723 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 write_lock_bh(&idev->lock);
726#ifdef CONFIG_IPV6_PRIVACY
727 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +0000728 list_del(&ifp->tmp_list);
729 if (ifp->ifpub) {
730 in6_ifa_put(ifp->ifpub);
731 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
stephen hemminger372e6c82010-03-17 20:31:09 +0000733 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735#endif
736
stephen hemminger502a2ff2010-03-17 20:31:13 +0000737 list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (ifa == ifp) {
stephen hemminger502a2ff2010-03-17 20:31:13 +0000739 list_del_init(&ifp->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 __in6_ifa_put(ifp);
stephen hemminger502a2ff2010-03-17 20:31:13 +0000741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
743 break;
744 deleted = 1;
Kristian Slavov1d142802005-12-21 18:47:24 -0800745 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 } else if (ifp->flags & IFA_F_PERMANENT) {
747 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
748 ifp->prefix_len)) {
749 if (ifa->flags & IFA_F_PERMANENT) {
750 onlink = 1;
751 if (deleted)
752 break;
753 } else {
754 unsigned long lifetime;
755
756 if (!onlink)
757 onlink = -1;
758
759 spin_lock(&ifa->lock);
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900760
761 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
762 /*
763 * Note: Because this address is
764 * not permanent, lifetime <
765 * LONG_MAX / HZ here.
766 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (time_before(expires,
768 ifa->tstamp + lifetime * HZ))
769 expires = ifa->tstamp + lifetime * HZ;
770 spin_unlock(&ifa->lock);
771 }
772 }
773 }
774 }
775 write_unlock_bh(&idev->lock);
776
Andrey Vaginb2238562008-07-08 15:13:31 -0700777 addrconf_del_timer(ifp);
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 ipv6_ifa_notify(RTM_DELADDR, ifp);
780
Alan Sterne041c682006-03-27 01:16:30 -0800781 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 /*
784 * Purge or update corresponding prefix
785 *
786 * 1) we don't purge prefix here if address was not permanent.
787 * prefix is managed by its own lifetime.
788 * 2) if there're no addresses, delete prefix.
789 * 3) if there're still other permanent address(es),
790 * corresponding prefix is still permanent.
791 * 4) otherwise, update prefix lifetime to the
792 * longest valid lifetime among the corresponding
793 * addresses on the device.
794 * Note: subsequent RA will update lifetime.
795 *
796 * --yoshfuji
797 */
798 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
799 struct in6_addr prefix;
800 struct rt6_info *rt;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900801 struct net *net = dev_net(ifp->idev->dev);
Nicolas Dichteld5e36b02012-09-26 00:04:55 +0000802 struct flowi6 fl6 = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Nicolas Dichteld5e36b02012-09-26 00:04:55 +0000804 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
805 fl6.flowi6_oif = ifp->idev->dev->ifindex;
806 fl6.daddr = prefix;
807 rt = (struct rt6_info *)ip6_route_lookup(net, &fl6,
808 RT6_LOOKUP_F_IFACE);
809
810 if (rt != net->ipv6.ip6_null_entry &&
811 addrconf_is_prefix_route(rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (onlink == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700813 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 rt = NULL;
815 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
Gao feng1716a962012-04-06 00:13:10 +0000816 rt6_set_expires(rt, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700819 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821
Daniel Walterc3968a82011-04-13 21:10:57 +0000822 /* clean up prefsrc entries */
823 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700824out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 in6_ifa_put(ifp);
826}
827
828#ifdef CONFIG_IPV6_PRIVACY
829static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
830{
831 struct inet6_dev *idev = ifp->idev;
832 struct in6_addr addr, *tmpaddr;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +0000833 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700834 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 int tmp_plen;
836 int ret = 0;
837 int max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -0700838 u32 addr_flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +0000839 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 write_lock(&idev->lock);
842 if (ift) {
843 spin_lock_bh(&ift->lock);
844 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
845 spin_unlock_bh(&ift->lock);
846 tmpaddr = &addr;
847 } else {
848 tmpaddr = NULL;
849 }
850retry:
851 in6_dev_hold(idev);
852 if (idev->cnf.use_tempaddr <= 0) {
853 write_unlock(&idev->lock);
854 printk(KERN_INFO
855 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
856 in6_dev_put(idev);
857 ret = -1;
858 goto out;
859 }
860 spin_lock_bh(&ifp->lock);
861 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
862 idev->cnf.use_tempaddr = -1; /*XXX*/
863 spin_unlock_bh(&ifp->lock);
864 write_unlock(&idev->lock);
865 printk(KERN_WARNING
866 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
867 in6_dev_put(idev);
868 ret = -1;
869 goto out;
870 }
871 in6_ifa_hold(ifp);
872 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
873 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
874 spin_unlock_bh(&ifp->lock);
875 write_unlock(&idev->lock);
876 printk(KERN_WARNING
877 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
878 in6_ifa_put(ifp);
879 in6_dev_put(idev);
880 ret = -1;
881 goto out;
882 }
883 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +0000884 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 tmp_valid_lft = min_t(__u32,
886 ifp->valid_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +0000887 idev->cnf.temp_valid_lft + age);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900888 tmp_prefered_lft = min_t(__u32,
889 ifp->prefered_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +0000890 idev->cnf.temp_prefered_lft + age -
Ben Hutchings784e2712010-06-26 11:42:55 +0000891 idev->cnf.max_desync_factor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 tmp_plen = ifp->prefix_len;
893 max_addresses = idev->cnf.max_addresses;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 tmp_tstamp = ifp->tstamp;
895 spin_unlock_bh(&ifp->lock);
896
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700897 regen_advance = idev->cnf.regen_max_retry *
898 idev->cnf.dad_transmits *
899 idev->nd_parms->retrans_time / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 write_unlock(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -0700901
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700902 /* A temporary address is created only if this calculated Preferred
903 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
904 * an implementation must not create a temporary address with a zero
905 * Preferred Lifetime.
Heiner Kallweit4800c472014-03-12 22:13:19 +0100906 * Use age calculation as in addrconf_verify to avoid unnecessary
907 * temporary addresses being generated.
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700908 */
Heiner Kallweit4800c472014-03-12 22:13:19 +0100909 age = (now - tmp_tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
910 if (tmp_prefered_lft <= regen_advance + age) {
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700911 in6_ifa_put(ifp);
912 in6_dev_put(idev);
913 ret = -1;
914 goto out;
915 }
916
Neil Horman95c385b2007-04-25 17:08:10 -0700917 addr_flags = IFA_F_TEMPORARY;
918 /* set in addrconf_prefix_rcv() */
919 if (ifp->flags & IFA_F_OPTIMISTIC)
920 addr_flags |= IFA_F_OPTIMISTIC;
921
Hannes Frederic Sowaf4cb8372013-08-16 13:02:27 +0200922 ift = ipv6_add_addr(idev, &addr, tmp_plen,
923 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
924 addr_flags);
925 if (IS_ERR(ift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 in6_ifa_put(ifp);
927 in6_dev_put(idev);
928 printk(KERN_INFO
929 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
930 tmpaddr = &addr;
931 write_lock(&idev->lock);
932 goto retry;
933 }
934
935 spin_lock_bh(&ift->lock);
936 ift->ifpub = ifp;
937 ift->valid_lft = tmp_valid_lft;
938 ift->prefered_lft = tmp_prefered_lft;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +0000939 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 ift->tstamp = tmp_tstamp;
941 spin_unlock_bh(&ift->lock);
942
943 addrconf_dad_start(ift, 0);
944 in6_ifa_put(ift);
945 in6_dev_put(idev);
946out:
947 return ret;
948}
949#endif
950
951/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800952 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900954enum {
955 IPV6_SADDR_RULE_INIT = 0,
956 IPV6_SADDR_RULE_LOCAL,
957 IPV6_SADDR_RULE_SCOPE,
958 IPV6_SADDR_RULE_PREFERRED,
959#ifdef CONFIG_IPV6_MIP6
960 IPV6_SADDR_RULE_HOA,
961#endif
962 IPV6_SADDR_RULE_OIF,
963 IPV6_SADDR_RULE_LABEL,
964#ifdef CONFIG_IPV6_PRIVACY
965 IPV6_SADDR_RULE_PRIVACY,
966#endif
967 IPV6_SADDR_RULE_ORCHID,
968 IPV6_SADDR_RULE_PREFIX,
969 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800970};
971
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900972struct ipv6_saddr_score {
973 int rule;
974 int addr_type;
975 struct inet6_ifaddr *ifa;
976 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
977 int scopedist;
978 int matchlen;
979};
980
981struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900982 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900983 int ifindex;
984 int scope;
985 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900986 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900987};
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800988
Dave Jonesb6f99a22007-03-22 12:27:49 -0700989static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Ulrich Weber45bb0062010-02-25 23:28:58 +0000991 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800992 return 1;
993 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
Benjamin Thery3de23252008-05-28 14:51:24 +0200996static int ipv6_get_saddr_eval(struct net *net,
997 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900998 struct ipv6_saddr_dst *dst,
999 int i)
1000{
1001 int ret;
1002
1003 if (i <= score->rule) {
1004 switch (i) {
1005 case IPV6_SADDR_RULE_SCOPE:
1006 ret = score->scopedist;
1007 break;
1008 case IPV6_SADDR_RULE_PREFIX:
1009 ret = score->matchlen;
1010 break;
1011 default:
1012 ret = !!test_bit(i, score->scorebits);
1013 }
1014 goto out;
1015 }
1016
1017 switch (i) {
1018 case IPV6_SADDR_RULE_INIT:
1019 /* Rule 0: remember if hiscore is not ready yet */
1020 ret = !!score->ifa;
1021 break;
1022 case IPV6_SADDR_RULE_LOCAL:
1023 /* Rule 1: Prefer same address */
1024 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1025 break;
1026 case IPV6_SADDR_RULE_SCOPE:
1027 /* Rule 2: Prefer appropriate scope
1028 *
1029 * ret
1030 * ^
1031 * -1 | d 15
1032 * ---+--+-+---> scope
1033 * |
1034 * | d is scope of the destination.
1035 * B-d | \
1036 * | \ <- smaller scope is better if
1037 * B-15 | \ if scope is enough for destinaion.
1038 * | ret = B - scope (-1 <= scope >= d <= 15).
1039 * d-C-1 | /
1040 * |/ <- greater is better
1041 * -C / if scope is not enough for destination.
1042 * /| ret = scope - C (-1 <= d < scope <= 15).
1043 *
1044 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1045 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1046 * Assume B = 0 and we get C > 29.
1047 */
1048 ret = __ipv6_addr_src_scope(score->addr_type);
1049 if (ret >= dst->scope)
1050 ret = -ret;
1051 else
1052 ret -= 128; /* 30 is enough */
1053 score->scopedist = ret;
1054 break;
1055 case IPV6_SADDR_RULE_PREFERRED:
1056 /* Rule 3: Avoid deprecated and optimistic addresses */
1057 ret = ipv6_saddr_preferred(score->addr_type) ||
1058 !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1059 break;
1060#ifdef CONFIG_IPV6_MIP6
1061 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001062 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001063 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001064 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1065 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001066 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001067 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001068#endif
1069 case IPV6_SADDR_RULE_OIF:
1070 /* Rule 5: Prefer outgoing interface */
1071 ret = (!dst->ifindex ||
1072 dst->ifindex == score->ifa->idev->dev->ifindex);
1073 break;
1074 case IPV6_SADDR_RULE_LABEL:
1075 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001076 ret = ipv6_addr_label(net,
1077 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001078 score->ifa->idev->dev->ifindex) == dst->label;
1079 break;
1080#ifdef CONFIG_IPV6_PRIVACY
1081 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001082 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001083 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001084 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001085 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001086 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1087 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1088 score->ifa->idev->cnf.use_tempaddr >= 2;
1089 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001090 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001091 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001092#endif
1093 case IPV6_SADDR_RULE_ORCHID:
1094 /* Rule 8-: Prefer ORCHID vs ORCHID or
1095 * non-ORCHID vs non-ORCHID
1096 */
1097 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1098 ipv6_addr_orchid(dst->addr));
1099 break;
1100 case IPV6_SADDR_RULE_PREFIX:
1101 /* Rule 8: Use longest matching prefix */
1102 score->matchlen = ret = ipv6_addr_diff(&score->ifa->addr,
1103 dst->addr);
1104 break;
1105 default:
1106 ret = 0;
1107 }
1108
1109 if (ret)
1110 __set_bit(i, score->scorebits);
1111 score->rule = i;
1112out:
1113 return ret;
1114}
1115
Brian Haley191cd582008-08-14 15:33:21 -07001116int ipv6_dev_get_saddr(struct net *net, struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001117 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001118 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001120 struct ipv6_saddr_score scores[2],
1121 *score = &scores[0], *hiscore = &scores[1];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001122 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001123 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001124 int dst_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001126 dst_type = __ipv6_addr_type(daddr);
1127 dst.addr = daddr;
1128 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1129 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001130 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001131 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001132
1133 hiscore->rule = -1;
1134 hiscore->ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001136 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001137
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001138 for_each_netdev_rcu(net, dev) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001139 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001140
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001141 /* Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001142 * - multicast and link-local destination address,
1143 * the set of candidate source address MUST only
1144 * include addresses assigned to interfaces
1145 * belonging to the same link as the outgoing
1146 * interface.
1147 * (- For site-local destination addresses, the
1148 * set of candidate source addresses MUST only
1149 * include addresses assigned to interfaces
1150 * belonging to the same site as the outgoing
1151 * interface.)
1152 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001153 if (((dst_type & IPV6_ADDR_MULTICAST) ||
1154 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1155 dst.ifindex && dev->ifindex != dst.ifindex)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001156 continue;
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 idev = __in6_dev_get(dev);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001159 if (!idev)
1160 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001162 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001163 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001164 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001166 /*
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001167 * - Tentative Address (RFC2462 section 5.4)
1168 * - A tentative address is not considered
1169 * "assigned to an interface" in the traditional
Neil Horman95c385b2007-04-25 17:08:10 -07001170 * sense, unless it is also flagged as optimistic.
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001171 * - Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001172 * - In any case, anycast addresses, multicast
1173 * addresses, and the unspecified address MUST
1174 * NOT be included in a candidate set.
1175 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001176 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1177 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001178 continue;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001179
1180 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1181
1182 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1183 score->addr_type & IPV6_ADDR_MULTICAST)) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001184 LIMIT_NETDEBUG(KERN_DEBUG
Joe Perches3b6d8212007-11-19 23:47:25 -08001185 "ADDRCONF: unspecified / multicast address "
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001186 "assigned as unicast address on %s",
1187 dev->name);
1188 continue;
1189 }
1190
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001191 score->rule = -1;
1192 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001193
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001194 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1195 int minihiscore, miniscore;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001196
Benjamin Thery3de23252008-05-28 14:51:24 +02001197 minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
1198 miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001199
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001200 if (minihiscore > miniscore) {
1201 if (i == IPV6_SADDR_RULE_SCOPE &&
1202 score->scopedist > 0) {
1203 /*
1204 * special case:
1205 * each remaining entry
1206 * has too small (not enough)
1207 * scope, because ifa entries
1208 * are sorted by their scope
1209 * values.
1210 */
1211 goto try_nextdev;
1212 }
1213 break;
1214 } else if (minihiscore < miniscore) {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001215 if (hiscore->ifa)
1216 in6_ifa_put(hiscore->ifa);
1217
1218 in6_ifa_hold(score->ifa);
1219
Ilpo Järvinena0bffff2009-03-21 13:36:17 -07001220 swap(hiscore, score);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001221
1222 /* restore our iterator */
1223 score->ifa = hiscore->ifa;
1224
1225 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001228 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001229try_nextdev:
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001230 read_unlock_bh(&idev->lock);
1231 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001232 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001234 if (!hiscore->ifa)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001235 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001236
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001237 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001238 in6_ifa_put(hiscore->ifa);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001241EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Amerigo Wang3ef208a2013-06-29 21:30:49 +08001243int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1244 unsigned char banned_flags)
1245{
1246 struct inet6_ifaddr *ifp;
1247 int err = -EADDRNOTAVAIL;
1248
1249 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1250 if (ifp->scope == IFA_LINK &&
1251 !(ifp->flags & banned_flags)) {
1252 *addr = ifp->addr;
1253 err = 0;
1254 break;
1255 }
1256 }
1257 return err;
1258}
1259
Neil Horman95c385b2007-04-25 17:08:10 -07001260int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1261 unsigned char banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
1263 struct inet6_dev *idev;
1264 int err = -EADDRNOTAVAIL;
1265
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001266 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001267 idev = __in6_dev_get(dev);
1268 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 read_lock_bh(&idev->lock);
Amerigo Wang3ef208a2013-06-29 21:30:49 +08001270 err = __ipv6_get_lladdr(idev, addr, banned_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 read_unlock_bh(&idev->lock);
1272 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001273 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return err;
1275}
1276
1277static int ipv6_count_addresses(struct inet6_dev *idev)
1278{
1279 int cnt = 0;
1280 struct inet6_ifaddr *ifp;
1281
1282 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001283 list_for_each_entry(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 cnt++;
1285 read_unlock_bh(&idev->lock);
1286 return cnt;
1287}
1288
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001289int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001290 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001292 struct inet6_ifaddr *ifp;
stephen hemmingerc2e21292010-03-17 20:31:10 +00001293 struct hlist_node *node;
stephen hemminger3a88a812010-03-17 20:31:12 +00001294 unsigned int hash = ipv6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
stephen hemminger5c578ae2010-03-17 20:31:11 +00001296 rcu_read_lock_bh();
1297 hlist_for_each_entry_rcu(ifp, node, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001298 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001299 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (ipv6_addr_equal(&ifp->addr, addr) &&
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001301 !(ifp->flags&IFA_F_TENTATIVE) &&
1302 (dev == NULL || ifp->idev->dev == dev ||
1303 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1304 rcu_read_unlock_bh();
1305 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001308
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001309 rcu_read_unlock_bh();
1310 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001312EXPORT_SYMBOL(ipv6_chk_addr);
1313
David S. Miller3e81c6d2010-03-20 16:18:00 -07001314static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1315 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
David S. Miller3e81c6d2010-03-20 16:18:00 -07001317 unsigned int hash = ipv6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001318 struct inet6_ifaddr *ifp;
1319 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
stephen hemmingerc2e21292010-03-17 20:31:10 +00001321 hlist_for_each_entry(ifp, node, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001322 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001323 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (ipv6_addr_equal(&ifp->addr, addr)) {
1325 if (dev == NULL || ifp->idev->dev == dev)
David S. Miller3e81c6d2010-03-20 16:18:00 -07001326 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328 }
David S. Miller3e81c6d2010-03-20 16:18:00 -07001329 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330}
1331
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001332int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001333{
1334 struct inet6_dev *idev;
1335 struct inet6_ifaddr *ifa;
1336 int onlink;
1337
1338 onlink = 0;
1339 rcu_read_lock();
1340 idev = __in6_dev_get(dev);
1341 if (idev) {
1342 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001343 list_for_each_entry(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001344 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1345 ifa->prefix_len);
1346 if (onlink)
1347 break;
1348 }
1349 read_unlock_bh(&idev->lock);
1350 }
1351 rcu_read_unlock();
1352 return onlink;
1353}
1354
1355EXPORT_SYMBOL(ipv6_chk_prefix);
1356
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001357struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001358 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
David S. Millerb79d1d52010-03-25 21:39:21 -07001360 struct inet6_ifaddr *ifp, *result = NULL;
stephen hemminger3a88a812010-03-17 20:31:12 +00001361 unsigned int hash = ipv6_addr_hash(addr);
David S. Millerb79d1d52010-03-25 21:39:21 -07001362 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
stephen hemminger5c578ae2010-03-17 20:31:11 +00001364 rcu_read_lock_bh();
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00001365 hlist_for_each_entry_rcu_bh(ifp, node, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001366 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001367 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if (ipv6_addr_equal(&ifp->addr, addr)) {
1369 if (dev == NULL || ifp->idev->dev == dev ||
1370 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07001371 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 in6_ifa_hold(ifp);
1373 break;
1374 }
1375 }
1376 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001377 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
David S. Millerb79d1d52010-03-25 21:39:21 -07001379 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382/* Gets referenced address, destroys ifaddr */
1383
Brian Haleycc411d02009-09-09 14:41:32 +00001384static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (ifp->flags&IFA_F_PERMANENT) {
1387 spin_lock_bh(&ifp->lock);
1388 addrconf_del_timer(ifp);
1389 ifp->flags |= IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00001390 if (dad_failed)
1391 ifp->flags |= IFA_F_DADFAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 spin_unlock_bh(&ifp->lock);
Herbert Xue2577a02010-03-13 12:23:29 -08001393 if (dad_failed)
1394 ipv6_ifa_notify(0, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 in6_ifa_put(ifp);
1396#ifdef CONFIG_IPV6_PRIVACY
1397 } else if (ifp->flags&IFA_F_TEMPORARY) {
1398 struct inet6_ifaddr *ifpub;
1399 spin_lock_bh(&ifp->lock);
1400 ifpub = ifp->ifpub;
1401 if (ifpub) {
1402 in6_ifa_hold(ifpub);
1403 spin_unlock_bh(&ifp->lock);
1404 ipv6_create_tempaddr(ifpub, ifp);
1405 in6_ifa_put(ifpub);
1406 } else {
1407 spin_unlock_bh(&ifp->lock);
1408 }
1409 ipv6_del_addr(ifp);
1410#endif
1411 } else
1412 ipv6_del_addr(ifp);
1413}
1414
Herbert Xuf2344a12010-05-18 15:55:27 -07001415static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1416{
1417 int err = -ENOENT;
1418
1419 spin_lock(&ifp->state_lock);
1420 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1421 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1422 err = 0;
1423 }
1424 spin_unlock(&ifp->state_lock);
1425
1426 return err;
1427}
1428
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001429void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1430{
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001431 struct inet6_dev *idev = ifp->idev;
Brian Haley9bdd8d42009-03-18 18:22:48 -07001432
Ursula Braun853dc2e2010-10-24 23:06:43 +00001433 if (addrconf_dad_end(ifp)) {
1434 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07001435 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00001436 }
Herbert Xuf2344a12010-05-18 15:55:27 -07001437
Brian Haley9bdd8d42009-03-18 18:22:48 -07001438 if (net_ratelimit())
Jens Rosenboom0522fea2009-09-17 10:24:24 -07001439 printk(KERN_INFO "%s: IPv6 duplicate address %pI6c detected!\n",
1440 ifp->idev->dev->name, &ifp->addr);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001441
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001442 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
1443 struct in6_addr addr;
1444
1445 addr.s6_addr32[0] = htonl(0xfe800000);
1446 addr.s6_addr32[1] = 0;
1447
1448 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
1449 ipv6_addr_equal(&ifp->addr, &addr)) {
1450 /* DAD failed for link-local based on MAC address */
1451 idev->cnf.disable_ipv6 = 1;
Brian Haley9bdd8d42009-03-18 18:22:48 -07001452
1453 printk(KERN_INFO "%s: IPv6 being disabled!\n",
1454 ifp->idev->dev->name);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001455 }
1456 }
1457
Brian Haleycc411d02009-09-09 14:41:32 +00001458 addrconf_dad_stop(ifp, 1);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001459}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461/* Join to solicited addr multicast group. */
1462
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001463void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 struct in6_addr maddr;
1466
1467 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1468 return;
1469
1470 addrconf_addr_solict_mult(addr, &maddr);
1471 ipv6_dev_mc_inc(dev, &maddr);
1472}
1473
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001474void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
1476 struct in6_addr maddr;
1477
1478 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1479 return;
1480
1481 addrconf_addr_solict_mult(addr, &maddr);
1482 __ipv6_dev_mc_dec(idev, &maddr);
1483}
1484
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001485static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
1487 struct in6_addr addr;
Bjørn Mork2bda8a02011-07-05 23:04:13 +00001488 if (ifp->prefix_len == 127) /* RFC 6164 */
1489 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1491 if (ipv6_addr_any(&addr))
1492 return;
1493 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1494}
1495
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001496static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 struct in6_addr addr;
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00001499 if (ifp->prefix_len == 127) /* RFC 6164 */
1500 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1502 if (ipv6_addr_any(&addr))
1503 return;
1504 __ipv6_dev_ac_dec(ifp->idev, &addr);
1505}
1506
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001507static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1508{
1509 if (dev->addr_len != ETH_ALEN)
1510 return -1;
1511 memcpy(eui, dev->dev_addr, 3);
1512 memcpy(eui + 5, dev->dev_addr + 3, 3);
1513
1514 /*
1515 * The zSeries OSA network cards can be shared among various
1516 * OS instances, but the OSA cards have only one MAC address.
1517 * This leads to duplicate address conflicts in conjunction
1518 * with IPv6 if more than one instance uses the same card.
1519 *
1520 * The driver for these cards can deliver a unique 16-bit
1521 * identifier for each instance sharing the same card. It is
1522 * placed instead of 0xFFFE in the interface identifier. The
1523 * "u" bit of the interface identifier is not inverted in this
1524 * case. Hence the resulting interface identifier has local
1525 * scope according to RFC2373.
1526 */
1527 if (dev->dev_id) {
1528 eui[3] = (dev->dev_id >> 8) & 0xFF;
1529 eui[4] = dev->dev_id & 0xFF;
1530 } else {
1531 eui[3] = 0xFF;
1532 eui[4] = 0xFE;
1533 eui[0] ^= 2;
1534 }
1535 return 0;
1536}
1537
1538static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1539{
1540 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1541 if (dev->addr_len != ARCNET_ALEN)
1542 return -1;
1543 memset(eui, 0, 7);
1544 eui[7] = *(u8*)dev->dev_addr;
1545 return 0;
1546}
1547
1548static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1549{
1550 if (dev->addr_len != INFINIBAND_ALEN)
1551 return -1;
1552 memcpy(eui, dev->dev_addr + 12, 8);
1553 eui[0] |= 2;
1554 return 0;
1555}
1556
stephen hemmingerc61393e2010-10-04 20:17:53 +00001557static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001558{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00001559 if (addr == 0)
1560 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001561 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1562 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1563 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1564 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1565 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1566 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1567 eui[1] = 0;
1568 eui[2] = 0x5E;
1569 eui[3] = 0xFE;
1570 memcpy(eui + 4, &addr, 4);
1571 return 0;
1572}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001573
1574static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1575{
1576 if (dev->priv_flags & IFF_ISATAP)
1577 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1578 return -1;
1579}
1580
stephen hemmingeraee80b52011-06-08 10:44:30 +00001581static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1582{
1583 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1584}
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1587{
1588 switch (dev->type) {
1589 case ARPHRD_ETHER:
1590 case ARPHRD_FDDI:
1591 case ARPHRD_IEEE802_TR:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001592 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001594 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001596 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001597 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001598 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00001599 case ARPHRD_IPGRE:
1600 return addrconf_ifid_gre(eui, dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001601 case ARPHRD_RAWIP: {
1602 struct in6_addr lladdr;
1603
1604 if (ipv6_get_lladdr(dev, &lladdr, IFA_F_TENTATIVE))
1605 get_random_bytes(eui, 8);
1606 else
1607 memcpy(eui, lladdr.s6_addr + 8, 8);
1608
1609 return 0;
1610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
1612 return -1;
1613}
1614
1615static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1616{
1617 int err = -1;
1618 struct inet6_ifaddr *ifp;
1619
1620 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001621 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1623 memcpy(eui, ifp->addr.s6_addr+8, 8);
1624 err = 0;
1625 break;
1626 }
1627 }
1628 read_unlock_bh(&idev->lock);
1629 return err;
1630}
1631
1632#ifdef CONFIG_IPV6_PRIVACY
1633/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1634static int __ipv6_regen_rndid(struct inet6_dev *idev)
1635{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08001637 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 /*
1641 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1642 * check if generated address is not inappropriate
1643 *
1644 * - Reserved subnet anycast (RFC 2526)
1645 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001646 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 * 00-00-5E-FE-xx-xx-xx-xx
1648 * - value 0
1649 * - XXX: already assigned to an address on the device
1650 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001651 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1653 (idev->rndid[7]&0x80))
1654 goto regen;
1655 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1656 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1657 goto regen;
1658 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1659 goto regen;
1660 }
1661
1662 return 0;
1663}
1664
1665static void ipv6_regen_rndid(unsigned long data)
1666{
1667 struct inet6_dev *idev = (struct inet6_dev *) data;
1668 unsigned long expires;
1669
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001670 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 write_lock_bh(&idev->lock);
1672
1673 if (idev->dead)
1674 goto out;
1675
1676 if (__ipv6_regen_rndid(idev) < 0)
1677 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 expires = jiffies +
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001680 idev->cnf.temp_prefered_lft * HZ -
Ben Hutchings784e2712010-06-26 11:42:55 +00001681 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time -
1682 idev->cnf.max_desync_factor * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (time_before(expires, jiffies)) {
1684 printk(KERN_WARNING
1685 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1686 idev->dev->name);
1687 goto out;
1688 }
1689
1690 if (!mod_timer(&idev->regen_timer, expires))
1691 in6_dev_hold(idev);
1692
1693out:
1694 write_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001695 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 in6_dev_put(idev);
1697}
1698
1699static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1700 int ret = 0;
1701
1702 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1703 ret = __ipv6_regen_rndid(idev);
1704 return ret;
1705}
1706#endif
1707
Lorenzo Colitti653be182014-03-26 19:35:41 +09001708u32 addrconf_rt_table(const struct net_device *dev, u32 default_table) {
1709 /* Determines into what table to put autoconf PIO/RIO/default routes
1710 * learned on this device.
1711 *
1712 * - If 0, use the same table for every device. This puts routes into
1713 * one of RT_TABLE_{PREFIX,INFO,DFLT} depending on the type of route
1714 * (but note that these three are currently all equal to
1715 * RT6_TABLE_MAIN).
1716 * - If > 0, use the specified table.
1717 * - If < 0, put routes into table dev->ifindex + (-rt_table).
1718 */
1719 struct inet6_dev *idev = in6_dev_get(dev);
1720 u32 table;
1721 int sysctl = idev->cnf.accept_ra_rt_table;
1722 if (sysctl == 0) {
1723 table = default_table;
1724 } else if (sysctl > 0) {
1725 table = (u32) sysctl;
1726 } else {
1727 table = (unsigned) dev->ifindex + (-sysctl);
1728 }
1729 in6_dev_put(idev);
1730 return table;
1731}
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733/*
1734 * Add prefix route.
1735 */
1736
1737static void
1738addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001739 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
Thomas Graf86872cb2006-08-22 00:01:08 -07001741 struct fib6_config cfg = {
Lorenzo Colitti653be182014-03-26 19:35:41 +09001742 .fc_table = addrconf_rt_table(dev, RT6_TABLE_PREFIX),
Thomas Graf86872cb2006-08-22 00:01:08 -07001743 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1744 .fc_ifindex = dev->ifindex,
1745 .fc_expires = expires,
1746 .fc_dst_len = plen,
1747 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001748 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07001749 .fc_protocol = RTPROT_KERNEL,
Thomas Graf86872cb2006-08-22 00:01:08 -07001750 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001752 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 /* Prevent useless cloning on PtP SIT.
1755 This thing is done here expecting that the whole
1756 class of non-broadcast devices need not cloning.
1757 */
Joerg Roedel0be669b2006-10-10 14:49:53 -07001758#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Thomas Graf86872cb2006-08-22 00:01:08 -07001759 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1760 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07001761#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Thomas Graf86872cb2006-08-22 00:01:08 -07001763 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001766
1767static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
1768 int plen,
1769 const struct net_device *dev,
1770 u32 flags, u32 noflags)
1771{
1772 struct fib6_node *fn;
1773 struct rt6_info *rt = NULL;
1774 struct fib6_table *table;
1775
Lorenzo Colitti653be182014-03-26 19:35:41 +09001776 table = fib6_get_table(dev_net(dev),
1777 addrconf_rt_table(dev, RT6_TABLE_PREFIX));
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001778 if (table == NULL)
1779 return NULL;
1780
1781 write_lock_bh(&table->tb6_lock);
1782 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
1783 if (!fn)
1784 goto out;
1785 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05001786 if (rt->dst.dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001787 continue;
1788 if ((rt->rt6i_flags & flags) != flags)
1789 continue;
Romain Kuntz3b690552013-01-09 15:02:26 +01001790 if ((rt->rt6i_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001791 continue;
1792 dst_hold(&rt->dst);
1793 break;
1794 }
1795out:
1796 write_unlock_bh(&table->tb6_lock);
1797 return rt;
1798}
1799
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801/* Create "default" multicast route to the interface */
1802
1803static void addrconf_add_mroute(struct net_device *dev)
1804{
Thomas Graf86872cb2006-08-22 00:01:08 -07001805 struct fib6_config cfg = {
1806 .fc_table = RT6_TABLE_LOCAL,
1807 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1808 .fc_ifindex = dev->ifindex,
1809 .fc_dst_len = 8,
1810 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001811 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001812 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Thomas Graf86872cb2006-08-22 00:01:08 -07001814 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1815
1816 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
Joerg Roedel0be669b2006-10-10 14:49:53 -07001819#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820static void sit_route_add(struct net_device *dev)
1821{
Thomas Graf86872cb2006-08-22 00:01:08 -07001822 struct fib6_config cfg = {
1823 .fc_table = RT6_TABLE_MAIN,
1824 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1825 .fc_ifindex = dev->ifindex,
1826 .fc_dst_len = 96,
1827 .fc_flags = RTF_UP | RTF_NONEXTHOP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001828 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001829 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 /* prefix length - 96 bits "::d.d.d.d" */
Thomas Graf86872cb2006-08-22 00:01:08 -07001832 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
Joerg Roedel0be669b2006-10-10 14:49:53 -07001834#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836static void addrconf_add_lroute(struct net_device *dev)
1837{
1838 struct in6_addr addr;
1839
1840 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1841 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1842}
1843
1844static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1845{
1846 struct inet6_dev *idev;
1847
1848 ASSERT_RTNL();
1849
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001850 idev = ipv6_find_idev(dev);
1851 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00001852 return ERR_PTR(-ENOBUFS);
1853
1854 if (idev->cnf.disable_ipv6)
1855 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 /* Add default multicast route */
Li Wei4af04ab2011-12-06 21:23:45 +00001858 if (!(dev->flags & IFF_LOOPBACK))
1859 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 /* Add link local route */
1862 addrconf_add_lroute(dev);
1863 return idev;
1864}
1865
Neil Hormane6bff992012-01-04 10:49:15 +00001866void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
1868 struct prefix_info *pinfo;
1869 __u32 valid_lft;
1870 __u32 prefered_lft;
1871 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07001873 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (len < sizeof(struct prefix_info)) {
1878 ADBG(("addrconf: prefix option too short\n"));
1879 return;
1880 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 /*
1883 * Validation checks ([ADDRCONF], page 19)
1884 */
1885
1886 addr_type = ipv6_addr_type(&pinfo->prefix);
1887
1888 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1889 return;
1890
1891 valid_lft = ntohl(pinfo->valid);
1892 prefered_lft = ntohl(pinfo->prefered);
1893
1894 if (prefered_lft > valid_lft) {
1895 if (net_ratelimit())
1896 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1897 return;
1898 }
1899
1900 in6_dev = in6_dev_get(dev);
1901
1902 if (in6_dev == NULL) {
1903 if (net_ratelimit())
1904 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1905 return;
1906 }
1907
1908 /*
1909 * Two things going on here:
1910 * 1) Add routes for on-link prefixes
1911 * 2) Configure prefixes with the auto flag set
1912 */
1913
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001914 if (pinfo->onlink) {
1915 struct rt6_info *rt;
1916 unsigned long rt_expires;
1917
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001918 /* Avoid arithmetic overflow. Really, we could
1919 * save rt_expires in seconds, likely valid_lft,
1920 * but it would require division in fib gc, that it
1921 * not good.
1922 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001923 if (HZ > USER_HZ)
1924 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
1925 else
1926 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001927
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001928 if (addrconf_finite_timeout(rt_expires))
1929 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001931 rt = addrconf_get_prefix_route(&pinfo->prefix,
1932 pinfo->prefix_len,
1933 dev,
1934 RTF_ADDRCONF | RTF_PREFIX_RT,
1935 RTF_GATEWAY | RTF_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001937 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001938 /* Autoconf prefix route */
1939 if (valid_lft == 0) {
1940 ip6_del_rt(rt);
1941 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001942 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001943 /* not infinity */
Gao feng1716a962012-04-06 00:13:10 +00001944 rt6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001945 } else {
Gao feng1716a962012-04-06 00:13:10 +00001946 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 }
1948 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001949 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001950 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
1951 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001952 /* not infinity */
1953 flags |= RTF_EXPIRES;
1954 expires = jiffies_to_clock_t(rt_expires);
1955 }
Harout Hedeshian8b2414d2012-12-21 12:38:38 -08001956 if (dev->ip6_ptr->cnf.accept_ra_prefix_route) {
1957 addrconf_prefix_route(&pinfo->prefix,
1958 pinfo->prefix_len, dev, expires, flags);
1959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
1961 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001962 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
1964
1965 /* Try to figure out our local address for this prefix */
1966
1967 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1968 struct inet6_ifaddr * ifp;
1969 struct in6_addr addr;
1970 int create = 0, update_lft = 0;
1971
1972 if (pinfo->prefix_len == 64) {
1973 memcpy(&addr, &pinfo->prefix, 8);
1974 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1975 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1976 in6_dev_put(in6_dev);
1977 return;
1978 }
1979 goto ok;
1980 }
1981 if (net_ratelimit())
1982 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1983 pinfo->prefix_len);
1984 in6_dev_put(in6_dev);
1985 return;
1986
1987ok:
1988
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001989 ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991 if (ifp == NULL && valid_lft) {
1992 int max_addresses = in6_dev->cnf.max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -07001993 u32 addr_flags = 0;
1994
1995#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1996 if (in6_dev->cnf.optimistic_dad &&
Neil Hormane6bff992012-01-04 10:49:15 +00001997 !net->ipv6.devconf_all->forwarding && sllao)
Neil Horman95c385b2007-04-25 17:08:10 -07001998 addr_flags = IFA_F_OPTIMISTIC;
1999#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 /* Do not allow to create too much of autoconfigured
2002 * addresses; this would be too easy way to crash kernel.
2003 */
2004 if (!max_addresses ||
2005 ipv6_count_addresses(in6_dev) < max_addresses)
2006 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
Neil Horman95c385b2007-04-25 17:08:10 -07002007 addr_type&IPV6_ADDR_SCOPE_MASK,
2008 addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 if (!ifp || IS_ERR(ifp)) {
2011 in6_dev_put(in6_dev);
2012 return;
2013 }
2014
2015 update_lft = create = 1;
2016 ifp->cstamp = jiffies;
2017 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
2018 }
2019
2020 if (ifp) {
2021 int flags;
2022 unsigned long now;
2023#ifdef CONFIG_IPV6_PRIVACY
2024 struct inet6_ifaddr *ift;
2025#endif
2026 u32 stored_lft;
2027
2028 /* update lifetime (RFC2462 5.5.3 e) */
2029 spin_lock(&ifp->lock);
2030 now = jiffies;
2031 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2032 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2033 else
2034 stored_lft = 0;
2035 if (!update_lft && stored_lft) {
2036 if (valid_lft > MIN_VALID_LIFETIME ||
2037 valid_lft > stored_lft)
2038 update_lft = 1;
2039 else if (stored_lft <= MIN_VALID_LIFETIME) {
2040 /* valid_lft <= stored_lft is always true */
Brian Haleya1ed0522009-07-02 07:10:52 +00002041 /*
2042 * RFC 4862 Section 5.5.3e:
2043 * "Note that the preferred lifetime of
2044 * the corresponding address is always
2045 * reset to the Preferred Lifetime in
2046 * the received Prefix Information
2047 * option, regardless of whether the
2048 * valid lifetime is also reset or
2049 * ignored."
2050 *
2051 * So if the preferred lifetime in
2052 * this advertisement is different
2053 * than what we have stored, but the
2054 * valid lifetime is invalid, just
2055 * reset prefered_lft.
2056 *
2057 * We must set the valid lifetime
2058 * to the stored lifetime since we'll
2059 * be updating the timestamp below,
2060 * else we'll set it back to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002061 * minimum.
Brian Haleya1ed0522009-07-02 07:10:52 +00002062 */
2063 if (prefered_lft != ifp->prefered_lft) {
2064 valid_lft = stored_lft;
2065 update_lft = 1;
2066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 } else {
2068 valid_lft = MIN_VALID_LIFETIME;
2069 if (valid_lft < prefered_lft)
2070 prefered_lft = valid_lft;
2071 update_lft = 1;
2072 }
2073 }
2074
2075 if (update_lft) {
2076 ifp->valid_lft = valid_lft;
2077 ifp->prefered_lft = prefered_lft;
2078 ifp->tstamp = now;
2079 flags = ifp->flags;
2080 ifp->flags &= ~IFA_F_DEPRECATED;
2081 spin_unlock(&ifp->lock);
2082
2083 if (!(flags&IFA_F_TENTATIVE))
2084 ipv6_ifa_notify(0, ifp);
2085 } else
2086 spin_unlock(&ifp->lock);
2087
2088#ifdef CONFIG_IPV6_PRIVACY
2089 read_lock_bh(&in6_dev->lock);
2090 /* update all temporary addresses in the list */
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002091 list_for_each_entry(ift, &in6_dev->tempaddr_list,
2092 tmp_list) {
2093 int age, max_valid, max_prefered;
2094
Benoit Boissinotc6fbfac2008-04-02 00:00:58 -07002095 if (ifp != ift->ifpub)
2096 continue;
2097
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002098 /*
2099 * RFC 4941 section 3.3:
2100 * If a received option will extend the lifetime
2101 * of a public address, the lifetimes of
2102 * temporary addresses should be extended,
2103 * subject to the overall constraint that no
2104 * temporary addresses should ever remain
2105 * "valid" or "preferred" for a time longer than
2106 * (TEMP_VALID_LIFETIME) or
2107 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR),
2108 * respectively.
2109 */
2110 age = (now - ift->cstamp) / HZ;
2111 max_valid = in6_dev->cnf.temp_valid_lft - age;
2112 if (max_valid < 0)
2113 max_valid = 0;
2114
2115 max_prefered = in6_dev->cnf.temp_prefered_lft -
2116 in6_dev->cnf.max_desync_factor -
2117 age;
2118 if (max_prefered < 0)
2119 max_prefered = 0;
2120
2121 if (valid_lft > max_valid)
2122 valid_lft = max_valid;
2123
2124 if (prefered_lft > max_prefered)
2125 prefered_lft = max_prefered;
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 spin_lock(&ift->lock);
2128 flags = ift->flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002129 ift->valid_lft = valid_lft;
2130 ift->prefered_lft = prefered_lft;
2131 ift->tstamp = now;
2132 if (prefered_lft > 0)
2133 ift->flags &= ~IFA_F_DEPRECATED;
2134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 spin_unlock(&ift->lock);
2136 if (!(flags&IFA_F_TENTATIVE))
2137 ipv6_ifa_notify(0, ift);
2138 }
2139
Glenn Wursteraed65502010-09-27 07:04:30 +00002140 if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 /*
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002142 * When a new public address is created as
2143 * described in [ADDRCONF], also create a new
2144 * temporary address. Also create a temporary
2145 * address if it's enabled but no temporary
2146 * address currently exists.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002148 read_unlock_bh(&in6_dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 ipv6_create_tempaddr(ifp, NULL);
2150 } else {
2151 read_unlock_bh(&in6_dev->lock);
2152 }
2153#endif
2154 in6_ifa_put(ifp);
2155 addrconf_verify(0);
2156 }
2157 }
2158 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2159 in6_dev_put(in6_dev);
2160}
2161
2162/*
2163 * Set destination address.
2164 * Special case for SIT interfaces where we create a new "virtual"
2165 * device.
2166 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002167int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
2169 struct in6_ifreq ireq;
2170 struct net_device *dev;
2171 int err = -EINVAL;
2172
2173 rtnl_lock();
2174
2175 err = -EFAULT;
2176 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2177 goto err_exit;
2178
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002179 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 err = -ENODEV;
2182 if (dev == NULL)
2183 goto err_exit;
2184
Joerg Roedel0be669b2006-10-10 14:49:53 -07002185#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002187 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 struct ip_tunnel_parm p;
2190
2191 err = -EADDRNOTAVAIL;
2192 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2193 goto err_exit;
2194
2195 memset(&p, 0, sizeof(p));
2196 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2197 p.iph.saddr = 0;
2198 p.iph.version = 4;
2199 p.iph.ihl = 5;
2200 p.iph.protocol = IPPROTO_IPV6;
2201 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002202 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002204 if (ops->ndo_do_ioctl) {
2205 mm_segment_t oldfs = get_fs();
2206
2207 set_fs(KERNEL_DS);
2208 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2209 set_fs(oldfs);
2210 } else
2211 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 if (err == 0) {
2214 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002215 dev = __dev_get_by_name(net, p.name);
2216 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 goto err_exit;
2218 err = dev_open(dev);
2219 }
2220 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002221#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
2223err_exit:
2224 rtnl_unlock();
2225 return err;
2226}
2227
2228/*
2229 * Manual configuration of address on an interface
2230 */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002231static int inet6_addr_add(struct net *net, int ifindex, const struct in6_addr *pfx,
Thomas Graf24ef0da2008-05-28 16:54:22 +02002232 unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002233 __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
2235 struct inet6_ifaddr *ifp;
2236 struct inet6_dev *idev;
2237 struct net_device *dev;
2238 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002239 u32 flags;
2240 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002241 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
2243 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002244
Thomas Graf24ef0da2008-05-28 16:54:22 +02002245 if (plen > 128)
2246 return -EINVAL;
2247
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002248 /* check the lifetime */
2249 if (!valid_lft || prefered_lft > valid_lft)
2250 return -EINVAL;
2251
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002252 dev = __dev_get_by_index(net, ifindex);
2253 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002255
Brian Haley64e724f2010-07-20 10:34:30 +00002256 idev = addrconf_add_dev(dev);
2257 if (IS_ERR(idev))
2258 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 scope = ipv6_addr_scope(pfx);
2261
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002262 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2263 if (addrconf_finite_timeout(timeout)) {
2264 expires = jiffies_to_clock_t(timeout * HZ);
2265 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002266 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002267 } else {
2268 expires = 0;
2269 flags = 0;
2270 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002271 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002272
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002273 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2274 if (addrconf_finite_timeout(timeout)) {
2275 if (timeout == 0)
2276 ifa_flags |= IFA_F_DEPRECATED;
2277 prefered_lft = timeout;
2278 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002279
2280 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
2281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (!IS_ERR(ifp)) {
Herbert Xu06aebfb2006-08-09 16:52:04 -07002283 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002284 ifp->valid_lft = valid_lft;
2285 ifp->prefered_lft = prefered_lft;
2286 ifp->tstamp = jiffies;
Herbert Xu06aebfb2006-08-09 16:52:04 -07002287 spin_unlock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002288
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002289 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002290 expires, flags);
Neil Horman95c385b2007-04-25 17:08:10 -07002291 /*
2292 * Note that section 3.1 of RFC 4429 indicates
2293 * that the Optimistic flag should not be set for
2294 * manually configured addresses
2295 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 addrconf_dad_start(ifp, 0);
2297 in6_ifa_put(ifp);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002298 addrconf_verify(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 return 0;
2300 }
2301
2302 return PTR_ERR(ifp);
2303}
2304
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002305static int inet6_addr_del(struct net *net, int ifindex, const struct in6_addr *pfx,
Thomas Graf24ef0da2008-05-28 16:54:22 +02002306 unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
2308 struct inet6_ifaddr *ifp;
2309 struct inet6_dev *idev;
2310 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002311
Thomas Graf24ef0da2008-05-28 16:54:22 +02002312 if (plen > 128)
2313 return -EINVAL;
2314
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002315 dev = __dev_get_by_index(net, ifindex);
2316 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 return -ENODEV;
2318
2319 if ((idev = __in6_dev_get(dev)) == NULL)
2320 return -ENXIO;
2321
2322 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002323 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 if (ifp->prefix_len == plen &&
2325 ipv6_addr_equal(pfx, &ifp->addr)) {
2326 in6_ifa_hold(ifp);
2327 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 ipv6_del_addr(ifp);
2330
2331 /* If the last address is deleted administratively,
2332 disable IPv6 on this interface.
2333 */
stephen hemminger502a2ff2010-03-17 20:31:13 +00002334 if (list_empty(&idev->addr_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 addrconf_ifdown(idev->dev, 1);
2336 return 0;
2337 }
2338 }
2339 read_unlock_bh(&idev->lock);
2340 return -EADDRNOTAVAIL;
2341}
2342
2343
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002344int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
2346 struct in6_ifreq ireq;
2347 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (!capable(CAP_NET_ADMIN))
2350 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2353 return -EFAULT;
2354
2355 rtnl_lock();
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002356 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2357 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2358 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 rtnl_unlock();
2360 return err;
2361}
2362
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002363int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364{
2365 struct in6_ifreq ireq;
2366 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002367
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (!capable(CAP_NET_ADMIN))
2369 return -EPERM;
2370
2371 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2372 return -EFAULT;
2373
2374 rtnl_lock();
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002375 err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2376 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 rtnl_unlock();
2378 return err;
2379}
2380
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002381static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2382 int plen, int scope)
2383{
2384 struct inet6_ifaddr *ifp;
2385
2386 ifp = ipv6_add_addr(idev, addr, plen, scope, IFA_F_PERMANENT);
2387 if (!IS_ERR(ifp)) {
2388 spin_lock_bh(&ifp->lock);
2389 ifp->flags &= ~IFA_F_TENTATIVE;
2390 spin_unlock_bh(&ifp->lock);
2391 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2392 in6_ifa_put(ifp);
2393 }
2394}
2395
Joerg Roedel0be669b2006-10-10 14:49:53 -07002396#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397static void sit_add_v4_addrs(struct inet6_dev *idev)
2398{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 struct in6_addr addr;
2400 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002401 struct net *net = dev_net(idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 int scope;
2403
2404 ASSERT_RTNL();
2405
2406 memset(&addr, 0, sizeof(struct in6_addr));
2407 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2408
2409 if (idev->dev->flags&IFF_POINTOPOINT) {
2410 addr.s6_addr32[0] = htonl(0xfe800000);
2411 scope = IFA_LINK;
2412 } else {
2413 scope = IPV6_ADDR_COMPATv4;
2414 }
2415
2416 if (addr.s6_addr32[3]) {
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002417 add_addr(idev, &addr, 128, scope);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return;
2419 }
2420
Benjamin Thery6fda7352008-03-05 10:47:47 -08002421 for_each_netdev(net, dev) {
Herbert Xue5ed6392005-10-03 14:35:55 -07002422 struct in_device * in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 if (in_dev && (dev->flags & IFF_UP)) {
2424 struct in_ifaddr * ifa;
2425
2426 int flag = scope;
2427
2428 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2429 int plen;
2430
2431 addr.s6_addr32[3] = ifa->ifa_local;
2432
2433 if (ifa->ifa_scope == RT_SCOPE_LINK)
2434 continue;
2435 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2436 if (idev->dev->flags&IFF_POINTOPOINT)
2437 continue;
2438 flag |= IFA_HOST;
2439 }
2440 if (idev->dev->flags&IFF_POINTOPOINT)
2441 plen = 64;
2442 else
2443 plen = 96;
2444
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002445 add_addr(idev, &addr, plen, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
2447 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002450#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
2452static void init_loopback(struct net_device *dev)
2453{
2454 struct inet6_dev *idev;
Balakumaran Kannanef95e3d2013-04-02 16:15:05 +05302455 struct net_device *sp_dev;
2456 struct inet6_ifaddr *sp_ifa;
2457 struct rt6_info *sp_rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
2459 /* ::1 */
2460
2461 ASSERT_RTNL();
2462
2463 if ((idev = ipv6_find_idev(dev)) == NULL) {
2464 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2465 return;
2466 }
2467
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002468 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Balakumaran Kannanef95e3d2013-04-02 16:15:05 +05302469
2470 /* Add routes to other interface's IPv6 addresses */
2471 for_each_netdev(dev_net(dev), sp_dev) {
2472 if (!strcmp(sp_dev->name, dev->name))
2473 continue;
2474
2475 idev = __in6_dev_get(sp_dev);
2476 if (!idev)
2477 continue;
2478
2479 read_lock_bh(&idev->lock);
2480 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
2481
2482 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
2483 continue;
2484
Gao fenga0667042014-01-24 16:29:11 +08002485 if (sp_ifa->rt) {
2486 /* This dst has been added to garbage list when
2487 * lo device down, release this obsolete dst and
2488 * reallocate a new router for ifa.
2489 */
2490 if (sp_ifa->rt->dst.obsolete > 0) {
2491 dst_release(&sp_ifa->rt->dst);
2492 sp_ifa->rt = NULL;
2493 } else {
2494 continue;
2495 }
2496 }
Gao fengdabed302013-06-16 11:14:30 +08002497
Balakumaran Kannanef95e3d2013-04-02 16:15:05 +05302498 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, 0);
2499
2500 /* Failure cases are ignored */
Gao feng2f4997c2013-06-02 22:16:21 +00002501 if (!IS_ERR(sp_rt)) {
2502 sp_ifa->rt = sp_rt;
Balakumaran Kannanef95e3d2013-04-02 16:15:05 +05302503 ip6_ins_rt(sp_rt);
Gao feng2f4997c2013-06-02 22:16:21 +00002504 }
Balakumaran Kannanef95e3d2013-04-02 16:15:05 +05302505 }
2506 read_unlock_bh(&idev->lock);
2507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002510static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
2512 struct inet6_ifaddr * ifp;
Neil Horman95c385b2007-04-25 17:08:10 -07002513 u32 addr_flags = IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
Neil Horman95c385b2007-04-25 17:08:10 -07002515#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2516 if (idev->cnf.optimistic_dad &&
David Miller702beb82008-07-20 18:17:02 -07002517 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
Neil Horman95c385b2007-04-25 17:08:10 -07002518 addr_flags |= IFA_F_OPTIMISTIC;
2519#endif
2520
2521
2522 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002524 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 addrconf_dad_start(ifp, 0);
2526 in6_ifa_put(ifp);
2527 }
2528}
2529
2530static void addrconf_dev_config(struct net_device *dev)
2531{
2532 struct in6_addr addr;
2533 struct inet6_dev * idev;
2534
2535 ASSERT_RTNL();
2536
Herbert Xu74235a22007-06-14 13:02:55 -07002537 if ((dev->type != ARPHRD_ETHER) &&
2538 (dev->type != ARPHRD_FDDI) &&
2539 (dev->type != ARPHRD_IEEE802_TR) &&
2540 (dev->type != ARPHRD_ARCNET) &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002541 (dev->type != ARPHRD_RAWIP) &&
Herbert Xu74235a22007-06-14 13:02:55 -07002542 (dev->type != ARPHRD_INFINIBAND)) {
2543 /* Alas, we support only Ethernet autoconfiguration. */
2544 return;
2545 }
2546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00002548 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 return;
2550
2551 memset(&addr, 0, sizeof(struct in6_addr));
2552 addr.s6_addr32[0] = htonl(0xFE800000);
2553
2554 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2555 addrconf_add_linklocal(idev, &addr);
2556}
2557
Joerg Roedel0be669b2006-10-10 14:49:53 -07002558#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559static void addrconf_sit_config(struct net_device *dev)
2560{
2561 struct inet6_dev *idev;
2562
2563 ASSERT_RTNL();
2564
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002565 /*
2566 * Configure the tunnel with one of our IPv4
2567 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 * our v4 addrs in the tunnel
2569 */
2570
2571 if ((idev = ipv6_find_idev(dev)) == NULL) {
2572 printk(KERN_DEBUG "init sit: add_dev failed\n");
2573 return;
2574 }
2575
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002576 if (dev->priv_flags & IFF_ISATAP) {
2577 struct in6_addr addr;
2578
2579 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2580 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2581 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2582 addrconf_add_linklocal(idev, &addr);
2583 return;
2584 }
2585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 sit_add_v4_addrs(idev);
2587
2588 if (dev->flags&IFF_POINTOPOINT) {
2589 addrconf_add_mroute(dev);
2590 addrconf_add_lroute(dev);
2591 } else
2592 sit_route_add(dev);
2593}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002594#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
stephen hemmingeraee80b52011-06-08 10:44:30 +00002596#if defined(CONFIG_NET_IPGRE) || defined(CONFIG_NET_IPGRE_MODULE)
2597static void addrconf_gre_config(struct net_device *dev)
2598{
2599 struct inet6_dev *idev;
2600 struct in6_addr addr;
2601
2602 pr_info("ipv6: addrconf_gre_config(%s)\n", dev->name);
2603
2604 ASSERT_RTNL();
2605
2606 if ((idev = ipv6_find_idev(dev)) == NULL) {
2607 printk(KERN_DEBUG "init gre: add_dev failed\n");
2608 return;
2609 }
2610
2611 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2612 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2613
2614 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2615 addrconf_add_linklocal(idev, &addr);
2616}
2617#endif
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619static inline int
2620ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2621{
2622 struct in6_addr lladdr;
2623
Neil Horman95c385b2007-04-25 17:08:10 -07002624 if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 addrconf_add_linklocal(idev, &lladdr);
2626 return 0;
2627 }
2628 return -1;
2629}
2630
2631static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2632{
2633 struct net_device *link_dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002634 struct net *net = dev_net(idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
2636 /* first try to inherit the link-local address from the link device */
2637 if (idev->dev->iflink &&
Benjamin Thery6fda7352008-03-05 10:47:47 -08002638 (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 if (!ipv6_inherit_linklocal(idev, link_dev))
2640 return;
2641 }
2642 /* then try to inherit it from any device */
Benjamin Thery6fda7352008-03-05 10:47:47 -08002643 for_each_netdev(net, link_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 if (!ipv6_inherit_linklocal(idev, link_dev))
2645 return;
2646 }
2647 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2648}
2649
2650/*
2651 * Autoconfigure tunnel with a link-local address so routing protocols,
2652 * DHCPv6, MLD etc. can be run over the virtual link
2653 */
2654
2655static void addrconf_ip6_tnl_config(struct net_device *dev)
2656{
2657 struct inet6_dev *idev;
2658
2659 ASSERT_RTNL();
2660
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002661 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00002662 if (IS_ERR(idev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2664 return;
2665 }
2666 ip6_tnl_add_linklocal(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667}
2668
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002669static int addrconf_notify(struct notifier_block *this, unsigned long event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 void * data)
2671{
2672 struct net_device *dev = (struct net_device *) data;
Herbert Xu74235a22007-06-14 13:02:55 -07002673 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002674 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07002675 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002677 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002678 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07002679 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002680 idev = ipv6_add_dev(dev);
2681 if (!idev)
Herbert Xub217d612007-07-30 17:04:52 -07002682 return notifier_from_errno(-ENOMEM);
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002683 }
2684 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002685
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002687 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07002688 if (dev->flags & IFF_SLAVE)
2689 break;
2690
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002691 if (event == NETDEV_UP) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002692 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002693 /* device is not ready yet. */
2694 printk(KERN_INFO
2695 "ADDRCONF(NETDEV_UP): %s: "
2696 "link is not ready\n",
2697 dev->name);
2698 break;
2699 }
Kristian Slavov99081042006-02-08 16:10:53 -08002700
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002701 if (!idev && dev->mtu >= IPV6_MIN_MTU)
2702 idev = ipv6_add_dev(dev);
2703
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08002704 if (idev) {
Kristian Slavov99081042006-02-08 16:10:53 -08002705 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08002706 run_pending = 1;
2707 }
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002708 } else {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002709 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002710 /* device is still not ready. */
2711 break;
2712 }
2713
2714 if (idev) {
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002715 if (idev->if_flags & IF_READY)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002716 /* device is already configured. */
2717 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002718 idev->if_flags |= IF_READY;
2719 }
2720
2721 printk(KERN_INFO
2722 "ADDRCONF(NETDEV_CHANGE): %s: "
2723 "link becomes ready\n",
2724 dev->name);
2725
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002726 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002727 }
2728
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002729 switch (dev->type) {
Joerg Roedel0be669b2006-10-10 14:49:53 -07002730#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 case ARPHRD_SIT:
2732 addrconf_sit_config(dev);
2733 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002734#endif
stephen hemmingeraee80b52011-06-08 10:44:30 +00002735#if defined(CONFIG_NET_IPGRE) || defined(CONFIG_NET_IPGRE_MODULE)
2736 case ARPHRD_IPGRE:
2737 addrconf_gre_config(dev);
2738 break;
2739#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 case ARPHRD_TUNNEL6:
2741 addrconf_ip6_tnl_config(dev);
2742 break;
2743 case ARPHRD_LOOPBACK:
2744 init_loopback(dev);
2745 break;
2746
2747 default:
2748 addrconf_dev_config(dev);
2749 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002750 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002751
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 if (idev) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002753 if (run_pending)
2754 addrconf_dad_run(idev);
2755
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002756 /*
2757 * If the MTU changed during the interface down,
2758 * when the interface up, the changed MTU must be
2759 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002761 if (idev->cnf.mtu6 != dev->mtu &&
2762 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 rt6_mtu_change(dev, dev->mtu);
2764 idev->cnf.mtu6 = dev->mtu;
2765 }
2766 idev->tstamp = jiffies;
2767 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002768
2769 /*
2770 * If the changed mtu during down is lower than
2771 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 */
2773 if (dev->mtu < IPV6_MIN_MTU)
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002774 addrconf_ifdown(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 }
2776 break;
2777
2778 case NETDEV_CHANGEMTU:
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002779 if (idev && dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 rt6_mtu_change(dev, dev->mtu);
2781 idev->cnf.mtu6 = dev->mtu;
2782 break;
2783 }
2784
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002785 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2786 idev = ipv6_add_dev(dev);
2787 if (idev)
2788 break;
2789 }
2790
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002791 /*
2792 * MTU falled under IPV6_MIN_MTU.
2793 * Stop IPv6 on this interface.
2794 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
2796 case NETDEV_DOWN:
2797 case NETDEV_UNREGISTER:
2798 /*
2799 * Remove all addresses from this interface.
2800 */
2801 addrconf_ifdown(dev, event != NETDEV_DOWN);
2802 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002803
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07002806 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08002807 addrconf_sysctl_unregister(idev);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11002808 addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07002809 err = snmp6_register_dev(idev);
2810 if (err)
2811 return notifier_from_errno(err);
Stephen Hemminger5632c512007-04-28 21:16:39 -07002812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002814
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002815 case NETDEV_PRE_TYPE_CHANGE:
2816 case NETDEV_POST_TYPE_CHANGE:
2817 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07002818 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
2821 return NOTIFY_OK;
2822}
2823
2824/*
2825 * addrconf module should be notified of a device going up
2826 */
2827static struct notifier_block ipv6_dev_notf = {
2828 .notifier_call = addrconf_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829};
2830
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002831static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07002832{
2833 struct inet6_dev *idev;
2834 ASSERT_RTNL();
2835
2836 idev = __in6_dev_get(dev);
2837
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002838 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07002839 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002840 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07002841 ipv6_mc_unmap(idev);
2842}
2843
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844static int addrconf_ifdown(struct net_device *dev, int how)
2845{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002846 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002847 struct inet6_dev *idev;
2848 struct inet6_ifaddr *ifa;
David S. Miller73a8bd72011-01-23 23:27:15 -08002849 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
2851 ASSERT_RTNL();
2852
David S. Miller73a8bd72011-01-23 23:27:15 -08002853 rt6_ifdown(net, dev);
2854 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
2856 idev = __in6_dev_get(dev);
2857 if (idev == NULL)
2858 return -ENODEV;
2859
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002860 /*
2861 * Step 1: remove reference to ipv6 device from parent device.
2862 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002864 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002866
2867 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002868 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870 /* Step 1.5: remove snmp6 entry */
2871 snmp6_unregister_dev(idev);
2872
2873 }
2874
David S. Miller73a8bd72011-01-23 23:27:15 -08002875 /* Step 2: clear hash table */
2876 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
2877 struct hlist_head *h = &inet6_addr_lst[i];
2878 struct hlist_node *n;
2879
2880 spin_lock_bh(&addrconf_hash_lock);
2881 restart:
2882 hlist_for_each_entry_rcu(ifa, n, h, addr_lst) {
2883 if (ifa->idev == idev) {
2884 hlist_del_init_rcu(&ifa->addr_lst);
2885 addrconf_del_timer(ifa);
2886 goto restart;
2887 }
2888 }
2889 spin_unlock_bh(&addrconf_hash_lock);
2890 }
2891
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 write_lock_bh(&idev->lock);
2893
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002894 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002895 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002896 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898#ifdef CONFIG_IPV6_PRIVACY
Denis V. Lunev439e2382008-04-03 13:30:17 -07002899 if (how && del_timer(&idev->regen_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 in6_dev_put(idev);
2901
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002902 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00002903 while (!list_empty(&idev->tempaddr_list)) {
2904 ifa = list_first_entry(&idev->tempaddr_list,
2905 struct inet6_ifaddr, tmp_list);
2906 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 write_unlock_bh(&idev->lock);
2908 spin_lock_bh(&ifa->lock);
2909
2910 if (ifa->ifpub) {
2911 in6_ifa_put(ifa->ifpub);
2912 ifa->ifpub = NULL;
2913 }
2914 spin_unlock_bh(&ifa->lock);
2915 in6_ifa_put(ifa);
2916 write_lock_bh(&idev->lock);
2917 }
2918#endif
stephen hemminger8f37ada2010-03-03 08:19:59 +00002919
stephen hemminger502a2ff2010-03-17 20:31:13 +00002920 while (!list_empty(&idev->addr_list)) {
2921 ifa = list_first_entry(&idev->addr_list,
2922 struct inet6_ifaddr, if_list);
stephen hemminger84e8b802010-03-02 13:32:46 +00002923 addrconf_del_timer(ifa);
2924
David S. Miller73a8bd72011-01-23 23:27:15 -08002925 list_del(&ifa->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
David S. Miller73a8bd72011-01-23 23:27:15 -08002927 write_unlock_bh(&idev->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00002928
David S. Miller73a8bd72011-01-23 23:27:15 -08002929 spin_lock_bh(&ifa->state_lock);
2930 state = ifa->state;
2931 ifa->state = INET6_IFADDR_STATE_DEAD;
2932 spin_unlock_bh(&ifa->state_lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00002933
David S. Miller73a8bd72011-01-23 23:27:15 -08002934 if (state != INET6_IFADDR_STATE_DEAD) {
2935 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2936 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00002937 }
David S. Miller73a8bd72011-01-23 23:27:15 -08002938 in6_ifa_put(ifa);
stephen hemminger8f37ada2010-03-03 08:19:59 +00002939
David S. Miller73a8bd72011-01-23 23:27:15 -08002940 write_lock_bh(&idev->lock);
2941 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00002942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 write_unlock_bh(&idev->lock);
2944
2945 /* Step 5: Discard multicast list */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002946 if (how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 ipv6_mc_destroy_dev(idev);
2948 else
2949 ipv6_mc_down(idev);
2950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002952
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002953 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002954 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08002955 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 neigh_parms_release(&nd_tbl, idev->nd_parms);
2957 neigh_ifdown(&nd_tbl, dev);
2958 in6_dev_put(idev);
2959 }
2960 return 0;
2961}
2962
2963static void addrconf_rs_timer(unsigned long data)
2964{
2965 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
stephen hemminger5b2a1952010-03-02 13:32:45 +00002966 struct inet6_dev *idev = ifp->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
stephen hemminger5b2a1952010-03-02 13:32:45 +00002968 read_lock(&idev->lock);
2969 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 goto out;
2971
stephen hemminger5b2a1952010-03-02 13:32:45 +00002972 if (idev->cnf.forwarding)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00002974
2975 /* Announcement received after solicitation was sent */
2976 if (idev->if_flags & IF_RA_RCVD)
2977 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
2979 spin_lock(&ifp->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00002980 if (ifp->probes++ < idev->cnf.rtr_solicits) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 /* The wait after the last probe can be shorter */
2982 addrconf_mod_timer(ifp, AC_RS,
stephen hemminger5b2a1952010-03-02 13:32:45 +00002983 (ifp->probes == idev->cnf.rtr_solicits) ?
2984 idev->cnf.rtr_solicit_delay :
2985 idev->cnf.rtr_solicit_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 spin_unlock(&ifp->lock);
2987
stephen hemminger5b2a1952010-03-02 13:32:45 +00002988 ndisc_send_rs(idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 } else {
2990 spin_unlock(&ifp->lock);
2991 /*
2992 * Note: we do not support deprecated "all on-link"
2993 * assumption any longer.
2994 */
2995 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
stephen hemminger5b2a1952010-03-02 13:32:45 +00002996 idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 }
2998
2999out:
stephen hemminger5b2a1952010-03-02 13:32:45 +00003000 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 in6_ifa_put(ifp);
3002}
3003
3004/*
3005 * Duplicate Address Detection
3006 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003007static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3008{
3009 unsigned long rand_num;
3010 struct inet6_dev *idev = ifp->idev;
3011
Neil Horman95c385b2007-04-25 17:08:10 -07003012 if (ifp->flags & IFA_F_OPTIMISTIC)
3013 rand_num = 0;
3014 else
3015 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
3016
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003017 ifp->probes = idev->cnf.dad_transmits;
3018 addrconf_mod_timer(ifp, AC_DAD, rand_num);
3019}
3020
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07003021static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
3023 struct inet6_dev *idev = ifp->idev;
3024 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
3026 addrconf_join_solict(dev, &ifp->addr);
3027
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 net_srandom(ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
3030 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003031 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003032 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
3035 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09003036 idev->cnf.accept_dad < 1 ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003037 !(ifp->flags&IFA_F_TENTATIVE) ||
3038 ifp->flags & IFA_F_NODAD) {
Brian Haleycc411d02009-09-09 14:41:32 +00003039 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003040 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 read_unlock_bh(&idev->lock);
3042
3043 addrconf_dad_completed(ifp);
3044 return;
3045 }
3046
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003047 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003048 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003049 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003050 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003051 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003052 * - keep it tentative if it is a permanent address.
3053 * - otherwise, kill it.
3054 */
3055 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003056 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003057 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003058 }
Neil Horman95c385b2007-04-25 17:08:10 -07003059
3060 /*
3061 * Optimistic nodes can start receiving
3062 * Frames right away
3063 */
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003064 if (ifp->flags & IFA_F_OPTIMISTIC)
Neil Horman95c385b2007-04-25 17:08:10 -07003065 ip6_ins_rt(ifp->rt);
3066
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003067 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003069 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 read_unlock_bh(&idev->lock);
3071}
3072
3073static void addrconf_dad_timer(unsigned long data)
3074{
3075 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
3076 struct inet6_dev *idev = ifp->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 struct in6_addr mcaddr;
3078
Herbert Xuf2344a12010-05-18 15:55:27 -07003079 if (!ifp->probes && addrconf_dad_end(ifp))
3080 goto out;
3081
stephen hemminger122e4512010-03-02 13:32:44 +00003082 read_lock(&idev->lock);
3083 if (idev->dead || !(idev->if_flags & IF_READY)) {
3084 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 goto out;
3086 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003087
3088 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003089 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3090 spin_unlock(&ifp->lock);
3091 read_unlock(&idev->lock);
3092 goto out;
3093 }
3094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 if (ifp->probes == 0) {
3096 /*
3097 * DAD was successful
3098 */
3099
Brian Haleycc411d02009-09-09 14:41:32 +00003100 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003101 spin_unlock(&ifp->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003102 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
3104 addrconf_dad_completed(ifp);
3105
3106 goto out;
3107 }
3108
3109 ifp->probes--;
3110 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
stephen hemminger21809fa2010-02-08 19:48:52 +00003111 spin_unlock(&ifp->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003112 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
3114 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09003116 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117out:
3118 in6_ifa_put(ifp);
3119}
3120
3121static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3122{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003123 struct net_device *dev = ifp->idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
3125 /*
3126 * Configure the address for reception. Now it is valid.
3127 */
3128
3129 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3130
Tore Anderson026359b2011-08-28 23:47:33 +00003131 /* If added prefix is link local and we are prepared to process
3132 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 */
3134
Tore Anderson026359b2011-08-28 23:47:33 +00003135 if (((ifp->idev->cnf.accept_ra == 1 && !ifp->idev->cnf.forwarding) ||
3136 ifp->idev->cnf.accept_ra == 2) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 ifp->idev->cnf.rtr_solicits > 0 &&
3138 (dev->flags&IFF_LOOPBACK) == 0 &&
3139 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 /*
3141 * If a host as already performed a random delay
3142 * [...] as part of DAD [...] there is no need
3143 * to delay again before sending the first RS
3144 */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09003145 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
3147 spin_lock_bh(&ifp->lock);
3148 ifp->probes = 1;
3149 ifp->idev->if_flags |= IF_RS_SENT;
3150 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
3151 spin_unlock_bh(&ifp->lock);
3152 }
3153}
3154
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003155static void addrconf_dad_run(struct inet6_dev *idev)
3156{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003157 struct inet6_ifaddr *ifp;
3158
3159 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003160 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003161 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07003162 if (ifp->flags & IFA_F_TENTATIVE &&
3163 ifp->state == INET6_IFADDR_STATE_DAD)
3164 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00003165 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003166 }
3167 read_unlock_bh(&idev->lock);
3168}
3169
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170#ifdef CONFIG_PROC_FS
3171struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08003172 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003174 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175};
3176
Mihai Maruseac1d578302012-01-03 23:31:35 +00003177static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178{
3179 struct inet6_ifaddr *ifa = NULL;
3180 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003181 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00003182 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Mihai Maruseac1d578302012-01-03 23:31:35 +00003184 /* initial bucket if pos is 0 */
3185 if (pos == 0) {
3186 state->bucket = 0;
3187 state->offset = 0;
3188 }
3189
3190 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
stephen hemmingerc2e21292010-03-17 20:31:10 +00003191 struct hlist_node *n;
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003192 hlist_for_each_entry_rcu_bh(ifa, n, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00003193 addr_lst) {
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003194 if (!net_eq(dev_net(ifa->idev->dev), net))
3195 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003196 /* sync with offset */
3197 if (p < state->offset) {
3198 p++;
3199 continue;
3200 }
3201 state->offset++;
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003202 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003203 }
3204
3205 /* prepare for next bucket */
3206 state->offset = 0;
3207 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00003209 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210}
3211
stephen hemmingerc2e21292010-03-17 20:31:10 +00003212static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3213 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214{
3215 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003216 struct net *net = seq_file_net(seq);
stephen hemmingerc2e21292010-03-17 20:31:10 +00003217 struct hlist_node *n = &ifa->addr_lst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Mihai Maruseac1d578302012-01-03 23:31:35 +00003219 hlist_for_each_entry_continue_rcu_bh(ifa, n, addr_lst) {
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003220 if (!net_eq(dev_net(ifa->idev->dev), net))
3221 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003222 state->offset++;
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003223 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003224 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00003225
3226 while (++state->bucket < IN6_ADDR_HSIZE) {
Mihai Maruseac1d578302012-01-03 23:31:35 +00003227 state->offset = 0;
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003228 hlist_for_each_entry_rcu_bh(ifa, n,
stephen hemmingerc2e21292010-03-17 20:31:10 +00003229 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003230 if (!net_eq(dev_net(ifa->idev->dev), net))
3231 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003232 state->offset++;
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003233 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08003234 }
3235 }
3236
stephen hemmingerc2e21292010-03-17 20:31:10 +00003237 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238}
3239
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003241 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242{
stephen hemminger5c578ae2010-03-17 20:31:11 +00003243 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00003244 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245}
3246
3247static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3248{
3249 struct inet6_ifaddr *ifa;
3250
3251 ifa = if6_get_next(seq, v);
3252 ++*pos;
3253 return ifa;
3254}
3255
3256static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003257 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258{
stephen hemminger5c578ae2010-03-17 20:31:11 +00003259 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260}
3261
3262static int if6_seq_show(struct seq_file *seq, void *v)
3263{
3264 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Harvey Harrison4b7a4272008-10-29 12:50:24 -07003265 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07003266 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 ifp->idev->dev->ifindex,
3268 ifp->prefix_len,
3269 ifp->scope,
3270 ifp->flags,
3271 ifp->idev->dev->name);
3272 return 0;
3273}
3274
Philippe De Muyter56b3d972007-07-10 23:07:31 -07003275static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 .start = if6_seq_start,
3277 .next = if6_seq_next,
3278 .show = if6_seq_show,
3279 .stop = if6_seq_stop,
3280};
3281
3282static int if6_seq_open(struct inode *inode, struct file *file)
3283{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003284 return seq_open_net(inode, file, &if6_seq_ops,
3285 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286}
3287
Arjan van de Ven9a321442007-02-12 00:55:35 -08003288static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 .owner = THIS_MODULE,
3290 .open = if6_seq_open,
3291 .read = seq_read,
3292 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08003293 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294};
3295
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003296static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003298 if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 return -ENOMEM;
3300 return 0;
3301}
3302
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003303static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08003304{
3305 proc_net_remove(net, "if_inet6");
3306}
3307
3308static struct pernet_operations if6_proc_net_ops = {
3309 .init = if6_proc_net_init,
3310 .exit = if6_proc_net_exit,
3311};
3312
3313int __init if6_proc_init(void)
3314{
3315 return register_pernet_subsys(&if6_proc_net_ops);
3316}
3317
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318void if6_proc_exit(void)
3319{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003320 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321}
3322#endif /* CONFIG_PROC_FS */
3323
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -07003324#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003325/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003326int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003327{
3328 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00003329 struct inet6_ifaddr *ifp = NULL;
3330 struct hlist_node *n;
stephen hemminger3a88a812010-03-17 20:31:12 +00003331 unsigned int hash = ipv6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00003332
stephen hemminger5c578ae2010-03-17 20:31:11 +00003333 rcu_read_lock_bh();
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003334 hlist_for_each_entry_rcu_bh(ifp, n, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09003335 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08003336 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09003337 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003338 (ifp->flags & IFA_F_HOMEADDRESS)) {
3339 ret = 1;
3340 break;
3341 }
3342 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00003343 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003344 return ret;
3345}
3346#endif
3347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348/*
3349 * Periodic address status verification
3350 */
3351
3352static void addrconf_verify(unsigned long foo)
3353{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003354 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 struct inet6_ifaddr *ifp;
stephen hemmingerc2e21292010-03-17 20:31:10 +00003356 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 int i;
3358
stephen hemminger5c578ae2010-03-17 20:31:11 +00003359 rcu_read_lock_bh();
3360 spin_lock(&addrconf_verify_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003362 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
3364 del_timer(&addr_chk_timer);
3365
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003366 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367restart:
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003368 hlist_for_each_entry_rcu_bh(ifp, node,
stephen hemminger5c578ae2010-03-17 20:31:11 +00003369 &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
3372 if (ifp->flags & IFA_F_PERMANENT)
3373 continue;
3374
3375 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003376 /* We try to batch several events at once. */
3377 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09003379 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
3380 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 spin_unlock(&ifp->lock);
3382 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 ipv6_del_addr(ifp);
3384 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09003385 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
3386 spin_unlock(&ifp->lock);
3387 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00003389 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 int deprecate = 0;
3391
3392 if (!(ifp->flags&IFA_F_DEPRECATED)) {
3393 deprecate = 1;
3394 ifp->flags |= IFA_F_DEPRECATED;
3395 }
3396
3397 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
3398 next = ifp->tstamp + ifp->valid_lft * HZ;
3399
3400 spin_unlock(&ifp->lock);
3401
3402 if (deprecate) {
3403 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
3405 ipv6_ifa_notify(0, ifp);
3406 in6_ifa_put(ifp);
3407 goto restart;
3408 }
3409#ifdef CONFIG_IPV6_PRIVACY
3410 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3411 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00003412 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
3413 ifp->idev->cnf.dad_transmits *
3414 ifp->idev->nd_parms->retrans_time / HZ;
3415
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 if (age >= ifp->prefered_lft - regen_advance) {
3417 struct inet6_ifaddr *ifpub = ifp->ifpub;
3418 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3419 next = ifp->tstamp + ifp->prefered_lft * HZ;
3420 if (!ifp->regen_count && ifpub) {
3421 ifp->regen_count++;
3422 in6_ifa_hold(ifp);
3423 in6_ifa_hold(ifpub);
3424 spin_unlock(&ifp->lock);
stephen hemminger5c578ae2010-03-17 20:31:11 +00003425
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08003426 spin_lock(&ifpub->lock);
3427 ifpub->regen_count = 0;
3428 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 ipv6_create_tempaddr(ifpub, ifp);
3430 in6_ifa_put(ifpub);
3431 in6_ifa_put(ifp);
3432 goto restart;
3433 }
3434 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3435 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3436 spin_unlock(&ifp->lock);
3437#endif
3438 } else {
3439 /* ifp->prefered_lft <= ifp->valid_lft */
3440 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3441 next = ifp->tstamp + ifp->prefered_lft * HZ;
3442 spin_unlock(&ifp->lock);
3443 }
3444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 }
3446
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003447 next_sec = round_jiffies_up(next);
3448 next_sched = next;
3449
3450 /* If rounded timeout is accurate enough, accept it. */
3451 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3452 next_sched = next_sec;
3453
3454 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3455 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3456 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3457
3458 ADBG((KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3459 now, next, next_sec, next_sched));
3460
3461 addr_chk_timer.expires = next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 add_timer(&addr_chk_timer);
stephen hemminger5c578ae2010-03-17 20:31:11 +00003463 spin_unlock(&addrconf_verify_lock);
3464 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465}
3466
Thomas Graf461d8832006-09-18 00:09:49 -07003467static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
3468{
3469 struct in6_addr *pfx = NULL;
3470
3471 if (addr)
3472 pfx = nla_data(addr);
3473
3474 if (local) {
3475 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3476 pfx = NULL;
3477 else
3478 pfx = nla_data(local);
3479 }
3480
3481 return pfx;
3482}
3483
Patrick McHardyef7c79e2007-06-05 12:38:30 -07003484static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07003485 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
3486 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
3487 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
3488};
3489
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490static int
3491inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3492{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003493 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07003494 struct ifaddrmsg *ifm;
3495 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 struct in6_addr *pfx;
Thomas Grafb933f712006-09-18 00:10:19 -07003497 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
Thomas Grafb933f712006-09-18 00:10:19 -07003499 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3500 if (err < 0)
3501 return err;
3502
3503 ifm = nlmsg_data(nlh);
3504 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 if (pfx == NULL)
3506 return -EINVAL;
3507
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003508 return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509}
3510
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003511static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
3512 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003513{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003514 u32 flags;
3515 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003516 unsigned long timeout;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003517
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003518 if (!valid_lft || (prefered_lft > valid_lft))
3519 return -EINVAL;
3520
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003521 timeout = addrconf_timeout_fixup(valid_lft, HZ);
3522 if (addrconf_finite_timeout(timeout)) {
3523 expires = jiffies_to_clock_t(timeout * HZ);
3524 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003525 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003526 } else {
3527 expires = 0;
3528 flags = 0;
3529 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003530 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003531
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003532 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
3533 if (addrconf_finite_timeout(timeout)) {
3534 if (timeout == 0)
3535 ifa_flags |= IFA_F_DEPRECATED;
3536 prefered_lft = timeout;
3537 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003538
3539 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003540 ifp->flags = (ifp->flags & ~(IFA_F_DEPRECATED | IFA_F_PERMANENT | IFA_F_NODAD | IFA_F_HOMEADDRESS)) | ifa_flags;
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003541 ifp->tstamp = jiffies;
3542 ifp->valid_lft = valid_lft;
3543 ifp->prefered_lft = prefered_lft;
3544
3545 spin_unlock_bh(&ifp->lock);
3546 if (!(ifp->flags&IFA_F_TENTATIVE))
3547 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003548
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003549 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003550 expires, flags);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003551 addrconf_verify(0);
3552
3553 return 0;
3554}
3555
3556static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3558{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003559 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07003560 struct ifaddrmsg *ifm;
3561 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 struct in6_addr *pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07003563 struct inet6_ifaddr *ifa;
3564 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003565 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3566 u8 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07003567 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Thomas Graf461d8832006-09-18 00:09:49 -07003569 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3570 if (err < 0)
3571 return err;
3572
3573 ifm = nlmsg_data(nlh);
3574 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 if (pfx == NULL)
3576 return -EINVAL;
3577
Thomas Graf461d8832006-09-18 00:09:49 -07003578 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003579 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07003580
3581 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003582 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07003583 preferred_lft = ci->ifa_prefered;
3584 } else {
3585 preferred_lft = INFINITY_LIFE_TIME;
3586 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003587 }
3588
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003589 dev = __dev_get_by_index(net, ifm->ifa_index);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003590 if (dev == NULL)
3591 return -ENODEV;
3592
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003593 /* We ignore other flags so far. */
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003594 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003595
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08003596 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003597 if (ifa == NULL) {
3598 /*
3599 * It would be best to check for !NLM_F_CREATE here but
3600 * userspace alreay relies on not having to provide this.
3601 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003602 return inet6_addr_add(net, ifm->ifa_index, pfx,
3603 ifm->ifa_prefixlen, ifa_flags,
3604 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003605 }
3606
Thomas Graf7198f8c2006-09-18 00:13:46 -07003607 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3608 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3609 err = -EEXIST;
3610 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003611 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003612
3613 in6_ifa_put(ifa);
3614
3615 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616}
3617
Thomas Graf101bb222006-09-18 00:11:52 -07003618static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3619 u8 scope, int ifindex)
3620{
3621 struct ifaddrmsg *ifm;
3622
3623 ifm = nlmsg_data(nlh);
3624 ifm->ifa_family = AF_INET6;
3625 ifm->ifa_prefixlen = prefixlen;
3626 ifm->ifa_flags = flags;
3627 ifm->ifa_scope = scope;
3628 ifm->ifa_index = ifindex;
3629}
3630
Thomas Graf85486af2006-09-18 00:11:24 -07003631static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3632 unsigned long tstamp, u32 preferred, u32 valid)
3633{
3634 struct ifa_cacheinfo ci;
3635
Thomas Graf18a31e12010-11-17 04:12:02 +00003636 ci.cstamp = cstamp_delta(cstamp);
3637 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07003638 ci.ifa_prefered = preferred;
3639 ci.ifa_valid = valid;
3640
3641 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3642}
3643
Thomas Graf101bb222006-09-18 00:11:52 -07003644static inline int rt_scope(int ifa_scope)
3645{
3646 if (ifa_scope & IFA_HOST)
3647 return RT_SCOPE_HOST;
3648 else if (ifa_scope & IFA_LINK)
3649 return RT_SCOPE_LINK;
3650 else if (ifa_scope & IFA_SITE)
3651 return RT_SCOPE_SITE;
3652 else
3653 return RT_SCOPE_UNIVERSE;
3654}
3655
Thomas Graf0ab68032006-09-18 00:12:35 -07003656static inline int inet6_ifaddr_msgsize(void)
3657{
Thomas Graf339bf982006-11-10 14:10:15 -08003658 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3659 + nla_total_size(16) /* IFA_ADDRESS */
3660 + nla_total_size(sizeof(struct ifa_cacheinfo));
Thomas Graf0ab68032006-09-18 00:12:35 -07003661}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003664 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07003667 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
Thomas Graf0ab68032006-09-18 00:12:35 -07003669 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3670 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003671 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003672
Thomas Graf101bb222006-09-18 00:11:52 -07003673 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3674 ifa->idev->dev->ifindex);
3675
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 if (!(ifa->flags&IFA_F_PERMANENT)) {
Thomas Graf85486af2006-09-18 00:11:24 -07003677 preferred = ifa->prefered_lft;
3678 valid = ifa->valid_lft;
3679 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00003681 if (preferred > tval)
3682 preferred -= tval;
3683 else
3684 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00003685 if (valid != INFINITY_LIFE_TIME) {
3686 if (valid > tval)
3687 valid -= tval;
3688 else
3689 valid = 0;
3690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 }
3692 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07003693 preferred = INFINITY_LIFE_TIME;
3694 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 }
Thomas Graf85486af2006-09-18 00:11:24 -07003696
Thomas Graf0ab68032006-09-18 00:12:35 -07003697 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
Patrick McHardy26932562007-01-31 23:16:40 -08003698 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3699 nlmsg_cancel(skb, nlh);
3700 return -EMSGSIZE;
3701 }
Thomas Graf85486af2006-09-18 00:11:24 -07003702
Thomas Graf0ab68032006-09-18 00:12:35 -07003703 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704}
3705
3706static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07003707 u32 pid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003710 u8 scope = RT_SCOPE_UNIVERSE;
3711 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
Thomas Graf101bb222006-09-18 00:11:52 -07003713 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3714 scope = RT_SCOPE_SITE;
3715
Thomas Graf0ab68032006-09-18 00:12:35 -07003716 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3717 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003718 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003719
Thomas Graf101bb222006-09-18 00:11:52 -07003720 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003721 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3722 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003723 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3724 nlmsg_cancel(skb, nlh);
3725 return -EMSGSIZE;
3726 }
Thomas Graf85486af2006-09-18 00:11:24 -07003727
Thomas Graf0ab68032006-09-18 00:12:35 -07003728 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729}
3730
3731static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003732 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003735 u8 scope = RT_SCOPE_UNIVERSE;
3736 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
Thomas Graf101bb222006-09-18 00:11:52 -07003738 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3739 scope = RT_SCOPE_SITE;
3740
Thomas Graf0ab68032006-09-18 00:12:35 -07003741 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3742 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003743 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003744
Thomas Graf101bb222006-09-18 00:11:52 -07003745 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003746 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3747 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003748 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3749 nlmsg_cancel(skb, nlh);
3750 return -EMSGSIZE;
3751 }
Thomas Graf85486af2006-09-18 00:11:24 -07003752
Thomas Graf0ab68032006-09-18 00:12:35 -07003753 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754}
3755
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003756enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 UNICAST_ADDR,
3758 MULTICAST_ADDR,
3759 ANYCAST_ADDR,
3760};
3761
Eric Dumazet234b27c2009-11-12 04:11:50 +00003762/* called with rcu_read_lock() */
3763static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
3764 struct netlink_callback *cb, enum addr_type_t type,
3765 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 struct ifmcaddr6 *ifmca;
3768 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00003769 int err = 1;
3770 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
Eric Dumazet234b27c2009-11-12 04:11:50 +00003772 read_lock_bh(&idev->lock);
3773 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00003774 case UNICAST_ADDR: {
3775 struct inet6_ifaddr *ifa;
3776
Eric Dumazet234b27c2009-11-12 04:11:50 +00003777 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00003778 list_for_each_entry(ifa, &idev->addr_list, if_list) {
3779 if (++ip_idx < s_ip_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00003780 continue;
3781 err = inet6_fill_ifaddr(skb, ifa,
3782 NETLINK_CB(cb->skb).pid,
3783 cb->nlh->nlmsg_seq,
3784 RTM_NEWADDR,
3785 NLM_F_MULTI);
3786 if (err <= 0)
3787 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00003789 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00003790 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00003791 case MULTICAST_ADDR:
3792 /* multicast address */
3793 for (ifmca = idev->mc_list; ifmca;
3794 ifmca = ifmca->next, ip_idx++) {
3795 if (ip_idx < s_ip_idx)
3796 continue;
3797 err = inet6_fill_ifmcaddr(skb, ifmca,
3798 NETLINK_CB(cb->skb).pid,
3799 cb->nlh->nlmsg_seq,
3800 RTM_GETMULTICAST,
3801 NLM_F_MULTI);
3802 if (err <= 0)
3803 break;
3804 }
3805 break;
3806 case ANYCAST_ADDR:
3807 /* anycast address */
3808 for (ifaca = idev->ac_list; ifaca;
3809 ifaca = ifaca->aca_next, ip_idx++) {
3810 if (ip_idx < s_ip_idx)
3811 continue;
3812 err = inet6_fill_ifacaddr(skb, ifaca,
3813 NETLINK_CB(cb->skb).pid,
3814 cb->nlh->nlmsg_seq,
3815 RTM_GETANYCAST,
3816 NLM_F_MULTI);
3817 if (err <= 0)
3818 break;
3819 }
3820 break;
3821 default:
3822 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00003824 read_unlock_bh(&idev->lock);
3825 *p_ip_idx = ip_idx;
3826 return err;
3827}
3828
3829static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3830 enum addr_type_t type)
3831{
3832 struct net *net = sock_net(skb->sk);
3833 int h, s_h;
3834 int idx, ip_idx;
3835 int s_idx, s_ip_idx;
3836 struct net_device *dev;
3837 struct inet6_dev *idev;
3838 struct hlist_head *head;
3839 struct hlist_node *node;
3840
3841 s_h = cb->args[0];
3842 s_idx = idx = cb->args[1];
3843 s_ip_idx = ip_idx = cb->args[2];
3844
3845 rcu_read_lock();
3846 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3847 idx = 0;
3848 head = &net->dev_index_head[h];
3849 hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
3850 if (idx < s_idx)
3851 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07003852 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00003853 s_ip_idx = 0;
3854 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003855 idev = __in6_dev_get(dev);
3856 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00003857 goto cont;
3858
3859 if (in6_dump_addrs(idev, skb, cb, type,
3860 s_ip_idx, &ip_idx) <= 0)
3861 goto done;
3862cont:
3863 idx++;
3864 }
3865 }
3866done:
3867 rcu_read_unlock();
3868 cb->args[0] = h;
3869 cb->args[1] = idx;
3870 cb->args[2] = ip_idx;
3871
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 return skb->len;
3873}
3874
3875static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3876{
3877 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003878
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 return inet6_dump_addr(skb, cb, type);
3880}
3881
3882static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3883{
3884 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003885
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 return inet6_dump_addr(skb, cb, type);
3887}
3888
3889
3890static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3891{
3892 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003893
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 return inet6_dump_addr(skb, cb, type);
3895}
3896
Thomas Graf1b29fc22006-09-18 00:10:50 -07003897static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
3898 void *arg)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003899{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003900 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003901 struct ifaddrmsg *ifm;
3902 struct nlattr *tb[IFA_MAX+1];
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003903 struct in6_addr *addr = NULL;
3904 struct net_device *dev = NULL;
3905 struct inet6_ifaddr *ifa;
3906 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003907 int err;
3908
Thomas Graf1b29fc22006-09-18 00:10:50 -07003909 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3910 if (err < 0)
3911 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003912
Thomas Graf1b29fc22006-09-18 00:10:50 -07003913 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3914 if (addr == NULL) {
3915 err = -EINVAL;
3916 goto errout;
3917 }
3918
3919 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003920 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08003921 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003922
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003923 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
3924 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003925 err = -EADDRNOTAVAIL;
3926 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003927 }
3928
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003929 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
3930 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003931 err = -ENOBUFS;
3932 goto errout_ifa;
3933 }
3934
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003935 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
3936 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003937 if (err < 0) {
3938 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3939 WARN_ON(err == -EMSGSIZE);
3940 kfree_skb(skb);
3941 goto errout_ifa;
3942 }
Benjamin Thery6fda7352008-03-05 10:47:47 -08003943 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003944errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003945 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003946errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003947 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003948}
3949
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3951{
3952 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003953 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07003954 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
Thomas Graf0ab68032006-09-18 00:12:35 -07003956 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07003957 if (skb == NULL)
3958 goto errout;
3959
3960 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003961 if (err < 0) {
3962 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3963 WARN_ON(err == -EMSGSIZE);
3964 kfree_skb(skb);
3965 goto errout;
3966 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08003967 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3968 return;
Thomas Graf5d620262006-08-15 00:35:02 -07003969errout:
3970 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08003971 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972}
3973
Dave Jonesb6f99a22007-03-22 12:27:49 -07003974static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 __s32 *array, int bytes)
3976{
Thomas Graf04561c12006-11-14 19:53:58 -08003977 BUG_ON(bytes < (DEVCONF_MAX * 4));
3978
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 memset(array, 0, bytes);
3980 array[DEVCONF_FORWARDING] = cnf->forwarding;
3981 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3982 array[DEVCONF_MTU6] = cnf->mtu6;
3983 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3984 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3985 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3986 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3987 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00003988 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
3989 jiffies_to_msecs(cnf->rtr_solicit_interval);
3990 array[DEVCONF_RTR_SOLICIT_DELAY] =
3991 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3993#ifdef CONFIG_IPV6_PRIVACY
3994 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3995 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3996 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3997 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3998 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3999#endif
4000 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004001 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004002 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004003#ifdef CONFIG_IPV6_ROUTER_PREF
4004 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00004005 array[DEVCONF_RTR_PROBE_INTERVAL] =
4006 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08004007#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004008 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4009#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004010#endif
Lorenzo Colitti653be182014-03-26 19:35:41 +09004011 array[DEVCONF_ACCEPT_RA_RT_TABLE] = cnf->accept_ra_rt_table;
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004012 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004013 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07004014#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4015 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
4016#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004017#ifdef CONFIG_IPV6_MROUTE
4018 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4019#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09004020 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09004021 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00004022 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023}
4024
Thomas Grafb382b192010-11-16 04:33:57 +00004025static inline size_t inet6_ifla6_size(void)
4026{
4027 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4028 + nla_total_size(sizeof(struct ifla_cacheinfo))
4029 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4030 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
4031 + nla_total_size(ICMP6_MIB_MAX * 8); /* IFLA_INET6_ICMP6STATS */
4032}
4033
Thomas Graf339bf982006-11-10 14:10:15 -08004034static inline size_t inet6_if_nlmsg_size(void)
4035{
4036 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4037 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4038 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4039 + nla_total_size(4) /* IFLA_MTU */
4040 + nla_total_size(4) /* IFLA_LINK */
Thomas Grafb382b192010-11-16 04:33:57 +00004041 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08004042}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004043
Eric Dumazetbe281e52011-05-19 01:14:23 +00004044static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Tejun Heo7d720c32010-02-16 15:20:26 +00004045 int items, int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004046{
4047 int i;
4048 int pad = bytes - sizeof(u64) * items;
4049 BUG_ON(pad < 0);
4050
4051 /* Use put_unaligned() because stats may not be aligned for u64. */
4052 put_unaligned(items, &stats[0]);
4053 for (i = 1; i < items; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00004054 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004055
4056 memset(&stats[items], 0, pad);
4057}
4058
Eric Dumazet4ce3c182010-06-30 13:31:19 -07004059static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib,
4060 int items, int bytes, size_t syncpoff)
4061{
4062 int i;
4063 int pad = bytes - sizeof(u64) * items;
4064 BUG_ON(pad < 0);
4065
4066 /* Use put_unaligned() because stats may not be aligned for u64. */
4067 put_unaligned(items, &stats[0]);
4068 for (i = 1; i < items; i++)
4069 put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
4070
4071 memset(&stats[items], 0, pad);
4072}
4073
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004074static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
4075 int bytes)
4076{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004077 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004078 case IFLA_INET6_STATS:
Eric Dumazet4ce3c182010-06-30 13:31:19 -07004079 __snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6,
4080 IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004081 break;
4082 case IFLA_INET6_ICMP6STATS:
Eric Dumazetbe281e52011-05-19 01:14:23 +00004083 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004084 break;
4085 }
4086}
4087
Thomas Grafb382b192010-11-16 04:33:57 +00004088static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004090 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08004091 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
Thomas Graf04561c12006-11-14 19:53:58 -08004093 NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08004096 ci.tstamp = cstamp_delta(idev->tstamp);
4097 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
4098 ci.retrans_time = jiffies_to_msecs(idev->nd_parms->retrans_time);
Thomas Graf04561c12006-11-14 19:53:58 -08004099 NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
4100
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004101 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
4102 if (nla == NULL)
Thomas Graf04561c12006-11-14 19:53:58 -08004103 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004104 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004106 /* XXX - MC not implemented */
4107
4108 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
4109 if (nla == NULL)
4110 goto nla_put_failure;
4111 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4112
4113 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4114 if (nla == NULL)
4115 goto nla_put_failure;
4116 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
Thomas Grafb382b192010-11-16 04:33:57 +00004118 return 0;
4119
4120nla_put_failure:
4121 return -EMSGSIZE;
4122}
4123
4124static size_t inet6_get_link_af_size(const struct net_device *dev)
4125{
4126 if (!__in6_dev_get(dev))
4127 return 0;
4128
4129 return inet6_ifla6_size();
4130}
4131
4132static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4133{
4134 struct inet6_dev *idev = __in6_dev_get(dev);
4135
4136 if (!idev)
4137 return -ENODATA;
4138
4139 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4140 return -EMSGSIZE;
4141
4142 return 0;
4143}
4144
Thomas Grafb382b192010-11-16 04:33:57 +00004145static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
4146 u32 pid, u32 seq, int event, unsigned int flags)
4147{
4148 struct net_device *dev = idev->dev;
4149 struct ifinfomsg *hdr;
4150 struct nlmsghdr *nlh;
4151 void *protoinfo;
4152
4153 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
4154 if (nlh == NULL)
4155 return -EMSGSIZE;
4156
4157 hdr = nlmsg_data(nlh);
4158 hdr->ifi_family = AF_INET6;
4159 hdr->__ifi_pad = 0;
4160 hdr->ifi_type = dev->type;
4161 hdr->ifi_index = dev->ifindex;
4162 hdr->ifi_flags = dev_get_flags(dev);
4163 hdr->ifi_change = 0;
4164
4165 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
4166
4167 if (dev->addr_len)
4168 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
4169
4170 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
4171 if (dev->ifindex != dev->iflink)
4172 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
4173
4174 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4175 if (protoinfo == NULL)
4176 goto nla_put_failure;
4177
4178 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4179 goto nla_put_failure;
4180
Thomas Graf04561c12006-11-14 19:53:58 -08004181 nla_nest_end(skb, protoinfo);
4182 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
Thomas Graf04561c12006-11-14 19:53:58 -08004184nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004185 nlmsg_cancel(skb, nlh);
4186 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187}
4188
4189static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
4190{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004191 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00004192 int h, s_h;
David S. Miller434a8a52009-11-11 18:53:00 -08004193 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 struct net_device *dev;
4195 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00004196 struct hlist_head *head;
4197 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
Eric Dumazet84d26972009-11-09 12:11:28 +00004199 s_h = cb->args[0];
4200 s_idx = cb->args[1];
4201
4202 rcu_read_lock();
4203 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4204 idx = 0;
4205 head = &net->dev_index_head[h];
4206 hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
4207 if (idx < s_idx)
4208 goto cont;
4209 idev = __in6_dev_get(dev);
4210 if (!idev)
4211 goto cont;
4212 if (inet6_fill_ifinfo(skb, idev,
4213 NETLINK_CB(cb->skb).pid,
4214 cb->nlh->nlmsg_seq,
4215 RTM_NEWLINK, NLM_F_MULTI) <= 0)
4216 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07004217cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00004218 idx++;
4219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 }
Eric Dumazet84d26972009-11-09 12:11:28 +00004221out:
4222 rcu_read_unlock();
4223 cb->args[1] = idx;
4224 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
4226 return skb->len;
4227}
4228
4229void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
4230{
4231 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004232 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004233 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004234
Thomas Graf339bf982006-11-10 14:10:15 -08004235 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004236 if (skb == NULL)
4237 goto errout;
4238
4239 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004240 if (err < 0) {
4241 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
4242 WARN_ON(err == -EMSGSIZE);
4243 kfree_skb(skb);
4244 goto errout;
4245 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00004246 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004247 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004248errout:
4249 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00004250 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251}
4252
Thomas Graf339bf982006-11-10 14:10:15 -08004253static inline size_t inet6_prefix_nlmsg_size(void)
4254{
4255 return NLMSG_ALIGN(sizeof(struct prefixmsg))
4256 + nla_total_size(sizeof(struct in6_addr))
4257 + nla_total_size(sizeof(struct prefix_cacheinfo));
4258}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004259
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Thomas Graf6051e2f2006-11-14 19:54:19 -08004261 struct prefix_info *pinfo, u32 pid, u32 seq,
4262 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263{
Thomas Graf6051e2f2006-11-14 19:54:19 -08004264 struct prefixmsg *pmsg;
4265 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 struct prefix_cacheinfo ci;
4267
Thomas Graf6051e2f2006-11-14 19:54:19 -08004268 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
4269 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08004270 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08004271
4272 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07004274 pmsg->prefix_pad1 = 0;
4275 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 pmsg->prefix_ifindex = idev->dev->ifindex;
4277 pmsg->prefix_len = pinfo->prefix_len;
4278 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07004279 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 pmsg->prefix_flags = 0;
4281 if (pinfo->onlink)
4282 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
4283 if (pinfo->autoconf)
4284 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
4285
Thomas Graf6051e2f2006-11-14 19:54:19 -08004286 NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287
4288 ci.preferred_time = ntohl(pinfo->prefered);
4289 ci.valid_time = ntohl(pinfo->valid);
Thomas Graf6051e2f2006-11-14 19:54:19 -08004290 NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
Thomas Graf6051e2f2006-11-14 19:54:19 -08004292 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293
Thomas Graf6051e2f2006-11-14 19:54:19 -08004294nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004295 nlmsg_cancel(skb, nlh);
4296 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297}
4298
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004299static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 struct prefix_info *pinfo)
4301{
4302 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004303 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004304 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
Thomas Graf339bf982006-11-10 14:10:15 -08004306 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004307 if (skb == NULL)
4308 goto errout;
4309
4310 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004311 if (err < 0) {
4312 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
4313 WARN_ON(err == -EMSGSIZE);
4314 kfree_skb(skb);
4315 goto errout;
4316 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004317 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
4318 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004319errout:
4320 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004321 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322}
4323
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4325{
4326 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
4327
4328 switch (event) {
4329 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07004330 /*
4331 * If the address was optimistic
4332 * we inserted the route at the start of
4333 * our DAD process, so we don't need
4334 * to do it again
4335 */
4336 if (!(ifp->rt->rt6i_node))
4337 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 if (ifp->idev->cnf.forwarding)
4339 addrconf_join_anycast(ifp);
4340 break;
4341 case RTM_DELADDR:
4342 if (ifp->idev->cnf.forwarding)
4343 addrconf_leave_anycast(ifp);
4344 addrconf_leave_solict(ifp->idev, &ifp->addr);
Changli Gaod8d1f302010-06-10 23:31:35 -07004345 dst_hold(&ifp->rt->dst);
stephen hemminger93fa1592010-04-12 05:41:31 +00004346
David S. Miller73a8bd72011-01-23 23:27:15 -08004347 if (ip6_del_rt(ifp->rt))
Changli Gaod8d1f302010-06-10 23:31:35 -07004348 dst_free(&ifp->rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 break;
4350 }
4351}
4352
4353static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4354{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07004355 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 if (likely(ifp->idev->dead == 0))
4357 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07004358 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359}
4360
4361#ifdef CONFIG_SYSCTL
4362
4363static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004364int addrconf_sysctl_forward(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 void __user *buffer, size_t *lenp, loff_t *ppos)
4366{
4367 int *valp = ctl->data;
4368 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004369 loff_t pos = *ppos;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004370 ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 int ret;
4372
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004373 /*
4374 * ctl->data points to idev->cnf.forwarding, we should
4375 * not modify it until we get the rtnl lock.
4376 */
4377 lctl = *ctl;
4378 lctl.data = &val;
4379
4380 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08004382 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00004383 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00004384 if (ret)
4385 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004386 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387}
4388
Brian Haley56d417b2009-06-01 03:07:33 -07004389static void dev_disable_change(struct inet6_dev *idev)
4390{
4391 if (!idev || !idev->dev)
4392 return;
4393
4394 if (idev->cnf.disable_ipv6)
4395 addrconf_notify(NULL, NETDEV_DOWN, idev->dev);
4396 else
4397 addrconf_notify(NULL, NETDEV_UP, idev->dev);
4398}
4399
4400static void addrconf_disable_change(struct net *net, __s32 newf)
4401{
4402 struct net_device *dev;
4403 struct inet6_dev *idev;
4404
Eric Dumazetc6d14c82009-11-04 05:43:23 -08004405 rcu_read_lock();
4406 for_each_netdev_rcu(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07004407 idev = __in6_dev_get(dev);
4408 if (idev) {
4409 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
4410 idev->cnf.disable_ipv6 = newf;
4411 if (changed)
4412 dev_disable_change(idev);
4413 }
Brian Haley56d417b2009-06-01 03:07:33 -07004414 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -08004415 rcu_read_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07004416}
4417
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004418static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07004419{
4420 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004421 int old;
4422
4423 if (!rtnl_trylock())
4424 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07004425
4426 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004427 old = *p;
4428 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07004429
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004430 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
4431 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07004432 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004433 }
Brian Haley56d417b2009-06-01 03:07:33 -07004434
4435 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07004436 net->ipv6.devconf_dflt->disable_ipv6 = newf;
4437 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004438 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07004439 dev_disable_change((struct inet6_dev *)table->extra1);
4440
4441 rtnl_unlock();
4442 return 0;
4443}
4444
4445static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004446int addrconf_sysctl_disable(ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07004447 void __user *buffer, size_t *lenp, loff_t *ppos)
4448{
4449 int *valp = ctl->data;
4450 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004451 loff_t pos = *ppos;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004452 ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07004453 int ret;
4454
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004455 /*
4456 * ctl->data points to idev->cnf.disable_ipv6, we should
4457 * not modify it until we get the rtnl lock.
4458 */
4459 lctl = *ctl;
4460 lctl.data = &val;
4461
4462 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07004463
4464 if (write)
4465 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00004466 if (ret)
4467 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07004468 return ret;
4469}
4470
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471static struct addrconf_sysctl_table
4472{
4473 struct ctl_table_header *sysctl_header;
YOSHIFUJI Hideakif6a07b22008-03-25 00:25:11 +09004474 ctl_table addrconf_vars[DEVCONF_MAX+1];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004475 char *dev_name;
Brian Haleyab32ea52006-09-22 14:15:41 -07004476} addrconf_sysctl __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 .sysctl_header = NULL,
4478 .addrconf_vars = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004479 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004480 .procname = "forwarding",
4481 .data = &ipv6_devconf.forwarding,
4482 .maxlen = sizeof(int),
4483 .mode = 0644,
4484 .proc_handler = addrconf_sysctl_forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 },
4486 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004487 .procname = "hop_limit",
4488 .data = &ipv6_devconf.hop_limit,
4489 .maxlen = sizeof(int),
4490 .mode = 0644,
4491 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 },
4493 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004494 .procname = "mtu",
4495 .data = &ipv6_devconf.mtu6,
4496 .maxlen = sizeof(int),
4497 .mode = 0644,
4498 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 },
4500 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004501 .procname = "accept_ra",
4502 .data = &ipv6_devconf.accept_ra,
4503 .maxlen = sizeof(int),
4504 .mode = 0644,
4505 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 },
4507 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004508 .procname = "accept_redirects",
4509 .data = &ipv6_devconf.accept_redirects,
4510 .maxlen = sizeof(int),
4511 .mode = 0644,
4512 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 },
4514 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004515 .procname = "autoconf",
4516 .data = &ipv6_devconf.autoconf,
4517 .maxlen = sizeof(int),
4518 .mode = 0644,
4519 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 },
4521 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004522 .procname = "dad_transmits",
4523 .data = &ipv6_devconf.dad_transmits,
4524 .maxlen = sizeof(int),
4525 .mode = 0644,
4526 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 },
4528 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004529 .procname = "router_solicitations",
4530 .data = &ipv6_devconf.rtr_solicits,
4531 .maxlen = sizeof(int),
4532 .mode = 0644,
4533 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 },
4535 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004536 .procname = "router_solicitation_interval",
4537 .data = &ipv6_devconf.rtr_solicit_interval,
4538 .maxlen = sizeof(int),
4539 .mode = 0644,
4540 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 },
4542 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004543 .procname = "router_solicitation_delay",
4544 .data = &ipv6_devconf.rtr_solicit_delay,
4545 .maxlen = sizeof(int),
4546 .mode = 0644,
4547 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 },
4549 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004550 .procname = "force_mld_version",
4551 .data = &ipv6_devconf.force_mld_version,
4552 .maxlen = sizeof(int),
4553 .mode = 0644,
4554 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 },
4556#ifdef CONFIG_IPV6_PRIVACY
4557 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004558 .procname = "use_tempaddr",
4559 .data = &ipv6_devconf.use_tempaddr,
4560 .maxlen = sizeof(int),
4561 .mode = 0644,
4562 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 },
4564 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004565 .procname = "temp_valid_lft",
4566 .data = &ipv6_devconf.temp_valid_lft,
4567 .maxlen = sizeof(int),
4568 .mode = 0644,
4569 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 },
4571 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004572 .procname = "temp_prefered_lft",
4573 .data = &ipv6_devconf.temp_prefered_lft,
4574 .maxlen = sizeof(int),
4575 .mode = 0644,
4576 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 },
4578 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004579 .procname = "regen_max_retry",
4580 .data = &ipv6_devconf.regen_max_retry,
4581 .maxlen = sizeof(int),
4582 .mode = 0644,
4583 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 },
4585 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004586 .procname = "max_desync_factor",
4587 .data = &ipv6_devconf.max_desync_factor,
4588 .maxlen = sizeof(int),
4589 .mode = 0644,
4590 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 },
4592#endif
4593 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004594 .procname = "max_addresses",
4595 .data = &ipv6_devconf.max_addresses,
4596 .maxlen = sizeof(int),
4597 .mode = 0644,
4598 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 },
4600 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004601 .procname = "accept_ra_defrtr",
4602 .data = &ipv6_devconf.accept_ra_defrtr,
4603 .maxlen = sizeof(int),
4604 .mode = 0644,
4605 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004606 },
4607 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004608 .procname = "accept_ra_pinfo",
4609 .data = &ipv6_devconf.accept_ra_pinfo,
4610 .maxlen = sizeof(int),
4611 .mode = 0644,
4612 .proc_handler = proc_dointvec,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004613 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004614#ifdef CONFIG_IPV6_ROUTER_PREF
4615 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004616 .procname = "accept_ra_rtr_pref",
4617 .data = &ipv6_devconf.accept_ra_rtr_pref,
4618 .maxlen = sizeof(int),
4619 .mode = 0644,
4620 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004621 },
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08004622 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004623 .procname = "router_probe_interval",
4624 .data = &ipv6_devconf.rtr_probe_interval,
4625 .maxlen = sizeof(int),
4626 .mode = 0644,
4627 .proc_handler = proc_dointvec_jiffies,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08004628 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08004629#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004630 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004631 .procname = "accept_ra_rt_info_max_plen",
4632 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
4633 .maxlen = sizeof(int),
4634 .mode = 0644,
4635 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004636 },
4637#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004638#endif
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004639 {
Lorenzo Colitti653be182014-03-26 19:35:41 +09004640 .procname = "accept_ra_rt_table",
4641 .data = &ipv6_devconf.accept_ra_rt_table,
4642 .maxlen = sizeof(int),
4643 .mode = 0644,
4644 .proc_handler = proc_dointvec,
4645 },
4646 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004647 .procname = "proxy_ndp",
4648 .data = &ipv6_devconf.proxy_ndp,
4649 .maxlen = sizeof(int),
4650 .mode = 0644,
4651 .proc_handler = proc_dointvec,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004652 },
4653 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004654 .procname = "accept_source_route",
4655 .data = &ipv6_devconf.accept_source_route,
4656 .maxlen = sizeof(int),
4657 .mode = 0644,
4658 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004659 },
Neil Horman95c385b2007-04-25 17:08:10 -07004660#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4661 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004662 .procname = "optimistic_dad",
4663 .data = &ipv6_devconf.optimistic_dad,
4664 .maxlen = sizeof(int),
4665 .mode = 0644,
4666 .proc_handler = proc_dointvec,
Neil Horman95c385b2007-04-25 17:08:10 -07004667
4668 },
4669#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004670#ifdef CONFIG_IPV6_MROUTE
4671 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004672 .procname = "mc_forwarding",
4673 .data = &ipv6_devconf.mc_forwarding,
4674 .maxlen = sizeof(int),
4675 .mode = 0444,
4676 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004677 },
4678#endif
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004679 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004680 .procname = "disable_ipv6",
4681 .data = &ipv6_devconf.disable_ipv6,
4682 .maxlen = sizeof(int),
4683 .mode = 0644,
4684 .proc_handler = addrconf_sysctl_disable,
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09004685 },
4686 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004687 .procname = "accept_dad",
4688 .data = &ipv6_devconf.accept_dad,
4689 .maxlen = sizeof(int),
4690 .mode = 0644,
4691 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09004692 },
4693 {
Octavian Purdilaf7734fd2009-10-02 11:39:15 +00004694 .procname = "force_tllao",
4695 .data = &ipv6_devconf.force_tllao,
4696 .maxlen = sizeof(int),
4697 .mode = 0644,
4698 .proc_handler = proc_dointvec
4699 },
4700 {
Harout Hedeshian8b2414d2012-12-21 12:38:38 -08004701 .procname = "accept_ra_prefix_route",
4702 .data = &ipv6_devconf.accept_ra_prefix_route,
4703 .maxlen = sizeof(int),
4704 .mode = 0644,
4705 .proc_handler = proc_dointvec,
4706 },
4707 {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004708 /* sentinel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 }
4710 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711};
4712
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004713static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004714 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715{
4716 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 struct addrconf_sysctl_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004719#define ADDRCONF_CTL_PATH_DEV 3
4720
4721 struct ctl_path addrconf_ctl_path[] = {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004722 { .procname = "net", },
4723 { .procname = "ipv6", },
4724 { .procname = "conf", },
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004725 { /* to be set */ },
4726 { },
4727 };
4728
4729
Arnaldo Carvalho de Meloaf879cc2006-11-17 12:14:37 -02004730 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 if (t == NULL)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004732 goto out;
4733
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004734 for (i = 0; t->addrconf_vars[i].data; i++) {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004735 t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004737 t->addrconf_vars[i].extra2 = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004740 /*
4741 * Make a copy of dev_name, because '.procname' is regarded as const
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 * by sysctl and we wouldn't want anyone to change it under our feet
4743 * (see SIOCSIFNAME).
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004744 */
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004745 t->dev_name = kstrdup(dev_name, GFP_KERNEL);
4746 if (!t->dev_name)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004747 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004749 addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004751 t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path,
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004752 t->addrconf_vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 if (t->sysctl_header == NULL)
4754 goto free_procname;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004755
4756 p->sysctl = t;
Pavel Emelyanov95897312008-01-10 17:41:45 -08004757 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004759free_procname:
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004760 kfree(t->dev_name);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004761free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 kfree(t);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004763out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08004764 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765}
4766
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004767static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
4768{
4769 struct addrconf_sysctl_table *t;
4770
4771 if (p->sysctl == NULL)
4772 return;
4773
4774 t = p->sysctl;
4775 p->sysctl = NULL;
Lucian Adrian Grijincuff538812011-05-01 01:44:01 +00004776 unregister_net_sysctl_table(t->sysctl_header);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004777 kfree(t->dev_name);
4778 kfree(t);
4779}
4780
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004781static void addrconf_sysctl_register(struct inet6_dev *idev)
4782{
Eric W. Biederman54716e32010-02-14 03:27:03 +00004783 neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6",
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004784 &ndisc_ifinfo_sysctl_change);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004785 __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004786 idev, &idev->cnf);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004787}
4788
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004789static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004791 __addrconf_sysctl_unregister(&idev->cnf);
4792 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793}
4794
4795
4796#endif
4797
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004798static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004799{
Hong Zhiguo6a519972013-03-26 01:52:45 +08004800 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004801 struct ipv6_devconf *all, *dflt;
4802
Hong Zhiguo6a519972013-03-26 01:52:45 +08004803 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
4804 if (all == NULL)
4805 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004806
Hong Zhiguo6a519972013-03-26 01:52:45 +08004807 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
4808 if (dflt == NULL)
4809 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004810
Hong Zhiguo6a519972013-03-26 01:52:45 +08004811 /* these will be inherited by all namespaces */
4812 dflt->autoconf = ipv6_defaults.autoconf;
4813 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004814
4815 net->ipv6.devconf_all = all;
4816 net->ipv6.devconf_dflt = dflt;
4817
4818#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004819 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004820 if (err < 0)
4821 goto err_reg_all;
4822
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004823 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004824 if (err < 0)
4825 goto err_reg_dflt;
4826#endif
4827 return 0;
4828
4829#ifdef CONFIG_SYSCTL
4830err_reg_dflt:
4831 __addrconf_sysctl_unregister(all);
4832err_reg_all:
4833 kfree(dflt);
4834#endif
4835err_alloc_dflt:
4836 kfree(all);
4837err_alloc_all:
4838 return err;
4839}
4840
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004841static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004842{
4843#ifdef CONFIG_SYSCTL
4844 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
4845 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
4846#endif
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08004847 if (!net_eq(net, &init_net)) {
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004848 kfree(net->ipv6.devconf_dflt);
4849 kfree(net->ipv6.devconf_all);
4850 }
4851}
4852
4853static struct pernet_operations addrconf_ops = {
4854 .init = addrconf_init_net,
4855 .exit = addrconf_exit_net,
4856};
4857
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858/*
4859 * Device notifier
4860 */
4861
4862int register_inet6addr_notifier(struct notifier_block *nb)
4863{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004864 return atomic_notifier_chain_register(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09004866EXPORT_SYMBOL(register_inet6addr_notifier);
4867
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868int unregister_inet6addr_notifier(struct notifier_block *nb)
4869{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004870 return atomic_notifier_chain_unregister(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09004872EXPORT_SYMBOL(unregister_inet6addr_notifier);
4873
Thomas Grafb382b192010-11-16 04:33:57 +00004874static struct rtnl_af_ops inet6_ops = {
4875 .family = AF_INET6,
4876 .fill_link_af = inet6_fill_link_af,
4877 .get_link_af_size = inet6_get_link_af_size,
Thomas Grafb382b192010-11-16 04:33:57 +00004878};
4879
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880/*
4881 * Init / cleanup code
4882 */
4883
4884int __init addrconf_init(void)
4885{
stephen hemmingerc2e21292010-03-17 20:31:10 +00004886 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004887
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004888 err = ipv6_addr_label_init();
4889 if (err < 0) {
4890 printk(KERN_CRIT "IPv6 Addrconf:"
4891 " cannot initialize default policy table: %d.\n", err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004892 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004895 err = register_pernet_subsys(&addrconf_ops);
4896 if (err < 0)
4897 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004898
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 /* The addrconf netdev notifier requires that loopback_dev
4900 * has it's ipv6 private information allocated and setup
4901 * before it can bring up and give link-local addresses
4902 * to other devices which are up.
4903 *
4904 * Unfortunately, loopback_dev is not necessarily the first
4905 * entry in the global dev_base list of net devices. In fact,
4906 * it is likely to be the very last entry on that list.
4907 * So this causes the notifier registry below to try and
4908 * give link-local addresses to all devices besides loopback_dev
4909 * first, then loopback_dev, which cases all the non-loopback_dev
4910 * devices to fail to get a link-local address.
4911 *
4912 * So, as a temporary fix, allocate the ipv6 structure for
4913 * loopback_dev first by hand.
4914 * Longer term, all of the dependencies ipv6 has upon the loopback
4915 * device and it being up should be removed.
4916 */
4917 rtnl_lock();
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07004918 if (!ipv6_add_dev(init_net.loopback_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 err = -ENOMEM;
4920 rtnl_unlock();
4921 if (err)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004922 goto errlo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923
stephen hemmingerc2e21292010-03-17 20:31:10 +00004924 for (i = 0; i < IN6_ADDR_HSIZE; i++)
4925 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
4926
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 register_netdevice_notifier(&ipv6_dev_notf);
4928
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 addrconf_verify(0);
Thomas Grafc127ea22007-03-22 11:58:32 -07004930
Thomas Grafb382b192010-11-16 04:33:57 +00004931 err = rtnl_af_register(&inet6_ops);
4932 if (err < 0)
4933 goto errout_af;
4934
Greg Rosec7ac8672011-06-10 01:27:09 +00004935 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
4936 NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07004937 if (err < 0)
4938 goto errout;
4939
4940 /* Only the first call to __rtnl_register can fail */
Greg Rosec7ac8672011-06-10 01:27:09 +00004941 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
4942 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
4943 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
4944 inet6_dump_ifaddr, NULL);
4945 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
4946 inet6_dump_ifmcaddr, NULL);
4947 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
4948 inet6_dump_ifacaddr, NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07004949
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004950 ipv6_addr_label_rtnl_register();
4951
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07004953errout:
Thomas Grafb382b192010-11-16 04:33:57 +00004954 rtnl_af_unregister(&inet6_ops);
4955errout_af:
Thomas Grafc127ea22007-03-22 11:58:32 -07004956 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004957errlo:
4958 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004959out_addrlabel:
4960 ipv6_addr_label_cleanup();
4961out:
Thomas Grafc127ea22007-03-22 11:58:32 -07004962 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963}
4964
Daniel Lezcano09f77092007-12-13 05:34:58 -08004965void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08004967 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 int i;
4969
4970 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004971 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004972 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973
4974 rtnl_lock();
4975
Thomas Grafb382b192010-11-16 04:33:57 +00004976 __rtnl_af_unregister(&inet6_ops);
4977
Daniel Lezcano176c39a2009-03-03 01:06:45 -08004978 /* clean dev list */
4979 for_each_netdev(&init_net, dev) {
4980 if (__in6_dev_get(dev) == NULL)
4981 continue;
4982 addrconf_ifdown(dev, 1);
4983 }
4984 addrconf_ifdown(init_net.loopback_dev, 2);
4985
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 * Check hash table.
4988 */
stephen hemminger5c578ae2010-03-17 20:31:11 +00004989 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004990 for (i = 0; i < IN6_ADDR_HSIZE; i++)
4991 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578ae2010-03-17 20:31:11 +00004992 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993
4994 del_timer(&addr_chk_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996}