blob: d427f1bae2e26531a488c4a65d52dc6bfb40436b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
15/*
16 * Changes:
17 *
18 * Janos Farkas : delete timer on ifdown
19 * <chexum@bankinf.banki.hu>
20 * Andi Kleen : kill double kfree on module
21 * unload.
22 * Maciej W. Rozycki : FDDI support
23 * sekiya@USAGI : Don't send too many RS
24 * packets.
25 * yoshfuji@USAGI : Fixed interval between DAD
26 * packets.
27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
28 * address validation timer.
29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
30 * support.
31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
32 * address on a same interface.
33 * YOSHIFUJI Hideaki @USAGI : ARCnet support
34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
35 * seq_file.
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -080036 * YOSHIFUJI Hideaki @USAGI : improved source address
37 * selection; consider scope,
38 * status etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/errno.h>
42#include <linux/types.h>
Ilpo Järvinena0bffff2009-03-21 13:36:17 -070043#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/socket.h>
45#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/net.h>
47#include <linux/in6.h>
48#include <linux/netdevice.h>
Thomas Graf18237302006-08-04 23:04:54 -070049#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/if_arp.h>
51#include <linux/if_arcnet.h>
52#include <linux/if_infiniband.h>
53#include <linux/route.h>
54#include <linux/inetdevice.h>
55#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090056#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#ifdef CONFIG_SYSCTL
58#include <linux/sysctl.h>
59#endif
Randy Dunlap4fc268d2006-01-11 12:17:47 -080060#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/delay.h>
62#include <linux/notifier.h>
Paulo Marques543537b2005-06-23 00:09:02 -070063#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020065#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <net/sock.h>
67#include <net/snmp.h>
68
69#include <net/ipv6.h>
70#include <net/protocol.h>
71#include <net/ndisc.h>
72#include <net/ip6_route.h>
73#include <net/addrconf.h>
74#include <net/tcp.h>
75#include <net/ip.h>
Thomas Graf5d620262006-08-15 00:35:02 -070076#include <net/netlink.h>
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -070077#include <net/pkt_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/if_tunnel.h>
79#include <linux/rtnetlink.h>
80
81#ifdef CONFIG_IPV6_PRIVACY
82#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#endif
84
Stephen Hemmingere21e8462010-03-20 16:09:01 -070085#include <linux/uaccess.h>
Herbert Xu7f7d9a62007-04-24 21:54:09 -070086#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88#include <linux/proc_fs.h>
89#include <linux/seq_file.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040090#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/* Set to 3 to get tracing... */
93#define ACONF_DEBUG 2
94
95#if ACONF_DEBUG >= 3
96#define ADBG(x) printk x
97#else
98#define ADBG(x)
99#endif
100
101#define INFINITY_LIFE_TIME 0xFFFFFFFF
Thomas Graf18a31e12010-11-17 04:12:02 +0000102
103static inline u32 cstamp_delta(unsigned long cstamp)
104{
105 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
106}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -0700108#define ADDRCONF_TIMER_FUZZ_MINUS (HZ > 50 ? HZ/50 : 1)
109#define ADDRCONF_TIMER_FUZZ (HZ / 4)
110#define ADDRCONF_TIMER_FUZZ_MAX (HZ)
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#ifdef CONFIG_SYSCTL
Pavel Emelyanovf52295a2007-12-02 00:58:37 +1100113static void addrconf_sysctl_register(struct inet6_dev *idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800114static void addrconf_sysctl_unregister(struct inet6_dev *idev);
115#else
116static inline void addrconf_sysctl_register(struct inet6_dev *idev)
117{
118}
119
120static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
121{
122}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#endif
124
125#ifdef CONFIG_IPV6_PRIVACY
126static int __ipv6_regen_rndid(struct inet6_dev *idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900127static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static void ipv6_regen_rndid(unsigned long data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129#endif
130
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900131static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static int ipv6_count_addresses(struct inet6_dev *idev);
133
134/*
135 * Configured unicast address hash table
136 */
stephen hemmingerc2e21292010-03-17 20:31:10 +0000137static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
stephen hemminger5c578ae2010-03-17 20:31:11 +0000138static DEFINE_SPINLOCK(addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static void addrconf_verify(unsigned long);
141
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700142static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static DEFINE_SPINLOCK(addrconf_verify_lock);
144
145static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
146static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
147
Jiri Pirko93d9b7d2010-03-10 10:28:56 +0000148static void addrconf_type_change(struct net_device *dev,
149 unsigned long event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static int addrconf_ifdown(struct net_device *dev, int how);
151
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700152static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static void addrconf_dad_timer(unsigned long data);
154static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900155static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static void addrconf_rs_timer(unsigned long data);
157static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
158static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
159
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900160static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 struct prefix_info *pinfo);
David S. Miller3e81c6d2010-03-20 16:18:00 -0700162static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
163 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Alan Sterne041c682006-03-27 01:16:30 -0800165static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Adrian Bunk888c8482008-07-22 14:21:58 -0700167static struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 .forwarding = 0,
169 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
170 .mtu6 = IPV6_MIN_MTU,
171 .accept_ra = 1,
172 .accept_redirects = 1,
173 .autoconf = 1,
174 .force_mld_version = 0,
175 .dad_transmits = 1,
176 .rtr_solicits = MAX_RTR_SOLICITATIONS,
177 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
178 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
179#ifdef CONFIG_IPV6_PRIVACY
180 .use_tempaddr = 0,
181 .temp_valid_lft = TEMP_VALID_LIFETIME,
182 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
183 .regen_max_retry = REGEN_MAX_RETRY,
184 .max_desync_factor = MAX_DESYNC_FACTOR,
185#endif
186 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800187 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800188 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800189#ifdef CONFIG_IPV6_ROUTER_PREF
190 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800191 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800192#ifdef CONFIG_IPV6_ROUTE_INFO
193 .accept_ra_rt_info_max_plen = 0,
194#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800195#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700196 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700197 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900198 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900199 .accept_dad = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200};
201
Brian Haleyab32ea52006-09-22 14:15:41 -0700202static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 .forwarding = 0,
204 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
205 .mtu6 = IPV6_MIN_MTU,
206 .accept_ra = 1,
207 .accept_redirects = 1,
208 .autoconf = 1,
209 .dad_transmits = 1,
210 .rtr_solicits = MAX_RTR_SOLICITATIONS,
211 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
212 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
213#ifdef CONFIG_IPV6_PRIVACY
214 .use_tempaddr = 0,
215 .temp_valid_lft = TEMP_VALID_LIFETIME,
216 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
217 .regen_max_retry = REGEN_MAX_RETRY,
218 .max_desync_factor = MAX_DESYNC_FACTOR,
219#endif
220 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800221 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800222 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800223#ifdef CONFIG_IPV6_ROUTER_PREF
224 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800225 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800226#ifdef CONFIG_IPV6_ROUTE_INFO
227 .accept_ra_rt_info_max_plen = 0,
228#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800229#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700230 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700231 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900232 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900233 .accept_dad = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234};
235
236/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900239const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
240const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700242/* Check if a valid qdisc is available */
David S. Miller05297942008-07-08 23:01:27 -0700243static inline bool addrconf_qdisc_ok(const struct net_device *dev)
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700244{
David S. Miller05297942008-07-08 23:01:27 -0700245 return !qdisc_tx_is_noop(dev);
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700246}
247
YOSHIFUJI Hideaki2b5ead42008-05-13 01:16:24 +0900248/* Check if a route is valid prefix route */
249static inline int addrconf_is_prefix_route(const struct rt6_info *rt)
250{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000251 return (rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0;
YOSHIFUJI Hideaki2b5ead42008-05-13 01:16:24 +0900252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254static void addrconf_del_timer(struct inet6_ifaddr *ifp)
255{
256 if (del_timer(&ifp->timer))
257 __in6_ifa_put(ifp);
258}
259
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700260enum addrconf_timer_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 AC_NONE,
262 AC_DAD,
263 AC_RS,
264};
265
266static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
267 enum addrconf_timer_t what,
268 unsigned long when)
269{
270 if (!del_timer(&ifp->timer))
271 in6_ifa_hold(ifp);
272
273 switch (what) {
274 case AC_DAD:
275 ifp->timer.function = addrconf_dad_timer;
276 break;
277 case AC_RS:
278 ifp->timer.function = addrconf_rs_timer;
279 break;
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700280 default:
281 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283 ifp->timer.expires = jiffies + when;
284 add_timer(&ifp->timer);
285}
286
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700287static int snmp6_alloc_dev(struct inet6_dev *idev)
288{
Tejun Heo7d720c32010-02-16 15:20:26 +0000289 if (snmp_mib_init((void __percpu **)idev->stats.ipv6,
Eric Dumazet1823e4c82010-06-22 20:58:41 +0000290 sizeof(struct ipstats_mib),
291 __alignof__(struct ipstats_mib)) < 0)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700292 goto err_ip;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000293 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
294 GFP_KERNEL);
295 if (!idev->stats.icmpv6dev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700296 goto err_icmp;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000297 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
298 GFP_KERNEL);
299 if (!idev->stats.icmpv6msgdev)
David L Stevens14878f72007-09-16 16:52:35 -0700300 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700301
302 return 0;
303
David L Stevens14878f72007-09-16 16:52:35 -0700304err_icmpmsg:
Eric Dumazetbe281e52011-05-19 01:14:23 +0000305 kfree(idev->stats.icmpv6dev);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700306err_icmp:
Tejun Heo7d720c32010-02-16 15:20:26 +0000307 snmp_mib_free((void __percpu **)idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700308err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700309 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700310}
311
Pavel Emelyanov16910b92007-10-17 21:23:43 -0700312static void snmp6_free_dev(struct inet6_dev *idev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700313{
Eric Dumazetbe281e52011-05-19 01:14:23 +0000314 kfree(idev->stats.icmpv6msgdev);
315 kfree(idev->stats.icmpv6dev);
Tejun Heo7d720c32010-02-16 15:20:26 +0000316 snmp_mib_free((void __percpu **)idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700317}
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/* Nobody refers to this device, we may destroy it. */
320
321void in6_dev_finish_destroy(struct inet6_dev *idev)
322{
323 struct net_device *dev = idev->dev;
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700324
stephen hemminger502a2ff2010-03-17 20:31:13 +0000325 WARN_ON(!list_empty(&idev->addr_list));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700326 WARN_ON(idev->mc_list != NULL);
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328#ifdef NET_REFCNT_DEBUG
329 printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");
330#endif
331 dev_put(dev);
332 if (!idev->dead) {
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700333 pr_warning("Freeing alive inet6 device %p\n", idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return;
335 }
336 snmp6_free_dev(idev);
Lai Jiangshan38f57d12011-03-15 17:59:14 +0800337 kfree_rcu(idev, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900340EXPORT_SYMBOL(in6_dev_finish_destroy);
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
343{
344 struct inet6_dev *ndev;
345
346 ASSERT_RTNL();
347
348 if (dev->mtu < IPV6_MIN_MTU)
349 return NULL;
350
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900351 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900353 if (ndev == NULL)
354 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Ingo Oeser322f74a2006-03-20 23:01:47 -0800356 rwlock_init(&ndev->lock);
357 ndev->dev = dev;
stephen hemminger502a2ff2010-03-17 20:31:13 +0000358 INIT_LIST_HEAD(&ndev->addr_list);
359
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900360 memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800361 ndev->cnf.mtu6 = dev->mtu;
362 ndev->cnf.sysctl = NULL;
363 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
364 if (ndev->nd_parms == NULL) {
365 kfree(ndev);
366 return NULL;
367 }
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700368 if (ndev->cnf.forwarding)
369 dev_disable_lro(dev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800370 /* We refer to the device */
371 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Ingo Oeser322f74a2006-03-20 23:01:47 -0800373 if (snmp6_alloc_dev(ndev) < 0) {
374 ADBG((KERN_WARNING
375 "%s(): cannot allocate memory for statistics; dev=%s.\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800376 __func__, dev->name));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800377 neigh_parms_release(&nd_tbl, ndev->nd_parms);
Roy Li8603e332011-09-20 15:10:16 -0400378 dev_put(dev);
379 kfree(ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800380 return NULL;
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Ingo Oeser322f74a2006-03-20 23:01:47 -0800383 if (snmp6_register_dev(ndev) < 0) {
384 ADBG((KERN_WARNING
385 "%s(): cannot create /proc/net/dev_snmp6/%s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800386 __func__, dev->name));
Ingo Oeser322f74a2006-03-20 23:01:47 -0800387 neigh_parms_release(&nd_tbl, ndev->nd_parms);
388 ndev->dead = 1;
389 in6_dev_finish_destroy(ndev);
390 return NULL;
391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Ingo Oeser322f74a2006-03-20 23:01:47 -0800393 /* One reference from device. We must do this before
394 * we invoke __ipv6_regen_rndid().
395 */
396 in6_dev_hold(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900398 if (dev->flags & (IFF_NOARP | IFF_LOOPBACK))
399 ndev->cnf.accept_dad = -1;
400
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700401#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
402 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
403 printk(KERN_INFO
404 "%s: Disabled Multicast RS\n",
405 dev->name);
406 ndev->cnf.rtr_solicits = 0;
407 }
408#endif
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410#ifdef CONFIG_IPV6_PRIVACY
stephen hemminger372e6c82010-03-17 20:31:09 +0000411 INIT_LIST_HEAD(&ndev->tempaddr_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800412 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800413 if ((dev->flags&IFF_LOOPBACK) ||
414 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700415 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700416 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700417 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800418 ndev->cnf.use_tempaddr = -1;
419 } else {
420 in6_dev_hold(ndev);
421 ipv6_regen_rndid((unsigned long) ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
Ingo Oeser322f74a2006-03-20 23:01:47 -0800423#endif
424
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700425 if (netif_running(dev) && addrconf_qdisc_ok(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700426 ndev->if_flags |= IF_READY;
427
Ingo Oeser322f74a2006-03-20 23:01:47 -0800428 ipv6_mc_init_dev(ndev);
429 ndev->tstamp = jiffies;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +1100430 addrconf_sysctl_register(ndev);
David L Stevens30c4cf52007-01-04 12:31:14 -0800431 /* protected by rtnl_lock */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000432 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800433
434 /* Join all-node multicast group */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900435 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800436
Li Weid6ddef92012-03-05 14:45:17 +0000437 /* Join all-router multicast group if forwarding is set */
Li Wei8b2aaed2012-03-07 14:58:07 +0000438 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
Li Weid6ddef92012-03-05 14:45:17 +0000439 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return ndev;
442}
443
YOSHIFUJI Hideakicee89472008-04-13 23:21:16 -0700444static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 struct inet6_dev *idev;
447
448 ASSERT_RTNL();
449
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700450 idev = __in6_dev_get(dev);
451 if (!idev) {
452 idev = ipv6_add_dev(dev);
453 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return NULL;
455 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (dev->flags&IFF_UP)
458 ipv6_mc_up(idev);
459 return idev;
460}
461
462#ifdef CONFIG_SYSCTL
463static void dev_forward_change(struct inet6_dev *idev)
464{
465 struct net_device *dev;
466 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 if (!idev)
469 return;
470 dev = idev->dev;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700471 if (idev->cnf.forwarding)
472 dev_disable_lro(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (dev && (dev->flags & IFF_MULTICAST)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (idev->cnf.forwarding)
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900475 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 else
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900477 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
stephen hemminger502a2ff2010-03-17 20:31:13 +0000479
480 list_for_each_entry(ifa, &idev->addr_list, if_list) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800481 if (ifa->flags&IFA_F_TENTATIVE)
482 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (idev->cnf.forwarding)
484 addrconf_join_anycast(ifa);
485 else
486 addrconf_leave_anycast(ifa);
487 }
488}
489
490
Pavel Emelyanove1869322008-01-10 17:43:50 -0800491static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 struct net_device *dev;
494 struct inet6_dev *idev;
495
Ben Hutchings84a2d3c2012-08-14 08:54:51 +0000496 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 idev = __in6_dev_get(dev);
498 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800499 int changed = (!idev->cnf.forwarding) ^ (!newf);
500 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if (changed)
502 dev_forward_change(idev);
503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800506
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000507static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800508{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800509 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000510 int old;
511
512 if (!rtnl_trylock())
513 return restart_syscall();
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800514
515 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000516 old = *p;
517 *p = newf;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800518
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000519 if (p == &net->ipv6.devconf_dflt->forwarding) {
520 rtnl_unlock();
521 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +0000522 }
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000523
Pavel Emelyanove1869322008-01-10 17:43:50 -0800524 if (p == &net->ipv6.devconf_all->forwarding) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800525 net->ipv6.devconf_dflt->forwarding = newf;
526 addrconf_forward_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000527 } else if ((!newf) ^ (!old))
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800528 dev_forward_change((struct inet6_dev *)table->extra1);
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700529 rtnl_unlock();
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800530
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000531 if (newf)
Daniel Lezcano7b4da532008-03-04 13:47:14 -0800532 rt6_purge_dflt_routers(net);
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000533 return 1;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800534}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535#endif
536
stephen hemminger5c578ae2010-03-17 20:31:11 +0000537/* Nobody refers to this ifaddr, destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
539{
stephen hemmingerc2e21292010-03-17 20:31:10 +0000540 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542#ifdef NET_REFCNT_DEBUG
543 printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");
544#endif
545
546 in6_dev_put(ifp->idev);
547
548 if (del_timer(&ifp->timer))
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700549 pr_notice("Timer is still running, when freeing ifa=%p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Herbert Xue9d3e082010-05-18 15:36:06 -0700551 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700552 pr_warning("Freeing alive inet6 address %p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return;
554 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700555 dst_release(&ifp->rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Lai Jiangshane5785982011-03-15 18:00:14 +0800557 kfree_rcu(ifp, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Brian Haleye55ffac2006-07-10 15:25:51 -0700560static void
561ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
562{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000563 struct list_head *p;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700564 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700565
566 /*
567 * Each device address list is sorted in order of scope -
568 * global before linklocal.
569 */
stephen hemminger502a2ff2010-03-17 20:31:13 +0000570 list_for_each(p, &idev->addr_list) {
571 struct inet6_ifaddr *ifa
572 = list_entry(p, struct inet6_ifaddr, if_list);
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700573 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700574 break;
575 }
576
David S. Millerb54c9b92010-03-25 21:25:30 -0700577 list_add_tail(&ifp->if_list, p);
Brian Haleye55ffac2006-07-10 15:25:51 -0700578}
579
stephen hemminger3a88a812010-03-17 20:31:12 +0000580static u32 ipv6_addr_hash(const struct in6_addr *addr)
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900581{
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900582 /*
583 * We perform the hash function over the last 64 bits of the address
584 * This will include the IEEE address token on links that support it.
585 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -0700586 return jhash_2words((__force u32)addr->s6_addr32[2],
587 (__force u32)addr->s6_addr32[3], 0)
stephen hemminger3a88a812010-03-17 20:31:12 +0000588 & (IN6_ADDR_HSIZE - 1);
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900589}
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/* On success it returns ifp with increased reference count */
592
593static struct inet6_ifaddr *
594ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700595 int scope, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 struct inet6_ifaddr *ifa = NULL;
598 struct rt6_info *rt;
stephen hemminger3a88a812010-03-17 20:31:12 +0000599 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 int err = 0;
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +0900601 int addr_type = ipv6_addr_type(addr);
602
603 if (addr_type == IPV6_ADDR_ANY ||
604 addr_type & IPV6_ADDR_MULTICAST ||
605 (!(idev->dev->flags & IFF_LOOPBACK) &&
606 addr_type & IPV6_ADDR_LOOPBACK))
607 return ERR_PTR(-EADDRNOTAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700609 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (idev->dead) {
611 err = -ENODEV; /*XXX*/
612 goto out2;
613 }
614
Brian Haley56d417b2009-06-01 03:07:33 -0700615 if (idev->cnf.disable_ipv6) {
Brian Haley9bdd8d42009-03-18 18:22:48 -0700616 err = -EACCES;
617 goto out2;
618 }
619
stephen hemminger5c578ae2010-03-17 20:31:11 +0000620 spin_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 /* Ignore adding duplicate addresses on an interface */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900623 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 ADBG(("ipv6_add_addr: already assigned\n"));
625 err = -EEXIST;
626 goto out;
627 }
628
Ingo Oeser322f74a2006-03-20 23:01:47 -0800629 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 if (ifa == NULL) {
632 ADBG(("ipv6_add_addr: malloc failed\n"));
633 err = -ENOBUFS;
634 goto out;
635 }
636
David S. Miller8f031512011-12-06 16:48:14 -0500637 rt = addrconf_dst_alloc(idev, addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (IS_ERR(rt)) {
639 err = PTR_ERR(rt);
640 goto out;
641 }
642
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000643 ifa->addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 spin_lock_init(&ifa->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -0700646 spin_lock_init(&ifa->state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 init_timer(&ifa->timer);
stephen hemmingerc2e21292010-03-17 20:31:10 +0000648 INIT_HLIST_NODE(&ifa->addr_lst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 ifa->timer.data = (unsigned long) ifa;
650 ifa->scope = scope;
651 ifa->prefix_len = pfxlen;
652 ifa->flags = flags | IFA_F_TENTATIVE;
653 ifa->cstamp = ifa->tstamp = jiffies;
654
Keir Fraser57f5f542006-08-29 02:43:49 -0700655 ifa->rt = rt;
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 ifa->idev = idev;
658 in6_dev_hold(idev);
659 /* For caller */
660 in6_ifa_hold(ifa);
661
662 /* Add to big hash table */
663 hash = ipv6_addr_hash(addr);
664
stephen hemminger5c578ae2010-03-17 20:31:11 +0000665 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
stephen hemminger5c578ae2010-03-17 20:31:11 +0000666 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 write_lock(&idev->lock);
669 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -0700670 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672#ifdef CONFIG_IPV6_PRIVACY
673 if (ifa->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +0000674 list_add(&ifa->tmp_list, &idev->tempaddr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 in6_ifa_hold(ifa);
676 }
677#endif
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 in6_ifa_hold(ifa);
680 write_unlock(&idev->lock);
681out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700682 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -0700684 if (likely(err == 0))
Alan Sterne041c682006-03-27 01:16:30 -0800685 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 else {
687 kfree(ifa);
688 ifa = ERR_PTR(err);
689 }
690
691 return ifa;
692out:
stephen hemminger5c578ae2010-03-17 20:31:11 +0000693 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 goto out2;
695}
696
697/* This function wants to get referenced ifp and releases it before return */
698
699static void ipv6_del_addr(struct inet6_ifaddr *ifp)
700{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000701 struct inet6_ifaddr *ifa, *ifn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 struct inet6_dev *idev = ifp->idev;
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700703 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 int deleted = 0, onlink = 0;
705 unsigned long expires = jiffies;
706
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700707 spin_lock_bh(&ifp->state_lock);
708 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -0700709 ifp->state = INET6_IFADDR_STATE_DEAD;
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700710 spin_unlock_bh(&ifp->state_lock);
711
712 if (state == INET6_IFADDR_STATE_DEAD)
713 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
stephen hemminger5c578ae2010-03-17 20:31:11 +0000715 spin_lock_bh(&addrconf_hash_lock);
716 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578ae2010-03-17 20:31:11 +0000717 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 write_lock_bh(&idev->lock);
720#ifdef CONFIG_IPV6_PRIVACY
721 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +0000722 list_del(&ifp->tmp_list);
723 if (ifp->ifpub) {
724 in6_ifa_put(ifp->ifpub);
725 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
stephen hemminger372e6c82010-03-17 20:31:09 +0000727 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
729#endif
730
stephen hemminger502a2ff2010-03-17 20:31:13 +0000731 list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (ifa == ifp) {
stephen hemminger502a2ff2010-03-17 20:31:13 +0000733 list_del_init(&ifp->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 __in6_ifa_put(ifp);
stephen hemminger502a2ff2010-03-17 20:31:13 +0000735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
737 break;
738 deleted = 1;
Kristian Slavov1d142802005-12-21 18:47:24 -0800739 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 } else if (ifp->flags & IFA_F_PERMANENT) {
741 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
742 ifp->prefix_len)) {
743 if (ifa->flags & IFA_F_PERMANENT) {
744 onlink = 1;
745 if (deleted)
746 break;
747 } else {
748 unsigned long lifetime;
749
750 if (!onlink)
751 onlink = -1;
752
753 spin_lock(&ifa->lock);
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900754
755 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
756 /*
757 * Note: Because this address is
758 * not permanent, lifetime <
759 * LONG_MAX / HZ here.
760 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (time_before(expires,
762 ifa->tstamp + lifetime * HZ))
763 expires = ifa->tstamp + lifetime * HZ;
764 spin_unlock(&ifa->lock);
765 }
766 }
767 }
768 }
769 write_unlock_bh(&idev->lock);
770
Andrey Vaginb2238562008-07-08 15:13:31 -0700771 addrconf_del_timer(ifp);
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 ipv6_ifa_notify(RTM_DELADDR, ifp);
774
Alan Sterne041c682006-03-27 01:16:30 -0800775 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 /*
778 * Purge or update corresponding prefix
779 *
780 * 1) we don't purge prefix here if address was not permanent.
781 * prefix is managed by its own lifetime.
782 * 2) if there're no addresses, delete prefix.
783 * 3) if there're still other permanent address(es),
784 * corresponding prefix is still permanent.
785 * 4) otherwise, update prefix lifetime to the
786 * longest valid lifetime among the corresponding
787 * addresses on the device.
788 * Note: subsequent RA will update lifetime.
789 *
790 * --yoshfuji
791 */
792 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
793 struct in6_addr prefix;
794 struct rt6_info *rt;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900795 struct net *net = dev_net(ifp->idev->dev);
Nicolas Dichteld5e36b02012-09-26 00:04:55 +0000796 struct flowi6 fl6 = {};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Nicolas Dichteld5e36b02012-09-26 00:04:55 +0000798 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
799 fl6.flowi6_oif = ifp->idev->dev->ifindex;
800 fl6.daddr = prefix;
801 rt = (struct rt6_info *)ip6_route_lookup(net, &fl6,
802 RT6_LOOKUP_F_IFACE);
803
804 if (rt != net->ipv6.ip6_null_entry &&
805 addrconf_is_prefix_route(rt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (onlink == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700807 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 rt = NULL;
809 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
Gao feng1716a962012-04-06 00:13:10 +0000810 rt6_set_expires(rt, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812 }
Changli Gaod8d1f302010-06-10 23:31:35 -0700813 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815
Daniel Walterc3968a82011-04-13 21:10:57 +0000816 /* clean up prefsrc entries */
817 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700818out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 in6_ifa_put(ifp);
820}
821
822#ifdef CONFIG_IPV6_PRIVACY
823static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
824{
825 struct inet6_dev *idev = ifp->idev;
826 struct in6_addr addr, *tmpaddr;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +0000827 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700828 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 int tmp_plen;
830 int ret = 0;
831 int max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -0700832 u32 addr_flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +0000833 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 write_lock(&idev->lock);
836 if (ift) {
837 spin_lock_bh(&ift->lock);
838 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
839 spin_unlock_bh(&ift->lock);
840 tmpaddr = &addr;
841 } else {
842 tmpaddr = NULL;
843 }
844retry:
845 in6_dev_hold(idev);
846 if (idev->cnf.use_tempaddr <= 0) {
847 write_unlock(&idev->lock);
848 printk(KERN_INFO
849 "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
850 in6_dev_put(idev);
851 ret = -1;
852 goto out;
853 }
854 spin_lock_bh(&ifp->lock);
855 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
856 idev->cnf.use_tempaddr = -1; /*XXX*/
857 spin_unlock_bh(&ifp->lock);
858 write_unlock(&idev->lock);
859 printk(KERN_WARNING
860 "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
861 in6_dev_put(idev);
862 ret = -1;
863 goto out;
864 }
865 in6_ifa_hold(ifp);
866 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
867 if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
868 spin_unlock_bh(&ifp->lock);
869 write_unlock(&idev->lock);
870 printk(KERN_WARNING
871 "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
872 in6_ifa_put(ifp);
873 in6_dev_put(idev);
874 ret = -1;
875 goto out;
876 }
877 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +0000878 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 tmp_valid_lft = min_t(__u32,
880 ifp->valid_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +0000881 idev->cnf.temp_valid_lft + age);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900882 tmp_prefered_lft = min_t(__u32,
883 ifp->prefered_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +0000884 idev->cnf.temp_prefered_lft + age -
Ben Hutchings784e2712010-06-26 11:42:55 +0000885 idev->cnf.max_desync_factor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 tmp_plen = ifp->prefix_len;
887 max_addresses = idev->cnf.max_addresses;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 tmp_tstamp = ifp->tstamp;
889 spin_unlock_bh(&ifp->lock);
890
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700891 regen_advance = idev->cnf.regen_max_retry *
892 idev->cnf.dad_transmits *
893 idev->nd_parms->retrans_time / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 write_unlock(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -0700895
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700896 /* A temporary address is created only if this calculated Preferred
897 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
898 * an implementation must not create a temporary address with a zero
899 * Preferred Lifetime.
900 */
901 if (tmp_prefered_lft <= regen_advance) {
902 in6_ifa_put(ifp);
903 in6_dev_put(idev);
904 ret = -1;
905 goto out;
906 }
907
Neil Horman95c385b2007-04-25 17:08:10 -0700908 addr_flags = IFA_F_TEMPORARY;
909 /* set in addrconf_prefix_rcv() */
910 if (ifp->flags & IFA_F_OPTIMISTIC)
911 addr_flags |= IFA_F_OPTIMISTIC;
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 ift = !max_addresses ||
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900914 ipv6_count_addresses(idev) < max_addresses ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 ipv6_add_addr(idev, &addr, tmp_plen,
Neil Horman95c385b2007-04-25 17:08:10 -0700916 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
917 addr_flags) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (!ift || IS_ERR(ift)) {
919 in6_ifa_put(ifp);
920 in6_dev_put(idev);
921 printk(KERN_INFO
922 "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
923 tmpaddr = &addr;
924 write_lock(&idev->lock);
925 goto retry;
926 }
927
928 spin_lock_bh(&ift->lock);
929 ift->ifpub = ifp;
930 ift->valid_lft = tmp_valid_lft;
931 ift->prefered_lft = tmp_prefered_lft;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +0000932 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 ift->tstamp = tmp_tstamp;
934 spin_unlock_bh(&ift->lock);
935
936 addrconf_dad_start(ift, 0);
937 in6_ifa_put(ift);
938 in6_dev_put(idev);
939out:
940 return ret;
941}
942#endif
943
944/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800945 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900947enum {
948 IPV6_SADDR_RULE_INIT = 0,
949 IPV6_SADDR_RULE_LOCAL,
950 IPV6_SADDR_RULE_SCOPE,
951 IPV6_SADDR_RULE_PREFERRED,
952#ifdef CONFIG_IPV6_MIP6
953 IPV6_SADDR_RULE_HOA,
954#endif
955 IPV6_SADDR_RULE_OIF,
956 IPV6_SADDR_RULE_LABEL,
957#ifdef CONFIG_IPV6_PRIVACY
958 IPV6_SADDR_RULE_PRIVACY,
959#endif
960 IPV6_SADDR_RULE_ORCHID,
961 IPV6_SADDR_RULE_PREFIX,
962 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800963};
964
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900965struct ipv6_saddr_score {
966 int rule;
967 int addr_type;
968 struct inet6_ifaddr *ifa;
969 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
970 int scopedist;
971 int matchlen;
972};
973
974struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +0900975 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900976 int ifindex;
977 int scope;
978 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +0900979 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900980};
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800981
Dave Jonesb6f99a22007-03-22 12:27:49 -0700982static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Ulrich Weber45bb0062010-02-25 23:28:58 +0000984 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -0800985 return 1;
986 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
Benjamin Thery3de23252008-05-28 14:51:24 +0200989static int ipv6_get_saddr_eval(struct net *net,
990 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +0900991 struct ipv6_saddr_dst *dst,
992 int i)
993{
994 int ret;
995
996 if (i <= score->rule) {
997 switch (i) {
998 case IPV6_SADDR_RULE_SCOPE:
999 ret = score->scopedist;
1000 break;
1001 case IPV6_SADDR_RULE_PREFIX:
1002 ret = score->matchlen;
1003 break;
1004 default:
1005 ret = !!test_bit(i, score->scorebits);
1006 }
1007 goto out;
1008 }
1009
1010 switch (i) {
1011 case IPV6_SADDR_RULE_INIT:
1012 /* Rule 0: remember if hiscore is not ready yet */
1013 ret = !!score->ifa;
1014 break;
1015 case IPV6_SADDR_RULE_LOCAL:
1016 /* Rule 1: Prefer same address */
1017 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1018 break;
1019 case IPV6_SADDR_RULE_SCOPE:
1020 /* Rule 2: Prefer appropriate scope
1021 *
1022 * ret
1023 * ^
1024 * -1 | d 15
1025 * ---+--+-+---> scope
1026 * |
1027 * | d is scope of the destination.
1028 * B-d | \
1029 * | \ <- smaller scope is better if
1030 * B-15 | \ if scope is enough for destinaion.
1031 * | ret = B - scope (-1 <= scope >= d <= 15).
1032 * d-C-1 | /
1033 * |/ <- greater is better
1034 * -C / if scope is not enough for destination.
1035 * /| ret = scope - C (-1 <= d < scope <= 15).
1036 *
1037 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1038 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1039 * Assume B = 0 and we get C > 29.
1040 */
1041 ret = __ipv6_addr_src_scope(score->addr_type);
1042 if (ret >= dst->scope)
1043 ret = -ret;
1044 else
1045 ret -= 128; /* 30 is enough */
1046 score->scopedist = ret;
1047 break;
1048 case IPV6_SADDR_RULE_PREFERRED:
1049 /* Rule 3: Avoid deprecated and optimistic addresses */
1050 ret = ipv6_saddr_preferred(score->addr_type) ||
1051 !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1052 break;
1053#ifdef CONFIG_IPV6_MIP6
1054 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001055 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001056 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001057 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1058 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001059 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001060 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001061#endif
1062 case IPV6_SADDR_RULE_OIF:
1063 /* Rule 5: Prefer outgoing interface */
1064 ret = (!dst->ifindex ||
1065 dst->ifindex == score->ifa->idev->dev->ifindex);
1066 break;
1067 case IPV6_SADDR_RULE_LABEL:
1068 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001069 ret = ipv6_addr_label(net,
1070 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001071 score->ifa->idev->dev->ifindex) == dst->label;
1072 break;
1073#ifdef CONFIG_IPV6_PRIVACY
1074 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001075 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001076 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001077 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001078 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001079 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1080 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1081 score->ifa->idev->cnf.use_tempaddr >= 2;
1082 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001083 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001084 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001085#endif
1086 case IPV6_SADDR_RULE_ORCHID:
1087 /* Rule 8-: Prefer ORCHID vs ORCHID or
1088 * non-ORCHID vs non-ORCHID
1089 */
1090 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1091 ipv6_addr_orchid(dst->addr));
1092 break;
1093 case IPV6_SADDR_RULE_PREFIX:
1094 /* Rule 8: Use longest matching prefix */
1095 score->matchlen = ret = ipv6_addr_diff(&score->ifa->addr,
1096 dst->addr);
1097 break;
1098 default:
1099 ret = 0;
1100 }
1101
1102 if (ret)
1103 __set_bit(i, score->scorebits);
1104 score->rule = i;
1105out:
1106 return ret;
1107}
1108
Brian Haley191cd582008-08-14 15:33:21 -07001109int ipv6_dev_get_saddr(struct net *net, struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001110 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001111 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001113 struct ipv6_saddr_score scores[2],
1114 *score = &scores[0], *hiscore = &scores[1];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001115 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001116 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001117 int dst_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001119 dst_type = __ipv6_addr_type(daddr);
1120 dst.addr = daddr;
1121 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1122 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001123 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001124 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001125
1126 hiscore->rule = -1;
1127 hiscore->ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001129 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001130
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001131 for_each_netdev_rcu(net, dev) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001132 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001133
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001134 /* Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001135 * - multicast and link-local destination address,
1136 * the set of candidate source address MUST only
1137 * include addresses assigned to interfaces
1138 * belonging to the same link as the outgoing
1139 * interface.
1140 * (- For site-local destination addresses, the
1141 * set of candidate source addresses MUST only
1142 * include addresses assigned to interfaces
1143 * belonging to the same site as the outgoing
1144 * interface.)
1145 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001146 if (((dst_type & IPV6_ADDR_MULTICAST) ||
1147 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1148 dst.ifindex && dev->ifindex != dst.ifindex)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001149 continue;
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 idev = __in6_dev_get(dev);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001152 if (!idev)
1153 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001155 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001156 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001157 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001159 /*
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001160 * - Tentative Address (RFC2462 section 5.4)
1161 * - A tentative address is not considered
1162 * "assigned to an interface" in the traditional
Neil Horman95c385b2007-04-25 17:08:10 -07001163 * sense, unless it is also flagged as optimistic.
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001164 * - Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001165 * - In any case, anycast addresses, multicast
1166 * addresses, and the unspecified address MUST
1167 * NOT be included in a candidate set.
1168 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001169 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1170 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001171 continue;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001172
1173 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1174
1175 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1176 score->addr_type & IPV6_ADDR_MULTICAST)) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001177 LIMIT_NETDEBUG(KERN_DEBUG
Joe Perches3b6d8212007-11-19 23:47:25 -08001178 "ADDRCONF: unspecified / multicast address "
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001179 "assigned as unicast address on %s",
1180 dev->name);
1181 continue;
1182 }
1183
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001184 score->rule = -1;
1185 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001186
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001187 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1188 int minihiscore, miniscore;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001189
Benjamin Thery3de23252008-05-28 14:51:24 +02001190 minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
1191 miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001192
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001193 if (minihiscore > miniscore) {
1194 if (i == IPV6_SADDR_RULE_SCOPE &&
1195 score->scopedist > 0) {
1196 /*
1197 * special case:
1198 * each remaining entry
1199 * has too small (not enough)
1200 * scope, because ifa entries
1201 * are sorted by their scope
1202 * values.
1203 */
1204 goto try_nextdev;
1205 }
1206 break;
1207 } else if (minihiscore < miniscore) {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001208 if (hiscore->ifa)
1209 in6_ifa_put(hiscore->ifa);
1210
1211 in6_ifa_hold(score->ifa);
1212
Ilpo Järvinena0bffff2009-03-21 13:36:17 -07001213 swap(hiscore, score);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001214
1215 /* restore our iterator */
1216 score->ifa = hiscore->ifa;
1217
1218 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001221 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001222try_nextdev:
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001223 read_unlock_bh(&idev->lock);
1224 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001225 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001227 if (!hiscore->ifa)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001228 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001229
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001230 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001231 in6_ifa_put(hiscore->ifa);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001234EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Amerigo Wang3ef208a2013-06-29 21:30:49 +08001236int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr,
1237 unsigned char banned_flags)
1238{
1239 struct inet6_ifaddr *ifp;
1240 int err = -EADDRNOTAVAIL;
1241
1242 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1243 if (ifp->scope == IFA_LINK &&
1244 !(ifp->flags & banned_flags)) {
1245 *addr = ifp->addr;
1246 err = 0;
1247 break;
1248 }
1249 }
1250 return err;
1251}
1252
Neil Horman95c385b2007-04-25 17:08:10 -07001253int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1254 unsigned char banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 struct inet6_dev *idev;
1257 int err = -EADDRNOTAVAIL;
1258
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001259 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001260 idev = __in6_dev_get(dev);
1261 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 read_lock_bh(&idev->lock);
Amerigo Wang3ef208a2013-06-29 21:30:49 +08001263 err = __ipv6_get_lladdr(idev, addr, banned_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 read_unlock_bh(&idev->lock);
1265 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001266 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return err;
1268}
1269
1270static int ipv6_count_addresses(struct inet6_dev *idev)
1271{
1272 int cnt = 0;
1273 struct inet6_ifaddr *ifp;
1274
1275 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001276 list_for_each_entry(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 cnt++;
1278 read_unlock_bh(&idev->lock);
1279 return cnt;
1280}
1281
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001282int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001283 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001285 struct inet6_ifaddr *ifp;
stephen hemmingerc2e21292010-03-17 20:31:10 +00001286 struct hlist_node *node;
stephen hemminger3a88a812010-03-17 20:31:12 +00001287 unsigned int hash = ipv6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
stephen hemminger5c578ae2010-03-17 20:31:11 +00001289 rcu_read_lock_bh();
1290 hlist_for_each_entry_rcu(ifp, node, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001291 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001292 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (ipv6_addr_equal(&ifp->addr, addr) &&
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001294 !(ifp->flags&IFA_F_TENTATIVE) &&
1295 (dev == NULL || ifp->idev->dev == dev ||
1296 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1297 rcu_read_unlock_bh();
1298 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
1300 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001301
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001302 rcu_read_unlock_bh();
1303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001305EXPORT_SYMBOL(ipv6_chk_addr);
1306
David S. Miller3e81c6d2010-03-20 16:18:00 -07001307static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1308 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
David S. Miller3e81c6d2010-03-20 16:18:00 -07001310 unsigned int hash = ipv6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001311 struct inet6_ifaddr *ifp;
1312 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
stephen hemmingerc2e21292010-03-17 20:31:10 +00001314 hlist_for_each_entry(ifp, node, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001315 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001316 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (ipv6_addr_equal(&ifp->addr, addr)) {
1318 if (dev == NULL || ifp->idev->dev == dev)
David S. Miller3e81c6d2010-03-20 16:18:00 -07001319 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
1321 }
David S. Miller3e81c6d2010-03-20 16:18:00 -07001322 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001325int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001326{
1327 struct inet6_dev *idev;
1328 struct inet6_ifaddr *ifa;
1329 int onlink;
1330
1331 onlink = 0;
1332 rcu_read_lock();
1333 idev = __in6_dev_get(dev);
1334 if (idev) {
1335 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001336 list_for_each_entry(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001337 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1338 ifa->prefix_len);
1339 if (onlink)
1340 break;
1341 }
1342 read_unlock_bh(&idev->lock);
1343 }
1344 rcu_read_unlock();
1345 return onlink;
1346}
1347
1348EXPORT_SYMBOL(ipv6_chk_prefix);
1349
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001350struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001351 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
David S. Millerb79d1d52010-03-25 21:39:21 -07001353 struct inet6_ifaddr *ifp, *result = NULL;
stephen hemminger3a88a812010-03-17 20:31:12 +00001354 unsigned int hash = ipv6_addr_hash(addr);
David S. Millerb79d1d52010-03-25 21:39:21 -07001355 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
stephen hemminger5c578ae2010-03-17 20:31:11 +00001357 rcu_read_lock_bh();
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00001358 hlist_for_each_entry_rcu_bh(ifp, node, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001359 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001360 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (ipv6_addr_equal(&ifp->addr, addr)) {
1362 if (dev == NULL || ifp->idev->dev == dev ||
1363 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07001364 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 in6_ifa_hold(ifp);
1366 break;
1367 }
1368 }
1369 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001370 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
David S. Millerb79d1d52010-03-25 21:39:21 -07001372 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375/* Gets referenced address, destroys ifaddr */
1376
Brian Haleycc411d02009-09-09 14:41:32 +00001377static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (ifp->flags&IFA_F_PERMANENT) {
1380 spin_lock_bh(&ifp->lock);
1381 addrconf_del_timer(ifp);
1382 ifp->flags |= IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00001383 if (dad_failed)
1384 ifp->flags |= IFA_F_DADFAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 spin_unlock_bh(&ifp->lock);
Herbert Xue2577a02010-03-13 12:23:29 -08001386 if (dad_failed)
1387 ipv6_ifa_notify(0, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 in6_ifa_put(ifp);
1389#ifdef CONFIG_IPV6_PRIVACY
1390 } else if (ifp->flags&IFA_F_TEMPORARY) {
1391 struct inet6_ifaddr *ifpub;
1392 spin_lock_bh(&ifp->lock);
1393 ifpub = ifp->ifpub;
1394 if (ifpub) {
1395 in6_ifa_hold(ifpub);
1396 spin_unlock_bh(&ifp->lock);
1397 ipv6_create_tempaddr(ifpub, ifp);
1398 in6_ifa_put(ifpub);
1399 } else {
1400 spin_unlock_bh(&ifp->lock);
1401 }
1402 ipv6_del_addr(ifp);
1403#endif
1404 } else
1405 ipv6_del_addr(ifp);
1406}
1407
Herbert Xuf2344a12010-05-18 15:55:27 -07001408static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1409{
1410 int err = -ENOENT;
1411
1412 spin_lock(&ifp->state_lock);
1413 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1414 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1415 err = 0;
1416 }
1417 spin_unlock(&ifp->state_lock);
1418
1419 return err;
1420}
1421
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001422void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1423{
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001424 struct inet6_dev *idev = ifp->idev;
Brian Haley9bdd8d42009-03-18 18:22:48 -07001425
Ursula Braun853dc2e2010-10-24 23:06:43 +00001426 if (addrconf_dad_end(ifp)) {
1427 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07001428 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00001429 }
Herbert Xuf2344a12010-05-18 15:55:27 -07001430
Brian Haley9bdd8d42009-03-18 18:22:48 -07001431 if (net_ratelimit())
Jens Rosenboom0522fea2009-09-17 10:24:24 -07001432 printk(KERN_INFO "%s: IPv6 duplicate address %pI6c detected!\n",
1433 ifp->idev->dev->name, &ifp->addr);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001434
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001435 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
1436 struct in6_addr addr;
1437
1438 addr.s6_addr32[0] = htonl(0xfe800000);
1439 addr.s6_addr32[1] = 0;
1440
1441 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
1442 ipv6_addr_equal(&ifp->addr, &addr)) {
1443 /* DAD failed for link-local based on MAC address */
1444 idev->cnf.disable_ipv6 = 1;
Brian Haley9bdd8d42009-03-18 18:22:48 -07001445
1446 printk(KERN_INFO "%s: IPv6 being disabled!\n",
1447 ifp->idev->dev->name);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001448 }
1449 }
1450
Brian Haleycc411d02009-09-09 14:41:32 +00001451 addrconf_dad_stop(ifp, 1);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001452}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454/* Join to solicited addr multicast group. */
1455
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001456void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
1458 struct in6_addr maddr;
1459
1460 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1461 return;
1462
1463 addrconf_addr_solict_mult(addr, &maddr);
1464 ipv6_dev_mc_inc(dev, &maddr);
1465}
1466
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001467void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
1469 struct in6_addr maddr;
1470
1471 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1472 return;
1473
1474 addrconf_addr_solict_mult(addr, &maddr);
1475 __ipv6_dev_mc_dec(idev, &maddr);
1476}
1477
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001478static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
1480 struct in6_addr addr;
Bjørn Mork2bda8a02011-07-05 23:04:13 +00001481 if (ifp->prefix_len == 127) /* RFC 6164 */
1482 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1484 if (ipv6_addr_any(&addr))
1485 return;
1486 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1487}
1488
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001489static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
1491 struct in6_addr addr;
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00001492 if (ifp->prefix_len == 127) /* RFC 6164 */
1493 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1495 if (ipv6_addr_any(&addr))
1496 return;
1497 __ipv6_dev_ac_dec(ifp->idev, &addr);
1498}
1499
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001500static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1501{
1502 if (dev->addr_len != ETH_ALEN)
1503 return -1;
1504 memcpy(eui, dev->dev_addr, 3);
1505 memcpy(eui + 5, dev->dev_addr + 3, 3);
1506
1507 /*
1508 * The zSeries OSA network cards can be shared among various
1509 * OS instances, but the OSA cards have only one MAC address.
1510 * This leads to duplicate address conflicts in conjunction
1511 * with IPv6 if more than one instance uses the same card.
1512 *
1513 * The driver for these cards can deliver a unique 16-bit
1514 * identifier for each instance sharing the same card. It is
1515 * placed instead of 0xFFFE in the interface identifier. The
1516 * "u" bit of the interface identifier is not inverted in this
1517 * case. Hence the resulting interface identifier has local
1518 * scope according to RFC2373.
1519 */
1520 if (dev->dev_id) {
1521 eui[3] = (dev->dev_id >> 8) & 0xFF;
1522 eui[4] = dev->dev_id & 0xFF;
1523 } else {
1524 eui[3] = 0xFF;
1525 eui[4] = 0xFE;
1526 eui[0] ^= 2;
1527 }
1528 return 0;
1529}
1530
1531static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1532{
1533 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1534 if (dev->addr_len != ARCNET_ALEN)
1535 return -1;
1536 memset(eui, 0, 7);
1537 eui[7] = *(u8*)dev->dev_addr;
1538 return 0;
1539}
1540
1541static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1542{
1543 if (dev->addr_len != INFINIBAND_ALEN)
1544 return -1;
1545 memcpy(eui, dev->dev_addr + 12, 8);
1546 eui[0] |= 2;
1547 return 0;
1548}
1549
stephen hemmingerc61393e2010-10-04 20:17:53 +00001550static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001551{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00001552 if (addr == 0)
1553 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001554 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1555 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1556 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1557 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1558 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1559 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1560 eui[1] = 0;
1561 eui[2] = 0x5E;
1562 eui[3] = 0xFE;
1563 memcpy(eui + 4, &addr, 4);
1564 return 0;
1565}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001566
1567static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1568{
1569 if (dev->priv_flags & IFF_ISATAP)
1570 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1571 return -1;
1572}
1573
stephen hemmingeraee80b52011-06-08 10:44:30 +00001574static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1575{
1576 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1577}
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1580{
1581 switch (dev->type) {
1582 case ARPHRD_ETHER:
1583 case ARPHRD_FDDI:
1584 case ARPHRD_IEEE802_TR:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001585 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001587 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001589 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001590 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001591 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00001592 case ARPHRD_IPGRE:
1593 return addrconf_ifid_gre(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
1595 return -1;
1596}
1597
1598static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1599{
1600 int err = -1;
1601 struct inet6_ifaddr *ifp;
1602
1603 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001604 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1606 memcpy(eui, ifp->addr.s6_addr+8, 8);
1607 err = 0;
1608 break;
1609 }
1610 }
1611 read_unlock_bh(&idev->lock);
1612 return err;
1613}
1614
1615#ifdef CONFIG_IPV6_PRIVACY
1616/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1617static int __ipv6_regen_rndid(struct inet6_dev *idev)
1618{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08001620 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 /*
1624 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1625 * check if generated address is not inappropriate
1626 *
1627 * - Reserved subnet anycast (RFC 2526)
1628 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001629 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 * 00-00-5E-FE-xx-xx-xx-xx
1631 * - value 0
1632 * - XXX: already assigned to an address on the device
1633 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001634 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1636 (idev->rndid[7]&0x80))
1637 goto regen;
1638 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1639 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1640 goto regen;
1641 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1642 goto regen;
1643 }
1644
1645 return 0;
1646}
1647
1648static void ipv6_regen_rndid(unsigned long data)
1649{
1650 struct inet6_dev *idev = (struct inet6_dev *) data;
1651 unsigned long expires;
1652
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001653 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 write_lock_bh(&idev->lock);
1655
1656 if (idev->dead)
1657 goto out;
1658
1659 if (__ipv6_regen_rndid(idev) < 0)
1660 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 expires = jiffies +
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001663 idev->cnf.temp_prefered_lft * HZ -
Ben Hutchings784e2712010-06-26 11:42:55 +00001664 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time -
1665 idev->cnf.max_desync_factor * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 if (time_before(expires, jiffies)) {
1667 printk(KERN_WARNING
1668 "ipv6_regen_rndid(): too short regeneration interval; timer disabled for %s.\n",
1669 idev->dev->name);
1670 goto out;
1671 }
1672
1673 if (!mod_timer(&idev->regen_timer, expires))
1674 in6_dev_hold(idev);
1675
1676out:
1677 write_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001678 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 in6_dev_put(idev);
1680}
1681
1682static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr) {
1683 int ret = 0;
1684
1685 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1686 ret = __ipv6_regen_rndid(idev);
1687 return ret;
1688}
1689#endif
1690
1691/*
1692 * Add prefix route.
1693 */
1694
1695static void
1696addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001697 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
Thomas Graf86872cb2006-08-22 00:01:08 -07001699 struct fib6_config cfg = {
1700 .fc_table = RT6_TABLE_PREFIX,
1701 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1702 .fc_ifindex = dev->ifindex,
1703 .fc_expires = expires,
1704 .fc_dst_len = plen,
1705 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001706 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07001707 .fc_protocol = RTPROT_KERNEL,
Thomas Graf86872cb2006-08-22 00:01:08 -07001708 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001710 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 /* Prevent useless cloning on PtP SIT.
1713 This thing is done here expecting that the whole
1714 class of non-broadcast devices need not cloning.
1715 */
Joerg Roedel0be669b2006-10-10 14:49:53 -07001716#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Thomas Graf86872cb2006-08-22 00:01:08 -07001717 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1718 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07001719#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Thomas Graf86872cb2006-08-22 00:01:08 -07001721 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001724
1725static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
1726 int plen,
1727 const struct net_device *dev,
1728 u32 flags, u32 noflags)
1729{
1730 struct fib6_node *fn;
1731 struct rt6_info *rt = NULL;
1732 struct fib6_table *table;
1733
1734 table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
1735 if (table == NULL)
1736 return NULL;
1737
1738 write_lock_bh(&table->tb6_lock);
1739 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
1740 if (!fn)
1741 goto out;
1742 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05001743 if (rt->dst.dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001744 continue;
1745 if ((rt->rt6i_flags & flags) != flags)
1746 continue;
Romain Kuntz3b690552013-01-09 15:02:26 +01001747 if ((rt->rt6i_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001748 continue;
1749 dst_hold(&rt->dst);
1750 break;
1751 }
1752out:
1753 write_unlock_bh(&table->tb6_lock);
1754 return rt;
1755}
1756
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758/* Create "default" multicast route to the interface */
1759
1760static void addrconf_add_mroute(struct net_device *dev)
1761{
Thomas Graf86872cb2006-08-22 00:01:08 -07001762 struct fib6_config cfg = {
1763 .fc_table = RT6_TABLE_LOCAL,
1764 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1765 .fc_ifindex = dev->ifindex,
1766 .fc_dst_len = 8,
1767 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001768 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001769 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Thomas Graf86872cb2006-08-22 00:01:08 -07001771 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1772
1773 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
Joerg Roedel0be669b2006-10-10 14:49:53 -07001776#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777static void sit_route_add(struct net_device *dev)
1778{
Thomas Graf86872cb2006-08-22 00:01:08 -07001779 struct fib6_config cfg = {
1780 .fc_table = RT6_TABLE_MAIN,
1781 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1782 .fc_ifindex = dev->ifindex,
1783 .fc_dst_len = 96,
1784 .fc_flags = RTF_UP | RTF_NONEXTHOP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001785 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001786 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 /* prefix length - 96 bits "::d.d.d.d" */
Thomas Graf86872cb2006-08-22 00:01:08 -07001789 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790}
Joerg Roedel0be669b2006-10-10 14:49:53 -07001791#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793static void addrconf_add_lroute(struct net_device *dev)
1794{
1795 struct in6_addr addr;
1796
1797 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
1798 addrconf_prefix_route(&addr, 64, dev, 0, 0);
1799}
1800
1801static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1802{
1803 struct inet6_dev *idev;
1804
1805 ASSERT_RTNL();
1806
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001807 idev = ipv6_find_idev(dev);
1808 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00001809 return ERR_PTR(-ENOBUFS);
1810
1811 if (idev->cnf.disable_ipv6)
1812 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 /* Add default multicast route */
Li Wei4af04ab2011-12-06 21:23:45 +00001815 if (!(dev->flags & IFF_LOOPBACK))
1816 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 /* Add link local route */
1819 addrconf_add_lroute(dev);
1820 return idev;
1821}
1822
Neil Hormane6bff992012-01-04 10:49:15 +00001823void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
1825 struct prefix_info *pinfo;
1826 __u32 valid_lft;
1827 __u32 prefered_lft;
1828 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07001830 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 if (len < sizeof(struct prefix_info)) {
1835 ADBG(("addrconf: prefix option too short\n"));
1836 return;
1837 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 /*
1840 * Validation checks ([ADDRCONF], page 19)
1841 */
1842
1843 addr_type = ipv6_addr_type(&pinfo->prefix);
1844
1845 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1846 return;
1847
1848 valid_lft = ntohl(pinfo->valid);
1849 prefered_lft = ntohl(pinfo->prefered);
1850
1851 if (prefered_lft > valid_lft) {
1852 if (net_ratelimit())
1853 printk(KERN_WARNING "addrconf: prefix option has invalid lifetime\n");
1854 return;
1855 }
1856
1857 in6_dev = in6_dev_get(dev);
1858
1859 if (in6_dev == NULL) {
1860 if (net_ratelimit())
1861 printk(KERN_DEBUG "addrconf: device %s not configured\n", dev->name);
1862 return;
1863 }
1864
1865 /*
1866 * Two things going on here:
1867 * 1) Add routes for on-link prefixes
1868 * 2) Configure prefixes with the auto flag set
1869 */
1870
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001871 if (pinfo->onlink) {
1872 struct rt6_info *rt;
1873 unsigned long rt_expires;
1874
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001875 /* Avoid arithmetic overflow. Really, we could
1876 * save rt_expires in seconds, likely valid_lft,
1877 * but it would require division in fib gc, that it
1878 * not good.
1879 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001880 if (HZ > USER_HZ)
1881 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
1882 else
1883 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08001884
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001885 if (addrconf_finite_timeout(rt_expires))
1886 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001888 rt = addrconf_get_prefix_route(&pinfo->prefix,
1889 pinfo->prefix_len,
1890 dev,
1891 RTF_ADDRCONF | RTF_PREFIX_RT,
1892 RTF_GATEWAY | RTF_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001894 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001895 /* Autoconf prefix route */
1896 if (valid_lft == 0) {
1897 ip6_del_rt(rt);
1898 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001899 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001900 /* not infinity */
Gao feng1716a962012-04-06 00:13:10 +00001901 rt6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001902 } else {
Gao feng1716a962012-04-06 00:13:10 +00001903 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
1905 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001906 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001907 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
1908 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001909 /* not infinity */
1910 flags |= RTF_EXPIRES;
1911 expires = jiffies_to_clock_t(rt_expires);
1912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001914 dev, expires, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
1916 if (rt)
Changli Gaod8d1f302010-06-10 23:31:35 -07001917 dst_release(&rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 }
1919
1920 /* Try to figure out our local address for this prefix */
1921
1922 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
1923 struct inet6_ifaddr * ifp;
1924 struct in6_addr addr;
1925 int create = 0, update_lft = 0;
1926
1927 if (pinfo->prefix_len == 64) {
1928 memcpy(&addr, &pinfo->prefix, 8);
1929 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
1930 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
1931 in6_dev_put(in6_dev);
1932 return;
1933 }
1934 goto ok;
1935 }
1936 if (net_ratelimit())
1937 printk(KERN_DEBUG "IPv6 addrconf: prefix with wrong length %d\n",
1938 pinfo->prefix_len);
1939 in6_dev_put(in6_dev);
1940 return;
1941
1942ok:
1943
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07001944 ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 if (ifp == NULL && valid_lft) {
1947 int max_addresses = in6_dev->cnf.max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -07001948 u32 addr_flags = 0;
1949
1950#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
1951 if (in6_dev->cnf.optimistic_dad &&
Neil Hormane6bff992012-01-04 10:49:15 +00001952 !net->ipv6.devconf_all->forwarding && sllao)
Neil Horman95c385b2007-04-25 17:08:10 -07001953 addr_flags = IFA_F_OPTIMISTIC;
1954#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 /* Do not allow to create too much of autoconfigured
1957 * addresses; this would be too easy way to crash kernel.
1958 */
1959 if (!max_addresses ||
1960 ipv6_count_addresses(in6_dev) < max_addresses)
1961 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
Neil Horman95c385b2007-04-25 17:08:10 -07001962 addr_type&IPV6_ADDR_SCOPE_MASK,
1963 addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 if (!ifp || IS_ERR(ifp)) {
1966 in6_dev_put(in6_dev);
1967 return;
1968 }
1969
1970 update_lft = create = 1;
1971 ifp->cstamp = jiffies;
1972 addrconf_dad_start(ifp, RTF_ADDRCONF|RTF_PREFIX_RT);
1973 }
1974
1975 if (ifp) {
1976 int flags;
1977 unsigned long now;
1978#ifdef CONFIG_IPV6_PRIVACY
1979 struct inet6_ifaddr *ift;
1980#endif
1981 u32 stored_lft;
1982
1983 /* update lifetime (RFC2462 5.5.3 e) */
1984 spin_lock(&ifp->lock);
1985 now = jiffies;
1986 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
1987 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
1988 else
1989 stored_lft = 0;
1990 if (!update_lft && stored_lft) {
1991 if (valid_lft > MIN_VALID_LIFETIME ||
1992 valid_lft > stored_lft)
1993 update_lft = 1;
1994 else if (stored_lft <= MIN_VALID_LIFETIME) {
1995 /* valid_lft <= stored_lft is always true */
Brian Haleya1ed0522009-07-02 07:10:52 +00001996 /*
1997 * RFC 4862 Section 5.5.3e:
1998 * "Note that the preferred lifetime of
1999 * the corresponding address is always
2000 * reset to the Preferred Lifetime in
2001 * the received Prefix Information
2002 * option, regardless of whether the
2003 * valid lifetime is also reset or
2004 * ignored."
2005 *
2006 * So if the preferred lifetime in
2007 * this advertisement is different
2008 * than what we have stored, but the
2009 * valid lifetime is invalid, just
2010 * reset prefered_lft.
2011 *
2012 * We must set the valid lifetime
2013 * to the stored lifetime since we'll
2014 * be updating the timestamp below,
2015 * else we'll set it back to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002016 * minimum.
Brian Haleya1ed0522009-07-02 07:10:52 +00002017 */
2018 if (prefered_lft != ifp->prefered_lft) {
2019 valid_lft = stored_lft;
2020 update_lft = 1;
2021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 } else {
2023 valid_lft = MIN_VALID_LIFETIME;
2024 if (valid_lft < prefered_lft)
2025 prefered_lft = valid_lft;
2026 update_lft = 1;
2027 }
2028 }
2029
2030 if (update_lft) {
2031 ifp->valid_lft = valid_lft;
2032 ifp->prefered_lft = prefered_lft;
2033 ifp->tstamp = now;
2034 flags = ifp->flags;
2035 ifp->flags &= ~IFA_F_DEPRECATED;
2036 spin_unlock(&ifp->lock);
2037
2038 if (!(flags&IFA_F_TENTATIVE))
2039 ipv6_ifa_notify(0, ifp);
2040 } else
2041 spin_unlock(&ifp->lock);
2042
2043#ifdef CONFIG_IPV6_PRIVACY
2044 read_lock_bh(&in6_dev->lock);
2045 /* update all temporary addresses in the list */
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002046 list_for_each_entry(ift, &in6_dev->tempaddr_list,
2047 tmp_list) {
2048 int age, max_valid, max_prefered;
2049
Benoit Boissinotc6fbfac2008-04-02 00:00:58 -07002050 if (ifp != ift->ifpub)
2051 continue;
2052
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002053 /*
2054 * RFC 4941 section 3.3:
2055 * If a received option will extend the lifetime
2056 * of a public address, the lifetimes of
2057 * temporary addresses should be extended,
2058 * subject to the overall constraint that no
2059 * temporary addresses should ever remain
2060 * "valid" or "preferred" for a time longer than
2061 * (TEMP_VALID_LIFETIME) or
2062 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR),
2063 * respectively.
2064 */
2065 age = (now - ift->cstamp) / HZ;
2066 max_valid = in6_dev->cnf.temp_valid_lft - age;
2067 if (max_valid < 0)
2068 max_valid = 0;
2069
2070 max_prefered = in6_dev->cnf.temp_prefered_lft -
2071 in6_dev->cnf.max_desync_factor -
2072 age;
2073 if (max_prefered < 0)
2074 max_prefered = 0;
2075
2076 if (valid_lft > max_valid)
2077 valid_lft = max_valid;
2078
2079 if (prefered_lft > max_prefered)
2080 prefered_lft = max_prefered;
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 spin_lock(&ift->lock);
2083 flags = ift->flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002084 ift->valid_lft = valid_lft;
2085 ift->prefered_lft = prefered_lft;
2086 ift->tstamp = now;
2087 if (prefered_lft > 0)
2088 ift->flags &= ~IFA_F_DEPRECATED;
2089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 spin_unlock(&ift->lock);
2091 if (!(flags&IFA_F_TENTATIVE))
2092 ipv6_ifa_notify(0, ift);
2093 }
2094
Glenn Wursteraed65502010-09-27 07:04:30 +00002095 if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 /*
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002097 * When a new public address is created as
2098 * described in [ADDRCONF], also create a new
2099 * temporary address. Also create a temporary
2100 * address if it's enabled but no temporary
2101 * address currently exists.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002103 read_unlock_bh(&in6_dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 ipv6_create_tempaddr(ifp, NULL);
2105 } else {
2106 read_unlock_bh(&in6_dev->lock);
2107 }
2108#endif
2109 in6_ifa_put(ifp);
2110 addrconf_verify(0);
2111 }
2112 }
2113 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2114 in6_dev_put(in6_dev);
2115}
2116
2117/*
2118 * Set destination address.
2119 * Special case for SIT interfaces where we create a new "virtual"
2120 * device.
2121 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002122int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
2124 struct in6_ifreq ireq;
2125 struct net_device *dev;
2126 int err = -EINVAL;
2127
2128 rtnl_lock();
2129
2130 err = -EFAULT;
2131 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2132 goto err_exit;
2133
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002134 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 err = -ENODEV;
2137 if (dev == NULL)
2138 goto err_exit;
2139
Joerg Roedel0be669b2006-10-10 14:49:53 -07002140#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002142 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 struct ip_tunnel_parm p;
2145
2146 err = -EADDRNOTAVAIL;
2147 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2148 goto err_exit;
2149
2150 memset(&p, 0, sizeof(p));
2151 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2152 p.iph.saddr = 0;
2153 p.iph.version = 4;
2154 p.iph.ihl = 5;
2155 p.iph.protocol = IPPROTO_IPV6;
2156 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002157 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002159 if (ops->ndo_do_ioctl) {
2160 mm_segment_t oldfs = get_fs();
2161
2162 set_fs(KERNEL_DS);
2163 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2164 set_fs(oldfs);
2165 } else
2166 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 if (err == 0) {
2169 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002170 dev = __dev_get_by_name(net, p.name);
2171 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 goto err_exit;
2173 err = dev_open(dev);
2174 }
2175 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002176#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178err_exit:
2179 rtnl_unlock();
2180 return err;
2181}
2182
2183/*
2184 * Manual configuration of address on an interface
2185 */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002186static int inet6_addr_add(struct net *net, int ifindex, const struct in6_addr *pfx,
Thomas Graf24ef0da2008-05-28 16:54:22 +02002187 unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002188 __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
2190 struct inet6_ifaddr *ifp;
2191 struct inet6_dev *idev;
2192 struct net_device *dev;
2193 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002194 u32 flags;
2195 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002196 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002199
Thomas Graf24ef0da2008-05-28 16:54:22 +02002200 if (plen > 128)
2201 return -EINVAL;
2202
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002203 /* check the lifetime */
2204 if (!valid_lft || prefered_lft > valid_lft)
2205 return -EINVAL;
2206
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002207 dev = __dev_get_by_index(net, ifindex);
2208 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002210
Brian Haley64e724f2010-07-20 10:34:30 +00002211 idev = addrconf_add_dev(dev);
2212 if (IS_ERR(idev))
2213 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
2215 scope = ipv6_addr_scope(pfx);
2216
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002217 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2218 if (addrconf_finite_timeout(timeout)) {
2219 expires = jiffies_to_clock_t(timeout * HZ);
2220 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002221 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002222 } else {
2223 expires = 0;
2224 flags = 0;
2225 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002226 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002227
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002228 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2229 if (addrconf_finite_timeout(timeout)) {
2230 if (timeout == 0)
2231 ifa_flags |= IFA_F_DEPRECATED;
2232 prefered_lft = timeout;
2233 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002234
2235 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
2236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 if (!IS_ERR(ifp)) {
Herbert Xu06aebfb2006-08-09 16:52:04 -07002238 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002239 ifp->valid_lft = valid_lft;
2240 ifp->prefered_lft = prefered_lft;
2241 ifp->tstamp = jiffies;
Herbert Xu06aebfb2006-08-09 16:52:04 -07002242 spin_unlock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002243
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002244 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002245 expires, flags);
Neil Horman95c385b2007-04-25 17:08:10 -07002246 /*
2247 * Note that section 3.1 of RFC 4429 indicates
2248 * that the Optimistic flag should not be set for
2249 * manually configured addresses
2250 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 addrconf_dad_start(ifp, 0);
2252 in6_ifa_put(ifp);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002253 addrconf_verify(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 return 0;
2255 }
2256
2257 return PTR_ERR(ifp);
2258}
2259
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002260static int inet6_addr_del(struct net *net, int ifindex, const struct in6_addr *pfx,
Thomas Graf24ef0da2008-05-28 16:54:22 +02002261 unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
2263 struct inet6_ifaddr *ifp;
2264 struct inet6_dev *idev;
2265 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002266
Thomas Graf24ef0da2008-05-28 16:54:22 +02002267 if (plen > 128)
2268 return -EINVAL;
2269
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002270 dev = __dev_get_by_index(net, ifindex);
2271 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 return -ENODEV;
2273
2274 if ((idev = __in6_dev_get(dev)) == NULL)
2275 return -ENXIO;
2276
2277 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002278 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (ifp->prefix_len == plen &&
2280 ipv6_addr_equal(pfx, &ifp->addr)) {
2281 in6_ifa_hold(ifp);
2282 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 ipv6_del_addr(ifp);
2285
2286 /* If the last address is deleted administratively,
2287 disable IPv6 on this interface.
2288 */
stephen hemminger502a2ff2010-03-17 20:31:13 +00002289 if (list_empty(&idev->addr_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 addrconf_ifdown(idev->dev, 1);
2291 return 0;
2292 }
2293 }
2294 read_unlock_bh(&idev->lock);
2295 return -EADDRNOTAVAIL;
2296}
2297
2298
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002299int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
2301 struct in6_ifreq ireq;
2302 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 if (!capable(CAP_NET_ADMIN))
2305 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2308 return -EFAULT;
2309
2310 rtnl_lock();
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002311 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2312 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2313 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 rtnl_unlock();
2315 return err;
2316}
2317
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002318int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
2320 struct in6_ifreq ireq;
2321 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 if (!capable(CAP_NET_ADMIN))
2324 return -EPERM;
2325
2326 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2327 return -EFAULT;
2328
2329 rtnl_lock();
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002330 err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2331 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 rtnl_unlock();
2333 return err;
2334}
2335
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002336static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2337 int plen, int scope)
2338{
2339 struct inet6_ifaddr *ifp;
2340
2341 ifp = ipv6_add_addr(idev, addr, plen, scope, IFA_F_PERMANENT);
2342 if (!IS_ERR(ifp)) {
2343 spin_lock_bh(&ifp->lock);
2344 ifp->flags &= ~IFA_F_TENTATIVE;
2345 spin_unlock_bh(&ifp->lock);
2346 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2347 in6_ifa_put(ifp);
2348 }
2349}
2350
Joerg Roedel0be669b2006-10-10 14:49:53 -07002351#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352static void sit_add_v4_addrs(struct inet6_dev *idev)
2353{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 struct in6_addr addr;
2355 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002356 struct net *net = dev_net(idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 int scope;
2358
2359 ASSERT_RTNL();
2360
2361 memset(&addr, 0, sizeof(struct in6_addr));
2362 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2363
2364 if (idev->dev->flags&IFF_POINTOPOINT) {
2365 addr.s6_addr32[0] = htonl(0xfe800000);
2366 scope = IFA_LINK;
2367 } else {
2368 scope = IPV6_ADDR_COMPATv4;
2369 }
2370
2371 if (addr.s6_addr32[3]) {
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002372 add_addr(idev, &addr, 128, scope);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 return;
2374 }
2375
Benjamin Thery6fda7352008-03-05 10:47:47 -08002376 for_each_netdev(net, dev) {
Herbert Xue5ed6392005-10-03 14:35:55 -07002377 struct in_device * in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (in_dev && (dev->flags & IFF_UP)) {
2379 struct in_ifaddr * ifa;
2380
2381 int flag = scope;
2382
2383 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2384 int plen;
2385
2386 addr.s6_addr32[3] = ifa->ifa_local;
2387
2388 if (ifa->ifa_scope == RT_SCOPE_LINK)
2389 continue;
2390 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2391 if (idev->dev->flags&IFF_POINTOPOINT)
2392 continue;
2393 flag |= IFA_HOST;
2394 }
2395 if (idev->dev->flags&IFF_POINTOPOINT)
2396 plen = 64;
2397 else
2398 plen = 96;
2399
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002400 add_addr(idev, &addr, plen, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
2402 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002405#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407static void init_loopback(struct net_device *dev)
2408{
2409 struct inet6_dev *idev;
Balakumaran Kannanef95e3d2013-04-02 16:15:05 +05302410 struct net_device *sp_dev;
2411 struct inet6_ifaddr *sp_ifa;
2412 struct rt6_info *sp_rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
2414 /* ::1 */
2415
2416 ASSERT_RTNL();
2417
2418 if ((idev = ipv6_find_idev(dev)) == NULL) {
2419 printk(KERN_DEBUG "init loopback: add_dev failed\n");
2420 return;
2421 }
2422
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002423 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Balakumaran Kannanef95e3d2013-04-02 16:15:05 +05302424
2425 /* Add routes to other interface's IPv6 addresses */
2426 for_each_netdev(dev_net(dev), sp_dev) {
2427 if (!strcmp(sp_dev->name, dev->name))
2428 continue;
2429
2430 idev = __in6_dev_get(sp_dev);
2431 if (!idev)
2432 continue;
2433
2434 read_lock_bh(&idev->lock);
2435 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
2436
2437 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
2438 continue;
2439
Gao fengdabed302013-06-16 11:14:30 +08002440 if (sp_ifa->rt)
2441 continue;
2442
Balakumaran Kannanef95e3d2013-04-02 16:15:05 +05302443 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, 0);
2444
2445 /* Failure cases are ignored */
Gao feng2f4997c2013-06-02 22:16:21 +00002446 if (!IS_ERR(sp_rt)) {
2447 sp_ifa->rt = sp_rt;
Balakumaran Kannanef95e3d2013-04-02 16:15:05 +05302448 ip6_ins_rt(sp_rt);
Gao feng2f4997c2013-06-02 22:16:21 +00002449 }
Balakumaran Kannanef95e3d2013-04-02 16:15:05 +05302450 }
2451 read_unlock_bh(&idev->lock);
2452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
2454
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002455static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
2457 struct inet6_ifaddr * ifp;
Neil Horman95c385b2007-04-25 17:08:10 -07002458 u32 addr_flags = IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Neil Horman95c385b2007-04-25 17:08:10 -07002460#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2461 if (idev->cnf.optimistic_dad &&
David Miller702beb82008-07-20 18:17:02 -07002462 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
Neil Horman95c385b2007-04-25 17:08:10 -07002463 addr_flags |= IFA_F_OPTIMISTIC;
2464#endif
2465
2466
2467 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002469 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 addrconf_dad_start(ifp, 0);
2471 in6_ifa_put(ifp);
2472 }
2473}
2474
2475static void addrconf_dev_config(struct net_device *dev)
2476{
2477 struct in6_addr addr;
2478 struct inet6_dev * idev;
2479
2480 ASSERT_RTNL();
2481
Herbert Xu74235a22007-06-14 13:02:55 -07002482 if ((dev->type != ARPHRD_ETHER) &&
2483 (dev->type != ARPHRD_FDDI) &&
2484 (dev->type != ARPHRD_IEEE802_TR) &&
2485 (dev->type != ARPHRD_ARCNET) &&
2486 (dev->type != ARPHRD_INFINIBAND)) {
2487 /* Alas, we support only Ethernet autoconfiguration. */
2488 return;
2489 }
2490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00002492 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 return;
2494
2495 memset(&addr, 0, sizeof(struct in6_addr));
2496 addr.s6_addr32[0] = htonl(0xFE800000);
2497
2498 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2499 addrconf_add_linklocal(idev, &addr);
2500}
2501
Joerg Roedel0be669b2006-10-10 14:49:53 -07002502#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503static void addrconf_sit_config(struct net_device *dev)
2504{
2505 struct inet6_dev *idev;
2506
2507 ASSERT_RTNL();
2508
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002509 /*
2510 * Configure the tunnel with one of our IPv4
2511 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 * our v4 addrs in the tunnel
2513 */
2514
2515 if ((idev = ipv6_find_idev(dev)) == NULL) {
2516 printk(KERN_DEBUG "init sit: add_dev failed\n");
2517 return;
2518 }
2519
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002520 if (dev->priv_flags & IFF_ISATAP) {
2521 struct in6_addr addr;
2522
2523 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2524 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2525 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2526 addrconf_add_linklocal(idev, &addr);
2527 return;
2528 }
2529
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 sit_add_v4_addrs(idev);
2531
2532 if (dev->flags&IFF_POINTOPOINT) {
2533 addrconf_add_mroute(dev);
2534 addrconf_add_lroute(dev);
2535 } else
2536 sit_route_add(dev);
2537}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002538#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
stephen hemmingeraee80b52011-06-08 10:44:30 +00002540#if defined(CONFIG_NET_IPGRE) || defined(CONFIG_NET_IPGRE_MODULE)
2541static void addrconf_gre_config(struct net_device *dev)
2542{
2543 struct inet6_dev *idev;
2544 struct in6_addr addr;
2545
2546 pr_info("ipv6: addrconf_gre_config(%s)\n", dev->name);
2547
2548 ASSERT_RTNL();
2549
2550 if ((idev = ipv6_find_idev(dev)) == NULL) {
2551 printk(KERN_DEBUG "init gre: add_dev failed\n");
2552 return;
2553 }
2554
2555 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2556 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2557
2558 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2559 addrconf_add_linklocal(idev, &addr);
2560}
2561#endif
2562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563static inline int
2564ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2565{
2566 struct in6_addr lladdr;
2567
Neil Horman95c385b2007-04-25 17:08:10 -07002568 if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 addrconf_add_linklocal(idev, &lladdr);
2570 return 0;
2571 }
2572 return -1;
2573}
2574
2575static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2576{
2577 struct net_device *link_dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002578 struct net *net = dev_net(idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
2580 /* first try to inherit the link-local address from the link device */
2581 if (idev->dev->iflink &&
Benjamin Thery6fda7352008-03-05 10:47:47 -08002582 (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 if (!ipv6_inherit_linklocal(idev, link_dev))
2584 return;
2585 }
2586 /* then try to inherit it from any device */
Benjamin Thery6fda7352008-03-05 10:47:47 -08002587 for_each_netdev(net, link_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 if (!ipv6_inherit_linklocal(idev, link_dev))
2589 return;
2590 }
2591 printk(KERN_DEBUG "init ip6-ip6: add_linklocal failed\n");
2592}
2593
2594/*
2595 * Autoconfigure tunnel with a link-local address so routing protocols,
2596 * DHCPv6, MLD etc. can be run over the virtual link
2597 */
2598
2599static void addrconf_ip6_tnl_config(struct net_device *dev)
2600{
2601 struct inet6_dev *idev;
2602
2603 ASSERT_RTNL();
2604
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002605 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00002606 if (IS_ERR(idev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 printk(KERN_DEBUG "init ip6-ip6: add_dev failed\n");
2608 return;
2609 }
2610 ip6_tnl_add_linklocal(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611}
2612
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002613static int addrconf_notify(struct notifier_block *this, unsigned long event,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 void * data)
2615{
2616 struct net_device *dev = (struct net_device *) data;
Herbert Xu74235a22007-06-14 13:02:55 -07002617 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002618 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07002619 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002621 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002622 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07002623 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002624 idev = ipv6_add_dev(dev);
2625 if (!idev)
Herbert Xub217d612007-07-30 17:04:52 -07002626 return notifier_from_errno(-ENOMEM);
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002627 }
2628 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002631 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07002632 if (dev->flags & IFF_SLAVE)
2633 break;
2634
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002635 if (event == NETDEV_UP) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002636 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002637 /* device is not ready yet. */
2638 printk(KERN_INFO
2639 "ADDRCONF(NETDEV_UP): %s: "
2640 "link is not ready\n",
2641 dev->name);
2642 break;
2643 }
Kristian Slavov99081042006-02-08 16:10:53 -08002644
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002645 if (!idev && dev->mtu >= IPV6_MIN_MTU)
2646 idev = ipv6_add_dev(dev);
2647
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08002648 if (idev) {
Kristian Slavov99081042006-02-08 16:10:53 -08002649 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08002650 run_pending = 1;
2651 }
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002652 } else {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002653 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002654 /* device is still not ready. */
2655 break;
2656 }
2657
2658 if (idev) {
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002659 if (idev->if_flags & IF_READY)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002660 /* device is already configured. */
2661 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002662 idev->if_flags |= IF_READY;
2663 }
2664
2665 printk(KERN_INFO
2666 "ADDRCONF(NETDEV_CHANGE): %s: "
2667 "link becomes ready\n",
2668 dev->name);
2669
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002670 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002671 }
2672
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002673 switch (dev->type) {
Joerg Roedel0be669b2006-10-10 14:49:53 -07002674#if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 case ARPHRD_SIT:
2676 addrconf_sit_config(dev);
2677 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002678#endif
stephen hemmingeraee80b52011-06-08 10:44:30 +00002679#if defined(CONFIG_NET_IPGRE) || defined(CONFIG_NET_IPGRE_MODULE)
2680 case ARPHRD_IPGRE:
2681 addrconf_gre_config(dev);
2682 break;
2683#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 case ARPHRD_TUNNEL6:
2685 addrconf_ip6_tnl_config(dev);
2686 break;
2687 case ARPHRD_LOOPBACK:
2688 init_loopback(dev);
2689 break;
2690
2691 default:
2692 addrconf_dev_config(dev);
2693 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002694 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 if (idev) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002697 if (run_pending)
2698 addrconf_dad_run(idev);
2699
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002700 /*
2701 * If the MTU changed during the interface down,
2702 * when the interface up, the changed MTU must be
2703 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002705 if (idev->cnf.mtu6 != dev->mtu &&
2706 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 rt6_mtu_change(dev, dev->mtu);
2708 idev->cnf.mtu6 = dev->mtu;
2709 }
2710 idev->tstamp = jiffies;
2711 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002712
2713 /*
2714 * If the changed mtu during down is lower than
2715 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 */
2717 if (dev->mtu < IPV6_MIN_MTU)
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002718 addrconf_ifdown(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 }
2720 break;
2721
2722 case NETDEV_CHANGEMTU:
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002723 if (idev && dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 rt6_mtu_change(dev, dev->mtu);
2725 idev->cnf.mtu6 = dev->mtu;
2726 break;
2727 }
2728
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002729 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2730 idev = ipv6_add_dev(dev);
2731 if (idev)
2732 break;
2733 }
2734
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002735 /*
2736 * MTU falled under IPV6_MIN_MTU.
2737 * Stop IPv6 on this interface.
2738 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
2740 case NETDEV_DOWN:
2741 case NETDEV_UNREGISTER:
2742 /*
2743 * Remove all addresses from this interface.
2744 */
2745 addrconf_ifdown(dev, event != NETDEV_DOWN);
2746 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07002750 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08002751 addrconf_sysctl_unregister(idev);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11002752 addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07002753 err = snmp6_register_dev(idev);
2754 if (err)
2755 return notifier_from_errno(err);
Stephen Hemminger5632c512007-04-28 21:16:39 -07002756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002758
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002759 case NETDEV_PRE_TYPE_CHANGE:
2760 case NETDEV_POST_TYPE_CHANGE:
2761 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07002762 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 return NOTIFY_OK;
2766}
2767
2768/*
2769 * addrconf module should be notified of a device going up
2770 */
2771static struct notifier_block ipv6_dev_notf = {
2772 .notifier_call = addrconf_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773};
2774
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002775static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07002776{
2777 struct inet6_dev *idev;
2778 ASSERT_RTNL();
2779
2780 idev = __in6_dev_get(dev);
2781
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002782 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07002783 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002784 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07002785 ipv6_mc_unmap(idev);
2786}
2787
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788static int addrconf_ifdown(struct net_device *dev, int how)
2789{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002790 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002791 struct inet6_dev *idev;
2792 struct inet6_ifaddr *ifa;
David S. Miller73a8bd72011-01-23 23:27:15 -08002793 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
2795 ASSERT_RTNL();
2796
David S. Miller73a8bd72011-01-23 23:27:15 -08002797 rt6_ifdown(net, dev);
2798 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 idev = __in6_dev_get(dev);
2801 if (idev == NULL)
2802 return -ENODEV;
2803
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002804 /*
2805 * Step 1: remove reference to ipv6 device from parent device.
2806 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002808 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002810
2811 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002812 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
2814 /* Step 1.5: remove snmp6 entry */
2815 snmp6_unregister_dev(idev);
2816
2817 }
2818
David S. Miller73a8bd72011-01-23 23:27:15 -08002819 /* Step 2: clear hash table */
2820 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
2821 struct hlist_head *h = &inet6_addr_lst[i];
2822 struct hlist_node *n;
2823
2824 spin_lock_bh(&addrconf_hash_lock);
2825 restart:
2826 hlist_for_each_entry_rcu(ifa, n, h, addr_lst) {
2827 if (ifa->idev == idev) {
2828 hlist_del_init_rcu(&ifa->addr_lst);
2829 addrconf_del_timer(ifa);
2830 goto restart;
2831 }
2832 }
2833 spin_unlock_bh(&addrconf_hash_lock);
2834 }
2835
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 write_lock_bh(&idev->lock);
2837
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002838 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002839 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002840 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842#ifdef CONFIG_IPV6_PRIVACY
Denis V. Lunev439e2382008-04-03 13:30:17 -07002843 if (how && del_timer(&idev->regen_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 in6_dev_put(idev);
2845
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002846 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00002847 while (!list_empty(&idev->tempaddr_list)) {
2848 ifa = list_first_entry(&idev->tempaddr_list,
2849 struct inet6_ifaddr, tmp_list);
2850 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 write_unlock_bh(&idev->lock);
2852 spin_lock_bh(&ifa->lock);
2853
2854 if (ifa->ifpub) {
2855 in6_ifa_put(ifa->ifpub);
2856 ifa->ifpub = NULL;
2857 }
2858 spin_unlock_bh(&ifa->lock);
2859 in6_ifa_put(ifa);
2860 write_lock_bh(&idev->lock);
2861 }
2862#endif
stephen hemminger8f37ada2010-03-03 08:19:59 +00002863
stephen hemminger502a2ff2010-03-17 20:31:13 +00002864 while (!list_empty(&idev->addr_list)) {
2865 ifa = list_first_entry(&idev->addr_list,
2866 struct inet6_ifaddr, if_list);
stephen hemminger84e8b802010-03-02 13:32:46 +00002867 addrconf_del_timer(ifa);
2868
David S. Miller73a8bd72011-01-23 23:27:15 -08002869 list_del(&ifa->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
David S. Miller73a8bd72011-01-23 23:27:15 -08002871 write_unlock_bh(&idev->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00002872
David S. Miller73a8bd72011-01-23 23:27:15 -08002873 spin_lock_bh(&ifa->state_lock);
2874 state = ifa->state;
2875 ifa->state = INET6_IFADDR_STATE_DEAD;
2876 spin_unlock_bh(&ifa->state_lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00002877
David S. Miller73a8bd72011-01-23 23:27:15 -08002878 if (state != INET6_IFADDR_STATE_DEAD) {
2879 __ipv6_ifa_notify(RTM_DELADDR, ifa);
2880 atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00002881 }
David S. Miller73a8bd72011-01-23 23:27:15 -08002882 in6_ifa_put(ifa);
stephen hemminger8f37ada2010-03-03 08:19:59 +00002883
David S. Miller73a8bd72011-01-23 23:27:15 -08002884 write_lock_bh(&idev->lock);
2885 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00002886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 write_unlock_bh(&idev->lock);
2888
2889 /* Step 5: Discard multicast list */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002890 if (how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 ipv6_mc_destroy_dev(idev);
2892 else
2893 ipv6_mc_down(idev);
2894
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002896
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002897 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002898 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08002899 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 neigh_parms_release(&nd_tbl, idev->nd_parms);
2901 neigh_ifdown(&nd_tbl, dev);
2902 in6_dev_put(idev);
2903 }
2904 return 0;
2905}
2906
2907static void addrconf_rs_timer(unsigned long data)
2908{
2909 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
stephen hemminger5b2a1952010-03-02 13:32:45 +00002910 struct inet6_dev *idev = ifp->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
stephen hemminger5b2a1952010-03-02 13:32:45 +00002912 read_lock(&idev->lock);
2913 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 goto out;
2915
stephen hemminger5b2a1952010-03-02 13:32:45 +00002916 if (idev->cnf.forwarding)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00002918
2919 /* Announcement received after solicitation was sent */
2920 if (idev->if_flags & IF_RA_RCVD)
2921 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
2923 spin_lock(&ifp->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00002924 if (ifp->probes++ < idev->cnf.rtr_solicits) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 /* The wait after the last probe can be shorter */
2926 addrconf_mod_timer(ifp, AC_RS,
stephen hemminger5b2a1952010-03-02 13:32:45 +00002927 (ifp->probes == idev->cnf.rtr_solicits) ?
2928 idev->cnf.rtr_solicit_delay :
2929 idev->cnf.rtr_solicit_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 spin_unlock(&ifp->lock);
2931
stephen hemminger5b2a1952010-03-02 13:32:45 +00002932 ndisc_send_rs(idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 } else {
2934 spin_unlock(&ifp->lock);
2935 /*
2936 * Note: we do not support deprecated "all on-link"
2937 * assumption any longer.
2938 */
2939 printk(KERN_DEBUG "%s: no IPv6 routers present\n",
stephen hemminger5b2a1952010-03-02 13:32:45 +00002940 idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 }
2942
2943out:
stephen hemminger5b2a1952010-03-02 13:32:45 +00002944 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 in6_ifa_put(ifp);
2946}
2947
2948/*
2949 * Duplicate Address Detection
2950 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002951static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
2952{
2953 unsigned long rand_num;
2954 struct inet6_dev *idev = ifp->idev;
2955
Neil Horman95c385b2007-04-25 17:08:10 -07002956 if (ifp->flags & IFA_F_OPTIMISTIC)
2957 rand_num = 0;
2958 else
2959 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
2960
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002961 ifp->probes = idev->cnf.dad_transmits;
2962 addrconf_mod_timer(ifp, AC_DAD, rand_num);
2963}
2964
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07002965static void addrconf_dad_start(struct inet6_ifaddr *ifp, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966{
2967 struct inet6_dev *idev = ifp->idev;
2968 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
2970 addrconf_join_solict(dev, &ifp->addr);
2971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 net_srandom(ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
2974 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07002975 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07002976 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
2979 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09002980 idev->cnf.accept_dad < 1 ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07002981 !(ifp->flags&IFA_F_TENTATIVE) ||
2982 ifp->flags & IFA_F_NODAD) {
Brian Haleycc411d02009-09-09 14:41:32 +00002983 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00002984 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 read_unlock_bh(&idev->lock);
2986
2987 addrconf_dad_completed(ifp);
2988 return;
2989 }
2990
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002991 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00002992 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08002993 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002994 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09002995 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002996 * - keep it tentative if it is a permanent address.
2997 * - otherwise, kill it.
2998 */
2999 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003000 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003001 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003002 }
Neil Horman95c385b2007-04-25 17:08:10 -07003003
3004 /*
3005 * Optimistic nodes can start receiving
3006 * Frames right away
3007 */
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003008 if (ifp->flags & IFA_F_OPTIMISTIC)
Neil Horman95c385b2007-04-25 17:08:10 -07003009 ip6_ins_rt(ifp->rt);
3010
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003011 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003013 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 read_unlock_bh(&idev->lock);
3015}
3016
3017static void addrconf_dad_timer(unsigned long data)
3018{
3019 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
3020 struct inet6_dev *idev = ifp->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 struct in6_addr mcaddr;
3022
Herbert Xuf2344a12010-05-18 15:55:27 -07003023 if (!ifp->probes && addrconf_dad_end(ifp))
3024 goto out;
3025
stephen hemminger122e4512010-03-02 13:32:44 +00003026 read_lock(&idev->lock);
3027 if (idev->dead || !(idev->if_flags & IF_READY)) {
3028 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 goto out;
3030 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003031
3032 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003033 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3034 spin_unlock(&ifp->lock);
3035 read_unlock(&idev->lock);
3036 goto out;
3037 }
3038
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 if (ifp->probes == 0) {
3040 /*
3041 * DAD was successful
3042 */
3043
Brian Haleycc411d02009-09-09 14:41:32 +00003044 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003045 spin_unlock(&ifp->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003046 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
3048 addrconf_dad_completed(ifp);
3049
3050 goto out;
3051 }
3052
3053 ifp->probes--;
3054 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
stephen hemminger21809fa2010-02-08 19:48:52 +00003055 spin_unlock(&ifp->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003056 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
3058 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09003060 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061out:
3062 in6_ifa_put(ifp);
3063}
3064
3065static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3066{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003067 struct net_device *dev = ifp->idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
3069 /*
3070 * Configure the address for reception. Now it is valid.
3071 */
3072
3073 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3074
Tore Anderson026359b2011-08-28 23:47:33 +00003075 /* If added prefix is link local and we are prepared to process
3076 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 */
3078
Tore Anderson026359b2011-08-28 23:47:33 +00003079 if (((ifp->idev->cnf.accept_ra == 1 && !ifp->idev->cnf.forwarding) ||
3080 ifp->idev->cnf.accept_ra == 2) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 ifp->idev->cnf.rtr_solicits > 0 &&
3082 (dev->flags&IFF_LOOPBACK) == 0 &&
3083 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 /*
3085 * If a host as already performed a random delay
3086 * [...] as part of DAD [...] there is no need
3087 * to delay again before sending the first RS
3088 */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09003089 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
3091 spin_lock_bh(&ifp->lock);
3092 ifp->probes = 1;
3093 ifp->idev->if_flags |= IF_RS_SENT;
3094 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
3095 spin_unlock_bh(&ifp->lock);
3096 }
3097}
3098
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003099static void addrconf_dad_run(struct inet6_dev *idev)
3100{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003101 struct inet6_ifaddr *ifp;
3102
3103 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003104 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003105 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07003106 if (ifp->flags & IFA_F_TENTATIVE &&
3107 ifp->state == INET6_IFADDR_STATE_DAD)
3108 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00003109 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003110 }
3111 read_unlock_bh(&idev->lock);
3112}
3113
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114#ifdef CONFIG_PROC_FS
3115struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08003116 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003118 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119};
3120
Mihai Maruseac1d578302012-01-03 23:31:35 +00003121static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122{
3123 struct inet6_ifaddr *ifa = NULL;
3124 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003125 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00003126 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
Mihai Maruseac1d578302012-01-03 23:31:35 +00003128 /* initial bucket if pos is 0 */
3129 if (pos == 0) {
3130 state->bucket = 0;
3131 state->offset = 0;
3132 }
3133
3134 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
stephen hemmingerc2e21292010-03-17 20:31:10 +00003135 struct hlist_node *n;
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003136 hlist_for_each_entry_rcu_bh(ifa, n, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00003137 addr_lst) {
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003138 if (!net_eq(dev_net(ifa->idev->dev), net))
3139 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003140 /* sync with offset */
3141 if (p < state->offset) {
3142 p++;
3143 continue;
3144 }
3145 state->offset++;
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003146 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003147 }
3148
3149 /* prepare for next bucket */
3150 state->offset = 0;
3151 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00003153 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154}
3155
stephen hemmingerc2e21292010-03-17 20:31:10 +00003156static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3157 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
3159 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003160 struct net *net = seq_file_net(seq);
stephen hemmingerc2e21292010-03-17 20:31:10 +00003161 struct hlist_node *n = &ifa->addr_lst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Mihai Maruseac1d578302012-01-03 23:31:35 +00003163 hlist_for_each_entry_continue_rcu_bh(ifa, n, addr_lst) {
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003164 if (!net_eq(dev_net(ifa->idev->dev), net))
3165 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003166 state->offset++;
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003167 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003168 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00003169
3170 while (++state->bucket < IN6_ADDR_HSIZE) {
Mihai Maruseac1d578302012-01-03 23:31:35 +00003171 state->offset = 0;
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003172 hlist_for_each_entry_rcu_bh(ifa, n,
stephen hemmingerc2e21292010-03-17 20:31:10 +00003173 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003174 if (!net_eq(dev_net(ifa->idev->dev), net))
3175 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003176 state->offset++;
Eric Dumazetdd2c50e2012-10-16 07:37:27 +00003177 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08003178 }
3179 }
3180
stephen hemmingerc2e21292010-03-17 20:31:10 +00003181 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182}
3183
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003185 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186{
stephen hemminger5c578ae2010-03-17 20:31:11 +00003187 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00003188 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189}
3190
3191static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3192{
3193 struct inet6_ifaddr *ifa;
3194
3195 ifa = if6_get_next(seq, v);
3196 ++*pos;
3197 return ifa;
3198}
3199
3200static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003201 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202{
stephen hemminger5c578ae2010-03-17 20:31:11 +00003203 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204}
3205
3206static int if6_seq_show(struct seq_file *seq, void *v)
3207{
3208 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Harvey Harrison4b7a4272008-10-29 12:50:24 -07003209 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07003210 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 ifp->idev->dev->ifindex,
3212 ifp->prefix_len,
3213 ifp->scope,
3214 ifp->flags,
3215 ifp->idev->dev->name);
3216 return 0;
3217}
3218
Philippe De Muyter56b3d972007-07-10 23:07:31 -07003219static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 .start = if6_seq_start,
3221 .next = if6_seq_next,
3222 .show = if6_seq_show,
3223 .stop = if6_seq_stop,
3224};
3225
3226static int if6_seq_open(struct inode *inode, struct file *file)
3227{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003228 return seq_open_net(inode, file, &if6_seq_ops,
3229 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230}
3231
Arjan van de Ven9a321442007-02-12 00:55:35 -08003232static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 .owner = THIS_MODULE,
3234 .open = if6_seq_open,
3235 .read = seq_read,
3236 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08003237 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238};
3239
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003240static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003242 if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 return -ENOMEM;
3244 return 0;
3245}
3246
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003247static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08003248{
3249 proc_net_remove(net, "if_inet6");
3250}
3251
3252static struct pernet_operations if6_proc_net_ops = {
3253 .init = if6_proc_net_init,
3254 .exit = if6_proc_net_exit,
3255};
3256
3257int __init if6_proc_init(void)
3258{
3259 return register_pernet_subsys(&if6_proc_net_ops);
3260}
3261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262void if6_proc_exit(void)
3263{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003264 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265}
3266#endif /* CONFIG_PROC_FS */
3267
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -07003268#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003269/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003270int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003271{
3272 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00003273 struct inet6_ifaddr *ifp = NULL;
3274 struct hlist_node *n;
stephen hemminger3a88a812010-03-17 20:31:12 +00003275 unsigned int hash = ipv6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00003276
stephen hemminger5c578ae2010-03-17 20:31:11 +00003277 rcu_read_lock_bh();
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003278 hlist_for_each_entry_rcu_bh(ifp, n, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09003279 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08003280 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09003281 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003282 (ifp->flags & IFA_F_HOMEADDRESS)) {
3283 ret = 1;
3284 break;
3285 }
3286 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00003287 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003288 return ret;
3289}
3290#endif
3291
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292/*
3293 * Periodic address status verification
3294 */
3295
3296static void addrconf_verify(unsigned long foo)
3297{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003298 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 struct inet6_ifaddr *ifp;
stephen hemmingerc2e21292010-03-17 20:31:10 +00003300 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 int i;
3302
stephen hemminger5c578ae2010-03-17 20:31:11 +00003303 rcu_read_lock_bh();
3304 spin_lock(&addrconf_verify_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003306 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
3308 del_timer(&addr_chk_timer);
3309
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003310 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311restart:
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003312 hlist_for_each_entry_rcu_bh(ifp, node,
stephen hemminger5c578ae2010-03-17 20:31:11 +00003313 &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
3316 if (ifp->flags & IFA_F_PERMANENT)
3317 continue;
3318
3319 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003320 /* We try to batch several events at once. */
3321 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09003323 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
3324 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 spin_unlock(&ifp->lock);
3326 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 ipv6_del_addr(ifp);
3328 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09003329 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
3330 spin_unlock(&ifp->lock);
3331 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00003333 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 int deprecate = 0;
3335
3336 if (!(ifp->flags&IFA_F_DEPRECATED)) {
3337 deprecate = 1;
3338 ifp->flags |= IFA_F_DEPRECATED;
3339 }
3340
3341 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
3342 next = ifp->tstamp + ifp->valid_lft * HZ;
3343
3344 spin_unlock(&ifp->lock);
3345
3346 if (deprecate) {
3347 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
3349 ipv6_ifa_notify(0, ifp);
3350 in6_ifa_put(ifp);
3351 goto restart;
3352 }
3353#ifdef CONFIG_IPV6_PRIVACY
3354 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3355 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00003356 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
3357 ifp->idev->cnf.dad_transmits *
3358 ifp->idev->nd_parms->retrans_time / HZ;
3359
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 if (age >= ifp->prefered_lft - regen_advance) {
3361 struct inet6_ifaddr *ifpub = ifp->ifpub;
3362 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3363 next = ifp->tstamp + ifp->prefered_lft * HZ;
3364 if (!ifp->regen_count && ifpub) {
3365 ifp->regen_count++;
3366 in6_ifa_hold(ifp);
3367 in6_ifa_hold(ifpub);
3368 spin_unlock(&ifp->lock);
stephen hemminger5c578ae2010-03-17 20:31:11 +00003369
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08003370 spin_lock(&ifpub->lock);
3371 ifpub->regen_count = 0;
3372 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 ipv6_create_tempaddr(ifpub, ifp);
3374 in6_ifa_put(ifpub);
3375 in6_ifa_put(ifp);
3376 goto restart;
3377 }
3378 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3379 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3380 spin_unlock(&ifp->lock);
3381#endif
3382 } else {
3383 /* ifp->prefered_lft <= ifp->valid_lft */
3384 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3385 next = ifp->tstamp + ifp->prefered_lft * HZ;
3386 spin_unlock(&ifp->lock);
3387 }
3388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 }
3390
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003391 next_sec = round_jiffies_up(next);
3392 next_sched = next;
3393
3394 /* If rounded timeout is accurate enough, accept it. */
3395 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3396 next_sched = next_sec;
3397
3398 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3399 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3400 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3401
3402 ADBG((KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3403 now, next, next_sec, next_sched));
3404
3405 addr_chk_timer.expires = next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 add_timer(&addr_chk_timer);
stephen hemminger5c578ae2010-03-17 20:31:11 +00003407 spin_unlock(&addrconf_verify_lock);
3408 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409}
3410
Thomas Graf461d8832006-09-18 00:09:49 -07003411static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
3412{
3413 struct in6_addr *pfx = NULL;
3414
3415 if (addr)
3416 pfx = nla_data(addr);
3417
3418 if (local) {
3419 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3420 pfx = NULL;
3421 else
3422 pfx = nla_data(local);
3423 }
3424
3425 return pfx;
3426}
3427
Patrick McHardyef7c79e2007-06-05 12:38:30 -07003428static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07003429 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
3430 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
3431 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
3432};
3433
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434static int
3435inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3436{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003437 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07003438 struct ifaddrmsg *ifm;
3439 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 struct in6_addr *pfx;
Thomas Grafb933f712006-09-18 00:10:19 -07003441 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
Thomas Grafb933f712006-09-18 00:10:19 -07003443 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3444 if (err < 0)
3445 return err;
3446
3447 ifm = nlmsg_data(nlh);
3448 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 if (pfx == NULL)
3450 return -EINVAL;
3451
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003452 return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453}
3454
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003455static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
3456 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003457{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003458 u32 flags;
3459 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003460 unsigned long timeout;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003461
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003462 if (!valid_lft || (prefered_lft > valid_lft))
3463 return -EINVAL;
3464
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003465 timeout = addrconf_timeout_fixup(valid_lft, HZ);
3466 if (addrconf_finite_timeout(timeout)) {
3467 expires = jiffies_to_clock_t(timeout * HZ);
3468 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003469 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003470 } else {
3471 expires = 0;
3472 flags = 0;
3473 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003474 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003475
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003476 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
3477 if (addrconf_finite_timeout(timeout)) {
3478 if (timeout == 0)
3479 ifa_flags |= IFA_F_DEPRECATED;
3480 prefered_lft = timeout;
3481 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003482
3483 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003484 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 +09003485 ifp->tstamp = jiffies;
3486 ifp->valid_lft = valid_lft;
3487 ifp->prefered_lft = prefered_lft;
3488
3489 spin_unlock_bh(&ifp->lock);
3490 if (!(ifp->flags&IFA_F_TENTATIVE))
3491 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003492
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003493 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003494 expires, flags);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003495 addrconf_verify(0);
3496
3497 return 0;
3498}
3499
3500static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3502{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003503 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07003504 struct ifaddrmsg *ifm;
3505 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 struct in6_addr *pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07003507 struct inet6_ifaddr *ifa;
3508 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003509 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3510 u8 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07003511 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
Thomas Graf461d8832006-09-18 00:09:49 -07003513 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3514 if (err < 0)
3515 return err;
3516
3517 ifm = nlmsg_data(nlh);
3518 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 if (pfx == NULL)
3520 return -EINVAL;
3521
Thomas Graf461d8832006-09-18 00:09:49 -07003522 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003523 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07003524
3525 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003526 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07003527 preferred_lft = ci->ifa_prefered;
3528 } else {
3529 preferred_lft = INFINITY_LIFE_TIME;
3530 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003531 }
3532
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003533 dev = __dev_get_by_index(net, ifm->ifa_index);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003534 if (dev == NULL)
3535 return -ENODEV;
3536
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003537 /* We ignore other flags so far. */
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003538 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003539
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08003540 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003541 if (ifa == NULL) {
3542 /*
3543 * It would be best to check for !NLM_F_CREATE here but
3544 * userspace alreay relies on not having to provide this.
3545 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003546 return inet6_addr_add(net, ifm->ifa_index, pfx,
3547 ifm->ifa_prefixlen, ifa_flags,
3548 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003549 }
3550
Thomas Graf7198f8c2006-09-18 00:13:46 -07003551 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3552 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3553 err = -EEXIST;
3554 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003555 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003556
3557 in6_ifa_put(ifa);
3558
3559 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560}
3561
Thomas Graf101bb222006-09-18 00:11:52 -07003562static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3563 u8 scope, int ifindex)
3564{
3565 struct ifaddrmsg *ifm;
3566
3567 ifm = nlmsg_data(nlh);
3568 ifm->ifa_family = AF_INET6;
3569 ifm->ifa_prefixlen = prefixlen;
3570 ifm->ifa_flags = flags;
3571 ifm->ifa_scope = scope;
3572 ifm->ifa_index = ifindex;
3573}
3574
Thomas Graf85486af2006-09-18 00:11:24 -07003575static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3576 unsigned long tstamp, u32 preferred, u32 valid)
3577{
3578 struct ifa_cacheinfo ci;
3579
Thomas Graf18a31e12010-11-17 04:12:02 +00003580 ci.cstamp = cstamp_delta(cstamp);
3581 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07003582 ci.ifa_prefered = preferred;
3583 ci.ifa_valid = valid;
3584
3585 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3586}
3587
Thomas Graf101bb222006-09-18 00:11:52 -07003588static inline int rt_scope(int ifa_scope)
3589{
3590 if (ifa_scope & IFA_HOST)
3591 return RT_SCOPE_HOST;
3592 else if (ifa_scope & IFA_LINK)
3593 return RT_SCOPE_LINK;
3594 else if (ifa_scope & IFA_SITE)
3595 return RT_SCOPE_SITE;
3596 else
3597 return RT_SCOPE_UNIVERSE;
3598}
3599
Thomas Graf0ab68032006-09-18 00:12:35 -07003600static inline int inet6_ifaddr_msgsize(void)
3601{
Thomas Graf339bf982006-11-10 14:10:15 -08003602 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3603 + nla_total_size(16) /* IFA_ADDRESS */
3604 + nla_total_size(sizeof(struct ifa_cacheinfo));
Thomas Graf0ab68032006-09-18 00:12:35 -07003605}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003606
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003608 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07003611 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
Thomas Graf0ab68032006-09-18 00:12:35 -07003613 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3614 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003615 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003616
Thomas Graf101bb222006-09-18 00:11:52 -07003617 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3618 ifa->idev->dev->ifindex);
3619
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 if (!(ifa->flags&IFA_F_PERMANENT)) {
Thomas Graf85486af2006-09-18 00:11:24 -07003621 preferred = ifa->prefered_lft;
3622 valid = ifa->valid_lft;
3623 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00003625 if (preferred > tval)
3626 preferred -= tval;
3627 else
3628 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00003629 if (valid != INFINITY_LIFE_TIME) {
3630 if (valid > tval)
3631 valid -= tval;
3632 else
3633 valid = 0;
3634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 }
3636 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07003637 preferred = INFINITY_LIFE_TIME;
3638 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 }
Thomas Graf85486af2006-09-18 00:11:24 -07003640
Thomas Graf0ab68032006-09-18 00:12:35 -07003641 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
Patrick McHardy26932562007-01-31 23:16:40 -08003642 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3643 nlmsg_cancel(skb, nlh);
3644 return -EMSGSIZE;
3645 }
Thomas Graf85486af2006-09-18 00:11:24 -07003646
Thomas Graf0ab68032006-09-18 00:12:35 -07003647 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648}
3649
3650static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07003651 u32 pid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003654 u8 scope = RT_SCOPE_UNIVERSE;
3655 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
Thomas Graf101bb222006-09-18 00:11:52 -07003657 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3658 scope = RT_SCOPE_SITE;
3659
Thomas Graf0ab68032006-09-18 00:12:35 -07003660 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3661 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003662 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003663
Thomas Graf101bb222006-09-18 00:11:52 -07003664 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003665 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3666 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003667 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3668 nlmsg_cancel(skb, nlh);
3669 return -EMSGSIZE;
3670 }
Thomas Graf85486af2006-09-18 00:11:24 -07003671
Thomas Graf0ab68032006-09-18 00:12:35 -07003672 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673}
3674
3675static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -07003676 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003679 u8 scope = RT_SCOPE_UNIVERSE;
3680 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
Thomas Graf101bb222006-09-18 00:11:52 -07003682 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3683 scope = RT_SCOPE_SITE;
3684
Thomas Graf0ab68032006-09-18 00:12:35 -07003685 nlh = nlmsg_put(skb, pid, seq, event, sizeof(struct ifaddrmsg), flags);
3686 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003687 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003688
Thomas Graf101bb222006-09-18 00:11:52 -07003689 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003690 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3691 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003692 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3693 nlmsg_cancel(skb, nlh);
3694 return -EMSGSIZE;
3695 }
Thomas Graf85486af2006-09-18 00:11:24 -07003696
Thomas Graf0ab68032006-09-18 00:12:35 -07003697 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698}
3699
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003700enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 UNICAST_ADDR,
3702 MULTICAST_ADDR,
3703 ANYCAST_ADDR,
3704};
3705
Eric Dumazet234b27c2009-11-12 04:11:50 +00003706/* called with rcu_read_lock() */
3707static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
3708 struct netlink_callback *cb, enum addr_type_t type,
3709 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 struct ifmcaddr6 *ifmca;
3712 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00003713 int err = 1;
3714 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715
Eric Dumazet234b27c2009-11-12 04:11:50 +00003716 read_lock_bh(&idev->lock);
3717 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00003718 case UNICAST_ADDR: {
3719 struct inet6_ifaddr *ifa;
3720
Eric Dumazet234b27c2009-11-12 04:11:50 +00003721 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00003722 list_for_each_entry(ifa, &idev->addr_list, if_list) {
3723 if (++ip_idx < s_ip_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00003724 continue;
3725 err = inet6_fill_ifaddr(skb, ifa,
3726 NETLINK_CB(cb->skb).pid,
3727 cb->nlh->nlmsg_seq,
3728 RTM_NEWADDR,
3729 NLM_F_MULTI);
3730 if (err <= 0)
3731 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00003733 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00003734 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00003735 case MULTICAST_ADDR:
3736 /* multicast address */
3737 for (ifmca = idev->mc_list; ifmca;
3738 ifmca = ifmca->next, ip_idx++) {
3739 if (ip_idx < s_ip_idx)
3740 continue;
3741 err = inet6_fill_ifmcaddr(skb, ifmca,
3742 NETLINK_CB(cb->skb).pid,
3743 cb->nlh->nlmsg_seq,
3744 RTM_GETMULTICAST,
3745 NLM_F_MULTI);
3746 if (err <= 0)
3747 break;
3748 }
3749 break;
3750 case ANYCAST_ADDR:
3751 /* anycast address */
3752 for (ifaca = idev->ac_list; ifaca;
3753 ifaca = ifaca->aca_next, ip_idx++) {
3754 if (ip_idx < s_ip_idx)
3755 continue;
3756 err = inet6_fill_ifacaddr(skb, ifaca,
3757 NETLINK_CB(cb->skb).pid,
3758 cb->nlh->nlmsg_seq,
3759 RTM_GETANYCAST,
3760 NLM_F_MULTI);
3761 if (err <= 0)
3762 break;
3763 }
3764 break;
3765 default:
3766 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00003768 read_unlock_bh(&idev->lock);
3769 *p_ip_idx = ip_idx;
3770 return err;
3771}
3772
3773static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3774 enum addr_type_t type)
3775{
3776 struct net *net = sock_net(skb->sk);
3777 int h, s_h;
3778 int idx, ip_idx;
3779 int s_idx, s_ip_idx;
3780 struct net_device *dev;
3781 struct inet6_dev *idev;
3782 struct hlist_head *head;
3783 struct hlist_node *node;
3784
3785 s_h = cb->args[0];
3786 s_idx = idx = cb->args[1];
3787 s_ip_idx = ip_idx = cb->args[2];
3788
3789 rcu_read_lock();
3790 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3791 idx = 0;
3792 head = &net->dev_index_head[h];
3793 hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
3794 if (idx < s_idx)
3795 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07003796 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00003797 s_ip_idx = 0;
3798 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003799 idev = __in6_dev_get(dev);
3800 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00003801 goto cont;
3802
3803 if (in6_dump_addrs(idev, skb, cb, type,
3804 s_ip_idx, &ip_idx) <= 0)
3805 goto done;
3806cont:
3807 idx++;
3808 }
3809 }
3810done:
3811 rcu_read_unlock();
3812 cb->args[0] = h;
3813 cb->args[1] = idx;
3814 cb->args[2] = ip_idx;
3815
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 return skb->len;
3817}
3818
3819static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3820{
3821 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003822
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 return inet6_dump_addr(skb, cb, type);
3824}
3825
3826static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3827{
3828 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003829
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 return inet6_dump_addr(skb, cb, type);
3831}
3832
3833
3834static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3835{
3836 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003837
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 return inet6_dump_addr(skb, cb, type);
3839}
3840
Thomas Graf1b29fc22006-09-18 00:10:50 -07003841static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
3842 void *arg)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003843{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003844 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003845 struct ifaddrmsg *ifm;
3846 struct nlattr *tb[IFA_MAX+1];
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003847 struct in6_addr *addr = NULL;
3848 struct net_device *dev = NULL;
3849 struct inet6_ifaddr *ifa;
3850 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003851 int err;
3852
Thomas Graf1b29fc22006-09-18 00:10:50 -07003853 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3854 if (err < 0)
3855 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003856
Thomas Graf1b29fc22006-09-18 00:10:50 -07003857 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3858 if (addr == NULL) {
3859 err = -EINVAL;
3860 goto errout;
3861 }
3862
3863 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003864 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08003865 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003866
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003867 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
3868 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003869 err = -EADDRNOTAVAIL;
3870 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003871 }
3872
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003873 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
3874 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003875 err = -ENOBUFS;
3876 goto errout_ifa;
3877 }
3878
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003879 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).pid,
3880 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003881 if (err < 0) {
3882 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3883 WARN_ON(err == -EMSGSIZE);
3884 kfree_skb(skb);
3885 goto errout_ifa;
3886 }
Benjamin Thery6fda7352008-03-05 10:47:47 -08003887 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003888errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003889 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003890errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003891 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003892}
3893
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3895{
3896 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003897 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07003898 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
Thomas Graf0ab68032006-09-18 00:12:35 -07003900 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07003901 if (skb == NULL)
3902 goto errout;
3903
3904 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003905 if (err < 0) {
3906 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3907 WARN_ON(err == -EMSGSIZE);
3908 kfree_skb(skb);
3909 goto errout;
3910 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08003911 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
3912 return;
Thomas Graf5d620262006-08-15 00:35:02 -07003913errout:
3914 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08003915 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916}
3917
Dave Jonesb6f99a22007-03-22 12:27:49 -07003918static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 __s32 *array, int bytes)
3920{
Thomas Graf04561c12006-11-14 19:53:58 -08003921 BUG_ON(bytes < (DEVCONF_MAX * 4));
3922
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 memset(array, 0, bytes);
3924 array[DEVCONF_FORWARDING] = cnf->forwarding;
3925 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
3926 array[DEVCONF_MTU6] = cnf->mtu6;
3927 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
3928 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
3929 array[DEVCONF_AUTOCONF] = cnf->autoconf;
3930 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
3931 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00003932 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
3933 jiffies_to_msecs(cnf->rtr_solicit_interval);
3934 array[DEVCONF_RTR_SOLICIT_DELAY] =
3935 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
3937#ifdef CONFIG_IPV6_PRIVACY
3938 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
3939 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
3940 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
3941 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
3942 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
3943#endif
3944 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08003945 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08003946 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003947#ifdef CONFIG_IPV6_ROUTER_PREF
3948 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00003949 array[DEVCONF_RTR_PROBE_INTERVAL] =
3950 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08003951#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08003952 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
3953#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08003954#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07003955 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07003956 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07003957#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
3958 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
3959#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09003960#ifdef CONFIG_IPV6_MROUTE
3961 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
3962#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09003963 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09003964 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00003965 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966}
3967
Thomas Grafb382b192010-11-16 04:33:57 +00003968static inline size_t inet6_ifla6_size(void)
3969{
3970 return nla_total_size(4) /* IFLA_INET6_FLAGS */
3971 + nla_total_size(sizeof(struct ifla_cacheinfo))
3972 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
3973 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
3974 + nla_total_size(ICMP6_MIB_MAX * 8); /* IFLA_INET6_ICMP6STATS */
3975}
3976
Thomas Graf339bf982006-11-10 14:10:15 -08003977static inline size_t inet6_if_nlmsg_size(void)
3978{
3979 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3980 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
3981 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
3982 + nla_total_size(4) /* IFLA_MTU */
3983 + nla_total_size(4) /* IFLA_LINK */
Thomas Grafb382b192010-11-16 04:33:57 +00003984 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08003985}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003986
Eric Dumazetbe281e52011-05-19 01:14:23 +00003987static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Tejun Heo7d720c32010-02-16 15:20:26 +00003988 int items, int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07003989{
3990 int i;
3991 int pad = bytes - sizeof(u64) * items;
3992 BUG_ON(pad < 0);
3993
3994 /* Use put_unaligned() because stats may not be aligned for u64. */
3995 put_unaligned(items, &stats[0]);
3996 for (i = 1; i < items; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00003997 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07003998
3999 memset(&stats[items], 0, pad);
4000}
4001
Eric Dumazet4ce3c182010-06-30 13:31:19 -07004002static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib,
4003 int items, int bytes, size_t syncpoff)
4004{
4005 int i;
4006 int pad = bytes - sizeof(u64) * items;
4007 BUG_ON(pad < 0);
4008
4009 /* Use put_unaligned() because stats may not be aligned for u64. */
4010 put_unaligned(items, &stats[0]);
4011 for (i = 1; i < items; i++)
4012 put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
4013
4014 memset(&stats[items], 0, pad);
4015}
4016
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004017static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
4018 int bytes)
4019{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004020 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004021 case IFLA_INET6_STATS:
Eric Dumazet4ce3c182010-06-30 13:31:19 -07004022 __snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6,
4023 IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004024 break;
4025 case IFLA_INET6_ICMP6STATS:
Eric Dumazetbe281e52011-05-19 01:14:23 +00004026 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004027 break;
4028 }
4029}
4030
Thomas Grafb382b192010-11-16 04:33:57 +00004031static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004033 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08004034 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
Thomas Graf04561c12006-11-14 19:53:58 -08004036 NLA_PUT_U32(skb, IFLA_INET6_FLAGS, idev->if_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08004039 ci.tstamp = cstamp_delta(idev->tstamp);
4040 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
4041 ci.retrans_time = jiffies_to_msecs(idev->nd_parms->retrans_time);
Thomas Graf04561c12006-11-14 19:53:58 -08004042 NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
4043
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004044 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
4045 if (nla == NULL)
Thomas Graf04561c12006-11-14 19:53:58 -08004046 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004047 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004049 /* XXX - MC not implemented */
4050
4051 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
4052 if (nla == NULL)
4053 goto nla_put_failure;
4054 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4055
4056 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4057 if (nla == NULL)
4058 goto nla_put_failure;
4059 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
Thomas Grafb382b192010-11-16 04:33:57 +00004061 return 0;
4062
4063nla_put_failure:
4064 return -EMSGSIZE;
4065}
4066
4067static size_t inet6_get_link_af_size(const struct net_device *dev)
4068{
4069 if (!__in6_dev_get(dev))
4070 return 0;
4071
4072 return inet6_ifla6_size();
4073}
4074
4075static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4076{
4077 struct inet6_dev *idev = __in6_dev_get(dev);
4078
4079 if (!idev)
4080 return -ENODATA;
4081
4082 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4083 return -EMSGSIZE;
4084
4085 return 0;
4086}
4087
Thomas Grafb382b192010-11-16 04:33:57 +00004088static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
4089 u32 pid, u32 seq, int event, unsigned int flags)
4090{
4091 struct net_device *dev = idev->dev;
4092 struct ifinfomsg *hdr;
4093 struct nlmsghdr *nlh;
4094 void *protoinfo;
4095
4096 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
4097 if (nlh == NULL)
4098 return -EMSGSIZE;
4099
4100 hdr = nlmsg_data(nlh);
4101 hdr->ifi_family = AF_INET6;
4102 hdr->__ifi_pad = 0;
4103 hdr->ifi_type = dev->type;
4104 hdr->ifi_index = dev->ifindex;
4105 hdr->ifi_flags = dev_get_flags(dev);
4106 hdr->ifi_change = 0;
4107
4108 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
4109
4110 if (dev->addr_len)
4111 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
4112
4113 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
4114 if (dev->ifindex != dev->iflink)
4115 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
4116
4117 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4118 if (protoinfo == NULL)
4119 goto nla_put_failure;
4120
4121 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4122 goto nla_put_failure;
4123
Thomas Graf04561c12006-11-14 19:53:58 -08004124 nla_nest_end(skb, protoinfo);
4125 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
Thomas Graf04561c12006-11-14 19:53:58 -08004127nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004128 nlmsg_cancel(skb, nlh);
4129 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130}
4131
4132static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
4133{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004134 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00004135 int h, s_h;
David S. Miller434a8a52009-11-11 18:53:00 -08004136 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 struct net_device *dev;
4138 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00004139 struct hlist_head *head;
4140 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141
Eric Dumazet84d26972009-11-09 12:11:28 +00004142 s_h = cb->args[0];
4143 s_idx = cb->args[1];
4144
4145 rcu_read_lock();
4146 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4147 idx = 0;
4148 head = &net->dev_index_head[h];
4149 hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
4150 if (idx < s_idx)
4151 goto cont;
4152 idev = __in6_dev_get(dev);
4153 if (!idev)
4154 goto cont;
4155 if (inet6_fill_ifinfo(skb, idev,
4156 NETLINK_CB(cb->skb).pid,
4157 cb->nlh->nlmsg_seq,
4158 RTM_NEWLINK, NLM_F_MULTI) <= 0)
4159 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07004160cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00004161 idx++;
4162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 }
Eric Dumazet84d26972009-11-09 12:11:28 +00004164out:
4165 rcu_read_unlock();
4166 cb->args[1] = idx;
4167 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168
4169 return skb->len;
4170}
4171
4172void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
4173{
4174 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004175 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004176 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004177
Thomas Graf339bf982006-11-10 14:10:15 -08004178 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004179 if (skb == NULL)
4180 goto errout;
4181
4182 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004183 if (err < 0) {
4184 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
4185 WARN_ON(err == -EMSGSIZE);
4186 kfree_skb(skb);
4187 goto errout;
4188 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00004189 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004190 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004191errout:
4192 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00004193 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194}
4195
Thomas Graf339bf982006-11-10 14:10:15 -08004196static inline size_t inet6_prefix_nlmsg_size(void)
4197{
4198 return NLMSG_ALIGN(sizeof(struct prefixmsg))
4199 + nla_total_size(sizeof(struct in6_addr))
4200 + nla_total_size(sizeof(struct prefix_cacheinfo));
4201}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004202
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Thomas Graf6051e2f2006-11-14 19:54:19 -08004204 struct prefix_info *pinfo, u32 pid, u32 seq,
4205 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206{
Thomas Graf6051e2f2006-11-14 19:54:19 -08004207 struct prefixmsg *pmsg;
4208 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 struct prefix_cacheinfo ci;
4210
Thomas Graf6051e2f2006-11-14 19:54:19 -08004211 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*pmsg), flags);
4212 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08004213 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08004214
4215 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07004217 pmsg->prefix_pad1 = 0;
4218 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 pmsg->prefix_ifindex = idev->dev->ifindex;
4220 pmsg->prefix_len = pinfo->prefix_len;
4221 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07004222 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 pmsg->prefix_flags = 0;
4224 if (pinfo->onlink)
4225 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
4226 if (pinfo->autoconf)
4227 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
4228
Thomas Graf6051e2f2006-11-14 19:54:19 -08004229 NLA_PUT(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230
4231 ci.preferred_time = ntohl(pinfo->prefered);
4232 ci.valid_time = ntohl(pinfo->valid);
Thomas Graf6051e2f2006-11-14 19:54:19 -08004233 NLA_PUT(skb, PREFIX_CACHEINFO, sizeof(ci), &ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
Thomas Graf6051e2f2006-11-14 19:54:19 -08004235 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
Thomas Graf6051e2f2006-11-14 19:54:19 -08004237nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004238 nlmsg_cancel(skb, nlh);
4239 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240}
4241
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004242static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 struct prefix_info *pinfo)
4244{
4245 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004246 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004247 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Thomas Graf339bf982006-11-10 14:10:15 -08004249 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004250 if (skb == NULL)
4251 goto errout;
4252
4253 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004254 if (err < 0) {
4255 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
4256 WARN_ON(err == -EMSGSIZE);
4257 kfree_skb(skb);
4258 goto errout;
4259 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004260 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
4261 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004262errout:
4263 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004264 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265}
4266
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4268{
4269 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
4270
4271 switch (event) {
4272 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07004273 /*
4274 * If the address was optimistic
4275 * we inserted the route at the start of
4276 * our DAD process, so we don't need
4277 * to do it again
4278 */
4279 if (!(ifp->rt->rt6i_node))
4280 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 if (ifp->idev->cnf.forwarding)
4282 addrconf_join_anycast(ifp);
4283 break;
4284 case RTM_DELADDR:
4285 if (ifp->idev->cnf.forwarding)
4286 addrconf_leave_anycast(ifp);
4287 addrconf_leave_solict(ifp->idev, &ifp->addr);
Changli Gaod8d1f302010-06-10 23:31:35 -07004288 dst_hold(&ifp->rt->dst);
stephen hemminger93fa1592010-04-12 05:41:31 +00004289
David S. Miller73a8bd72011-01-23 23:27:15 -08004290 if (ip6_del_rt(ifp->rt))
Changli Gaod8d1f302010-06-10 23:31:35 -07004291 dst_free(&ifp->rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 break;
4293 }
4294}
4295
4296static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4297{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07004298 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 if (likely(ifp->idev->dead == 0))
4300 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07004301 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302}
4303
4304#ifdef CONFIG_SYSCTL
4305
4306static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004307int addrconf_sysctl_forward(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 void __user *buffer, size_t *lenp, loff_t *ppos)
4309{
4310 int *valp = ctl->data;
4311 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004312 loff_t pos = *ppos;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004313 ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 int ret;
4315
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004316 /*
4317 * ctl->data points to idev->cnf.forwarding, we should
4318 * not modify it until we get the rtnl lock.
4319 */
4320 lctl = *ctl;
4321 lctl.data = &val;
4322
4323 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08004325 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00004326 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00004327 if (ret)
4328 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004329 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330}
4331
Brian Haley56d417b2009-06-01 03:07:33 -07004332static void dev_disable_change(struct inet6_dev *idev)
4333{
4334 if (!idev || !idev->dev)
4335 return;
4336
4337 if (idev->cnf.disable_ipv6)
4338 addrconf_notify(NULL, NETDEV_DOWN, idev->dev);
4339 else
4340 addrconf_notify(NULL, NETDEV_UP, idev->dev);
4341}
4342
4343static void addrconf_disable_change(struct net *net, __s32 newf)
4344{
4345 struct net_device *dev;
4346 struct inet6_dev *idev;
4347
Eric Dumazetc6d14c82009-11-04 05:43:23 -08004348 rcu_read_lock();
4349 for_each_netdev_rcu(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07004350 idev = __in6_dev_get(dev);
4351 if (idev) {
4352 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
4353 idev->cnf.disable_ipv6 = newf;
4354 if (changed)
4355 dev_disable_change(idev);
4356 }
Brian Haley56d417b2009-06-01 03:07:33 -07004357 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -08004358 rcu_read_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07004359}
4360
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004361static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07004362{
4363 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004364 int old;
4365
4366 if (!rtnl_trylock())
4367 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07004368
4369 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004370 old = *p;
4371 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07004372
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004373 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
4374 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07004375 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004376 }
Brian Haley56d417b2009-06-01 03:07:33 -07004377
4378 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07004379 net->ipv6.devconf_dflt->disable_ipv6 = newf;
4380 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004381 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07004382 dev_disable_change((struct inet6_dev *)table->extra1);
4383
4384 rtnl_unlock();
4385 return 0;
4386}
4387
4388static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004389int addrconf_sysctl_disable(ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07004390 void __user *buffer, size_t *lenp, loff_t *ppos)
4391{
4392 int *valp = ctl->data;
4393 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004394 loff_t pos = *ppos;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004395 ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07004396 int ret;
4397
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004398 /*
4399 * ctl->data points to idev->cnf.disable_ipv6, we should
4400 * not modify it until we get the rtnl lock.
4401 */
4402 lctl = *ctl;
4403 lctl.data = &val;
4404
4405 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07004406
4407 if (write)
4408 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00004409 if (ret)
4410 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07004411 return ret;
4412}
4413
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414static struct addrconf_sysctl_table
4415{
4416 struct ctl_table_header *sysctl_header;
YOSHIFUJI Hideakif6a07b22008-03-25 00:25:11 +09004417 ctl_table addrconf_vars[DEVCONF_MAX+1];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004418 char *dev_name;
Brian Haleyab32ea52006-09-22 14:15:41 -07004419} addrconf_sysctl __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 .sysctl_header = NULL,
4421 .addrconf_vars = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004422 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004423 .procname = "forwarding",
4424 .data = &ipv6_devconf.forwarding,
4425 .maxlen = sizeof(int),
4426 .mode = 0644,
4427 .proc_handler = addrconf_sysctl_forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 },
4429 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004430 .procname = "hop_limit",
4431 .data = &ipv6_devconf.hop_limit,
4432 .maxlen = sizeof(int),
4433 .mode = 0644,
4434 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 },
4436 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004437 .procname = "mtu",
4438 .data = &ipv6_devconf.mtu6,
4439 .maxlen = sizeof(int),
4440 .mode = 0644,
4441 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 },
4443 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004444 .procname = "accept_ra",
4445 .data = &ipv6_devconf.accept_ra,
4446 .maxlen = sizeof(int),
4447 .mode = 0644,
4448 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 },
4450 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004451 .procname = "accept_redirects",
4452 .data = &ipv6_devconf.accept_redirects,
4453 .maxlen = sizeof(int),
4454 .mode = 0644,
4455 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 },
4457 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004458 .procname = "autoconf",
4459 .data = &ipv6_devconf.autoconf,
4460 .maxlen = sizeof(int),
4461 .mode = 0644,
4462 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 },
4464 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004465 .procname = "dad_transmits",
4466 .data = &ipv6_devconf.dad_transmits,
4467 .maxlen = sizeof(int),
4468 .mode = 0644,
4469 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 },
4471 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004472 .procname = "router_solicitations",
4473 .data = &ipv6_devconf.rtr_solicits,
4474 .maxlen = sizeof(int),
4475 .mode = 0644,
4476 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 },
4478 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004479 .procname = "router_solicitation_interval",
4480 .data = &ipv6_devconf.rtr_solicit_interval,
4481 .maxlen = sizeof(int),
4482 .mode = 0644,
4483 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 },
4485 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004486 .procname = "router_solicitation_delay",
4487 .data = &ipv6_devconf.rtr_solicit_delay,
4488 .maxlen = sizeof(int),
4489 .mode = 0644,
4490 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 },
4492 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004493 .procname = "force_mld_version",
4494 .data = &ipv6_devconf.force_mld_version,
4495 .maxlen = sizeof(int),
4496 .mode = 0644,
4497 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 },
4499#ifdef CONFIG_IPV6_PRIVACY
4500 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004501 .procname = "use_tempaddr",
4502 .data = &ipv6_devconf.use_tempaddr,
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 = "temp_valid_lft",
4509 .data = &ipv6_devconf.temp_valid_lft,
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 = "temp_prefered_lft",
4516 .data = &ipv6_devconf.temp_prefered_lft,
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 = "regen_max_retry",
4523 .data = &ipv6_devconf.regen_max_retry,
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 = "max_desync_factor",
4530 .data = &ipv6_devconf.max_desync_factor,
4531 .maxlen = sizeof(int),
4532 .mode = 0644,
4533 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 },
4535#endif
4536 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004537 .procname = "max_addresses",
4538 .data = &ipv6_devconf.max_addresses,
4539 .maxlen = sizeof(int),
4540 .mode = 0644,
4541 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 },
4543 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004544 .procname = "accept_ra_defrtr",
4545 .data = &ipv6_devconf.accept_ra_defrtr,
4546 .maxlen = sizeof(int),
4547 .mode = 0644,
4548 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004549 },
4550 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004551 .procname = "accept_ra_pinfo",
4552 .data = &ipv6_devconf.accept_ra_pinfo,
4553 .maxlen = sizeof(int),
4554 .mode = 0644,
4555 .proc_handler = proc_dointvec,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004556 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004557#ifdef CONFIG_IPV6_ROUTER_PREF
4558 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004559 .procname = "accept_ra_rtr_pref",
4560 .data = &ipv6_devconf.accept_ra_rtr_pref,
4561 .maxlen = sizeof(int),
4562 .mode = 0644,
4563 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004564 },
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08004565 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004566 .procname = "router_probe_interval",
4567 .data = &ipv6_devconf.rtr_probe_interval,
4568 .maxlen = sizeof(int),
4569 .mode = 0644,
4570 .proc_handler = proc_dointvec_jiffies,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08004571 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08004572#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004573 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004574 .procname = "accept_ra_rt_info_max_plen",
4575 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
4576 .maxlen = sizeof(int),
4577 .mode = 0644,
4578 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004579 },
4580#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004581#endif
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004582 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004583 .procname = "proxy_ndp",
4584 .data = &ipv6_devconf.proxy_ndp,
4585 .maxlen = sizeof(int),
4586 .mode = 0644,
4587 .proc_handler = proc_dointvec,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004588 },
4589 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004590 .procname = "accept_source_route",
4591 .data = &ipv6_devconf.accept_source_route,
4592 .maxlen = sizeof(int),
4593 .mode = 0644,
4594 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004595 },
Neil Horman95c385b2007-04-25 17:08:10 -07004596#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4597 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004598 .procname = "optimistic_dad",
4599 .data = &ipv6_devconf.optimistic_dad,
4600 .maxlen = sizeof(int),
4601 .mode = 0644,
4602 .proc_handler = proc_dointvec,
Neil Horman95c385b2007-04-25 17:08:10 -07004603
4604 },
4605#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004606#ifdef CONFIG_IPV6_MROUTE
4607 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004608 .procname = "mc_forwarding",
4609 .data = &ipv6_devconf.mc_forwarding,
4610 .maxlen = sizeof(int),
4611 .mode = 0444,
4612 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004613 },
4614#endif
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004615 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004616 .procname = "disable_ipv6",
4617 .data = &ipv6_devconf.disable_ipv6,
4618 .maxlen = sizeof(int),
4619 .mode = 0644,
4620 .proc_handler = addrconf_sysctl_disable,
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09004621 },
4622 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004623 .procname = "accept_dad",
4624 .data = &ipv6_devconf.accept_dad,
4625 .maxlen = sizeof(int),
4626 .mode = 0644,
4627 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09004628 },
4629 {
Octavian Purdilaf7734fd2009-10-02 11:39:15 +00004630 .procname = "force_tllao",
4631 .data = &ipv6_devconf.force_tllao,
4632 .maxlen = sizeof(int),
4633 .mode = 0644,
4634 .proc_handler = proc_dointvec
4635 },
4636 {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004637 /* sentinel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 }
4639 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640};
4641
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004642static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004643 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644{
4645 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 struct addrconf_sysctl_table *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004648#define ADDRCONF_CTL_PATH_DEV 3
4649
4650 struct ctl_path addrconf_ctl_path[] = {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004651 { .procname = "net", },
4652 { .procname = "ipv6", },
4653 { .procname = "conf", },
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004654 { /* to be set */ },
4655 { },
4656 };
4657
4658
Arnaldo Carvalho de Meloaf879cc2006-11-17 12:14:37 -02004659 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 if (t == NULL)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004661 goto out;
4662
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004663 for (i = 0; t->addrconf_vars[i].data; i++) {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004664 t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004666 t->addrconf_vars[i].extra2 = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004669 /*
4670 * Make a copy of dev_name, because '.procname' is regarded as const
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 * by sysctl and we wouldn't want anyone to change it under our feet
4672 * (see SIOCSIFNAME).
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004673 */
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004674 t->dev_name = kstrdup(dev_name, GFP_KERNEL);
4675 if (!t->dev_name)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004676 goto free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004678 addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004680 t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path,
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004681 t->addrconf_vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 if (t->sysctl_header == NULL)
4683 goto free_procname;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004684
4685 p->sysctl = t;
Pavel Emelyanov95897312008-01-10 17:41:45 -08004686 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004688free_procname:
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004689 kfree(t->dev_name);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004690free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 kfree(t);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004692out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08004693 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694}
4695
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004696static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
4697{
4698 struct addrconf_sysctl_table *t;
4699
4700 if (p->sysctl == NULL)
4701 return;
4702
4703 t = p->sysctl;
4704 p->sysctl = NULL;
Lucian Adrian Grijincuff538812011-05-01 01:44:01 +00004705 unregister_net_sysctl_table(t->sysctl_header);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004706 kfree(t->dev_name);
4707 kfree(t);
4708}
4709
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004710static void addrconf_sysctl_register(struct inet6_dev *idev)
4711{
Eric W. Biederman54716e32010-02-14 03:27:03 +00004712 neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6",
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004713 &ndisc_ifinfo_sysctl_change);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004714 __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004715 idev, &idev->cnf);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004716}
4717
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004718static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004720 __addrconf_sysctl_unregister(&idev->cnf);
4721 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722}
4723
4724
4725#endif
4726
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004727static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004728{
Hong Zhiguo6a519972013-03-26 01:52:45 +08004729 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004730 struct ipv6_devconf *all, *dflt;
4731
Hong Zhiguo6a519972013-03-26 01:52:45 +08004732 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
4733 if (all == NULL)
4734 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004735
Hong Zhiguo6a519972013-03-26 01:52:45 +08004736 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
4737 if (dflt == NULL)
4738 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004739
Hong Zhiguo6a519972013-03-26 01:52:45 +08004740 /* these will be inherited by all namespaces */
4741 dflt->autoconf = ipv6_defaults.autoconf;
4742 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004743
4744 net->ipv6.devconf_all = all;
4745 net->ipv6.devconf_dflt = dflt;
4746
4747#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004748 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004749 if (err < 0)
4750 goto err_reg_all;
4751
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004752 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004753 if (err < 0)
4754 goto err_reg_dflt;
4755#endif
4756 return 0;
4757
4758#ifdef CONFIG_SYSCTL
4759err_reg_dflt:
4760 __addrconf_sysctl_unregister(all);
4761err_reg_all:
4762 kfree(dflt);
4763#endif
4764err_alloc_dflt:
4765 kfree(all);
4766err_alloc_all:
4767 return err;
4768}
4769
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004770static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004771{
4772#ifdef CONFIG_SYSCTL
4773 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
4774 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
4775#endif
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08004776 if (!net_eq(net, &init_net)) {
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004777 kfree(net->ipv6.devconf_dflt);
4778 kfree(net->ipv6.devconf_all);
4779 }
4780}
4781
4782static struct pernet_operations addrconf_ops = {
4783 .init = addrconf_init_net,
4784 .exit = addrconf_exit_net,
4785};
4786
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787/*
4788 * Device notifier
4789 */
4790
4791int register_inet6addr_notifier(struct notifier_block *nb)
4792{
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004793 return atomic_notifier_chain_register(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09004795EXPORT_SYMBOL(register_inet6addr_notifier);
4796
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797int unregister_inet6addr_notifier(struct notifier_block *nb)
4798{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004799 return atomic_notifier_chain_unregister(&inet6addr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09004801EXPORT_SYMBOL(unregister_inet6addr_notifier);
4802
Thomas Grafb382b192010-11-16 04:33:57 +00004803static struct rtnl_af_ops inet6_ops = {
4804 .family = AF_INET6,
4805 .fill_link_af = inet6_fill_link_af,
4806 .get_link_af_size = inet6_get_link_af_size,
Thomas Grafb382b192010-11-16 04:33:57 +00004807};
4808
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809/*
4810 * Init / cleanup code
4811 */
4812
4813int __init addrconf_init(void)
4814{
stephen hemmingerc2e21292010-03-17 20:31:10 +00004815 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004816
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004817 err = ipv6_addr_label_init();
4818 if (err < 0) {
4819 printk(KERN_CRIT "IPv6 Addrconf:"
4820 " cannot initialize default policy table: %d.\n", err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004821 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004824 err = register_pernet_subsys(&addrconf_ops);
4825 if (err < 0)
4826 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004827
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 /* The addrconf netdev notifier requires that loopback_dev
4829 * has it's ipv6 private information allocated and setup
4830 * before it can bring up and give link-local addresses
4831 * to other devices which are up.
4832 *
4833 * Unfortunately, loopback_dev is not necessarily the first
4834 * entry in the global dev_base list of net devices. In fact,
4835 * it is likely to be the very last entry on that list.
4836 * So this causes the notifier registry below to try and
4837 * give link-local addresses to all devices besides loopback_dev
4838 * first, then loopback_dev, which cases all the non-loopback_dev
4839 * devices to fail to get a link-local address.
4840 *
4841 * So, as a temporary fix, allocate the ipv6 structure for
4842 * loopback_dev first by hand.
4843 * Longer term, all of the dependencies ipv6 has upon the loopback
4844 * device and it being up should be removed.
4845 */
4846 rtnl_lock();
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07004847 if (!ipv6_add_dev(init_net.loopback_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 err = -ENOMEM;
4849 rtnl_unlock();
4850 if (err)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004851 goto errlo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852
stephen hemmingerc2e21292010-03-17 20:31:10 +00004853 for (i = 0; i < IN6_ADDR_HSIZE; i++)
4854 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
4855
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 register_netdevice_notifier(&ipv6_dev_notf);
4857
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 addrconf_verify(0);
Thomas Grafc127ea22007-03-22 11:58:32 -07004859
Thomas Grafb382b192010-11-16 04:33:57 +00004860 err = rtnl_af_register(&inet6_ops);
4861 if (err < 0)
4862 goto errout_af;
4863
Greg Rosec7ac8672011-06-10 01:27:09 +00004864 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
4865 NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07004866 if (err < 0)
4867 goto errout;
4868
4869 /* Only the first call to __rtnl_register can fail */
Greg Rosec7ac8672011-06-10 01:27:09 +00004870 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
4871 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
4872 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
4873 inet6_dump_ifaddr, NULL);
4874 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
4875 inet6_dump_ifmcaddr, NULL);
4876 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
4877 inet6_dump_ifacaddr, NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07004878
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004879 ipv6_addr_label_rtnl_register();
4880
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07004882errout:
Thomas Grafb382b192010-11-16 04:33:57 +00004883 rtnl_af_unregister(&inet6_ops);
4884errout_af:
Thomas Grafc127ea22007-03-22 11:58:32 -07004885 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004886errlo:
4887 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004888out_addrlabel:
4889 ipv6_addr_label_cleanup();
4890out:
Thomas Grafc127ea22007-03-22 11:58:32 -07004891 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892}
4893
Daniel Lezcano09f77092007-12-13 05:34:58 -08004894void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08004896 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 int i;
4898
4899 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004900 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004901 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902
4903 rtnl_lock();
4904
Thomas Grafb382b192010-11-16 04:33:57 +00004905 __rtnl_af_unregister(&inet6_ops);
4906
Daniel Lezcano176c39a2009-03-03 01:06:45 -08004907 /* clean dev list */
4908 for_each_netdev(&init_net, dev) {
4909 if (__in6_dev_get(dev) == NULL)
4910 continue;
4911 addrconf_ifdown(dev, 1);
4912 }
4913 addrconf_ifdown(init_net.loopback_dev, 2);
4914
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 * Check hash table.
4917 */
stephen hemminger5c578ae2010-03-17 20:31:11 +00004918 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004919 for (i = 0; i < IN6_ADDR_HSIZE; i++)
4920 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578ae2010-03-17 20:31:11 +00004921 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
4923 del_timer(&addr_chk_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925}