blob: dae802c0af7c002df341d3dfe4ed94db6584118e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IPv6 Address [auto]configuration
3 * Linux INET6 implementation
4 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
15/*
16 * Changes:
17 *
18 * Janos Farkas : delete timer on ifdown
19 * <chexum@bankinf.banki.hu>
20 * Andi Kleen : kill double kfree on module
21 * unload.
22 * Maciej W. Rozycki : FDDI support
23 * sekiya@USAGI : Don't send too many RS
24 * packets.
25 * yoshfuji@USAGI : Fixed interval between DAD
26 * packets.
27 * YOSHIFUJI Hideaki @USAGI : improved accuracy of
28 * address validation timer.
29 * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
30 * support.
31 * Yuji SEKIYA @USAGI : Don't assign a same IPv6
32 * address on a same interface.
33 * YOSHIFUJI Hideaki @USAGI : ARCnet support
34 * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to
35 * seq_file.
YOSHIFUJI Hideakib1cacb62005-11-08 09:38:12 -080036 * YOSHIFUJI Hideaki @USAGI : improved source address
37 * selection; consider scope,
38 * status etc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
40
Joe Perchesf3213832012-05-15 14:11:53 +000041#define pr_fmt(fmt) "IPv6: " fmt
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/errno.h>
44#include <linux/types.h>
Ilpo Järvinena0bffff2009-03-21 13:36:17 -070045#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/socket.h>
47#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/net.h>
49#include <linux/in6.h>
50#include <linux/netdevice.h>
Thomas Graf18237302006-08-04 23:04:54 -070051#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/if_arp.h>
53#include <linux/if_arcnet.h>
54#include <linux/if_infiniband.h>
55#include <linux/route.h>
56#include <linux/inetdevice.h>
57#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090058#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#ifdef CONFIG_SYSCTL
60#include <linux/sysctl.h>
61#endif
Randy Dunlap4fc268d2006-01-11 12:17:47 -080062#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/delay.h>
64#include <linux/notifier.h>
Paulo Marques543537b2005-06-23 00:09:02 -070065#include <linux/string.h>
Eric Dumazetddbe5032012-07-18 08:11:12 +000066#include <linux/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020068#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <net/sock.h>
70#include <net/snmp.h>
71
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +000072#include <net/af_ieee802154.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <net/ipv6.h>
74#include <net/protocol.h>
75#include <net/ndisc.h>
76#include <net/ip6_route.h>
77#include <net/addrconf.h>
78#include <net/tcp.h>
79#include <net/ip.h>
Thomas Graf5d620262006-08-15 00:35:02 -070080#include <net/netlink.h>
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -070081#include <net/pkt_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <linux/if_tunnel.h>
83#include <linux/rtnetlink.h>
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +000084#include <linux/netconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86#ifdef CONFIG_IPV6_PRIVACY
87#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#endif
89
Stephen Hemmingere21e8462010-03-20 16:09:01 -070090#include <linux/uaccess.h>
Herbert Xu7f7d9a62007-04-24 21:54:09 -070091#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93#include <linux/proc_fs.h>
94#include <linux/seq_file.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040095#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/* Set to 3 to get tracing... */
98#define ACONF_DEBUG 2
99
100#if ACONF_DEBUG >= 3
101#define ADBG(x) printk x
102#else
103#define ADBG(x)
104#endif
105
106#define INFINITY_LIFE_TIME 0xFFFFFFFF
Thomas Graf18a31e12010-11-17 04:12:02 +0000107
108static inline u32 cstamp_delta(unsigned long cstamp)
109{
110 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113#ifdef CONFIG_SYSCTL
Pavel Emelyanovf52295a2007-12-02 00:58:37 +1100114static void addrconf_sysctl_register(struct inet6_dev *idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -0800115static void addrconf_sysctl_unregister(struct inet6_dev *idev);
116#else
117static inline void addrconf_sysctl_register(struct inet6_dev *idev)
118{
119}
120
121static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
122{
123}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124#endif
125
126#ifdef CONFIG_IPV6_PRIVACY
Sorin Dumitrueb7e0572012-08-30 02:01:45 +0000127static void __ipv6_regen_rndid(struct inet6_dev *idev);
128static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static void ipv6_regen_rndid(unsigned long data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#endif
131
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900132static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133static int ipv6_count_addresses(struct inet6_dev *idev);
134
135/*
136 * Configured unicast address hash table
137 */
stephen hemmingerc2e21292010-03-17 20:31:10 +0000138static struct hlist_head inet6_addr_lst[IN6_ADDR_HSIZE];
stephen hemminger5c578ae2010-03-17 20:31:11 +0000139static DEFINE_SPINLOCK(addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static void addrconf_verify(unsigned long);
142
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700143static DEFINE_TIMER(addr_chk_timer, addrconf_verify, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static DEFINE_SPINLOCK(addrconf_verify_lock);
145
146static void addrconf_join_anycast(struct inet6_ifaddr *ifp);
147static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);
148
Jiri Pirko93d9b7d2010-03-10 10:28:56 +0000149static void addrconf_type_change(struct net_device *dev,
150 unsigned long event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static int addrconf_ifdown(struct net_device *dev, int how);
152
Romain Kuntz21caa662013-01-09 21:06:03 +0000153static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
154 int plen,
155 const struct net_device *dev,
156 u32 flags, u32 noflags);
157
David S. Millercf22f9a2012-04-14 21:37:40 -0400158static void addrconf_dad_start(struct inet6_ifaddr *ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static void addrconf_dad_timer(unsigned long data);
160static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900161static void addrconf_dad_run(struct inet6_dev *idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162static void addrconf_rs_timer(unsigned long data);
163static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
164static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
165
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900166static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 struct prefix_info *pinfo);
David S. Miller3e81c6d2010-03-20 16:18:00 -0700168static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
169 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Adrian Bunk888c8482008-07-22 14:21:58 -0700171static struct ipv6_devconf ipv6_devconf __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 .forwarding = 0,
173 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
174 .mtu6 = IPV6_MIN_MTU,
175 .accept_ra = 1,
176 .accept_redirects = 1,
177 .autoconf = 1,
178 .force_mld_version = 0,
179 .dad_transmits = 1,
180 .rtr_solicits = MAX_RTR_SOLICITATIONS,
181 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
182 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
183#ifdef CONFIG_IPV6_PRIVACY
184 .use_tempaddr = 0,
185 .temp_valid_lft = TEMP_VALID_LIFETIME,
186 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
187 .regen_max_retry = REGEN_MAX_RETRY,
188 .max_desync_factor = MAX_DESYNC_FACTOR,
189#endif
190 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800191 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800192 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800193#ifdef CONFIG_IPV6_ROUTER_PREF
194 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800195 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800196#ifdef CONFIG_IPV6_ROUTE_INFO
197 .accept_ra_rt_info_max_plen = 0,
198#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800199#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700200 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700201 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900202 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900203 .accept_dad = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Brian Haleyab32ea52006-09-22 14:15:41 -0700206static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 .forwarding = 0,
208 .hop_limit = IPV6_DEFAULT_HOPLIMIT,
209 .mtu6 = IPV6_MIN_MTU,
210 .accept_ra = 1,
211 .accept_redirects = 1,
212 .autoconf = 1,
213 .dad_transmits = 1,
214 .rtr_solicits = MAX_RTR_SOLICITATIONS,
215 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
216 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
217#ifdef CONFIG_IPV6_PRIVACY
218 .use_tempaddr = 0,
219 .temp_valid_lft = TEMP_VALID_LIFETIME,
220 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
221 .regen_max_retry = REGEN_MAX_RETRY,
222 .max_desync_factor = MAX_DESYNC_FACTOR,
223#endif
224 .max_addresses = IPV6_MAX_ADDRESSES,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -0800225 .accept_ra_defrtr = 1,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -0800226 .accept_ra_pinfo = 1,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800227#ifdef CONFIG_IPV6_ROUTER_PREF
228 .accept_ra_rtr_pref = 1,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -0800229 .rtr_probe_interval = 60 * HZ,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -0800230#ifdef CONFIG_IPV6_ROUTE_INFO
231 .accept_ra_rt_info_max_plen = 0,
232#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -0800233#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -0700234 .proxy_ndp = 0,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -0700235 .accept_source_route = 0, /* we do not accept RH0 by default. */
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +0900236 .disable_ipv6 = 0,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +0900237 .accept_dad = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238};
239
240/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900243const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
244const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000245const struct in6_addr in6addr_interfacelocal_allnodes = IN6ADDR_INTERFACELOCAL_ALLNODES_INIT;
246const struct in6_addr in6addr_interfacelocal_allrouters = IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT;
247const struct in6_addr in6addr_sitelocal_allrouters = IN6ADDR_SITELOCAL_ALLROUTERS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700249/* Check if a valid qdisc is available */
David S. Miller05297942008-07-08 23:01:27 -0700250static inline bool addrconf_qdisc_ok(const struct net_device *dev)
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700251{
David S. Miller05297942008-07-08 23:01:27 -0700252 return !qdisc_tx_is_noop(dev);
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255static void addrconf_del_timer(struct inet6_ifaddr *ifp)
256{
257 if (del_timer(&ifp->timer))
258 __in6_ifa_put(ifp);
259}
260
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700261enum addrconf_timer_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 AC_NONE,
263 AC_DAD,
264 AC_RS,
265};
266
267static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
268 enum addrconf_timer_t what,
269 unsigned long when)
270{
271 if (!del_timer(&ifp->timer))
272 in6_ifa_hold(ifp);
273
274 switch (what) {
275 case AC_DAD:
276 ifp->timer.function = addrconf_dad_timer;
277 break;
278 case AC_RS:
279 ifp->timer.function = addrconf_rs_timer;
280 break;
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700281 default:
282 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284 ifp->timer.expires = jiffies + when;
285 add_timer(&ifp->timer);
286}
287
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700288static int snmp6_alloc_dev(struct inet6_dev *idev)
289{
Tejun Heo7d720c32010-02-16 15:20:26 +0000290 if (snmp_mib_init((void __percpu **)idev->stats.ipv6,
Eric Dumazet1823e4c82010-06-22 20:58:41 +0000291 sizeof(struct ipstats_mib),
292 __alignof__(struct ipstats_mib)) < 0)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700293 goto err_ip;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000294 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
295 GFP_KERNEL);
296 if (!idev->stats.icmpv6dev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700297 goto err_icmp;
Eric Dumazetbe281e52011-05-19 01:14:23 +0000298 idev->stats.icmpv6msgdev = kzalloc(sizeof(struct icmpv6msg_mib_device),
299 GFP_KERNEL);
300 if (!idev->stats.icmpv6msgdev)
David L Stevens14878f72007-09-16 16:52:35 -0700301 goto err_icmpmsg;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700302
303 return 0;
304
David L Stevens14878f72007-09-16 16:52:35 -0700305err_icmpmsg:
Eric Dumazetbe281e52011-05-19 01:14:23 +0000306 kfree(idev->stats.icmpv6dev);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700307err_icmp:
Tejun Heo7d720c32010-02-16 15:20:26 +0000308 snmp_mib_free((void __percpu **)idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700309err_ip:
Pavel Emelyanovaaf70ec2007-10-17 21:25:32 -0700310 return -ENOMEM;
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700311}
312
Pavel Emelyanov16910b92007-10-17 21:23:43 -0700313static void snmp6_free_dev(struct inet6_dev *idev)
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700314{
Eric Dumazetbe281e52011-05-19 01:14:23 +0000315 kfree(idev->stats.icmpv6msgdev);
316 kfree(idev->stats.icmpv6dev);
Tejun Heo7d720c32010-02-16 15:20:26 +0000317 snmp_mib_free((void __percpu **)idev->stats.ipv6);
Herbert Xu7f7d9a62007-04-24 21:54:09 -0700318}
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320/* Nobody refers to this device, we may destroy it. */
321
322void in6_dev_finish_destroy(struct inet6_dev *idev)
323{
324 struct net_device *dev = idev->dev;
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700325
stephen hemminger502a2ff2010-03-17 20:31:13 +0000326 WARN_ON(!list_empty(&idev->addr_list));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700327 WARN_ON(idev->mc_list != NULL);
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329#ifdef NET_REFCNT_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +0000330 pr_debug("%s: %s\n", __func__, dev ? dev->name : "NIL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331#endif
332 dev_put(dev);
333 if (!idev->dead) {
Joe Perchesf3213832012-05-15 14:11:53 +0000334 pr_warn("Freeing alive inet6 device %p\n", idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return;
336 }
337 snmp6_free_dev(idev);
Lai Jiangshan38f57d12011-03-15 17:59:14 +0800338 kfree_rcu(idev, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +0900340EXPORT_SYMBOL(in6_dev_finish_destroy);
341
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000342static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
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
Joe Perchesf3213832012-05-15 14:11:53 +0000375 "%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
Joe Perchesf3213832012-05-15 14:11:53 +0000385 "%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
Amerigo Wang07a93622012-10-29 16:23:10 +0000401#if IS_ENABLED(CONFIG_IPV6_SIT)
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700402 if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
Joe Perchesf3213832012-05-15 14:11:53 +0000403 pr_info("%s: Disabled Multicast RS\n", dev->name);
YOSHIFUJI Hideakib077d7a2008-04-13 23:42:18 -0700404 ndev->cnf.rtr_solicits = 0;
405 }
406#endif
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408#ifdef CONFIG_IPV6_PRIVACY
stephen hemminger372e6c82010-03-17 20:31:09 +0000409 INIT_LIST_HEAD(&ndev->tempaddr_list);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800410 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
Ingo Oeser322f74a2006-03-20 23:01:47 -0800411 if ((dev->flags&IFF_LOOPBACK) ||
412 dev->type == ARPHRD_TUNNEL ||
YOSHIFUJI Hideaki9625ed72008-04-13 23:47:11 -0700413 dev->type == ARPHRD_TUNNEL6 ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700414 dev->type == ARPHRD_SIT ||
Joerg Roedel0be669b2006-10-10 14:49:53 -0700415 dev->type == ARPHRD_NONE) {
Ingo Oeser322f74a2006-03-20 23:01:47 -0800416 ndev->cnf.use_tempaddr = -1;
417 } else {
418 in6_dev_hold(ndev);
419 ipv6_regen_rndid((unsigned long) ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
Ingo Oeser322f74a2006-03-20 23:01:47 -0800421#endif
422
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -0700423 if (netif_running(dev) && addrconf_qdisc_ok(dev))
Herbert Xu53aadcc2007-03-27 14:31:52 -0700424 ndev->if_flags |= IF_READY;
425
Ingo Oeser322f74a2006-03-20 23:01:47 -0800426 ipv6_mc_init_dev(ndev);
427 ndev->tstamp = jiffies;
Pavel Emelyanovf52295a2007-12-02 00:58:37 +1100428 addrconf_sysctl_register(ndev);
David L Stevens30c4cf52007-01-04 12:31:14 -0800429 /* protected by rtnl_lock */
Eric Dumazetcf778b02012-01-12 04:41:32 +0000430 rcu_assign_pointer(dev->ip6_ptr, ndev);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800431
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000432 /* Join interface-local all-node multicast group */
433 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allnodes);
434
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800435 /* Join all-node multicast group */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900436 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allnodes);
YOSHIFUJI Hideakid88ae4c2007-01-14 21:48:40 -0800437
Li Weid6ddef92012-03-05 14:45:17 +0000438 /* Join all-router multicast group if forwarding is set */
Li Wei8b2aaed2012-03-07 14:58:07 +0000439 if (ndev->cnf.forwarding && (dev->flags & IFF_MULTICAST))
Li Weid6ddef92012-03-05 14:45:17 +0000440 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return ndev;
443}
444
Eldad Zack8e5e8f32012-04-01 07:49:08 +0000445static struct inet6_dev *ipv6_find_idev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 struct inet6_dev *idev;
448
449 ASSERT_RTNL();
450
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700451 idev = __in6_dev_get(dev);
452 if (!idev) {
453 idev = ipv6_add_dev(dev);
454 if (!idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return NULL;
456 }
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +0900457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (dev->flags&IFF_UP)
459 ipv6_mc_up(idev);
460 return idev;
461}
462
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000463static int inet6_netconf_msgsize_devconf(int type)
464{
465 int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
466 + nla_total_size(4); /* NETCONFA_IFINDEX */
467
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000468 /* type -1 is used for ALL */
469 if (type == -1 || type == NETCONFA_FORWARDING)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000470 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500471#ifdef CONFIG_IPV6_MROUTE
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000472 if (type == -1 || type == NETCONFA_MC_FORWARDING)
473 size += nla_total_size(4);
David S. Millerb1afce92012-12-04 14:46:34 -0500474#endif
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000475
476 return size;
477}
478
479static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
480 struct ipv6_devconf *devconf, u32 portid,
481 u32 seq, int event, unsigned int flags,
482 int type)
483{
484 struct nlmsghdr *nlh;
485 struct netconfmsg *ncm;
486
487 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
488 flags);
489 if (nlh == NULL)
490 return -EMSGSIZE;
491
492 ncm = nlmsg_data(nlh);
493 ncm->ncm_family = AF_INET6;
494
495 if (nla_put_s32(skb, NETCONFA_IFINDEX, ifindex) < 0)
496 goto nla_put_failure;
497
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000498 /* type -1 is used for ALL */
499 if ((type == -1 || type == NETCONFA_FORWARDING) &&
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000500 nla_put_s32(skb, NETCONFA_FORWARDING, devconf->forwarding) < 0)
501 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500502#ifdef CONFIG_IPV6_MROUTE
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000503 if ((type == -1 || type == NETCONFA_MC_FORWARDING) &&
504 nla_put_s32(skb, NETCONFA_MC_FORWARDING,
505 devconf->mc_forwarding) < 0)
506 goto nla_put_failure;
David S. Millerb1afce92012-12-04 14:46:34 -0500507#endif
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000508 return nlmsg_end(skb, nlh);
509
510nla_put_failure:
511 nlmsg_cancel(skb, nlh);
512 return -EMSGSIZE;
513}
514
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000515void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
516 struct ipv6_devconf *devconf)
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000517{
518 struct sk_buff *skb;
519 int err = -ENOBUFS;
520
521 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
522 if (skb == NULL)
523 goto errout;
524
525 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
526 RTM_NEWNETCONF, 0, type);
527 if (err < 0) {
528 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
529 WARN_ON(err == -EMSGSIZE);
530 kfree_skb(skb);
531 goto errout;
532 }
533 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_NETCONF, NULL, GFP_ATOMIC);
534 return;
535errout:
Cong Dingbd779022012-12-18 12:08:56 +0000536 rtnl_set_sk_err(net, RTNLGRP_IPV6_NETCONF, err);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000537}
538
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +0000539static const struct nla_policy devconf_ipv6_policy[NETCONFA_MAX+1] = {
540 [NETCONFA_IFINDEX] = { .len = sizeof(int) },
541 [NETCONFA_FORWARDING] = { .len = sizeof(int) },
542};
543
544static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
545 struct nlmsghdr *nlh,
546 void *arg)
547{
548 struct net *net = sock_net(in_skb->sk);
549 struct nlattr *tb[NETCONFA_MAX+1];
550 struct netconfmsg *ncm;
551 struct sk_buff *skb;
552 struct ipv6_devconf *devconf;
553 struct inet6_dev *in6_dev;
554 struct net_device *dev;
555 int ifindex;
556 int err;
557
558 err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
559 devconf_ipv6_policy);
560 if (err < 0)
561 goto errout;
562
563 err = EINVAL;
564 if (!tb[NETCONFA_IFINDEX])
565 goto errout;
566
567 ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
568 switch (ifindex) {
569 case NETCONFA_IFINDEX_ALL:
570 devconf = net->ipv6.devconf_all;
571 break;
572 case NETCONFA_IFINDEX_DEFAULT:
573 devconf = net->ipv6.devconf_dflt;
574 break;
575 default:
576 dev = __dev_get_by_index(net, ifindex);
577 if (dev == NULL)
578 goto errout;
579 in6_dev = __in6_dev_get(dev);
580 if (in6_dev == NULL)
581 goto errout;
582 devconf = &in6_dev->cnf;
583 break;
584 }
585
586 err = -ENOBUFS;
587 skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC);
588 if (skb == NULL)
589 goto errout;
590
591 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
592 NETLINK_CB(in_skb).portid,
593 nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
594 -1);
595 if (err < 0) {
596 /* -EMSGSIZE implies BUG in inet6_netconf_msgsize_devconf() */
597 WARN_ON(err == -EMSGSIZE);
598 kfree_skb(skb);
599 goto errout;
600 }
601 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
602errout:
603 return err;
604}
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606#ifdef CONFIG_SYSCTL
607static void dev_forward_change(struct inet6_dev *idev)
608{
609 struct net_device *dev;
610 struct inet6_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 if (!idev)
613 return;
614 dev = idev->dev;
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700615 if (idev->cnf.forwarding)
616 dev_disable_lro(dev);
Amerigo Wang1a940832012-10-28 17:43:53 +0000617 if (dev->flags & IFF_MULTICAST) {
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000618 if (idev->cnf.forwarding) {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900619 ipv6_dev_mc_inc(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000620 ipv6_dev_mc_inc(dev, &in6addr_interfacelocal_allrouters);
621 ipv6_dev_mc_inc(dev, &in6addr_sitelocal_allrouters);
622 } else {
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +0900623 ipv6_dev_mc_dec(dev, &in6addr_linklocal_allrouters);
Hannes Frederic Sowa2c5e8932013-02-10 03:50:18 +0000624 ipv6_dev_mc_dec(dev, &in6addr_interfacelocal_allrouters);
625 ipv6_dev_mc_dec(dev, &in6addr_sitelocal_allrouters);
626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
stephen hemminger502a2ff2010-03-17 20:31:13 +0000628
629 list_for_each_entry(ifa, &idev->addr_list, if_list) {
Michal Wrobel2c12a742007-02-26 15:36:10 -0800630 if (ifa->flags&IFA_F_TENTATIVE)
631 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (idev->cnf.forwarding)
633 addrconf_join_anycast(ifa);
634 else
635 addrconf_leave_anycast(ifa);
636 }
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000637 inet6_netconf_notify_devconf(dev_net(dev), NETCONFA_FORWARDING,
638 dev->ifindex, &idev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
641
Pavel Emelyanove1869322008-01-10 17:43:50 -0800642static void addrconf_forward_change(struct net *net, __s32 newf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 struct net_device *dev;
645 struct inet6_dev *idev;
646
Ben Hutchings4acd4942012-08-14 08:54:51 +0000647 for_each_netdev(net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 idev = __in6_dev_get(dev);
649 if (idev) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800650 int changed = (!idev->cnf.forwarding) ^ (!newf);
651 idev->cnf.forwarding = newf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (changed)
653 dev_forward_change(idev);
654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800657
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000658static int addrconf_fixup_forwarding(struct ctl_table *table, int *p, int newf)
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800659{
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800660 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000661 int old;
662
663 if (!rtnl_trylock())
664 return restart_syscall();
Pavel Emelyanovbff16c22008-01-10 17:42:13 -0800665
666 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000667 old = *p;
668 *p = newf;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800669
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000670 if (p == &net->ipv6.devconf_dflt->forwarding) {
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000671 if ((!newf) ^ (!old))
672 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
673 NETCONFA_IFINDEX_DEFAULT,
674 net->ipv6.devconf_dflt);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000675 rtnl_unlock();
676 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +0000677 }
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000678
Pavel Emelyanove1869322008-01-10 17:43:50 -0800679 if (p == &net->ipv6.devconf_all->forwarding) {
Pavel Emelyanove1869322008-01-10 17:43:50 -0800680 net->ipv6.devconf_dflt->forwarding = newf;
681 addrconf_forward_change(net, newf);
Nicolas Dichtelf3a1bfb2012-10-25 22:28:50 +0000682 if ((!newf) ^ (!old))
683 inet6_netconf_notify_devconf(net, NETCONFA_FORWARDING,
684 NETCONFA_IFINDEX_ALL,
685 net->ipv6.devconf_all);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000686 } else if ((!newf) ^ (!old))
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800687 dev_forward_change((struct inet6_dev *)table->extra1);
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700688 rtnl_unlock();
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800689
Francesco Ruggeri013d97e2012-01-16 10:40:10 +0000690 if (newf)
Daniel Lezcano7b4da532008-03-04 13:47:14 -0800691 rt6_purge_dflt_routers(net);
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +0000692 return 1;
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -0800693}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694#endif
695
stephen hemminger5c578ae2010-03-17 20:31:11 +0000696/* Nobody refers to this ifaddr, destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp)
698{
stephen hemmingerc2e21292010-03-17 20:31:10 +0000699 WARN_ON(!hlist_unhashed(&ifp->addr_lst));
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701#ifdef NET_REFCNT_DEBUG
Joe Perches91df42b2012-05-15 14:11:54 +0000702 pr_debug("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703#endif
704
705 in6_dev_put(ifp->idev);
706
707 if (del_timer(&ifp->timer))
Stephen Hemmingere21e8462010-03-20 16:09:01 -0700708 pr_notice("Timer is still running, when freeing ifa=%p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Herbert Xue9d3e082010-05-18 15:36:06 -0700710 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
Joe Perchesf3213832012-05-15 14:11:53 +0000711 pr_warn("Freeing alive inet6 address %p\n", ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return;
713 }
Amerigo Wang94e187c2012-10-29 00:13:19 +0000714 ip6_rt_put(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Lai Jiangshane5785982011-03-15 18:00:14 +0800716 kfree_rcu(ifp, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
Brian Haleye55ffac2006-07-10 15:25:51 -0700719static void
720ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
721{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000722 struct list_head *p;
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700723 int ifp_scope = ipv6_addr_src_scope(&ifp->addr);
Brian Haleye55ffac2006-07-10 15:25:51 -0700724
725 /*
726 * Each device address list is sorted in order of scope -
727 * global before linklocal.
728 */
stephen hemminger502a2ff2010-03-17 20:31:13 +0000729 list_for_each(p, &idev->addr_list) {
730 struct inet6_ifaddr *ifa
731 = list_entry(p, struct inet6_ifaddr, if_list);
YOSHIFUJI Hideaki8a6ce0c2006-07-11 13:05:30 -0700732 if (ifp_scope >= ipv6_addr_src_scope(&ifa->addr))
Brian Haleye55ffac2006-07-10 15:25:51 -0700733 break;
734 }
735
David S. Millerb54c9b92010-03-25 21:25:30 -0700736 list_add_tail(&ifp->if_list, p);
Brian Haleye55ffac2006-07-10 15:25:51 -0700737}
738
Eric Dumazetddbe5032012-07-18 08:11:12 +0000739static u32 inet6_addr_hash(const struct in6_addr *addr)
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900740{
Eric Dumazetddbe5032012-07-18 08:11:12 +0000741 return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
YOSHIFUJI Hideaki3eb84f42008-04-10 15:42:08 +0900742}
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744/* On success it returns ifp with increased reference count */
745
746static struct inet6_ifaddr *
747ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -0700748 int scope, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 struct inet6_ifaddr *ifa = NULL;
751 struct rt6_info *rt;
stephen hemminger3a88a812010-03-17 20:31:12 +0000752 unsigned int hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 int err = 0;
YOSHIFUJI Hideakid68b8272008-06-25 16:26:47 +0900754 int addr_type = ipv6_addr_type(addr);
755
756 if (addr_type == IPV6_ADDR_ANY ||
757 addr_type & IPV6_ADDR_MULTICAST ||
758 (!(idev->dev->flags & IFF_LOOPBACK) &&
759 addr_type & IPV6_ADDR_LOOPBACK))
760 return ERR_PTR(-EADDRNOTAVAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700762 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (idev->dead) {
764 err = -ENODEV; /*XXX*/
765 goto out2;
766 }
767
Brian Haley56d417b2009-06-01 03:07:33 -0700768 if (idev->cnf.disable_ipv6) {
Brian Haley9bdd8d42009-03-18 18:22:48 -0700769 err = -EACCES;
770 goto out2;
771 }
772
stephen hemminger5c578ae2010-03-17 20:31:11 +0000773 spin_lock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 /* Ignore adding duplicate addresses on an interface */
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900776 if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 ADBG(("ipv6_add_addr: already assigned\n"));
778 err = -EEXIST;
779 goto out;
780 }
781
Ingo Oeser322f74a2006-03-20 23:01:47 -0800782 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 if (ifa == NULL) {
785 ADBG(("ipv6_add_addr: malloc failed\n"));
786 err = -ENOBUFS;
787 goto out;
788 }
789
David S. Miller8f031512011-12-06 16:48:14 -0500790 rt = addrconf_dst_alloc(idev, addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (IS_ERR(rt)) {
792 err = PTR_ERR(rt);
793 goto out;
794 }
795
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000796 ifa->addr = *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 spin_lock_init(&ifa->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -0700799 spin_lock_init(&ifa->state_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 init_timer(&ifa->timer);
stephen hemmingerc2e21292010-03-17 20:31:10 +0000801 INIT_HLIST_NODE(&ifa->addr_lst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 ifa->timer.data = (unsigned long) ifa;
803 ifa->scope = scope;
804 ifa->prefix_len = pfxlen;
805 ifa->flags = flags | IFA_F_TENTATIVE;
806 ifa->cstamp = ifa->tstamp = jiffies;
807
Keir Fraser57f5f542006-08-29 02:43:49 -0700808 ifa->rt = rt;
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 ifa->idev = idev;
811 in6_dev_hold(idev);
812 /* For caller */
813 in6_ifa_hold(ifa);
814
815 /* Add to big hash table */
Eric Dumazetddbe5032012-07-18 08:11:12 +0000816 hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
stephen hemminger5c578ae2010-03-17 20:31:11 +0000818 hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
stephen hemminger5c578ae2010-03-17 20:31:11 +0000819 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 write_lock(&idev->lock);
822 /* Add to inet6_dev unicast addr list. */
Brian Haleye55ffac2006-07-10 15:25:51 -0700823 ipv6_link_dev_addr(idev, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825#ifdef CONFIG_IPV6_PRIVACY
826 if (ifa->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +0000827 list_add(&ifa->tmp_list, &idev->tempaddr_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 in6_ifa_hold(ifa);
829 }
830#endif
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 in6_ifa_hold(ifa);
833 write_unlock(&idev->lock);
834out2:
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700835 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
YOSHIFUJI Hideakifd928332005-04-19 22:27:09 -0700837 if (likely(err == 0))
Cong Wangf88c91d2013-04-14 23:18:43 +0800838 inet6addr_notifier_call_chain(NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 else {
840 kfree(ifa);
841 ifa = ERR_PTR(err);
842 }
843
844 return ifa;
845out:
stephen hemminger5c578ae2010-03-17 20:31:11 +0000846 spin_unlock(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 goto out2;
848}
849
850/* This function wants to get referenced ifp and releases it before return */
851
852static void ipv6_del_addr(struct inet6_ifaddr *ifp)
853{
stephen hemminger502a2ff2010-03-17 20:31:13 +0000854 struct inet6_ifaddr *ifa, *ifn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 struct inet6_dev *idev = ifp->idev;
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700856 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 int deleted = 0, onlink = 0;
858 unsigned long expires = jiffies;
859
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700860 spin_lock_bh(&ifp->state_lock);
861 state = ifp->state;
Herbert Xue9d3e082010-05-18 15:36:06 -0700862 ifp->state = INET6_IFADDR_STATE_DEAD;
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700863 spin_unlock_bh(&ifp->state_lock);
864
865 if (state == INET6_IFADDR_STATE_DEAD)
866 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
stephen hemminger5c578ae2010-03-17 20:31:11 +0000868 spin_lock_bh(&addrconf_hash_lock);
869 hlist_del_init_rcu(&ifp->addr_lst);
stephen hemminger5c578ae2010-03-17 20:31:11 +0000870 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 write_lock_bh(&idev->lock);
873#ifdef CONFIG_IPV6_PRIVACY
874 if (ifp->flags&IFA_F_TEMPORARY) {
stephen hemminger372e6c82010-03-17 20:31:09 +0000875 list_del(&ifp->tmp_list);
876 if (ifp->ifpub) {
877 in6_ifa_put(ifp->ifpub);
878 ifp->ifpub = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
stephen hemminger372e6c82010-03-17 20:31:09 +0000880 __in6_ifa_put(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
882#endif
883
stephen hemminger502a2ff2010-03-17 20:31:13 +0000884 list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (ifa == ifp) {
stephen hemminger502a2ff2010-03-17 20:31:13 +0000886 list_del_init(&ifp->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 __in6_ifa_put(ifp);
stephen hemminger502a2ff2010-03-17 20:31:13 +0000888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (!(ifp->flags & IFA_F_PERMANENT) || onlink > 0)
890 break;
891 deleted = 1;
Kristian Slavov1d142802005-12-21 18:47:24 -0800892 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 } else if (ifp->flags & IFA_F_PERMANENT) {
894 if (ipv6_prefix_equal(&ifa->addr, &ifp->addr,
895 ifp->prefix_len)) {
896 if (ifa->flags & IFA_F_PERMANENT) {
897 onlink = 1;
898 if (deleted)
899 break;
900 } else {
901 unsigned long lifetime;
902
903 if (!onlink)
904 onlink = -1;
905
906 spin_lock(&ifa->lock);
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +0900907
908 lifetime = addrconf_timeout_fixup(ifa->valid_lft, HZ);
909 /*
910 * Note: Because this address is
911 * not permanent, lifetime <
912 * LONG_MAX / HZ here.
913 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (time_before(expires,
915 ifa->tstamp + lifetime * HZ))
916 expires = ifa->tstamp + lifetime * HZ;
917 spin_unlock(&ifa->lock);
918 }
919 }
920 }
921 }
922 write_unlock_bh(&idev->lock);
923
Andrey Vaginb2238562008-07-08 15:13:31 -0700924 addrconf_del_timer(ifp);
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 ipv6_ifa_notify(RTM_DELADDR, ifp);
927
Cong Wangf88c91d2013-04-14 23:18:43 +0800928 inet6addr_notifier_call_chain(NETDEV_DOWN, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 /*
931 * Purge or update corresponding prefix
932 *
933 * 1) we don't purge prefix here if address was not permanent.
934 * prefix is managed by its own lifetime.
935 * 2) if there're no addresses, delete prefix.
936 * 3) if there're still other permanent address(es),
937 * corresponding prefix is still permanent.
938 * 4) otherwise, update prefix lifetime to the
939 * longest valid lifetime among the corresponding
940 * addresses on the device.
941 * Note: subsequent RA will update lifetime.
942 *
943 * --yoshfuji
944 */
945 if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
946 struct in6_addr prefix;
947 struct rt6_info *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Nicolas Dichtel64c6d082012-09-26 00:04:55 +0000949 ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
Nicolas Dichtel64c6d082012-09-26 00:04:55 +0000950
Romain Kuntz21caa662013-01-09 21:06:03 +0000951 rt = addrconf_get_prefix_route(&prefix,
952 ifp->prefix_len,
953 ifp->idev->dev,
954 0, RTF_GATEWAY | RTF_DEFAULT);
955
956 if (rt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (onlink == 0) {
Thomas Grafe0a1ad732006-08-22 00:00:21 -0700958 ip6_del_rt(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 rt = NULL;
960 } else if (!(rt->rt6i_flags & RTF_EXPIRES)) {
Gao feng1716a962012-04-06 00:13:10 +0000961 rt6_set_expires(rt, expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
963 }
Amerigo Wang94e187c2012-10-29 00:13:19 +0000964 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966
Daniel Walterc3968a82011-04-13 21:10:57 +0000967 /* clean up prefsrc entries */
968 rt6_remove_prefsrc(ifp);
Herbert Xu4c5ff6a2010-05-18 15:54:18 -0700969out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 in6_ifa_put(ifp);
971}
972
973#ifdef CONFIG_IPV6_PRIVACY
974static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
975{
976 struct inet6_dev *idev = ifp->idev;
977 struct in6_addr addr, *tmpaddr;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +0000978 unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_tstamp, age;
Benoit Boissinoteac55bf2008-04-02 00:01:35 -0700979 unsigned long regen_advance;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 int tmp_plen;
981 int ret = 0;
982 int max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -0700983 u32 addr_flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +0000984 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 write_lock(&idev->lock);
987 if (ift) {
988 spin_lock_bh(&ift->lock);
989 memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
990 spin_unlock_bh(&ift->lock);
991 tmpaddr = &addr;
992 } else {
993 tmpaddr = NULL;
994 }
995retry:
996 in6_dev_hold(idev);
997 if (idev->cnf.use_tempaddr <= 0) {
998 write_unlock(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +0000999 pr_info("%s: use_tempaddr is disabled\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 in6_dev_put(idev);
1001 ret = -1;
1002 goto out;
1003 }
1004 spin_lock_bh(&ifp->lock);
1005 if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
1006 idev->cnf.use_tempaddr = -1; /*XXX*/
1007 spin_unlock_bh(&ifp->lock);
1008 write_unlock(&idev->lock);
Joe Perchesf3213832012-05-15 14:11:53 +00001009 pr_warn("%s: regeneration time exceeded - disabled temporary address support\n",
1010 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 in6_dev_put(idev);
1012 ret = -1;
1013 goto out;
1014 }
1015 in6_ifa_hold(ifp);
1016 memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001017 __ipv6_try_regen_rndid(idev, tmpaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 memcpy(&addr.s6_addr[8], idev->rndid, 8);
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001019 age = (now - ifp->tstamp) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 tmp_valid_lft = min_t(__u32,
1021 ifp->valid_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +00001022 idev->cnf.temp_valid_lft + age);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001023 tmp_prefered_lft = min_t(__u32,
1024 ifp->prefered_lft,
Glenn Wurster7a876b02010-09-27 07:10:10 +00001025 idev->cnf.temp_prefered_lft + age -
Ben Hutchings784e2712010-06-26 11:42:55 +00001026 idev->cnf.max_desync_factor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 tmp_plen = ifp->prefix_len;
1028 max_addresses = idev->cnf.max_addresses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 tmp_tstamp = ifp->tstamp;
1030 spin_unlock_bh(&ifp->lock);
1031
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001032 regen_advance = idev->cnf.regen_max_retry *
1033 idev->cnf.dad_transmits *
1034 idev->nd_parms->retrans_time / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 write_unlock(&idev->lock);
Neil Horman95c385b2007-04-25 17:08:10 -07001036
Benoit Boissinoteac55bf2008-04-02 00:01:35 -07001037 /* A temporary address is created only if this calculated Preferred
1038 * Lifetime is greater than REGEN_ADVANCE time units. In particular,
1039 * an implementation must not create a temporary address with a zero
1040 * Preferred Lifetime.
1041 */
1042 if (tmp_prefered_lft <= regen_advance) {
1043 in6_ifa_put(ifp);
1044 in6_dev_put(idev);
1045 ret = -1;
1046 goto out;
1047 }
1048
Neil Horman95c385b2007-04-25 17:08:10 -07001049 addr_flags = IFA_F_TEMPORARY;
1050 /* set in addrconf_prefix_rcv() */
1051 if (ifp->flags & IFA_F_OPTIMISTIC)
1052 addr_flags |= IFA_F_OPTIMISTIC;
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 ift = !max_addresses ||
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001055 ipv6_count_addresses(idev) < max_addresses ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 ipv6_add_addr(idev, &addr, tmp_plen,
Neil Horman95c385b2007-04-25 17:08:10 -07001057 ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK,
1058 addr_flags) : NULL;
YOSHIFUJI Hideaki / 吉藤英明3f0d2ba2013-01-22 06:32:54 +00001059 if (IS_ERR_OR_NULL(ift)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 in6_ifa_put(ifp);
1061 in6_dev_put(idev);
Joe Perchesf3213832012-05-15 14:11:53 +00001062 pr_info("%s: retry temporary address regeneration\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 tmpaddr = &addr;
1064 write_lock(&idev->lock);
1065 goto retry;
1066 }
1067
1068 spin_lock_bh(&ift->lock);
1069 ift->ifpub = ifp;
1070 ift->valid_lft = tmp_valid_lft;
1071 ift->prefered_lft = tmp_prefered_lft;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00001072 ift->cstamp = now;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 ift->tstamp = tmp_tstamp;
1074 spin_unlock_bh(&ift->lock);
1075
David S. Millercf22f9a2012-04-14 21:37:40 -04001076 addrconf_dad_start(ift);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 in6_ifa_put(ift);
1078 in6_dev_put(idev);
1079out:
1080 return ret;
1081}
1082#endif
1083
1084/*
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001085 * Choose an appropriate source address (RFC3484)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001087enum {
1088 IPV6_SADDR_RULE_INIT = 0,
1089 IPV6_SADDR_RULE_LOCAL,
1090 IPV6_SADDR_RULE_SCOPE,
1091 IPV6_SADDR_RULE_PREFERRED,
1092#ifdef CONFIG_IPV6_MIP6
1093 IPV6_SADDR_RULE_HOA,
1094#endif
1095 IPV6_SADDR_RULE_OIF,
1096 IPV6_SADDR_RULE_LABEL,
1097#ifdef CONFIG_IPV6_PRIVACY
1098 IPV6_SADDR_RULE_PRIVACY,
1099#endif
1100 IPV6_SADDR_RULE_ORCHID,
1101 IPV6_SADDR_RULE_PREFIX,
1102 IPV6_SADDR_RULE_MAX
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001103};
1104
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001105struct ipv6_saddr_score {
1106 int rule;
1107 int addr_type;
1108 struct inet6_ifaddr *ifa;
1109 DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
1110 int scopedist;
1111 int matchlen;
1112};
1113
1114struct ipv6_saddr_dst {
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001115 const struct in6_addr *addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001116 int ifindex;
1117 int scope;
1118 int label;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001119 unsigned int prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001120};
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001121
Dave Jonesb6f99a22007-03-22 12:27:49 -07001122static inline int ipv6_saddr_preferred(int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Ulrich Weber45bb0062010-02-25 23:28:58 +00001124 if (type & (IPV6_ADDR_MAPPED|IPV6_ADDR_COMPATv4|IPV6_ADDR_LOOPBACK))
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001125 return 1;
1126 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
Benjamin Thery3de23252008-05-28 14:51:24 +02001129static int ipv6_get_saddr_eval(struct net *net,
1130 struct ipv6_saddr_score *score,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001131 struct ipv6_saddr_dst *dst,
1132 int i)
1133{
1134 int ret;
1135
1136 if (i <= score->rule) {
1137 switch (i) {
1138 case IPV6_SADDR_RULE_SCOPE:
1139 ret = score->scopedist;
1140 break;
1141 case IPV6_SADDR_RULE_PREFIX:
1142 ret = score->matchlen;
1143 break;
1144 default:
1145 ret = !!test_bit(i, score->scorebits);
1146 }
1147 goto out;
1148 }
1149
1150 switch (i) {
1151 case IPV6_SADDR_RULE_INIT:
1152 /* Rule 0: remember if hiscore is not ready yet */
1153 ret = !!score->ifa;
1154 break;
1155 case IPV6_SADDR_RULE_LOCAL:
1156 /* Rule 1: Prefer same address */
1157 ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
1158 break;
1159 case IPV6_SADDR_RULE_SCOPE:
1160 /* Rule 2: Prefer appropriate scope
1161 *
1162 * ret
1163 * ^
1164 * -1 | d 15
1165 * ---+--+-+---> scope
1166 * |
1167 * | d is scope of the destination.
1168 * B-d | \
1169 * | \ <- smaller scope is better if
1170 * B-15 | \ if scope is enough for destinaion.
1171 * | ret = B - scope (-1 <= scope >= d <= 15).
1172 * d-C-1 | /
1173 * |/ <- greater is better
1174 * -C / if scope is not enough for destination.
1175 * /| ret = scope - C (-1 <= d < scope <= 15).
1176 *
1177 * d - C - 1 < B -15 (for all -1 <= d <= 15).
1178 * C > d + 14 - B >= 15 + 14 - B = 29 - B.
1179 * Assume B = 0 and we get C > 29.
1180 */
1181 ret = __ipv6_addr_src_scope(score->addr_type);
1182 if (ret >= dst->scope)
1183 ret = -ret;
1184 else
1185 ret -= 128; /* 30 is enough */
1186 score->scopedist = ret;
1187 break;
1188 case IPV6_SADDR_RULE_PREFERRED:
1189 /* Rule 3: Avoid deprecated and optimistic addresses */
1190 ret = ipv6_saddr_preferred(score->addr_type) ||
1191 !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
1192 break;
1193#ifdef CONFIG_IPV6_MIP6
1194 case IPV6_SADDR_RULE_HOA:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001195 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001196 /* Rule 4: Prefer home address */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001197 int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
1198 ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001199 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001200 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001201#endif
1202 case IPV6_SADDR_RULE_OIF:
1203 /* Rule 5: Prefer outgoing interface */
1204 ret = (!dst->ifindex ||
1205 dst->ifindex == score->ifa->idev->dev->ifindex);
1206 break;
1207 case IPV6_SADDR_RULE_LABEL:
1208 /* Rule 6: Prefer matching label */
Benjamin Thery3de23252008-05-28 14:51:24 +02001209 ret = ipv6_addr_label(net,
1210 &score->ifa->addr, score->addr_type,
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001211 score->ifa->idev->dev->ifindex) == dst->label;
1212 break;
1213#ifdef CONFIG_IPV6_PRIVACY
1214 case IPV6_SADDR_RULE_PRIVACY:
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001215 {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001216 /* Rule 7: Prefer public address
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001217 * Note: prefer temporary address if use_tempaddr >= 2
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001218 */
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001219 int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
1220 !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
1221 score->ifa->idev->cnf.use_tempaddr >= 2;
1222 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001223 break;
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001224 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001225#endif
1226 case IPV6_SADDR_RULE_ORCHID:
1227 /* Rule 8-: Prefer ORCHID vs ORCHID or
1228 * non-ORCHID vs non-ORCHID
1229 */
1230 ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
1231 ipv6_addr_orchid(dst->addr));
1232 break;
1233 case IPV6_SADDR_RULE_PREFIX:
1234 /* Rule 8: Use longest matching prefix */
YOSHIFUJI Hideaki / 吉藤英明91b4b042012-09-10 18:41:07 +00001235 ret = ipv6_addr_diff(&score->ifa->addr, dst->addr);
1236 if (ret > score->ifa->prefix_len)
1237 ret = score->ifa->prefix_len;
1238 score->matchlen = ret;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001239 break;
1240 default:
1241 ret = 0;
1242 }
1243
1244 if (ret)
1245 __set_bit(i, score->scorebits);
1246 score->rule = i;
1247out:
1248 return ret;
1249}
1250
Patrick McHardyb3f644f2012-08-26 19:14:14 +02001251int ipv6_dev_get_saddr(struct net *net, const struct net_device *dst_dev,
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001252 const struct in6_addr *daddr, unsigned int prefs,
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001253 struct in6_addr *saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001255 struct ipv6_saddr_score scores[2],
1256 *score = &scores[0], *hiscore = &scores[1];
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001257 struct ipv6_saddr_dst dst;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001258 struct net_device *dev;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001259 int dst_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001261 dst_type = __ipv6_addr_type(daddr);
1262 dst.addr = daddr;
1263 dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
1264 dst.scope = __ipv6_addr_src_scope(dst_type);
Benjamin Thery3de23252008-05-28 14:51:24 +02001265 dst.label = ipv6_addr_label(net, daddr, dst_type, dst.ifindex);
YOSHIFUJI Hideaki7cbca672008-03-25 09:37:42 +09001266 dst.prefs = prefs;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001267
1268 hiscore->rule = -1;
1269 hiscore->ifa = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001271 rcu_read_lock();
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001272
Eric Dumazetc6d14c82009-11-04 05:43:23 -08001273 for_each_netdev_rcu(net, dev) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001274 struct inet6_dev *idev;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001275
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001276 /* Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001277 * - multicast and link-local destination address,
1278 * the set of candidate source address MUST only
1279 * include addresses assigned to interfaces
1280 * belonging to the same link as the outgoing
1281 * interface.
1282 * (- For site-local destination addresses, the
1283 * set of candidate source addresses MUST only
1284 * include addresses assigned to interfaces
1285 * belonging to the same site as the outgoing
1286 * interface.)
1287 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001288 if (((dst_type & IPV6_ADDR_MULTICAST) ||
1289 dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
1290 dst.ifindex && dev->ifindex != dst.ifindex)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001291 continue;
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 idev = __in6_dev_get(dev);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001294 if (!idev)
1295 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001297 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001298 list_for_each_entry(score->ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001299 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001301 /*
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001302 * - Tentative Address (RFC2462 section 5.4)
1303 * - A tentative address is not considered
1304 * "assigned to an interface" in the traditional
Neil Horman95c385b2007-04-25 17:08:10 -07001305 * sense, unless it is also flagged as optimistic.
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001306 * - Candidate Source Address (section 4)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001307 * - In any case, anycast addresses, multicast
1308 * addresses, and the unspecified address MUST
1309 * NOT be included in a candidate set.
1310 */
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001311 if ((score->ifa->flags & IFA_F_TENTATIVE) &&
1312 (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
YOSHIFUJI Hideaki6b3ae802005-12-21 22:58:01 +09001313 continue;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001314
1315 score->addr_type = __ipv6_addr_type(&score->ifa->addr);
1316
1317 if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
1318 score->addr_type & IPV6_ADDR_MULTICAST)) {
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001319 LIMIT_NETDEBUG(KERN_DEBUG
Joe Perches3b6d821c2007-11-19 23:47:25 -08001320 "ADDRCONF: unspecified / multicast address "
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001321 "assigned as unicast address on %s",
1322 dev->name);
1323 continue;
1324 }
1325
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001326 score->rule = -1;
1327 bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001328
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001329 for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
1330 int minihiscore, miniscore;
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001331
Benjamin Thery3de23252008-05-28 14:51:24 +02001332 minihiscore = ipv6_get_saddr_eval(net, hiscore, &dst, i);
1333 miniscore = ipv6_get_saddr_eval(net, score, &dst, i);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001334
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001335 if (minihiscore > miniscore) {
1336 if (i == IPV6_SADDR_RULE_SCOPE &&
1337 score->scopedist > 0) {
1338 /*
1339 * special case:
1340 * each remaining entry
1341 * has too small (not enough)
1342 * scope, because ifa entries
1343 * are sorted by their scope
1344 * values.
1345 */
1346 goto try_nextdev;
1347 }
1348 break;
1349 } else if (minihiscore < miniscore) {
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001350 if (hiscore->ifa)
1351 in6_ifa_put(hiscore->ifa);
1352
1353 in6_ifa_hold(score->ifa);
1354
Ilpo Järvinena0bffff2009-03-21 13:36:17 -07001355 swap(hiscore, score);
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001356
1357 /* restore our iterator */
1358 score->ifa = hiscore->ifa;
1359
1360 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
1362 }
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001363 }
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001364try_nextdev:
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001365 read_unlock_bh(&idev->lock);
1366 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001367 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001369 if (!hiscore->ifa)
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001370 return -EADDRNOTAVAIL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001371
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001372 *saddr = hiscore->ifa->addr;
YOSHIFUJI Hideakia9b05722008-03-02 10:48:21 +09001373 in6_ifa_put(hiscore->ifa);
YOSHIFUJI Hideaki072047e2005-11-08 09:38:30 -08001374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
YOSHIFUJI Hideaki5e5f3f02008-03-03 21:44:34 +09001376EXPORT_SYMBOL(ipv6_dev_get_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Neil Horman95c385b2007-04-25 17:08:10 -07001378int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
1379 unsigned char banned_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
1381 struct inet6_dev *idev;
1382 int err = -EADDRNOTAVAIL;
1383
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001384 rcu_read_lock();
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001385 idev = __in6_dev_get(dev);
1386 if (idev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 struct inet6_ifaddr *ifp;
1388
1389 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001390 list_for_each_entry(ifp, &idev->addr_list, if_list) {
1391 if (ifp->scope == IFA_LINK &&
1392 !(ifp->flags & banned_flags)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001393 *addr = ifp->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 err = 0;
1395 break;
1396 }
1397 }
1398 read_unlock_bh(&idev->lock);
1399 }
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001400 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 return err;
1402}
1403
1404static int ipv6_count_addresses(struct inet6_dev *idev)
1405{
1406 int cnt = 0;
1407 struct inet6_ifaddr *ifp;
1408
1409 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001410 list_for_each_entry(ifp, &idev->addr_list, if_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 cnt++;
1412 read_unlock_bh(&idev->lock);
1413 return cnt;
1414}
1415
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001416int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001417 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001419 struct inet6_ifaddr *ifp;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001420 unsigned int hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
stephen hemminger5c578ae2010-03-17 20:31:11 +00001422 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001423 hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001424 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcanobfeade02008-01-10 22:43:18 -08001425 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (ipv6_addr_equal(&ifp->addr, addr) &&
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001427 !(ifp->flags&IFA_F_TENTATIVE) &&
1428 (dev == NULL || ifp->idev->dev == dev ||
1429 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1430 rcu_read_unlock_bh();
1431 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
1433 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001434
Stephen Hemmingereedf0422010-05-17 22:27:12 -07001435 rcu_read_unlock_bh();
1436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
YOSHIFUJI Hideaki71590392007-02-22 22:05:40 +09001438EXPORT_SYMBOL(ipv6_chk_addr);
1439
David S. Miller3e81c6d2010-03-20 16:18:00 -07001440static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1441 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
Eric Dumazetddbe5032012-07-18 08:11:12 +00001443 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00001444 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Sasha Levinb67bfe02013-02-27 17:06:00 -08001446 hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001447 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano06bfe652008-01-10 22:43:42 -08001448 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (ipv6_addr_equal(&ifp->addr, addr)) {
1450 if (dev == NULL || ifp->idev->dev == dev)
David S. Miller3e81c6d2010-03-20 16:18:00 -07001451 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453 }
David S. Miller3e81c6d2010-03-20 16:18:00 -07001454 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001457int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev)
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001458{
1459 struct inet6_dev *idev;
1460 struct inet6_ifaddr *ifa;
1461 int onlink;
1462
1463 onlink = 0;
1464 rcu_read_lock();
1465 idev = __in6_dev_get(dev);
1466 if (idev) {
1467 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001468 list_for_each_entry(ifa, &idev->addr_list, if_list) {
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001469 onlink = ipv6_prefix_equal(addr, &ifa->addr,
1470 ifa->prefix_len);
1471 if (onlink)
1472 break;
1473 }
1474 read_unlock_bh(&idev->lock);
1475 }
1476 rcu_read_unlock();
1477 return onlink;
1478}
YOSHIFUJI Hideaki52eeeb82008-03-15 22:54:23 -04001479EXPORT_SYMBOL(ipv6_chk_prefix);
1480
YOSHIFUJI Hideaki9acd9f32008-04-10 15:42:10 +09001481struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001482 struct net_device *dev, int strict)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
David S. Millerb79d1d52010-03-25 21:39:21 -07001484 struct inet6_ifaddr *ifp, *result = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00001485 unsigned int hash = inet6_addr_hash(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
stephen hemminger5c578ae2010-03-17 20:31:11 +00001487 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001488 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001489 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08001490 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (ipv6_addr_equal(&ifp->addr, addr)) {
1492 if (dev == NULL || ifp->idev->dev == dev ||
1493 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
David S. Millerb79d1d52010-03-25 21:39:21 -07001494 result = ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 in6_ifa_hold(ifp);
1496 break;
1497 }
1498 }
1499 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00001500 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
David S. Millerb79d1d52010-03-25 21:39:21 -07001502 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505/* Gets referenced address, destroys ifaddr */
1506
Brian Haleycc411d02009-09-09 14:41:32 +00001507static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (ifp->flags&IFA_F_PERMANENT) {
1510 spin_lock_bh(&ifp->lock);
1511 addrconf_del_timer(ifp);
1512 ifp->flags |= IFA_F_TENTATIVE;
Brian Haleycc411d02009-09-09 14:41:32 +00001513 if (dad_failed)
1514 ifp->flags |= IFA_F_DADFAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 spin_unlock_bh(&ifp->lock);
Herbert Xue2577a02010-03-13 12:23:29 -08001516 if (dad_failed)
1517 ipv6_ifa_notify(0, ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 in6_ifa_put(ifp);
1519#ifdef CONFIG_IPV6_PRIVACY
1520 } else if (ifp->flags&IFA_F_TEMPORARY) {
1521 struct inet6_ifaddr *ifpub;
1522 spin_lock_bh(&ifp->lock);
1523 ifpub = ifp->ifpub;
1524 if (ifpub) {
1525 in6_ifa_hold(ifpub);
1526 spin_unlock_bh(&ifp->lock);
1527 ipv6_create_tempaddr(ifpub, ifp);
1528 in6_ifa_put(ifpub);
1529 } else {
1530 spin_unlock_bh(&ifp->lock);
1531 }
1532 ipv6_del_addr(ifp);
1533#endif
1534 } else
1535 ipv6_del_addr(ifp);
1536}
1537
Herbert Xuf2344a12010-05-18 15:55:27 -07001538static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1539{
1540 int err = -ENOENT;
1541
1542 spin_lock(&ifp->state_lock);
1543 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1544 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1545 err = 0;
1546 }
1547 spin_unlock(&ifp->state_lock);
1548
1549 return err;
1550}
1551
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001552void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1553{
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001554 struct inet6_dev *idev = ifp->idev;
Brian Haley9bdd8d42009-03-18 18:22:48 -07001555
Ursula Braun853dc2e2010-10-24 23:06:43 +00001556 if (addrconf_dad_end(ifp)) {
1557 in6_ifa_put(ifp);
Herbert Xuf2344a12010-05-18 15:55:27 -07001558 return;
Ursula Braun853dc2e2010-10-24 23:06:43 +00001559 }
Herbert Xuf2344a12010-05-18 15:55:27 -07001560
Joe Perchese87cc472012-05-13 21:56:26 +00001561 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1562 ifp->idev->dev->name, &ifp->addr);
Brian Haley9bdd8d42009-03-18 18:22:48 -07001563
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001564 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
1565 struct in6_addr addr;
1566
1567 addr.s6_addr32[0] = htonl(0xfe800000);
1568 addr.s6_addr32[1] = 0;
1569
1570 if (!ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) &&
1571 ipv6_addr_equal(&ifp->addr, &addr)) {
1572 /* DAD failed for link-local based on MAC address */
1573 idev->cnf.disable_ipv6 = 1;
Brian Haley9bdd8d42009-03-18 18:22:48 -07001574
Joe Perchesf3213832012-05-15 14:11:53 +00001575 pr_info("%s: IPv6 being disabled!\n",
Brian Haley9bdd8d42009-03-18 18:22:48 -07001576 ifp->idev->dev->name);
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09001577 }
1578 }
1579
Brian Haleycc411d02009-09-09 14:41:32 +00001580 addrconf_dad_stop(ifp, 1);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09001581}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
1583/* Join to solicited addr multicast group. */
1584
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001585void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
1587 struct in6_addr maddr;
1588
1589 if (dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1590 return;
1591
1592 addrconf_addr_solict_mult(addr, &maddr);
1593 ipv6_dev_mc_inc(dev, &maddr);
1594}
1595
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001596void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
1598 struct in6_addr maddr;
1599
1600 if (idev->dev->flags&(IFF_LOOPBACK|IFF_NOARP))
1601 return;
1602
1603 addrconf_addr_solict_mult(addr, &maddr);
1604 __ipv6_dev_mc_dec(idev, &maddr);
1605}
1606
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001607static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
1609 struct in6_addr addr;
Bjørn Mork2bda8a02011-07-05 23:04:13 +00001610 if (ifp->prefix_len == 127) /* RFC 6164 */
1611 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1613 if (ipv6_addr_any(&addr))
1614 return;
1615 ipv6_dev_ac_inc(ifp->idev->dev, &addr);
1616}
1617
Arnaldo Carvalho de Melo20380732005-08-16 02:18:02 -03001618static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
1620 struct in6_addr addr;
YOSHIFUJI Hideaki32019e62011-07-24 11:44:34 +00001621 if (ifp->prefix_len == 127) /* RFC 6164 */
1622 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1624 if (ipv6_addr_any(&addr))
1625 return;
1626 __ipv6_dev_ac_dec(ifp->idev, &addr);
1627}
1628
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001629static int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
1630{
1631 if (dev->addr_len != ETH_ALEN)
1632 return -1;
1633 memcpy(eui, dev->dev_addr, 3);
1634 memcpy(eui + 5, dev->dev_addr + 3, 3);
1635
1636 /*
1637 * The zSeries OSA network cards can be shared among various
1638 * OS instances, but the OSA cards have only one MAC address.
1639 * This leads to duplicate address conflicts in conjunction
1640 * with IPv6 if more than one instance uses the same card.
1641 *
1642 * The driver for these cards can deliver a unique 16-bit
1643 * identifier for each instance sharing the same card. It is
1644 * placed instead of 0xFFFE in the interface identifier. The
1645 * "u" bit of the interface identifier is not inverted in this
1646 * case. Hence the resulting interface identifier has local
1647 * scope according to RFC2373.
1648 */
1649 if (dev->dev_id) {
1650 eui[3] = (dev->dev_id >> 8) & 0xFF;
1651 eui[4] = dev->dev_id & 0xFF;
1652 } else {
1653 eui[3] = 0xFF;
1654 eui[4] = 0xFE;
1655 eui[0] ^= 2;
1656 }
1657 return 0;
1658}
1659
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00001660static int addrconf_ifid_eui64(u8 *eui, struct net_device *dev)
1661{
1662 if (dev->addr_len != IEEE802154_ADDR_LEN)
1663 return -1;
1664 memcpy(eui, dev->dev_addr, 8);
YOSHIFUJI Hideaki / 吉藤英明5e98a362013-01-28 10:44:29 +00001665 eui[0] ^= 2;
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00001666 return 0;
1667}
1668
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001669static int addrconf_ifid_arcnet(u8 *eui, struct net_device *dev)
1670{
1671 /* XXX: inherit EUI-64 from other interface -- yoshfuji */
1672 if (dev->addr_len != ARCNET_ALEN)
1673 return -1;
1674 memset(eui, 0, 7);
Eldad Zack8e5e8f32012-04-01 07:49:08 +00001675 eui[7] = *(u8 *)dev->dev_addr;
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001676 return 0;
1677}
1678
1679static int addrconf_ifid_infiniband(u8 *eui, struct net_device *dev)
1680{
1681 if (dev->addr_len != INFINIBAND_ALEN)
1682 return -1;
1683 memcpy(eui, dev->dev_addr + 12, 8);
1684 eui[0] |= 2;
1685 return 0;
1686}
1687
stephen hemmingerc61393e2010-10-04 20:17:53 +00001688static int __ipv6_isatap_ifid(u8 *eui, __be32 addr)
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001689{
Sascha Hlusiak9af28512009-05-19 12:56:51 +00001690 if (addr == 0)
1691 return -1;
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001692 eui[0] = (ipv4_is_zeronet(addr) || ipv4_is_private_10(addr) ||
1693 ipv4_is_loopback(addr) || ipv4_is_linklocal_169(addr) ||
1694 ipv4_is_private_172(addr) || ipv4_is_test_192(addr) ||
1695 ipv4_is_anycast_6to4(addr) || ipv4_is_private_192(addr) ||
1696 ipv4_is_test_198(addr) || ipv4_is_multicast(addr) ||
1697 ipv4_is_lbcast(addr)) ? 0x00 : 0x02;
1698 eui[1] = 0;
1699 eui[2] = 0x5E;
1700 eui[3] = 0xFE;
1701 memcpy(eui + 4, &addr, 4);
1702 return 0;
1703}
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001704
1705static int addrconf_ifid_sit(u8 *eui, struct net_device *dev)
1706{
1707 if (dev->priv_flags & IFF_ISATAP)
1708 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1709 return -1;
1710}
1711
stephen hemmingeraee80b52011-06-08 10:44:30 +00001712static int addrconf_ifid_gre(u8 *eui, struct net_device *dev)
1713{
1714 return __ipv6_isatap_ifid(eui, *(__be32 *)dev->dev_addr);
1715}
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
1718{
1719 switch (dev->type) {
1720 case ARPHRD_ETHER:
1721 case ARPHRD_FDDI:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001722 return addrconf_ifid_eui48(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 case ARPHRD_ARCNET:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001724 return addrconf_ifid_arcnet(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 case ARPHRD_INFINIBAND:
YOSHIFUJI Hideaki073a8e02006-03-20 16:54:49 -08001726 return addrconf_ifid_infiniband(eui, dev);
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001727 case ARPHRD_SIT:
YOSHIFUJI Hideakidfd982b2008-04-10 15:42:09 +09001728 return addrconf_ifid_sit(eui, dev);
stephen hemmingeraee80b52011-06-08 10:44:30 +00001729 case ARPHRD_IPGRE:
1730 return addrconf_ifid_gre(eui, dev);
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00001731 case ARPHRD_IEEE802154:
1732 return addrconf_ifid_eui64(eui, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 }
1734 return -1;
1735}
1736
1737static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1738{
1739 int err = -1;
1740 struct inet6_ifaddr *ifp;
1741
1742 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00001743 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (ifp->scope == IFA_LINK && !(ifp->flags&IFA_F_TENTATIVE)) {
1745 memcpy(eui, ifp->addr.s6_addr+8, 8);
1746 err = 0;
1747 break;
1748 }
1749 }
1750 read_unlock_bh(&idev->lock);
1751 return err;
1752}
1753
1754#ifdef CONFIG_IPV6_PRIVACY
1755/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001756static void __ipv6_regen_rndid(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758regen:
YOSHIFUJI Hideaki955189e2006-03-20 16:54:09 -08001759 get_random_bytes(idev->rndid, sizeof(idev->rndid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 idev->rndid[0] &= ~0x02;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 /*
1763 * <draft-ietf-ipngwg-temp-addresses-v2-00.txt>:
1764 * check if generated address is not inappropriate
1765 *
1766 * - Reserved subnet anycast (RFC 2526)
1767 * 11111101 11....11 1xxxxxxx
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11001768 * - ISATAP (RFC4214) 6.1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 * 00-00-5E-FE-xx-xx-xx-xx
1770 * - value 0
1771 * - XXX: already assigned to an address on the device
1772 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001773 if (idev->rndid[0] == 0xfd &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 (idev->rndid[1]&idev->rndid[2]&idev->rndid[3]&idev->rndid[4]&idev->rndid[5]&idev->rndid[6]) == 0xff &&
1775 (idev->rndid[7]&0x80))
1776 goto regen;
1777 if ((idev->rndid[0]|idev->rndid[1]) == 0) {
1778 if (idev->rndid[2] == 0x5e && idev->rndid[3] == 0xfe)
1779 goto regen;
1780 if ((idev->rndid[2]|idev->rndid[3]|idev->rndid[4]|idev->rndid[5]|idev->rndid[6]|idev->rndid[7]) == 0x00)
1781 goto regen;
1782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
1785static void ipv6_regen_rndid(unsigned long data)
1786{
1787 struct inet6_dev *idev = (struct inet6_dev *) data;
1788 unsigned long expires;
1789
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001790 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 write_lock_bh(&idev->lock);
1792
1793 if (idev->dead)
1794 goto out;
1795
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001796 __ipv6_regen_rndid(idev);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 expires = jiffies +
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001799 idev->cnf.temp_prefered_lft * HZ -
Ben Hutchings784e2712010-06-26 11:42:55 +00001800 idev->cnf.regen_max_retry * idev->cnf.dad_transmits * idev->nd_parms->retrans_time -
1801 idev->cnf.max_desync_factor * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (time_before(expires, jiffies)) {
Joe Perchesf3213832012-05-15 14:11:53 +00001803 pr_warn("%s: too short regeneration interval; timer disabled for %s\n",
1804 __func__, idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 goto out;
1806 }
1807
1808 if (!mod_timer(&idev->regen_timer, expires))
1809 in6_dev_hold(idev);
1810
1811out:
1812 write_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07001813 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 in6_dev_put(idev);
1815}
1816
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001817static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr)
Eldad Zack8e5e8f32012-04-01 07:49:08 +00001818{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
Sorin Dumitrueb7e0572012-08-30 02:01:45 +00001820 __ipv6_regen_rndid(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
1822#endif
1823
1824/*
1825 * Add prefix route.
1826 */
1827
1828static void
1829addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
Jamal Hadi Salime431b8c2005-06-18 22:55:31 -07001830 unsigned long expires, u32 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
Thomas Graf86872cb2006-08-22 00:01:08 -07001832 struct fib6_config cfg = {
1833 .fc_table = RT6_TABLE_PREFIX,
1834 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1835 .fc_ifindex = dev->ifindex,
1836 .fc_expires = expires,
1837 .fc_dst_len = plen,
1838 .fc_flags = RTF_UP | flags,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001839 .fc_nlinfo.nl_net = dev_net(dev),
Stephen Hemmingerf410a1f2008-08-23 05:16:46 -07001840 .fc_protocol = RTPROT_KERNEL,
Thomas Graf86872cb2006-08-22 00:01:08 -07001841 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001843 cfg.fc_dst = *pfx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 /* Prevent useless cloning on PtP SIT.
1846 This thing is done here expecting that the whole
1847 class of non-broadcast devices need not cloning.
1848 */
Amerigo Wang07a93622012-10-29 16:23:10 +00001849#if IS_ENABLED(CONFIG_IPV6_SIT)
Thomas Graf86872cb2006-08-22 00:01:08 -07001850 if (dev->type == ARPHRD_SIT && (dev->flags & IFF_POINTOPOINT))
1851 cfg.fc_flags |= RTF_NONEXTHOP;
Joerg Roedel0be669b2006-10-10 14:49:53 -07001852#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Thomas Graf86872cb2006-08-22 00:01:08 -07001854 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
1856
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001857
1858static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
1859 int plen,
1860 const struct net_device *dev,
1861 u32 flags, u32 noflags)
1862{
1863 struct fib6_node *fn;
1864 struct rt6_info *rt = NULL;
1865 struct fib6_table *table;
1866
1867 table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
1868 if (table == NULL)
1869 return NULL;
1870
Li RongQing5744dd92012-09-11 21:59:01 +00001871 read_lock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001872 fn = fib6_locate(&table->tb6_root, pfx, plen, NULL, 0);
1873 if (!fn)
1874 goto out;
1875 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
David S. Millerd1918542011-12-28 20:19:20 -05001876 if (rt->dst.dev->ifindex != dev->ifindex)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001877 continue;
1878 if ((rt->rt6i_flags & flags) != flags)
1879 continue;
Romain Kuntz85da53b2013-01-09 15:02:26 +01001880 if ((rt->rt6i_flags & noflags) != 0)
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001881 continue;
1882 dst_hold(&rt->dst);
1883 break;
1884 }
1885out:
Li RongQing5744dd92012-09-11 21:59:01 +00001886 read_unlock_bh(&table->tb6_lock);
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00001887 return rt;
1888}
1889
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891/* Create "default" multicast route to the interface */
1892
1893static void addrconf_add_mroute(struct net_device *dev)
1894{
Thomas Graf86872cb2006-08-22 00:01:08 -07001895 struct fib6_config cfg = {
1896 .fc_table = RT6_TABLE_LOCAL,
1897 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1898 .fc_ifindex = dev->ifindex,
1899 .fc_dst_len = 8,
1900 .fc_flags = RTF_UP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001901 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001902 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Thomas Graf86872cb2006-08-22 00:01:08 -07001904 ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
1905
1906 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
1908
Amerigo Wang07a93622012-10-29 16:23:10 +00001909#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910static void sit_route_add(struct net_device *dev)
1911{
Thomas Graf86872cb2006-08-22 00:01:08 -07001912 struct fib6_config cfg = {
1913 .fc_table = RT6_TABLE_MAIN,
1914 .fc_metric = IP6_RT_PRIO_ADDRCONF,
1915 .fc_ifindex = dev->ifindex,
1916 .fc_dst_len = 96,
1917 .fc_flags = RTF_UP | RTF_NONEXTHOP,
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001918 .fc_nlinfo.nl_net = dev_net(dev),
Thomas Graf86872cb2006-08-22 00:01:08 -07001919 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 /* prefix length - 96 bits "::d.d.d.d" */
Thomas Graf86872cb2006-08-22 00:01:08 -07001922 ip6_route_add(&cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
Joerg Roedel0be669b2006-10-10 14:49:53 -07001924#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
1927{
1928 struct inet6_dev *idev;
1929
1930 ASSERT_RTNL();
1931
Stephen Hemmingere21e8462010-03-20 16:09:01 -07001932 idev = ipv6_find_idev(dev);
1933 if (!idev)
Brian Haley64e724f2010-07-20 10:34:30 +00001934 return ERR_PTR(-ENOBUFS);
1935
1936 if (idev->cnf.disable_ipv6)
1937 return ERR_PTR(-EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 /* Add default multicast route */
Li Wei4af04ab2011-12-06 21:23:45 +00001940 if (!(dev->flags & IFF_LOOPBACK))
1941 addrconf_add_mroute(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 return idev;
1944}
1945
Neil Hormane6bff992012-01-04 10:49:15 +00001946void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
1948 struct prefix_info *pinfo;
1949 __u32 valid_lft;
1950 __u32 prefered_lft;
1951 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 struct inet6_dev *in6_dev;
Brian Haley56d417b2009-06-01 03:07:33 -07001953 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 pinfo = (struct prefix_info *) opt;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (len < sizeof(struct prefix_info)) {
1958 ADBG(("addrconf: prefix option too short\n"));
1959 return;
1960 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 /*
1963 * Validation checks ([ADDRCONF], page 19)
1964 */
1965
1966 addr_type = ipv6_addr_type(&pinfo->prefix);
1967
1968 if (addr_type & (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL))
1969 return;
1970
1971 valid_lft = ntohl(pinfo->valid);
1972 prefered_lft = ntohl(pinfo->prefered);
1973
1974 if (prefered_lft > valid_lft) {
Joe Perchese87cc472012-05-13 21:56:26 +00001975 net_warn_ratelimited("addrconf: prefix option has invalid lifetime\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 return;
1977 }
1978
1979 in6_dev = in6_dev_get(dev);
1980
1981 if (in6_dev == NULL) {
Joe Perchese87cc472012-05-13 21:56:26 +00001982 net_dbg_ratelimited("addrconf: device %s not configured\n",
1983 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 return;
1985 }
1986
1987 /*
1988 * Two things going on here:
1989 * 1) Add routes for on-link prefixes
1990 * 2) Configure prefixes with the auto flag set
1991 */
1992
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09001993 if (pinfo->onlink) {
1994 struct rt6_info *rt;
1995 unsigned long rt_expires;
1996
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07001997 /* Avoid arithmetic overflow. Really, we could
1998 * save rt_expires in seconds, likely valid_lft,
1999 * but it would require division in fib gc, that it
2000 * not good.
2001 */
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002002 if (HZ > USER_HZ)
2003 rt_expires = addrconf_timeout_fixup(valid_lft, HZ);
2004 else
2005 rt_expires = addrconf_timeout_fixup(valid_lft, USER_HZ);
YOSHIFUJI Hideaki3dd4bc62005-12-19 14:02:45 -08002006
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002007 if (addrconf_finite_timeout(rt_expires))
2008 rt_expires *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002010 rt = addrconf_get_prefix_route(&pinfo->prefix,
2011 pinfo->prefix_len,
2012 dev,
2013 RTF_ADDRCONF | RTF_PREFIX_RT,
2014 RTF_GATEWAY | RTF_DEFAULT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Andreas Hofmeister14ef37b2011-10-26 03:24:29 +00002016 if (rt) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002017 /* Autoconf prefix route */
2018 if (valid_lft == 0) {
2019 ip6_del_rt(rt);
2020 rt = NULL;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002021 } else if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002022 /* not infinity */
Gao feng1716a962012-04-06 00:13:10 +00002023 rt6_set_expires(rt, jiffies + rt_expires);
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002024 } else {
Gao feng1716a962012-04-06 00:13:10 +00002025 rt6_clean_expires(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
2027 } else if (valid_lft) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002028 clock_t expires = 0;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002029 int flags = RTF_ADDRCONF | RTF_PREFIX_RT;
2030 if (addrconf_finite_timeout(rt_expires)) {
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002031 /* not infinity */
2032 flags |= RTF_EXPIRES;
2033 expires = jiffies_to_clock_t(rt_expires);
2034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 addrconf_prefix_route(&pinfo->prefix, pinfo->prefix_len,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002036 dev, expires, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 }
Amerigo Wang94e187c2012-10-29 00:13:19 +00002038 ip6_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
2040
2041 /* Try to figure out our local address for this prefix */
2042
2043 if (pinfo->autoconf && in6_dev->cnf.autoconf) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002044 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 struct in6_addr addr;
2046 int create = 0, update_lft = 0;
2047
2048 if (pinfo->prefix_len == 64) {
2049 memcpy(&addr, &pinfo->prefix, 8);
2050 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2051 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2052 in6_dev_put(in6_dev);
2053 return;
2054 }
2055 goto ok;
2056 }
Joe Perchese87cc472012-05-13 21:56:26 +00002057 net_dbg_ratelimited("IPv6 addrconf: prefix with wrong length %d\n",
2058 pinfo->prefix_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 in6_dev_put(in6_dev);
2060 return;
2061
2062ok:
2063
YOSHIFUJI Hideaki53b79972008-07-19 22:35:03 -07002064 ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066 if (ifp == NULL && valid_lft) {
2067 int max_addresses = in6_dev->cnf.max_addresses;
Neil Horman95c385b2007-04-25 17:08:10 -07002068 u32 addr_flags = 0;
2069
2070#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2071 if (in6_dev->cnf.optimistic_dad &&
Neil Hormane6bff992012-01-04 10:49:15 +00002072 !net->ipv6.devconf_all->forwarding && sllao)
Neil Horman95c385b2007-04-25 17:08:10 -07002073 addr_flags = IFA_F_OPTIMISTIC;
2074#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 /* Do not allow to create too much of autoconfigured
2077 * addresses; this would be too easy way to crash kernel.
2078 */
2079 if (!max_addresses ||
2080 ipv6_count_addresses(in6_dev) < max_addresses)
2081 ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
Neil Horman95c385b2007-04-25 17:08:10 -07002082 addr_type&IPV6_ADDR_SCOPE_MASK,
2083 addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
YOSHIFUJI Hideaki / 吉藤英明3f0d2ba2013-01-22 06:32:54 +00002085 if (IS_ERR_OR_NULL(ifp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 in6_dev_put(in6_dev);
2087 return;
2088 }
2089
2090 update_lft = create = 1;
2091 ifp->cstamp = jiffies;
David S. Millercf22f9a2012-04-14 21:37:40 -04002092 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
2094
2095 if (ifp) {
2096 int flags;
2097 unsigned long now;
2098#ifdef CONFIG_IPV6_PRIVACY
2099 struct inet6_ifaddr *ift;
2100#endif
2101 u32 stored_lft;
2102
2103 /* update lifetime (RFC2462 5.5.3 e) */
2104 spin_lock(&ifp->lock);
2105 now = jiffies;
2106 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2107 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
2108 else
2109 stored_lft = 0;
2110 if (!update_lft && stored_lft) {
2111 if (valid_lft > MIN_VALID_LIFETIME ||
2112 valid_lft > stored_lft)
2113 update_lft = 1;
2114 else if (stored_lft <= MIN_VALID_LIFETIME) {
2115 /* valid_lft <= stored_lft is always true */
Brian Haleya1ed0522009-07-02 07:10:52 +00002116 /*
2117 * RFC 4862 Section 5.5.3e:
2118 * "Note that the preferred lifetime of
2119 * the corresponding address is always
2120 * reset to the Preferred Lifetime in
2121 * the received Prefix Information
2122 * option, regardless of whether the
2123 * valid lifetime is also reset or
2124 * ignored."
2125 *
2126 * So if the preferred lifetime in
2127 * this advertisement is different
2128 * than what we have stored, but the
2129 * valid lifetime is invalid, just
2130 * reset prefered_lft.
2131 *
2132 * We must set the valid lifetime
2133 * to the stored lifetime since we'll
2134 * be updating the timestamp below,
2135 * else we'll set it back to the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002136 * minimum.
Brian Haleya1ed0522009-07-02 07:10:52 +00002137 */
2138 if (prefered_lft != ifp->prefered_lft) {
2139 valid_lft = stored_lft;
2140 update_lft = 1;
2141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 } else {
2143 valid_lft = MIN_VALID_LIFETIME;
2144 if (valid_lft < prefered_lft)
2145 prefered_lft = valid_lft;
2146 update_lft = 1;
2147 }
2148 }
2149
2150 if (update_lft) {
2151 ifp->valid_lft = valid_lft;
2152 ifp->prefered_lft = prefered_lft;
2153 ifp->tstamp = now;
2154 flags = ifp->flags;
2155 ifp->flags &= ~IFA_F_DEPRECATED;
2156 spin_unlock(&ifp->lock);
2157
2158 if (!(flags&IFA_F_TENTATIVE))
2159 ipv6_ifa_notify(0, ifp);
2160 } else
2161 spin_unlock(&ifp->lock);
2162
2163#ifdef CONFIG_IPV6_PRIVACY
2164 read_lock_bh(&in6_dev->lock);
2165 /* update all temporary addresses in the list */
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002166 list_for_each_entry(ift, &in6_dev->tempaddr_list,
2167 tmp_list) {
2168 int age, max_valid, max_prefered;
2169
Benoit Boissinotc6fbfac2008-04-02 00:00:58 -07002170 if (ifp != ift->ifpub)
2171 continue;
2172
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002173 /*
2174 * RFC 4941 section 3.3:
2175 * If a received option will extend the lifetime
2176 * of a public address, the lifetimes of
2177 * temporary addresses should be extended,
2178 * subject to the overall constraint that no
2179 * temporary addresses should ever remain
2180 * "valid" or "preferred" for a time longer than
2181 * (TEMP_VALID_LIFETIME) or
2182 * (TEMP_PREFERRED_LIFETIME - DESYNC_FACTOR),
2183 * respectively.
2184 */
2185 age = (now - ift->cstamp) / HZ;
2186 max_valid = in6_dev->cnf.temp_valid_lft - age;
2187 if (max_valid < 0)
2188 max_valid = 0;
2189
2190 max_prefered = in6_dev->cnf.temp_prefered_lft -
2191 in6_dev->cnf.max_desync_factor -
2192 age;
2193 if (max_prefered < 0)
2194 max_prefered = 0;
2195
2196 if (valid_lft > max_valid)
2197 valid_lft = max_valid;
2198
2199 if (prefered_lft > max_prefered)
2200 prefered_lft = max_prefered;
2201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 spin_lock(&ift->lock);
2203 flags = ift->flags;
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002204 ift->valid_lft = valid_lft;
2205 ift->prefered_lft = prefered_lft;
2206 ift->tstamp = now;
2207 if (prefered_lft > 0)
2208 ift->flags &= ~IFA_F_DEPRECATED;
2209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 spin_unlock(&ift->lock);
2211 if (!(flags&IFA_F_TENTATIVE))
2212 ipv6_ifa_notify(0, ift);
2213 }
2214
Glenn Wursteraed65502010-09-27 07:04:30 +00002215 if ((create || list_empty(&in6_dev->tempaddr_list)) && in6_dev->cnf.use_tempaddr > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 /*
Lorenzo Colitti76f793e2011-07-26 13:50:49 +00002217 * When a new public address is created as
2218 * described in [ADDRCONF], also create a new
2219 * temporary address. Also create a temporary
2220 * address if it's enabled but no temporary
2221 * address currently exists.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002223 read_unlock_bh(&in6_dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 ipv6_create_tempaddr(ifp, NULL);
2225 } else {
2226 read_unlock_bh(&in6_dev->lock);
2227 }
2228#endif
2229 in6_ifa_put(ifp);
2230 addrconf_verify(0);
2231 }
2232 }
2233 inet6_prefix_notify(RTM_NEWPREFIX, in6_dev, pinfo);
2234 in6_dev_put(in6_dev);
2235}
2236
2237/*
2238 * Set destination address.
2239 * Special case for SIT interfaces where we create a new "virtual"
2240 * device.
2241 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002242int addrconf_set_dstaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
2244 struct in6_ifreq ireq;
2245 struct net_device *dev;
2246 int err = -EINVAL;
2247
2248 rtnl_lock();
2249
2250 err = -EFAULT;
2251 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2252 goto err_exit;
2253
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002254 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 err = -ENODEV;
2257 if (dev == NULL)
2258 goto err_exit;
2259
Amerigo Wang07a93622012-10-29 16:23:10 +00002260#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (dev->type == ARPHRD_SIT) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002262 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 struct ip_tunnel_parm p;
2265
2266 err = -EADDRNOTAVAIL;
2267 if (!(ipv6_addr_type(&ireq.ifr6_addr) & IPV6_ADDR_COMPATv4))
2268 goto err_exit;
2269
2270 memset(&p, 0, sizeof(p));
2271 p.iph.daddr = ireq.ifr6_addr.s6_addr32[3];
2272 p.iph.saddr = 0;
2273 p.iph.version = 4;
2274 p.iph.ihl = 5;
2275 p.iph.protocol = IPPROTO_IPV6;
2276 p.iph.ttl = 64;
Stephen Hemmingerd20b3102008-01-21 00:48:43 -08002277 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -08002279 if (ops->ndo_do_ioctl) {
2280 mm_segment_t oldfs = get_fs();
2281
2282 set_fs(KERNEL_DS);
2283 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
2284 set_fs(oldfs);
2285 } else
2286 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 if (err == 0) {
2289 err = -ENOBUFS;
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002290 dev = __dev_get_by_name(net, p.name);
2291 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 goto err_exit;
2293 err = dev_open(dev);
2294 }
2295 }
Joerg Roedel0be669b2006-10-10 14:49:53 -07002296#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298err_exit:
2299 rtnl_unlock();
2300 return err;
2301}
2302
2303/*
2304 * Manual configuration of address on an interface
2305 */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002306static int inet6_addr_add(struct net *net, int ifindex, const struct in6_addr *pfx,
Thomas Graf24ef0da2008-05-28 16:54:22 +02002307 unsigned int plen, __u8 ifa_flags, __u32 prefered_lft,
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002308 __u32 valid_lft)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
2310 struct inet6_ifaddr *ifp;
2311 struct inet6_dev *idev;
2312 struct net_device *dev;
2313 int scope;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002314 u32 flags;
2315 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002316 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 ASSERT_RTNL();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002319
Thomas Graf24ef0da2008-05-28 16:54:22 +02002320 if (plen > 128)
2321 return -EINVAL;
2322
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002323 /* check the lifetime */
2324 if (!valid_lft || prefered_lft > valid_lft)
2325 return -EINVAL;
2326
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002327 dev = __dev_get_by_index(net, ifindex);
2328 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 return -ENODEV;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002330
Brian Haley64e724f2010-07-20 10:34:30 +00002331 idev = addrconf_add_dev(dev);
2332 if (IS_ERR(idev))
2333 return PTR_ERR(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335 scope = ipv6_addr_scope(pfx);
2336
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002337 timeout = addrconf_timeout_fixup(valid_lft, HZ);
2338 if (addrconf_finite_timeout(timeout)) {
2339 expires = jiffies_to_clock_t(timeout * HZ);
2340 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002341 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002342 } else {
2343 expires = 0;
2344 flags = 0;
2345 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002346 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002347
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09002348 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
2349 if (addrconf_finite_timeout(timeout)) {
2350 if (timeout == 0)
2351 ifa_flags |= IFA_F_DEPRECATED;
2352 prefered_lft = timeout;
2353 }
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002354
2355 ifp = ipv6_add_addr(idev, pfx, plen, scope, ifa_flags);
2356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 if (!IS_ERR(ifp)) {
Herbert Xu06aebfb2006-08-09 16:52:04 -07002358 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002359 ifp->valid_lft = valid_lft;
2360 ifp->prefered_lft = prefered_lft;
2361 ifp->tstamp = jiffies;
Herbert Xu06aebfb2006-08-09 16:52:04 -07002362 spin_unlock_bh(&ifp->lock);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002363
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002364 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, dev,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07002365 expires, flags);
Neil Horman95c385b2007-04-25 17:08:10 -07002366 /*
2367 * Note that section 3.1 of RFC 4429 indicates
2368 * that the Optimistic flag should not be set for
2369 * manually configured addresses
2370 */
David S. Millercf22f9a2012-04-14 21:37:40 -04002371 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 in6_ifa_put(ifp);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09002373 addrconf_verify(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 return 0;
2375 }
2376
2377 return PTR_ERR(ifp);
2378}
2379
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002380static int inet6_addr_del(struct net *net, int ifindex, const struct in6_addr *pfx,
Thomas Graf24ef0da2008-05-28 16:54:22 +02002381 unsigned int plen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
2383 struct inet6_ifaddr *ifp;
2384 struct inet6_dev *idev;
2385 struct net_device *dev;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002386
Thomas Graf24ef0da2008-05-28 16:54:22 +02002387 if (plen > 128)
2388 return -EINVAL;
2389
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002390 dev = __dev_get_by_index(net, ifindex);
2391 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 return -ENODEV;
2393
2394 if ((idev = __in6_dev_get(dev)) == NULL)
2395 return -ENXIO;
2396
2397 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002398 list_for_each_entry(ifp, &idev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 if (ifp->prefix_len == plen &&
2400 ipv6_addr_equal(pfx, &ifp->addr)) {
2401 in6_ifa_hold(ifp);
2402 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002403
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 ipv6_del_addr(ifp);
2405
2406 /* If the last address is deleted administratively,
2407 disable IPv6 on this interface.
2408 */
stephen hemminger502a2ff2010-03-17 20:31:13 +00002409 if (list_empty(&idev->addr_list))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 addrconf_ifdown(idev->dev, 1);
2411 return 0;
2412 }
2413 }
2414 read_unlock_bh(&idev->lock);
2415 return -EADDRNOTAVAIL;
2416}
2417
2418
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002419int addrconf_add_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420{
2421 struct in6_ifreq ireq;
2422 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002423
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002424 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 return -EPERM;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2428 return -EFAULT;
2429
2430 rtnl_lock();
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002431 err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2432 ireq.ifr6_prefixlen, IFA_F_PERMANENT,
2433 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 rtnl_unlock();
2435 return err;
2436}
2437
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002438int addrconf_del_ifaddr(struct net *net, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
2440 struct in6_ifreq ireq;
2441 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002442
Eric W. Biedermanaf31f412012-11-16 03:03:06 +00002443 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 return -EPERM;
2445
2446 if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
2447 return -EFAULT;
2448
2449 rtnl_lock();
Daniel Lezcanoaf284932008-03-05 10:46:57 -08002450 err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
2451 ireq.ifr6_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 rtnl_unlock();
2453 return err;
2454}
2455
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002456static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2457 int plen, int scope)
2458{
2459 struct inet6_ifaddr *ifp;
2460
2461 ifp = ipv6_add_addr(idev, addr, plen, scope, IFA_F_PERMANENT);
2462 if (!IS_ERR(ifp)) {
2463 spin_lock_bh(&ifp->lock);
2464 ifp->flags &= ~IFA_F_TENTATIVE;
2465 spin_unlock_bh(&ifp->lock);
2466 ipv6_ifa_notify(RTM_NEWADDR, ifp);
2467 in6_ifa_put(ifp);
2468 }
2469}
2470
Amerigo Wang07a93622012-10-29 16:23:10 +00002471#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472static void sit_add_v4_addrs(struct inet6_dev *idev)
2473{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 struct in6_addr addr;
2475 struct net_device *dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002476 struct net *net = dev_net(idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 int scope;
2478
2479 ASSERT_RTNL();
2480
2481 memset(&addr, 0, sizeof(struct in6_addr));
2482 memcpy(&addr.s6_addr32[3], idev->dev->dev_addr, 4);
2483
2484 if (idev->dev->flags&IFF_POINTOPOINT) {
2485 addr.s6_addr32[0] = htonl(0xfe800000);
2486 scope = IFA_LINK;
2487 } else {
2488 scope = IPV6_ADDR_COMPATv4;
2489 }
2490
2491 if (addr.s6_addr32[3]) {
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002492 add_addr(idev, &addr, 128, scope);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 return;
2494 }
2495
Benjamin Thery6fda7352008-03-05 10:47:47 -08002496 for_each_netdev(net, dev) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002497 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 if (in_dev && (dev->flags & IFF_UP)) {
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002499 struct in_ifaddr *ifa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
2501 int flag = scope;
2502
2503 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2504 int plen;
2505
2506 addr.s6_addr32[3] = ifa->ifa_local;
2507
2508 if (ifa->ifa_scope == RT_SCOPE_LINK)
2509 continue;
2510 if (ifa->ifa_scope >= RT_SCOPE_HOST) {
2511 if (idev->dev->flags&IFF_POINTOPOINT)
2512 continue;
2513 flag |= IFA_HOST;
2514 }
2515 if (idev->dev->flags&IFF_POINTOPOINT)
2516 plen = 64;
2517 else
2518 plen = 96;
2519
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002520 add_addr(idev, &addr, plen, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 }
2522 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002525#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527static void init_loopback(struct net_device *dev)
2528{
2529 struct inet6_dev *idev;
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05302530 struct net_device *sp_dev;
2531 struct inet6_ifaddr *sp_ifa;
2532 struct rt6_info *sp_rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534 /* ::1 */
2535
2536 ASSERT_RTNL();
2537
2538 if ((idev = ipv6_find_idev(dev)) == NULL) {
Joe Perches91df42b2012-05-15 14:11:54 +00002539 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 return;
2541 }
2542
Ilpo Järvinenb5f348e2009-02-06 23:48:01 -08002543 add_addr(idev, &in6addr_loopback, 128, IFA_HOST);
Balakumaran Kannan25fb6ca42013-04-02 16:15:05 +05302544
2545 /* Add routes to other interface's IPv6 addresses */
2546 for_each_netdev(dev_net(dev), sp_dev) {
2547 if (!strcmp(sp_dev->name, dev->name))
2548 continue;
2549
2550 idev = __in6_dev_get(sp_dev);
2551 if (!idev)
2552 continue;
2553
2554 read_lock_bh(&idev->lock);
2555 list_for_each_entry(sp_ifa, &idev->addr_list, if_list) {
2556
2557 if (sp_ifa->flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE))
2558 continue;
2559
2560 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, 0);
2561
2562 /* Failure cases are ignored */
2563 if (!IS_ERR(sp_rt))
2564 ip6_ins_rt(sp_rt);
2565 }
2566 read_unlock_bh(&idev->lock);
2567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
2569
Eric Dumazetb71d1d42011-04-22 04:53:02 +00002570static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002572 struct inet6_ifaddr *ifp;
Neil Horman95c385b2007-04-25 17:08:10 -07002573 u32 addr_flags = IFA_F_PERMANENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Neil Horman95c385b2007-04-25 17:08:10 -07002575#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2576 if (idev->cnf.optimistic_dad &&
David Miller702beb82008-07-20 18:17:02 -07002577 !dev_net(idev->dev)->ipv6.devconf_all->forwarding)
Neil Horman95c385b2007-04-25 17:08:10 -07002578 addr_flags |= IFA_F_OPTIMISTIC;
2579#endif
2580
2581
2582 ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, addr_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 if (!IS_ERR(ifp)) {
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09002584 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
David S. Millercf22f9a2012-04-14 21:37:40 -04002585 addrconf_dad_start(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 in6_ifa_put(ifp);
2587 }
2588}
2589
2590static void addrconf_dev_config(struct net_device *dev)
2591{
2592 struct in6_addr addr;
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002593 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 ASSERT_RTNL();
2596
Herbert Xu74235a22007-06-14 13:02:55 -07002597 if ((dev->type != ARPHRD_ETHER) &&
2598 (dev->type != ARPHRD_FDDI) &&
Herbert Xu74235a22007-06-14 13:02:55 -07002599 (dev->type != ARPHRD_ARCNET) &&
alex.bluesman.smirnov@gmail.com06a4c1c2012-05-10 03:25:52 +00002600 (dev->type != ARPHRD_INFINIBAND) &&
2601 (dev->type != ARPHRD_IEEE802154)) {
Herbert Xu74235a22007-06-14 13:02:55 -07002602 /* Alas, we support only Ethernet autoconfiguration. */
2603 return;
2604 }
2605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00002607 if (IS_ERR(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 return;
2609
2610 memset(&addr, 0, sizeof(struct in6_addr));
2611 addr.s6_addr32[0] = htonl(0xFE800000);
2612
2613 if (ipv6_generate_eui64(addr.s6_addr + 8, dev) == 0)
2614 addrconf_add_linklocal(idev, &addr);
2615}
2616
Amerigo Wang07a93622012-10-29 16:23:10 +00002617#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618static void addrconf_sit_config(struct net_device *dev)
2619{
2620 struct inet6_dev *idev;
2621
2622 ASSERT_RTNL();
2623
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002624 /*
2625 * Configure the tunnel with one of our IPv4
2626 * addresses... we should configure all of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 * our v4 addrs in the tunnel
2628 */
2629
2630 if ((idev = ipv6_find_idev(dev)) == NULL) {
Joe Perches91df42b2012-05-15 14:11:54 +00002631 pr_debug("%s: add_dev failed\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 return;
2633 }
2634
Fred L. Templinc7dc89c2007-11-29 22:11:40 +11002635 if (dev->priv_flags & IFF_ISATAP) {
2636 struct in6_addr addr;
2637
2638 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2639 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2640 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2641 addrconf_add_linklocal(idev, &addr);
2642 return;
2643 }
2644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 sit_add_v4_addrs(idev);
2646
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00002647 if (dev->flags&IFF_POINTOPOINT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 addrconf_add_mroute(dev);
Nicolas Dichtel62b54dd2012-10-01 23:19:14 +00002649 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 sit_route_add(dev);
2651}
Joerg Roedel0be669b2006-10-10 14:49:53 -07002652#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Amerigo Wang07a93622012-10-29 16:23:10 +00002654#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00002655static void addrconf_gre_config(struct net_device *dev)
2656{
2657 struct inet6_dev *idev;
2658 struct in6_addr addr;
2659
Joe Perchesf3213832012-05-15 14:11:53 +00002660 pr_info("%s(%s)\n", __func__, dev->name);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002661
2662 ASSERT_RTNL();
2663
2664 if ((idev = ipv6_find_idev(dev)) == NULL) {
Joe Perches91df42b2012-05-15 14:11:54 +00002665 pr_debug("%s: add_dev failed\n", __func__);
stephen hemmingeraee80b52011-06-08 10:44:30 +00002666 return;
2667 }
2668
2669 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2670 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2671
2672 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2673 addrconf_add_linklocal(idev, &addr);
2674}
2675#endif
2676
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677static inline int
2678ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
2679{
2680 struct in6_addr lladdr;
2681
Neil Horman95c385b2007-04-25 17:08:10 -07002682 if (!ipv6_get_lladdr(link_dev, &lladdr, IFA_F_TENTATIVE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 addrconf_add_linklocal(idev, &lladdr);
2684 return 0;
2685 }
2686 return -1;
2687}
2688
2689static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
2690{
2691 struct net_device *link_dev;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002692 struct net *net = dev_net(idev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
2694 /* first try to inherit the link-local address from the link device */
2695 if (idev->dev->iflink &&
Benjamin Thery6fda7352008-03-05 10:47:47 -08002696 (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 if (!ipv6_inherit_linklocal(idev, link_dev))
2698 return;
2699 }
2700 /* then try to inherit it from any device */
Benjamin Thery6fda7352008-03-05 10:47:47 -08002701 for_each_netdev(net, link_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 if (!ipv6_inherit_linklocal(idev, link_dev))
2703 return;
2704 }
Joe Perches91df42b2012-05-15 14:11:54 +00002705 pr_debug("init ip6-ip6: add_linklocal failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706}
2707
2708/*
2709 * Autoconfigure tunnel with a link-local address so routing protocols,
2710 * DHCPv6, MLD etc. can be run over the virtual link
2711 */
2712
2713static void addrconf_ip6_tnl_config(struct net_device *dev)
2714{
2715 struct inet6_dev *idev;
2716
2717 ASSERT_RTNL();
2718
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002719 idev = addrconf_add_dev(dev);
Brian Haley64e724f2010-07-20 10:34:30 +00002720 if (IS_ERR(idev)) {
Joe Perches91df42b2012-05-15 14:11:54 +00002721 pr_debug("init ip6-ip6: add_dev failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 return;
2723 }
2724 ip6_tnl_add_linklocal(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725}
2726
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09002727static int addrconf_notify(struct notifier_block *this, unsigned long event,
Eldad Zack8e5e8f32012-04-01 07:49:08 +00002728 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729{
2730 struct net_device *dev = (struct net_device *) data;
Eric Dumazet748e2d92012-08-22 21:50:59 +00002731 struct inet6_dev *idev = __in6_dev_get(dev);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002732 int run_pending = 0;
Herbert Xub217d612007-07-30 17:04:52 -07002733 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002735 switch (event) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002736 case NETDEV_REGISTER:
Herbert Xu74235a22007-06-14 13:02:55 -07002737 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002738 idev = ipv6_add_dev(dev);
2739 if (!idev)
Herbert Xub217d612007-07-30 17:04:52 -07002740 return notifier_from_errno(-ENOMEM);
YOSHIFUJI Hideaki45ba9dd2007-02-15 02:07:27 +09002741 }
2742 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 case NETDEV_UP:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002745 case NETDEV_CHANGE:
Jay Vosburghc2edacf2007-07-09 10:42:47 -07002746 if (dev->flags & IFF_SLAVE)
2747 break;
2748
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002749 if (event == NETDEV_UP) {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002750 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002751 /* device is not ready yet. */
Joe Perchesf3213832012-05-15 14:11:53 +00002752 pr_info("ADDRCONF(NETDEV_UP): %s: link is not ready\n",
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002753 dev->name);
2754 break;
2755 }
Kristian Slavov99081042006-02-08 16:10:53 -08002756
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002757 if (!idev && dev->mtu >= IPV6_MIN_MTU)
2758 idev = ipv6_add_dev(dev);
2759
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08002760 if (idev) {
Kristian Slavov99081042006-02-08 16:10:53 -08002761 idev->if_flags |= IF_READY;
Benjamin Therye3ec6cf2008-11-05 01:43:57 -08002762 run_pending = 1;
2763 }
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002764 } else {
Mitsuru Chinenf24e3d62007-10-10 02:53:43 -07002765 if (!addrconf_qdisc_ok(dev)) {
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002766 /* device is still not ready. */
2767 break;
2768 }
2769
2770 if (idev) {
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002771 if (idev->if_flags & IF_READY)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002772 /* device is already configured. */
2773 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002774 idev->if_flags |= IF_READY;
2775 }
2776
Joe Perchesf3213832012-05-15 14:11:53 +00002777 pr_info("ADDRCONF(NETDEV_CHANGE): %s: link becomes ready\n",
2778 dev->name);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002779
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002780 run_pending = 1;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002781 }
2782
Stephen Hemmingere21e8462010-03-20 16:09:01 -07002783 switch (dev->type) {
Amerigo Wang07a93622012-10-29 16:23:10 +00002784#if IS_ENABLED(CONFIG_IPV6_SIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 case ARPHRD_SIT:
2786 addrconf_sit_config(dev);
2787 break;
Joerg Roedel0be669b2006-10-10 14:49:53 -07002788#endif
Amerigo Wang07a93622012-10-29 16:23:10 +00002789#if IS_ENABLED(CONFIG_NET_IPGRE)
stephen hemmingeraee80b52011-06-08 10:44:30 +00002790 case ARPHRD_IPGRE:
2791 addrconf_gre_config(dev);
2792 break;
2793#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 case ARPHRD_TUNNEL6:
2795 addrconf_ip6_tnl_config(dev);
2796 break;
2797 case ARPHRD_LOOPBACK:
2798 init_loopback(dev);
2799 break;
2800
2801 default:
2802 addrconf_dev_config(dev);
2803 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002804 }
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002805
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 if (idev) {
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09002807 if (run_pending)
2808 addrconf_dad_run(idev);
2809
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002810 /*
2811 * If the MTU changed during the interface down,
2812 * when the interface up, the changed MTU must be
2813 * reflected in the idev as well as routers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 */
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002815 if (idev->cnf.mtu6 != dev->mtu &&
2816 dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 rt6_mtu_change(dev, dev->mtu);
2818 idev->cnf.mtu6 = dev->mtu;
2819 }
2820 idev->tstamp = jiffies;
2821 inet6_ifinfo_notify(RTM_NEWLINK, idev);
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002822
2823 /*
2824 * If the changed mtu during down is lower than
2825 * IPV6_MIN_MTU stop IPv6 on this interface.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 */
2827 if (dev->mtu < IPV6_MIN_MTU)
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002828 addrconf_ifdown(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 }
2830 break;
2831
2832 case NETDEV_CHANGEMTU:
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002833 if (idev && dev->mtu >= IPV6_MIN_MTU) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 rt6_mtu_change(dev, dev->mtu);
2835 idev->cnf.mtu6 = dev->mtu;
2836 break;
2837 }
2838
Evgeniy Polyakovd31c7b82007-11-30 23:36:08 +11002839 if (!idev && dev->mtu >= IPV6_MIN_MTU) {
2840 idev = ipv6_add_dev(dev);
2841 if (idev)
2842 break;
2843 }
2844
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002845 /*
2846 * MTU falled under IPV6_MIN_MTU.
2847 * Stop IPv6 on this interface.
2848 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 case NETDEV_DOWN:
2851 case NETDEV_UNREGISTER:
2852 /*
2853 * Remove all addresses from this interface.
2854 */
2855 addrconf_ifdown(dev, event != NETDEV_DOWN);
2856 break;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002857
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 case NETDEV_CHANGENAME:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 if (idev) {
Stephen Hemminger5632c512007-04-28 21:16:39 -07002860 snmp6_unregister_dev(idev);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08002861 addrconf_sysctl_unregister(idev);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11002862 addrconf_sysctl_register(idev);
Herbert Xub217d612007-07-30 17:04:52 -07002863 err = snmp6_register_dev(idev);
2864 if (err)
2865 return notifier_from_errno(err);
Stephen Hemminger5632c512007-04-28 21:16:39 -07002866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 break;
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002868
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002869 case NETDEV_PRE_TYPE_CHANGE:
2870 case NETDEV_POST_TYPE_CHANGE:
2871 addrconf_type_change(dev, event);
Moni Shoua75c78502009-09-15 02:37:40 -07002872 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
2875 return NOTIFY_OK;
2876}
2877
2878/*
2879 * addrconf module should be notified of a device going up
2880 */
2881static struct notifier_block ipv6_dev_notf = {
2882 .notifier_call = addrconf_notify,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883};
2884
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002885static void addrconf_type_change(struct net_device *dev, unsigned long event)
Moni Shoua75c78502009-09-15 02:37:40 -07002886{
2887 struct inet6_dev *idev;
2888 ASSERT_RTNL();
2889
2890 idev = __in6_dev_get(dev);
2891
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002892 if (event == NETDEV_POST_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07002893 ipv6_mc_remap(idev);
Jiri Pirko93d9b7d2010-03-10 10:28:56 +00002894 else if (event == NETDEV_PRE_TYPE_CHANGE)
Moni Shoua75c78502009-09-15 02:37:40 -07002895 ipv6_mc_unmap(idev);
2896}
2897
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898static int addrconf_ifdown(struct net_device *dev, int how)
2899{
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09002900 struct net *net = dev_net(dev);
stephen hemminger502a2ff2010-03-17 20:31:13 +00002901 struct inet6_dev *idev;
2902 struct inet6_ifaddr *ifa;
David S. Miller73a8bd72011-01-23 23:27:15 -08002903 int state, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
2905 ASSERT_RTNL();
2906
David S. Miller73a8bd72011-01-23 23:27:15 -08002907 rt6_ifdown(net, dev);
2908 neigh_ifdown(&nd_tbl, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
2910 idev = __in6_dev_get(dev);
2911 if (idev == NULL)
2912 return -ENODEV;
2913
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002914 /*
2915 * Step 1: remove reference to ipv6 device from parent device.
2916 * Do not dev_put!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002918 if (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 idev->dead = 1;
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07002920
2921 /* protected by rtnl_lock */
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002922 RCU_INIT_POINTER(dev->ip6_ptr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
2924 /* Step 1.5: remove snmp6 entry */
2925 snmp6_unregister_dev(idev);
2926
2927 }
2928
David S. Miller73a8bd72011-01-23 23:27:15 -08002929 /* Step 2: clear hash table */
2930 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
2931 struct hlist_head *h = &inet6_addr_lst[i];
David S. Miller73a8bd72011-01-23 23:27:15 -08002932
2933 spin_lock_bh(&addrconf_hash_lock);
2934 restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08002935 hlist_for_each_entry_rcu(ifa, h, addr_lst) {
David S. Miller73a8bd72011-01-23 23:27:15 -08002936 if (ifa->idev == idev) {
2937 hlist_del_init_rcu(&ifa->addr_lst);
2938 addrconf_del_timer(ifa);
2939 goto restart;
2940 }
2941 }
2942 spin_unlock_bh(&addrconf_hash_lock);
2943 }
2944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 write_lock_bh(&idev->lock);
2946
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002947 /* Step 2: clear flags for stateless addrconf */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002948 if (!how)
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09002949 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951#ifdef CONFIG_IPV6_PRIVACY
Denis V. Lunev439e2382008-04-03 13:30:17 -07002952 if (how && del_timer(&idev->regen_timer))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 in6_dev_put(idev);
2954
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07002955 /* Step 3: clear tempaddr list */
stephen hemminger372e6c82010-03-17 20:31:09 +00002956 while (!list_empty(&idev->tempaddr_list)) {
2957 ifa = list_first_entry(&idev->tempaddr_list,
2958 struct inet6_ifaddr, tmp_list);
2959 list_del(&ifa->tmp_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 write_unlock_bh(&idev->lock);
2961 spin_lock_bh(&ifa->lock);
2962
2963 if (ifa->ifpub) {
2964 in6_ifa_put(ifa->ifpub);
2965 ifa->ifpub = NULL;
2966 }
2967 spin_unlock_bh(&ifa->lock);
2968 in6_ifa_put(ifa);
2969 write_lock_bh(&idev->lock);
2970 }
2971#endif
stephen hemminger8f37ada2010-03-03 08:19:59 +00002972
stephen hemminger502a2ff2010-03-17 20:31:13 +00002973 while (!list_empty(&idev->addr_list)) {
2974 ifa = list_first_entry(&idev->addr_list,
2975 struct inet6_ifaddr, if_list);
stephen hemminger84e8b802010-03-02 13:32:46 +00002976 addrconf_del_timer(ifa);
2977
David S. Miller73a8bd72011-01-23 23:27:15 -08002978 list_del(&ifa->if_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
David S. Miller73a8bd72011-01-23 23:27:15 -08002980 write_unlock_bh(&idev->lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00002981
David S. Miller73a8bd72011-01-23 23:27:15 -08002982 spin_lock_bh(&ifa->state_lock);
2983 state = ifa->state;
2984 ifa->state = INET6_IFADDR_STATE_DEAD;
2985 spin_unlock_bh(&ifa->state_lock);
stephen hemminger84e8b802010-03-02 13:32:46 +00002986
David S. Miller73a8bd72011-01-23 23:27:15 -08002987 if (state != INET6_IFADDR_STATE_DEAD) {
2988 __ipv6_ifa_notify(RTM_DELADDR, ifa);
Cong Wangf88c91d2013-04-14 23:18:43 +08002989 inet6addr_notifier_call_chain(NETDEV_DOWN, ifa);
stephen hemminger27bdb2a2010-04-12 05:41:32 +00002990 }
David S. Miller73a8bd72011-01-23 23:27:15 -08002991 in6_ifa_put(ifa);
stephen hemminger8f37ada2010-03-03 08:19:59 +00002992
David S. Miller73a8bd72011-01-23 23:27:15 -08002993 write_lock_bh(&idev->lock);
2994 }
stephen hemminger8f37ada2010-03-03 08:19:59 +00002995
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 write_unlock_bh(&idev->lock);
2997
2998 /* Step 5: Discard multicast list */
Denis V. Lunev439e2382008-04-03 13:30:17 -07002999 if (how)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 ipv6_mc_destroy_dev(idev);
3001 else
3002 ipv6_mc_down(idev);
3003
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 idev->tstamp = jiffies;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003005
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003006 /* Last: Shot the device (if unregistered) */
Denis V. Lunev439e2382008-04-03 13:30:17 -07003007 if (how) {
Pavel Emelyanov408c4762008-01-10 17:41:21 -08003008 addrconf_sysctl_unregister(idev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 neigh_parms_release(&nd_tbl, idev->nd_parms);
3010 neigh_ifdown(&nd_tbl, dev);
3011 in6_dev_put(idev);
3012 }
3013 return 0;
3014}
3015
3016static void addrconf_rs_timer(unsigned long data)
3017{
3018 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003019 struct inet6_dev *idev = ifp->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
stephen hemminger5b2a1952010-03-02 13:32:45 +00003021 read_lock(&idev->lock);
3022 if (idev->dead || !(idev->if_flags & IF_READY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 goto out;
3024
Shmulik Ladkani9ba2add2012-12-02 01:44:53 +00003025 if (!ipv6_accept_ra(idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 goto out;
stephen hemminger5b2a1952010-03-02 13:32:45 +00003027
3028 /* Announcement received after solicitation was sent */
3029 if (idev->if_flags & IF_RA_RCVD)
3030 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
3032 spin_lock(&ifp->lock);
stephen hemminger5b2a1952010-03-02 13:32:45 +00003033 if (ifp->probes++ < idev->cnf.rtr_solicits) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 /* The wait after the last probe can be shorter */
3035 addrconf_mod_timer(ifp, AC_RS,
stephen hemminger5b2a1952010-03-02 13:32:45 +00003036 (ifp->probes == idev->cnf.rtr_solicits) ?
3037 idev->cnf.rtr_solicit_delay :
3038 idev->cnf.rtr_solicit_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 spin_unlock(&ifp->lock);
3040
stephen hemminger5b2a1952010-03-02 13:32:45 +00003041 ndisc_send_rs(idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 } else {
3043 spin_unlock(&ifp->lock);
3044 /*
3045 * Note: we do not support deprecated "all on-link"
3046 * assumption any longer.
3047 */
Joe Perches91df42b2012-05-15 14:11:54 +00003048 pr_debug("%s: no IPv6 routers present\n", idev->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 }
3050
3051out:
stephen hemminger5b2a1952010-03-02 13:32:45 +00003052 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 in6_ifa_put(ifp);
3054}
3055
3056/*
3057 * Duplicate Address Detection
3058 */
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003059static void addrconf_dad_kick(struct inet6_ifaddr *ifp)
3060{
3061 unsigned long rand_num;
3062 struct inet6_dev *idev = ifp->idev;
3063
Neil Horman95c385b2007-04-25 17:08:10 -07003064 if (ifp->flags & IFA_F_OPTIMISTIC)
3065 rand_num = 0;
3066 else
3067 rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
3068
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003069 ifp->probes = idev->cnf.dad_transmits;
3070 addrconf_mod_timer(ifp, AC_DAD, rand_num);
3071}
3072
David S. Millercf22f9a2012-04-14 21:37:40 -04003073static void addrconf_dad_start(struct inet6_ifaddr *ifp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
3075 struct inet6_dev *idev = ifp->idev;
3076 struct net_device *dev = idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
3078 addrconf_join_solict(dev, &ifp->addr);
3079
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 net_srandom(ifp->addr.s6_addr32[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
3082 read_lock_bh(&idev->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003083 spin_lock(&ifp->lock);
Herbert Xue9d3e082010-05-18 15:36:06 -07003084 if (ifp->state == INET6_IFADDR_STATE_DEAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
3087 if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09003088 idev->cnf.accept_dad < 1 ||
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003089 !(ifp->flags&IFA_F_TENTATIVE) ||
3090 ifp->flags & IFA_F_NODAD) {
Brian Haleycc411d02009-09-09 14:41:32 +00003091 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003092 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 read_unlock_bh(&idev->lock);
3094
3095 addrconf_dad_completed(ifp);
3096 return;
3097 }
3098
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003099 if (!(idev->if_flags & IF_READY)) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003100 spin_unlock(&ifp->lock);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003101 read_unlock_bh(&idev->lock);
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003102 /*
Masatake YAMATO590a9882009-06-09 10:41:12 +09003103 * If the device is not ready:
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003104 * - keep it tentative if it is a permanent address.
3105 * - otherwise, kill it.
3106 */
3107 in6_ifa_hold(ifp);
Brian Haleycc411d02009-09-09 14:41:32 +00003108 addrconf_dad_stop(ifp, 0);
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003109 return;
YOSHIFUJI Hideaki3c21edb2005-12-21 22:57:24 +09003110 }
Neil Horman95c385b2007-04-25 17:08:10 -07003111
3112 /*
3113 * Optimistic nodes can start receiving
3114 * Frames right away
3115 */
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003116 if (ifp->flags & IFA_F_OPTIMISTIC)
Neil Horman95c385b2007-04-25 17:08:10 -07003117 ip6_ins_rt(ifp->rt);
3118
YOSHIFUJI Hideaki6732bad2005-12-27 13:35:15 -08003119 addrconf_dad_kick(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120out:
Herbert Xu622ccdf2010-05-18 15:56:06 -07003121 spin_unlock(&ifp->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 read_unlock_bh(&idev->lock);
3123}
3124
3125static void addrconf_dad_timer(unsigned long data)
3126{
3127 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
3128 struct inet6_dev *idev = ifp->idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 struct in6_addr mcaddr;
3130
Herbert Xuf2344a12010-05-18 15:55:27 -07003131 if (!ifp->probes && addrconf_dad_end(ifp))
3132 goto out;
3133
stephen hemminger122e4512010-03-02 13:32:44 +00003134 read_lock(&idev->lock);
3135 if (idev->dead || !(idev->if_flags & IF_READY)) {
3136 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 goto out;
3138 }
stephen hemminger21809fa2010-02-08 19:48:52 +00003139
3140 spin_lock(&ifp->lock);
Herbert Xu622ccdf2010-05-18 15:56:06 -07003141 if (ifp->state == INET6_IFADDR_STATE_DEAD) {
3142 spin_unlock(&ifp->lock);
3143 read_unlock(&idev->lock);
3144 goto out;
3145 }
3146
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 if (ifp->probes == 0) {
3148 /*
3149 * DAD was successful
3150 */
3151
Brian Haleycc411d02009-09-09 14:41:32 +00003152 ifp->flags &= ~(IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|IFA_F_DADFAILED);
stephen hemminger21809fa2010-02-08 19:48:52 +00003153 spin_unlock(&ifp->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003154 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
3156 addrconf_dad_completed(ifp);
3157
3158 goto out;
3159 }
3160
3161 ifp->probes--;
3162 addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
stephen hemminger21809fa2010-02-08 19:48:52 +00003163 spin_unlock(&ifp->lock);
stephen hemminger122e4512010-03-02 13:32:44 +00003164 read_unlock(&idev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
3166 /* send a neighbour solicitation for our addr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
YOSHIFUJI Hideakid7aabf22008-04-10 15:42:11 +09003168 ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &in6addr_any);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169out:
3170 in6_ifa_put(ifp);
3171}
3172
3173static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3174{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003175 struct net_device *dev = ifp->idev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
3177 /*
3178 * Configure the address for reception. Now it is valid.
3179 */
3180
3181 ipv6_ifa_notify(RTM_NEWADDR, ifp);
3182
Tore Anderson026359b2011-08-28 23:47:33 +00003183 /* If added prefix is link local and we are prepared to process
3184 router advertisements, start sending router solicitations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 */
3186
Shmulik Ladkaniaeaf6e92012-11-30 10:25:59 +00003187 if (ipv6_accept_ra(ifp->idev) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 ifp->idev->cnf.rtr_solicits > 0 &&
3189 (dev->flags&IFF_LOOPBACK) == 0 &&
3190 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 /*
3192 * If a host as already performed a random delay
3193 * [...] as part of DAD [...] there is no need
3194 * to delay again before sending the first RS
3195 */
YOSHIFUJI Hideakif3ee4012008-04-10 15:42:11 +09003196 ndisc_send_rs(ifp->idev->dev, &ifp->addr, &in6addr_linklocal_allrouters);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
3198 spin_lock_bh(&ifp->lock);
3199 ifp->probes = 1;
3200 ifp->idev->if_flags |= IF_RS_SENT;
3201 addrconf_mod_timer(ifp, AC_RS, ifp->idev->cnf.rtr_solicit_interval);
3202 spin_unlock_bh(&ifp->lock);
3203 }
3204}
3205
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003206static void addrconf_dad_run(struct inet6_dev *idev)
3207{
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003208 struct inet6_ifaddr *ifp;
3209
3210 read_lock_bh(&idev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +00003211 list_for_each_entry(ifp, &idev->addr_list, if_list) {
stephen hemminger21809fa2010-02-08 19:48:52 +00003212 spin_lock(&ifp->lock);
Herbert Xuf2344a12010-05-18 15:55:27 -07003213 if (ifp->flags & IFA_F_TENTATIVE &&
3214 ifp->state == INET6_IFADDR_STATE_DAD)
3215 addrconf_dad_kick(ifp);
stephen hemminger21809fa2010-02-08 19:48:52 +00003216 spin_unlock(&ifp->lock);
YOSHIFUJI Hideakic5e33bd2005-12-21 22:57:44 +09003217 }
3218 read_unlock_bh(&idev->lock);
3219}
3220
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221#ifdef CONFIG_PROC_FS
3222struct if6_iter_state {
Daniel Lezcano3c400902008-01-10 22:42:49 -08003223 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 int bucket;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003225 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226};
3227
Mihai Maruseac1d578302012-01-03 23:31:35 +00003228static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229{
3230 struct inet6_ifaddr *ifa = NULL;
3231 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003232 struct net *net = seq_file_net(seq);
Mihai Maruseac1d578302012-01-03 23:31:35 +00003233 int p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Mihai Maruseac1d578302012-01-03 23:31:35 +00003235 /* initial bucket if pos is 0 */
3236 if (pos == 0) {
3237 state->bucket = 0;
3238 state->offset = 0;
3239 }
3240
3241 for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08003242 hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
Mihai Maruseac1d578302012-01-03 23:31:35 +00003243 addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003244 if (!net_eq(dev_net(ifa->idev->dev), net))
3245 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003246 /* sync with offset */
3247 if (p < state->offset) {
3248 p++;
3249 continue;
3250 }
3251 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003252 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003253 }
3254
3255 /* prepare for next bucket */
3256 state->offset = 0;
3257 p = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00003259 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260}
3261
stephen hemmingerc2e21292010-03-17 20:31:10 +00003262static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
3263 struct inet6_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264{
3265 struct if6_iter_state *state = seq->private;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09003266 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Sasha Levinb67bfe02013-02-27 17:06:00 -08003268 hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003269 if (!net_eq(dev_net(ifa->idev->dev), net))
3270 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003271 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003272 return ifa;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003273 }
stephen hemmingerc2e21292010-03-17 20:31:10 +00003274
3275 while (++state->bucket < IN6_ADDR_HSIZE) {
Mihai Maruseac1d578302012-01-03 23:31:35 +00003276 state->offset = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08003277 hlist_for_each_entry_rcu_bh(ifa,
stephen hemmingerc2e21292010-03-17 20:31:10 +00003278 &inet6_addr_lst[state->bucket], addr_lst) {
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003279 if (!net_eq(dev_net(ifa->idev->dev), net))
3280 continue;
Mihai Maruseac1d578302012-01-03 23:31:35 +00003281 state->offset++;
Eric Dumazet9f0d3c22012-10-16 07:37:27 +00003282 return ifa;
Daniel Lezcano3c400902008-01-10 22:42:49 -08003283 }
3284 }
3285
stephen hemmingerc2e21292010-03-17 20:31:10 +00003286 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287}
3288
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003290 __acquires(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291{
stephen hemminger5c578ae2010-03-17 20:31:11 +00003292 rcu_read_lock_bh();
Mihai Maruseac1d578302012-01-03 23:31:35 +00003293 return if6_get_first(seq, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
3295
3296static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3297{
3298 struct inet6_ifaddr *ifa;
3299
3300 ifa = if6_get_next(seq, v);
3301 ++*pos;
3302 return ifa;
3303}
3304
3305static void if6_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet4f70ecc2010-05-03 10:50:14 +00003306 __releases(rcu_bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307{
stephen hemminger5c578ae2010-03-17 20:31:11 +00003308 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309}
3310
3311static int if6_seq_show(struct seq_file *seq, void *v)
3312{
3313 struct inet6_ifaddr *ifp = (struct inet6_ifaddr *)v;
Harvey Harrison4b7a4272008-10-29 12:50:24 -07003314 seq_printf(seq, "%pi6 %02x %02x %02x %02x %8s\n",
Harvey Harrisonb0711952008-10-28 16:05:40 -07003315 &ifp->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 ifp->idev->dev->ifindex,
3317 ifp->prefix_len,
3318 ifp->scope,
3319 ifp->flags,
3320 ifp->idev->dev->name);
3321 return 0;
3322}
3323
Philippe De Muyter56b3d972007-07-10 23:07:31 -07003324static const struct seq_operations if6_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 .start = if6_seq_start,
3326 .next = if6_seq_next,
3327 .show = if6_seq_show,
3328 .stop = if6_seq_stop,
3329};
3330
3331static int if6_seq_open(struct inode *inode, struct file *file)
3332{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003333 return seq_open_net(inode, file, &if6_seq_ops,
3334 sizeof(struct if6_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335}
3336
Arjan van de Ven9a321442007-02-12 00:55:35 -08003337static const struct file_operations if6_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 .owner = THIS_MODULE,
3339 .open = if6_seq_open,
3340 .read = seq_read,
3341 .llseek = seq_lseek,
Daniel Lezcano3c400902008-01-10 22:42:49 -08003342 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343};
3344
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003345static int __net_init if6_proc_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346{
Gao fengd4beaa62013-02-18 01:34:54 +00003347 if (!proc_create("if_inet6", S_IRUGO, net->proc_net, &if6_fops))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 return -ENOMEM;
3349 return 0;
3350}
3351
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003352static void __net_exit if6_proc_net_exit(struct net *net)
Daniel Lezcano3c400902008-01-10 22:42:49 -08003353{
Gao fengece31ff2013-02-18 01:34:56 +00003354 remove_proc_entry("if_inet6", net->proc_net);
Daniel Lezcano3c400902008-01-10 22:42:49 -08003355}
3356
3357static struct pernet_operations if6_proc_net_ops = {
3358 .init = if6_proc_net_init,
3359 .exit = if6_proc_net_exit,
3360};
3361
3362int __init if6_proc_init(void)
3363{
3364 return register_pernet_subsys(&if6_proc_net_ops);
3365}
3366
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367void if6_proc_exit(void)
3368{
Daniel Lezcano3c400902008-01-10 22:42:49 -08003369 unregister_pernet_subsys(&if6_proc_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370}
3371#endif /* CONFIG_PROC_FS */
3372
Amerigo Wang07a93622012-10-29 16:23:10 +00003373#if IS_ENABLED(CONFIG_IPV6_MIP6)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003374/* Check if address is a home address configured on any interface. */
Eric Dumazetb71d1d42011-04-22 04:53:02 +00003375int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003376{
3377 int ret = 0;
stephen hemmingerc2e21292010-03-17 20:31:10 +00003378 struct inet6_ifaddr *ifp = NULL;
Eric Dumazetddbe5032012-07-18 08:11:12 +00003379 unsigned int hash = inet6_addr_hash(addr);
stephen hemmingerc2e21292010-03-17 20:31:10 +00003380
stephen hemminger5c578ae2010-03-17 20:31:11 +00003381 rcu_read_lock_bh();
Sasha Levinb67bfe02013-02-27 17:06:00 -08003382 hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09003383 if (!net_eq(dev_net(ifp->idev->dev), net))
Daniel Lezcano389f6612008-01-10 22:44:40 -08003384 continue;
YOSHIFUJI Hideakicaad2952008-04-10 15:42:07 +09003385 if (ipv6_addr_equal(&ifp->addr, addr) &&
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003386 (ifp->flags & IFA_F_HOMEADDRESS)) {
3387 ret = 1;
3388 break;
3389 }
3390 }
stephen hemminger5c578ae2010-03-17 20:31:11 +00003391 rcu_read_unlock_bh();
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003392 return ret;
3393}
3394#endif
3395
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396/*
3397 * Periodic address status verification
3398 */
3399
3400static void addrconf_verify(unsigned long foo)
3401{
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003402 unsigned long now, next, next_sec, next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 struct inet6_ifaddr *ifp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 int i;
3405
stephen hemminger5c578ae2010-03-17 20:31:11 +00003406 rcu_read_lock_bh();
3407 spin_lock(&addrconf_verify_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 now = jiffies;
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003409 next = round_jiffies_up(now + ADDR_CHECK_FREQUENCY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
3411 del_timer(&addr_chk_timer);
3412
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07003413 for (i = 0; i < IN6_ADDR_HSIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414restart:
Sasha Levinb67bfe02013-02-27 17:06:00 -08003415 hlist_for_each_entry_rcu_bh(ifp,
stephen hemminger5c578ae2010-03-17 20:31:11 +00003416 &inet6_addr_lst[i], addr_lst) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 unsigned long age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
3419 if (ifp->flags & IFA_F_PERMANENT)
3420 continue;
3421
3422 spin_lock(&ifp->lock);
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003423 /* We try to batch several events at once. */
3424 age = (now - ifp->tstamp + ADDRCONF_TIMER_FUZZ_MINUS) / HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09003426 if (ifp->valid_lft != INFINITY_LIFE_TIME &&
3427 age >= ifp->valid_lft) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 spin_unlock(&ifp->lock);
3429 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 ipv6_del_addr(ifp);
3431 goto restart;
YOSHIFUJI Hideaki8f27ebb2006-07-28 18:12:11 +09003432 } else if (ifp->prefered_lft == INFINITY_LIFE_TIME) {
3433 spin_unlock(&ifp->lock);
3434 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 } else if (age >= ifp->prefered_lft) {
Brian Haleya1ed0522009-07-02 07:10:52 +00003436 /* jiffies - ifp->tstamp > age >= ifp->prefered_lft */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 int deprecate = 0;
3438
3439 if (!(ifp->flags&IFA_F_DEPRECATED)) {
3440 deprecate = 1;
3441 ifp->flags |= IFA_F_DEPRECATED;
3442 }
3443
3444 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next))
3445 next = ifp->tstamp + ifp->valid_lft * HZ;
3446
3447 spin_unlock(&ifp->lock);
3448
3449 if (deprecate) {
3450 in6_ifa_hold(ifp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451
3452 ipv6_ifa_notify(0, ifp);
3453 in6_ifa_put(ifp);
3454 goto restart;
3455 }
3456#ifdef CONFIG_IPV6_PRIVACY
3457 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3458 !(ifp->flags&IFA_F_TENTATIVE)) {
stephen hemminger88949cf2010-03-17 20:31:17 +00003459 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
3460 ifp->idev->cnf.dad_transmits *
3461 ifp->idev->nd_parms->retrans_time / HZ;
3462
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 if (age >= ifp->prefered_lft - regen_advance) {
3464 struct inet6_ifaddr *ifpub = ifp->ifpub;
3465 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3466 next = ifp->tstamp + ifp->prefered_lft * HZ;
3467 if (!ifp->regen_count && ifpub) {
3468 ifp->regen_count++;
3469 in6_ifa_hold(ifp);
3470 in6_ifa_hold(ifpub);
3471 spin_unlock(&ifp->lock);
stephen hemminger5c578ae2010-03-17 20:31:11 +00003472
Hiroyuki YAMAMORI291d8092005-12-23 11:24:05 -08003473 spin_lock(&ifpub->lock);
3474 ifpub->regen_count = 0;
3475 spin_unlock(&ifpub->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 ipv6_create_tempaddr(ifpub, ifp);
3477 in6_ifa_put(ifpub);
3478 in6_ifa_put(ifp);
3479 goto restart;
3480 }
3481 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3482 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3483 spin_unlock(&ifp->lock);
3484#endif
3485 } else {
3486 /* ifp->prefered_lft <= ifp->valid_lft */
3487 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
3488 next = ifp->tstamp + ifp->prefered_lft * HZ;
3489 spin_unlock(&ifp->lock);
3490 }
3491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 }
3493
YOSHIFUJI Hideakib2db7562010-03-20 16:11:12 -07003494 next_sec = round_jiffies_up(next);
3495 next_sched = next;
3496
3497 /* If rounded timeout is accurate enough, accept it. */
3498 if (time_before(next_sec, next + ADDRCONF_TIMER_FUZZ))
3499 next_sched = next_sec;
3500
3501 /* And minimum interval is ADDRCONF_TIMER_FUZZ_MAX. */
3502 if (time_before(next_sched, jiffies + ADDRCONF_TIMER_FUZZ_MAX))
3503 next_sched = jiffies + ADDRCONF_TIMER_FUZZ_MAX;
3504
3505 ADBG((KERN_DEBUG "now = %lu, schedule = %lu, rounded schedule = %lu => %lu\n",
3506 now, next, next_sec, next_sched));
3507
3508 addr_chk_timer.expires = next_sched;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 add_timer(&addr_chk_timer);
stephen hemminger5c578ae2010-03-17 20:31:11 +00003510 spin_unlock(&addrconf_verify_lock);
3511 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512}
3513
Thomas Graf461d8832006-09-18 00:09:49 -07003514static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
3515{
3516 struct in6_addr *pfx = NULL;
3517
3518 if (addr)
3519 pfx = nla_data(addr);
3520
3521 if (local) {
3522 if (pfx && nla_memcmp(local, pfx, sizeof(*pfx)))
3523 pfx = NULL;
3524 else
3525 pfx = nla_data(local);
3526 }
3527
3528 return pfx;
3529}
3530
Patrick McHardyef7c79e2007-06-05 12:38:30 -07003531static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
Thomas Graf461d8832006-09-18 00:09:49 -07003532 [IFA_ADDRESS] = { .len = sizeof(struct in6_addr) },
3533 [IFA_LOCAL] = { .len = sizeof(struct in6_addr) },
3534 [IFA_CACHEINFO] = { .len = sizeof(struct ifa_cacheinfo) },
3535};
3536
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537static int
3538inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3539{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003540 struct net *net = sock_net(skb->sk);
Thomas Grafb933f712006-09-18 00:10:19 -07003541 struct ifaddrmsg *ifm;
3542 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 struct in6_addr *pfx;
Thomas Grafb933f712006-09-18 00:10:19 -07003544 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
Thomas Grafb933f712006-09-18 00:10:19 -07003546 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3547 if (err < 0)
3548 return err;
3549
3550 ifm = nlmsg_data(nlh);
3551 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 if (pfx == NULL)
3553 return -EINVAL;
3554
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003555 return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556}
3557
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003558static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
3559 u32 prefered_lft, u32 valid_lft)
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003560{
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003561 u32 flags;
3562 clock_t expires;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003563 unsigned long timeout;
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003564
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003565 if (!valid_lft || (prefered_lft > valid_lft))
3566 return -EINVAL;
3567
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003568 timeout = addrconf_timeout_fixup(valid_lft, HZ);
3569 if (addrconf_finite_timeout(timeout)) {
3570 expires = jiffies_to_clock_t(timeout * HZ);
3571 valid_lft = timeout;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003572 flags = RTF_EXPIRES;
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003573 } else {
3574 expires = 0;
3575 flags = 0;
3576 ifa_flags |= IFA_F_PERMANENT;
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003577 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003578
YOSHIFUJI Hideaki4bed72e2008-05-27 17:37:49 +09003579 timeout = addrconf_timeout_fixup(prefered_lft, HZ);
3580 if (addrconf_finite_timeout(timeout)) {
3581 if (timeout == 0)
3582 ifa_flags |= IFA_F_DEPRECATED;
3583 prefered_lft = timeout;
3584 }
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003585
3586 spin_lock_bh(&ifp->lock);
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003587 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 +09003588 ifp->tstamp = jiffies;
3589 ifp->valid_lft = valid_lft;
3590 ifp->prefered_lft = prefered_lft;
3591
3592 spin_unlock_bh(&ifp->lock);
3593 if (!(ifp->flags&IFA_F_TENTATIVE))
3594 ipv6_ifa_notify(0, ifp);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003595
YOSHIFUJI Hideaki46d48042007-02-07 20:36:26 +09003596 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, ifp->idev->dev,
YOSHIFUJI Hideaki6f704992008-05-19 16:56:11 -07003597 expires, flags);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003598 addrconf_verify(0);
3599
3600 return 0;
3601}
3602
3603static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
3605{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003606 struct net *net = sock_net(skb->sk);
Thomas Graf461d8832006-09-18 00:09:49 -07003607 struct ifaddrmsg *ifm;
3608 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 struct in6_addr *pfx;
Thomas Graf7198f8c2006-09-18 00:13:46 -07003610 struct inet6_ifaddr *ifa;
3611 struct net_device *dev;
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003612 u32 valid_lft = INFINITY_LIFE_TIME, preferred_lft = INFINITY_LIFE_TIME;
3613 u8 ifa_flags;
Thomas Graf461d8832006-09-18 00:09:49 -07003614 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
Thomas Graf461d8832006-09-18 00:09:49 -07003616 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3617 if (err < 0)
3618 return err;
3619
3620 ifm = nlmsg_data(nlh);
3621 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 if (pfx == NULL)
3623 return -EINVAL;
3624
Thomas Graf461d8832006-09-18 00:09:49 -07003625 if (tb[IFA_CACHEINFO]) {
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003626 struct ifa_cacheinfo *ci;
Thomas Graf461d8832006-09-18 00:09:49 -07003627
3628 ci = nla_data(tb[IFA_CACHEINFO]);
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003629 valid_lft = ci->ifa_valid;
Thomas Graf461d8832006-09-18 00:09:49 -07003630 preferred_lft = ci->ifa_prefered;
3631 } else {
3632 preferred_lft = INFINITY_LIFE_TIME;
3633 valid_lft = INFINITY_LIFE_TIME;
Noriaki TAKAMIYA07787692006-07-28 18:12:10 +09003634 }
3635
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003636 dev = __dev_get_by_index(net, ifm->ifa_index);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003637 if (dev == NULL)
3638 return -ENODEV;
3639
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003640 /* We ignore other flags so far. */
Noriaki TAKAMIYA3b9f9a12006-09-22 14:45:56 -07003641 ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003642
Daniel Lezcano1cab3da2008-01-10 22:44:09 -08003643 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003644 if (ifa == NULL) {
3645 /*
3646 * It would be best to check for !NLM_F_CREATE here but
3647 * userspace alreay relies on not having to provide this.
3648 */
Daniel Lezcanoaf284932008-03-05 10:46:57 -08003649 return inet6_addr_add(net, ifm->ifa_index, pfx,
3650 ifm->ifa_prefixlen, ifa_flags,
3651 preferred_lft, valid_lft);
Noriaki TAKAMIYA081bba52006-07-28 18:12:13 +09003652 }
3653
Thomas Graf7198f8c2006-09-18 00:13:46 -07003654 if (nlh->nlmsg_flags & NLM_F_EXCL ||
3655 !(nlh->nlmsg_flags & NLM_F_REPLACE))
3656 err = -EEXIST;
3657 else
Noriaki TAKAMIYA55ebaef2006-09-22 14:45:27 -07003658 err = inet6_addr_modify(ifa, ifa_flags, preferred_lft, valid_lft);
Thomas Graf7198f8c2006-09-18 00:13:46 -07003659
3660 in6_ifa_put(ifa);
3661
3662 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663}
3664
Thomas Graf101bb222006-09-18 00:11:52 -07003665static void put_ifaddrmsg(struct nlmsghdr *nlh, u8 prefixlen, u8 flags,
3666 u8 scope, int ifindex)
3667{
3668 struct ifaddrmsg *ifm;
3669
3670 ifm = nlmsg_data(nlh);
3671 ifm->ifa_family = AF_INET6;
3672 ifm->ifa_prefixlen = prefixlen;
3673 ifm->ifa_flags = flags;
3674 ifm->ifa_scope = scope;
3675 ifm->ifa_index = ifindex;
3676}
3677
Thomas Graf85486af2006-09-18 00:11:24 -07003678static int put_cacheinfo(struct sk_buff *skb, unsigned long cstamp,
3679 unsigned long tstamp, u32 preferred, u32 valid)
3680{
3681 struct ifa_cacheinfo ci;
3682
Thomas Graf18a31e12010-11-17 04:12:02 +00003683 ci.cstamp = cstamp_delta(cstamp);
3684 ci.tstamp = cstamp_delta(tstamp);
Thomas Graf85486af2006-09-18 00:11:24 -07003685 ci.ifa_prefered = preferred;
3686 ci.ifa_valid = valid;
3687
3688 return nla_put(skb, IFA_CACHEINFO, sizeof(ci), &ci);
3689}
3690
Thomas Graf101bb222006-09-18 00:11:52 -07003691static inline int rt_scope(int ifa_scope)
3692{
3693 if (ifa_scope & IFA_HOST)
3694 return RT_SCOPE_HOST;
3695 else if (ifa_scope & IFA_LINK)
3696 return RT_SCOPE_LINK;
3697 else if (ifa_scope & IFA_SITE)
3698 return RT_SCOPE_SITE;
3699 else
3700 return RT_SCOPE_UNIVERSE;
3701}
3702
Thomas Graf0ab68032006-09-18 00:12:35 -07003703static inline int inet6_ifaddr_msgsize(void)
3704{
Thomas Graf339bf982006-11-10 14:10:15 -08003705 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
3706 + nla_total_size(16) /* IFA_ADDRESS */
3707 + nla_total_size(sizeof(struct ifa_cacheinfo));
Thomas Graf0ab68032006-09-18 00:12:35 -07003708}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07003709
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003711 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 struct nlmsghdr *nlh;
Thomas Graf85486af2006-09-18 00:11:24 -07003714 u32 preferred, valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715
Eric W. Biederman15e47302012-09-07 20:12:54 +00003716 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Thomas Graf0ab68032006-09-18 00:12:35 -07003717 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003718 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003719
Thomas Graf101bb222006-09-18 00:11:52 -07003720 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3721 ifa->idev->dev->ifindex);
3722
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 if (!(ifa->flags&IFA_F_PERMANENT)) {
Thomas Graf85486af2006-09-18 00:11:24 -07003724 preferred = ifa->prefered_lft;
3725 valid = ifa->valid_lft;
3726 if (preferred != INFINITY_LIFE_TIME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 long tval = (jiffies - ifa->tstamp)/HZ;
Jens Rosenbooma1faa692009-06-25 04:55:50 +00003728 if (preferred > tval)
3729 preferred -= tval;
3730 else
3731 preferred = 0;
Ben Hutchingsf56619f2010-06-26 11:37:47 +00003732 if (valid != INFINITY_LIFE_TIME) {
3733 if (valid > tval)
3734 valid -= tval;
3735 else
3736 valid = 0;
3737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 }
3739 } else {
Thomas Graf85486af2006-09-18 00:11:24 -07003740 preferred = INFINITY_LIFE_TIME;
3741 valid = INFINITY_LIFE_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 }
Thomas Graf85486af2006-09-18 00:11:24 -07003743
Thomas Graf0ab68032006-09-18 00:12:35 -07003744 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0 ||
Patrick McHardy26932562007-01-31 23:16:40 -08003745 put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) {
3746 nlmsg_cancel(skb, nlh);
3747 return -EMSGSIZE;
3748 }
Thomas Graf85486af2006-09-18 00:11:24 -07003749
Thomas Graf0ab68032006-09-18 00:12:35 -07003750 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751}
3752
3753static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003754 u32 portid, u32 seq, int event, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003757 u8 scope = RT_SCOPE_UNIVERSE;
3758 int ifindex = ifmca->idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
Thomas Graf101bb222006-09-18 00:11:52 -07003760 if (ipv6_addr_scope(&ifmca->mca_addr) & IFA_SITE)
3761 scope = RT_SCOPE_SITE;
3762
Eric W. Biederman15e47302012-09-07 20:12:54 +00003763 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Thomas Graf0ab68032006-09-18 00:12:35 -07003764 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003765 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003766
Thomas Graf101bb222006-09-18 00:11:52 -07003767 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003768 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 ||
3769 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003770 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3771 nlmsg_cancel(skb, nlh);
3772 return -EMSGSIZE;
3773 }
Thomas Graf85486af2006-09-18 00:11:24 -07003774
Thomas Graf0ab68032006-09-18 00:12:35 -07003775 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776}
3777
3778static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003779 u32 portid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 struct nlmsghdr *nlh;
Thomas Graf101bb222006-09-18 00:11:52 -07003782 u8 scope = RT_SCOPE_UNIVERSE;
3783 int ifindex = ifaca->aca_idev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784
Thomas Graf101bb222006-09-18 00:11:52 -07003785 if (ipv6_addr_scope(&ifaca->aca_addr) & IFA_SITE)
3786 scope = RT_SCOPE_SITE;
3787
Eric W. Biederman15e47302012-09-07 20:12:54 +00003788 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
Thomas Graf0ab68032006-09-18 00:12:35 -07003789 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08003790 return -EMSGSIZE;
Thomas Graf0ab68032006-09-18 00:12:35 -07003791
Thomas Graf101bb222006-09-18 00:11:52 -07003792 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
Thomas Graf0ab68032006-09-18 00:12:35 -07003793 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 ||
3794 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
Patrick McHardy26932562007-01-31 23:16:40 -08003795 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
3796 nlmsg_cancel(skb, nlh);
3797 return -EMSGSIZE;
3798 }
Thomas Graf85486af2006-09-18 00:11:24 -07003799
Thomas Graf0ab68032006-09-18 00:12:35 -07003800 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801}
3802
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003803enum addr_type_t {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 UNICAST_ADDR,
3805 MULTICAST_ADDR,
3806 ANYCAST_ADDR,
3807};
3808
Eric Dumazet234b27c2009-11-12 04:11:50 +00003809/* called with rcu_read_lock() */
3810static int in6_dump_addrs(struct inet6_dev *idev, struct sk_buff *skb,
3811 struct netlink_callback *cb, enum addr_type_t type,
3812 int s_ip_idx, int *p_ip_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 struct ifmcaddr6 *ifmca;
3815 struct ifacaddr6 *ifaca;
Eric Dumazet234b27c2009-11-12 04:11:50 +00003816 int err = 1;
3817 int ip_idx = *p_ip_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
Eric Dumazet234b27c2009-11-12 04:11:50 +00003819 read_lock_bh(&idev->lock);
3820 switch (type) {
stephen hemminger502a2ff2010-03-17 20:31:13 +00003821 case UNICAST_ADDR: {
3822 struct inet6_ifaddr *ifa;
3823
Eric Dumazet234b27c2009-11-12 04:11:50 +00003824 /* unicast address incl. temp addr */
stephen hemminger502a2ff2010-03-17 20:31:13 +00003825 list_for_each_entry(ifa, &idev->addr_list, if_list) {
3826 if (++ip_idx < s_ip_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00003827 continue;
3828 err = inet6_fill_ifaddr(skb, ifa,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003829 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00003830 cb->nlh->nlmsg_seq,
3831 RTM_NEWADDR,
3832 NLM_F_MULTI);
3833 if (err <= 0)
3834 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00003836 break;
stephen hemminger502a2ff2010-03-17 20:31:13 +00003837 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00003838 case MULTICAST_ADDR:
3839 /* multicast address */
3840 for (ifmca = idev->mc_list; ifmca;
3841 ifmca = ifmca->next, ip_idx++) {
3842 if (ip_idx < s_ip_idx)
3843 continue;
3844 err = inet6_fill_ifmcaddr(skb, ifmca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003845 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00003846 cb->nlh->nlmsg_seq,
3847 RTM_GETMULTICAST,
3848 NLM_F_MULTI);
3849 if (err <= 0)
3850 break;
3851 }
3852 break;
3853 case ANYCAST_ADDR:
3854 /* anycast address */
3855 for (ifaca = idev->ac_list; ifaca;
3856 ifaca = ifaca->aca_next, ip_idx++) {
3857 if (ip_idx < s_ip_idx)
3858 continue;
3859 err = inet6_fill_ifacaddr(skb, ifaca,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003860 NETLINK_CB(cb->skb).portid,
Eric Dumazet234b27c2009-11-12 04:11:50 +00003861 cb->nlh->nlmsg_seq,
3862 RTM_GETANYCAST,
3863 NLM_F_MULTI);
3864 if (err <= 0)
3865 break;
3866 }
3867 break;
3868 default:
3869 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 }
Eric Dumazet234b27c2009-11-12 04:11:50 +00003871 read_unlock_bh(&idev->lock);
3872 *p_ip_idx = ip_idx;
3873 return err;
3874}
3875
3876static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
3877 enum addr_type_t type)
3878{
3879 struct net *net = sock_net(skb->sk);
3880 int h, s_h;
3881 int idx, ip_idx;
3882 int s_idx, s_ip_idx;
3883 struct net_device *dev;
3884 struct inet6_dev *idev;
3885 struct hlist_head *head;
Eric Dumazet234b27c2009-11-12 04:11:50 +00003886
3887 s_h = cb->args[0];
3888 s_idx = idx = cb->args[1];
3889 s_ip_idx = ip_idx = cb->args[2];
3890
3891 rcu_read_lock();
3892 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3893 idx = 0;
3894 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08003895 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet234b27c2009-11-12 04:11:50 +00003896 if (idx < s_idx)
3897 goto cont;
Patrick McHardy4b97efd2010-03-26 20:27:49 -07003898 if (h > s_h || idx > s_idx)
Eric Dumazet234b27c2009-11-12 04:11:50 +00003899 s_ip_idx = 0;
3900 ip_idx = 0;
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003901 idev = __in6_dev_get(dev);
3902 if (!idev)
Eric Dumazet234b27c2009-11-12 04:11:50 +00003903 goto cont;
3904
3905 if (in6_dump_addrs(idev, skb, cb, type,
3906 s_ip_idx, &ip_idx) <= 0)
3907 goto done;
3908cont:
3909 idx++;
3910 }
3911 }
3912done:
3913 rcu_read_unlock();
3914 cb->args[0] = h;
3915 cb->args[1] = idx;
3916 cb->args[2] = ip_idx;
3917
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 return skb->len;
3919}
3920
3921static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
3922{
3923 enum addr_type_t type = UNICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003924
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 return inet6_dump_addr(skb, cb, type);
3926}
3927
3928static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
3929{
3930 enum addr_type_t type = MULTICAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003931
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 return inet6_dump_addr(skb, cb, type);
3933}
3934
3935
3936static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
3937{
3938 enum addr_type_t type = ANYCAST_ADDR;
Denis V. Lunevb8542722007-12-01 00:21:31 +11003939
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 return inet6_dump_addr(skb, cb, type);
3941}
3942
Eldad Zack8e5e8f32012-04-01 07:49:08 +00003943static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh,
Thomas Graf1b29fc22006-09-18 00:10:50 -07003944 void *arg)
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003945{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09003946 struct net *net = sock_net(in_skb->sk);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003947 struct ifaddrmsg *ifm;
3948 struct nlattr *tb[IFA_MAX+1];
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003949 struct in6_addr *addr = NULL;
3950 struct net_device *dev = NULL;
3951 struct inet6_ifaddr *ifa;
3952 struct sk_buff *skb;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003953 int err;
3954
Thomas Graf1b29fc22006-09-18 00:10:50 -07003955 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, ifa_ipv6_policy);
3956 if (err < 0)
3957 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003958
Thomas Graf1b29fc22006-09-18 00:10:50 -07003959 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL]);
3960 if (addr == NULL) {
3961 err = -EINVAL;
3962 goto errout;
3963 }
3964
3965 ifm = nlmsg_data(nlh);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003966 if (ifm->ifa_index)
Benjamin Thery6fda7352008-03-05 10:47:47 -08003967 dev = __dev_get_by_index(net, ifm->ifa_index);
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003968
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003969 ifa = ipv6_get_ifaddr(net, addr, dev, 1);
3970 if (!ifa) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003971 err = -EADDRNOTAVAIL;
3972 goto errout;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003973 }
3974
Stephen Hemmingere21e8462010-03-20 16:09:01 -07003975 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_KERNEL);
3976 if (!skb) {
Thomas Graf1b29fc22006-09-18 00:10:50 -07003977 err = -ENOBUFS;
3978 goto errout_ifa;
3979 }
3980
Eric W. Biederman15e47302012-09-07 20:12:54 +00003981 err = inet6_fill_ifaddr(skb, ifa, NETLINK_CB(in_skb).portid,
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003982 nlh->nlmsg_seq, RTM_NEWADDR, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08003983 if (err < 0) {
3984 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
3985 WARN_ON(err == -EMSGSIZE);
3986 kfree_skb(skb);
3987 goto errout_ifa;
3988 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00003989 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003990errout_ifa:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003991 in6_ifa_put(ifa);
Thomas Graf1b29fc22006-09-18 00:10:50 -07003992errout:
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003993 return err;
Noriaki TAKAMIYA6c223822006-07-28 18:12:12 +09003994}
3995
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
3997{
3998 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09003999 struct net *net = dev_net(ifa->idev->dev);
Thomas Graf5d620262006-08-15 00:35:02 -07004000 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001
Thomas Graf0ab68032006-09-18 00:12:35 -07004002 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
Thomas Graf5d620262006-08-15 00:35:02 -07004003 if (skb == NULL)
4004 goto errout;
4005
4006 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004007 if (err < 0) {
4008 /* -EMSGSIZE implies BUG in inet6_ifaddr_msgsize() */
4009 WARN_ON(err == -EMSGSIZE);
4010 kfree_skb(skb);
4011 goto errout;
4012 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004013 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
4014 return;
Thomas Graf5d620262006-08-15 00:35:02 -07004015errout:
4016 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004017 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018}
4019
Dave Jonesb6f99a22007-03-22 12:27:49 -07004020static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 __s32 *array, int bytes)
4022{
Thomas Graf04561c12006-11-14 19:53:58 -08004023 BUG_ON(bytes < (DEVCONF_MAX * 4));
4024
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 memset(array, 0, bytes);
4026 array[DEVCONF_FORWARDING] = cnf->forwarding;
4027 array[DEVCONF_HOPLIMIT] = cnf->hop_limit;
4028 array[DEVCONF_MTU6] = cnf->mtu6;
4029 array[DEVCONF_ACCEPT_RA] = cnf->accept_ra;
4030 array[DEVCONF_ACCEPT_REDIRECTS] = cnf->accept_redirects;
4031 array[DEVCONF_AUTOCONF] = cnf->autoconf;
4032 array[DEVCONF_DAD_TRANSMITS] = cnf->dad_transmits;
4033 array[DEVCONF_RTR_SOLICITS] = cnf->rtr_solicits;
Thomas Graf93908d12010-11-17 01:44:24 +00004034 array[DEVCONF_RTR_SOLICIT_INTERVAL] =
4035 jiffies_to_msecs(cnf->rtr_solicit_interval);
4036 array[DEVCONF_RTR_SOLICIT_DELAY] =
4037 jiffies_to_msecs(cnf->rtr_solicit_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 array[DEVCONF_FORCE_MLD_VERSION] = cnf->force_mld_version;
4039#ifdef CONFIG_IPV6_PRIVACY
4040 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4041 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4042 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4043 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4044 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
4045#endif
4046 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004047 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004048 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004049#ifdef CONFIG_IPV6_ROUTER_PREF
4050 array[DEVCONF_ACCEPT_RA_RTR_PREF] = cnf->accept_ra_rtr_pref;
Thomas Graf93908d12010-11-17 01:44:24 +00004051 array[DEVCONF_RTR_PROBE_INTERVAL] =
4052 jiffies_to_msecs(cnf->rtr_probe_interval);
Neil Hormanfa03ef32007-01-30 14:30:10 -08004053#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004054 array[DEVCONF_ACCEPT_RA_RT_INFO_MAX_PLEN] = cnf->accept_ra_rt_info_max_plen;
4055#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004056#endif
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004057 array[DEVCONF_PROXY_NDP] = cnf->proxy_ndp;
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004058 array[DEVCONF_ACCEPT_SOURCE_ROUTE] = cnf->accept_source_route;
Neil Horman95c385b2007-04-25 17:08:10 -07004059#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4060 array[DEVCONF_OPTIMISTIC_DAD] = cnf->optimistic_dad;
4061#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004062#ifdef CONFIG_IPV6_MROUTE
4063 array[DEVCONF_MC_FORWARDING] = cnf->mc_forwarding;
4064#endif
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09004065 array[DEVCONF_DISABLE_IPV6] = cnf->disable_ipv6;
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09004066 array[DEVCONF_ACCEPT_DAD] = cnf->accept_dad;
Cosmin Ratiuc3faca02009-10-09 03:11:14 +00004067 array[DEVCONF_FORCE_TLLAO] = cnf->force_tllao;
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00004068 array[DEVCONF_NDISC_NOTIFY] = cnf->ndisc_notify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069}
4070
Thomas Grafb382b192010-11-16 04:33:57 +00004071static inline size_t inet6_ifla6_size(void)
4072{
4073 return nla_total_size(4) /* IFLA_INET6_FLAGS */
4074 + nla_total_size(sizeof(struct ifla_cacheinfo))
4075 + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
4076 + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
4077 + nla_total_size(ICMP6_MIB_MAX * 8); /* IFLA_INET6_ICMP6STATS */
4078}
4079
Thomas Graf339bf982006-11-10 14:10:15 -08004080static inline size_t inet6_if_nlmsg_size(void)
4081{
4082 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4083 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
4084 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
4085 + nla_total_size(4) /* IFLA_MTU */
4086 + nla_total_size(4) /* IFLA_LINK */
Thomas Grafb382b192010-11-16 04:33:57 +00004087 + nla_total_size(inet6_ifla6_size()); /* IFLA_PROTINFO */
Thomas Graf339bf982006-11-10 14:10:15 -08004088}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004089
Eric Dumazetbe281e52011-05-19 01:14:23 +00004090static inline void __snmp6_fill_statsdev(u64 *stats, atomic_long_t *mib,
Tejun Heo7d720c32010-02-16 15:20:26 +00004091 int items, int bytes)
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004092{
4093 int i;
4094 int pad = bytes - sizeof(u64) * items;
4095 BUG_ON(pad < 0);
4096
4097 /* Use put_unaligned() because stats may not be aligned for u64. */
4098 put_unaligned(items, &stats[0]);
4099 for (i = 1; i < items; i++)
Eric Dumazetbe281e52011-05-19 01:14:23 +00004100 put_unaligned(atomic_long_read(&mib[i]), &stats[i]);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004101
4102 memset(&stats[items], 0, pad);
4103}
4104
Eric Dumazet4ce3c182010-06-30 13:31:19 -07004105static inline void __snmp6_fill_stats64(u64 *stats, void __percpu **mib,
4106 int items, int bytes, size_t syncpoff)
4107{
4108 int i;
4109 int pad = bytes - sizeof(u64) * items;
4110 BUG_ON(pad < 0);
4111
4112 /* Use put_unaligned() because stats may not be aligned for u64. */
4113 put_unaligned(items, &stats[0]);
4114 for (i = 1; i < items; i++)
4115 put_unaligned(snmp_fold_field64(mib, i, syncpoff), &stats[i]);
4116
4117 memset(&stats[items], 0, pad);
4118}
4119
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004120static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
4121 int bytes)
4122{
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004123 switch (attrtype) {
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004124 case IFLA_INET6_STATS:
Eric Dumazet4ce3c182010-06-30 13:31:19 -07004125 __snmp6_fill_stats64(stats, (void __percpu **)idev->stats.ipv6,
4126 IPSTATS_MIB_MAX, bytes, offsetof(struct ipstats_mib, syncp));
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004127 break;
4128 case IFLA_INET6_ICMP6STATS:
Eric Dumazetbe281e52011-05-19 01:14:23 +00004129 __snmp6_fill_statsdev(stats, idev->stats.icmpv6dev->mibs, ICMP6_MIB_MAX, bytes);
Herbert Xu7f7d9a62007-04-24 21:54:09 -07004130 break;
4131 }
4132}
4133
Thomas Grafb382b192010-11-16 04:33:57 +00004134static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135{
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004136 struct nlattr *nla;
Thomas Graf04561c12006-11-14 19:53:58 -08004137 struct ifla_cacheinfo ci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138
David S. Millerc78679e2012-04-01 20:27:33 -04004139 if (nla_put_u32(skb, IFLA_INET6_FLAGS, idev->if_flags))
4140 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 ci.max_reasm_len = IPV6_MAXPLEN;
David S. Miller24912422010-11-19 13:13:47 -08004142 ci.tstamp = cstamp_delta(idev->tstamp);
4143 ci.reachable_time = jiffies_to_msecs(idev->nd_parms->reachable_time);
4144 ci.retrans_time = jiffies_to_msecs(idev->nd_parms->retrans_time);
David S. Millerc78679e2012-04-01 20:27:33 -04004145 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
4146 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004147 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
4148 if (nla == NULL)
Thomas Graf04561c12006-11-14 19:53:58 -08004149 goto nla_put_failure;
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004150 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
YOSHIFUJI Hideakibf99f1b2007-04-20 15:56:20 -07004152 /* XXX - MC not implemented */
4153
4154 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
4155 if (nla == NULL)
4156 goto nla_put_failure;
4157 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4158
4159 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4160 if (nla == NULL)
4161 goto nla_put_failure;
4162 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
Thomas Grafb382b192010-11-16 04:33:57 +00004164 return 0;
4165
4166nla_put_failure:
4167 return -EMSGSIZE;
4168}
4169
4170static size_t inet6_get_link_af_size(const struct net_device *dev)
4171{
4172 if (!__in6_dev_get(dev))
4173 return 0;
4174
4175 return inet6_ifla6_size();
4176}
4177
4178static int inet6_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
4179{
4180 struct inet6_dev *idev = __in6_dev_get(dev);
4181
4182 if (!idev)
4183 return -ENODATA;
4184
4185 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4186 return -EMSGSIZE;
4187
4188 return 0;
4189}
4190
Thomas Grafb382b192010-11-16 04:33:57 +00004191static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004192 u32 portid, u32 seq, int event, unsigned int flags)
Thomas Grafb382b192010-11-16 04:33:57 +00004193{
4194 struct net_device *dev = idev->dev;
4195 struct ifinfomsg *hdr;
4196 struct nlmsghdr *nlh;
4197 void *protoinfo;
4198
Eric W. Biederman15e47302012-09-07 20:12:54 +00004199 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
Thomas Grafb382b192010-11-16 04:33:57 +00004200 if (nlh == NULL)
4201 return -EMSGSIZE;
4202
4203 hdr = nlmsg_data(nlh);
4204 hdr->ifi_family = AF_INET6;
4205 hdr->__ifi_pad = 0;
4206 hdr->ifi_type = dev->type;
4207 hdr->ifi_index = dev->ifindex;
4208 hdr->ifi_flags = dev_get_flags(dev);
4209 hdr->ifi_change = 0;
4210
David S. Millerc78679e2012-04-01 20:27:33 -04004211 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4212 (dev->addr_len &&
4213 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4214 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4215 (dev->ifindex != dev->iflink &&
4216 nla_put_u32(skb, IFLA_LINK, dev->iflink)))
4217 goto nla_put_failure;
Thomas Grafb382b192010-11-16 04:33:57 +00004218 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4219 if (protoinfo == NULL)
4220 goto nla_put_failure;
4221
4222 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
4223 goto nla_put_failure;
4224
Thomas Graf04561c12006-11-14 19:53:58 -08004225 nla_nest_end(skb, protoinfo);
4226 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
Thomas Graf04561c12006-11-14 19:53:58 -08004228nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004229 nlmsg_cancel(skb, nlh);
4230 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231}
4232
4233static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
4234{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09004235 struct net *net = sock_net(skb->sk);
Eric Dumazet84d26972009-11-09 12:11:28 +00004236 int h, s_h;
David S. Miller434a8a582009-11-11 18:53:00 -08004237 int idx = 0, s_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 struct net_device *dev;
4239 struct inet6_dev *idev;
Eric Dumazet84d26972009-11-09 12:11:28 +00004240 struct hlist_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
Eric Dumazet84d26972009-11-09 12:11:28 +00004242 s_h = cb->args[0];
4243 s_idx = cb->args[1];
4244
4245 rcu_read_lock();
4246 for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4247 idx = 0;
4248 head = &net->dev_index_head[h];
Sasha Levinb67bfe02013-02-27 17:06:00 -08004249 hlist_for_each_entry_rcu(dev, head, index_hlist) {
Eric Dumazet84d26972009-11-09 12:11:28 +00004250 if (idx < s_idx)
4251 goto cont;
4252 idev = __in6_dev_get(dev);
4253 if (!idev)
4254 goto cont;
4255 if (inet6_fill_ifinfo(skb, idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004256 NETLINK_CB(cb->skb).portid,
Eric Dumazet84d26972009-11-09 12:11:28 +00004257 cb->nlh->nlmsg_seq,
4258 RTM_NEWLINK, NLM_F_MULTI) <= 0)
4259 goto out;
Pavel Emelianov7562f872007-05-03 15:13:45 -07004260cont:
Eric Dumazet84d26972009-11-09 12:11:28 +00004261 idx++;
4262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 }
Eric Dumazet84d26972009-11-09 12:11:28 +00004264out:
4265 rcu_read_unlock();
4266 cb->args[1] = idx;
4267 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
4269 return skb->len;
4270}
4271
4272void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
4273{
4274 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004275 struct net *net = dev_net(idev->dev);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004276 int err = -ENOBUFS;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004277
Thomas Graf339bf982006-11-10 14:10:15 -08004278 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004279 if (skb == NULL)
4280 goto errout;
4281
4282 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004283 if (err < 0) {
4284 /* -EMSGSIZE implies BUG in inet6_if_nlmsg_size() */
4285 WARN_ON(err == -EMSGSIZE);
4286 kfree_skb(skb);
4287 goto errout;
4288 }
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00004289 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFINFO, NULL, GFP_ATOMIC);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004290 return;
Thomas Graf8d7a76c2006-08-15 00:35:47 -07004291errout:
4292 if (err < 0)
Nicolas Dichtel5f75a102010-12-07 23:38:31 +00004293 rtnl_set_sk_err(net, RTNLGRP_IPV6_IFINFO, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294}
4295
Thomas Graf339bf982006-11-10 14:10:15 -08004296static inline size_t inet6_prefix_nlmsg_size(void)
4297{
4298 return NLMSG_ALIGN(sizeof(struct prefixmsg))
4299 + nla_total_size(sizeof(struct in6_addr))
4300 + nla_total_size(sizeof(struct prefix_cacheinfo));
4301}
YOSHIFUJI Hideakic5396a32006-06-17 22:48:48 -07004302
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004304 struct prefix_info *pinfo, u32 portid, u32 seq,
Thomas Graf6051e2f2006-11-14 19:54:19 -08004305 int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306{
Thomas Graf6051e2f2006-11-14 19:54:19 -08004307 struct prefixmsg *pmsg;
4308 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 struct prefix_cacheinfo ci;
4310
Eric W. Biederman15e47302012-09-07 20:12:54 +00004311 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
Thomas Graf6051e2f2006-11-14 19:54:19 -08004312 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -08004313 return -EMSGSIZE;
Thomas Graf6051e2f2006-11-14 19:54:19 -08004314
4315 pmsg = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 pmsg->prefix_family = AF_INET6;
Patrick McHardy8a470772005-06-28 12:56:45 -07004317 pmsg->prefix_pad1 = 0;
4318 pmsg->prefix_pad2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 pmsg->prefix_ifindex = idev->dev->ifindex;
4320 pmsg->prefix_len = pinfo->prefix_len;
4321 pmsg->prefix_type = pinfo->type;
Patrick McHardy8a470772005-06-28 12:56:45 -07004322 pmsg->prefix_pad3 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 pmsg->prefix_flags = 0;
4324 if (pinfo->onlink)
4325 pmsg->prefix_flags |= IF_PREFIX_ONLINK;
4326 if (pinfo->autoconf)
4327 pmsg->prefix_flags |= IF_PREFIX_AUTOCONF;
4328
David S. Millerc78679e2012-04-01 20:27:33 -04004329 if (nla_put(skb, PREFIX_ADDRESS, sizeof(pinfo->prefix), &pinfo->prefix))
4330 goto nla_put_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 ci.preferred_time = ntohl(pinfo->prefered);
4332 ci.valid_time = ntohl(pinfo->valid);
David S. Millerc78679e2012-04-01 20:27:33 -04004333 if (nla_put(skb, PREFIX_CACHEINFO, sizeof(ci), &ci))
4334 goto nla_put_failure;
Thomas Graf6051e2f2006-11-14 19:54:19 -08004335 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
Thomas Graf6051e2f2006-11-14 19:54:19 -08004337nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -08004338 nlmsg_cancel(skb, nlh);
4339 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340}
4341
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004342static void inet6_prefix_notify(int event, struct inet6_dev *idev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 struct prefix_info *pinfo)
4344{
4345 struct sk_buff *skb;
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004346 struct net *net = dev_net(idev->dev);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004347 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
Thomas Graf339bf982006-11-10 14:10:15 -08004349 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004350 if (skb == NULL)
4351 goto errout;
4352
4353 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -08004354 if (err < 0) {
4355 /* -EMSGSIZE implies BUG in inet6_prefix_nlmsg_size() */
4356 WARN_ON(err == -EMSGSIZE);
4357 kfree_skb(skb);
4358 goto errout;
4359 }
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08004360 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
4361 return;
Thomas Graf8c384bfa2006-08-15 00:36:07 -07004362errout:
4363 if (err < 0)
Benjamin Thery6fda7352008-03-05 10:47:47 -08004364 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365}
4366
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4368{
4369 inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
4370
4371 switch (event) {
4372 case RTM_NEWADDR:
Neil Horman95c385b2007-04-25 17:08:10 -07004373 /*
4374 * If the address was optimistic
4375 * we inserted the route at the start of
4376 * our DAD process, so we don't need
4377 * to do it again
4378 */
4379 if (!(ifp->rt->rt6i_node))
4380 ip6_ins_rt(ifp->rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 if (ifp->idev->cnf.forwarding)
4382 addrconf_join_anycast(ifp);
4383 break;
4384 case RTM_DELADDR:
4385 if (ifp->idev->cnf.forwarding)
4386 addrconf_leave_anycast(ifp);
4387 addrconf_leave_solict(ifp->idev, &ifp->addr);
Changli Gaod8d1f302010-06-10 23:31:35 -07004388 dst_hold(&ifp->rt->dst);
stephen hemminger93fa1592010-04-12 05:41:31 +00004389
David S. Miller73a8bd72011-01-23 23:27:15 -08004390 if (ip6_del_rt(ifp->rt))
Changli Gaod8d1f302010-06-10 23:31:35 -07004391 dst_free(&ifp->rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 break;
4393 }
4394}
4395
4396static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4397{
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07004398 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 if (likely(ifp->idev->dead == 0))
4400 __ipv6_ifa_notify(event, ifp);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -07004401 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402}
4403
4404#ifdef CONFIG_SYSCTL
4405
4406static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004407int addrconf_sysctl_forward(ctl_table *ctl, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 void __user *buffer, size_t *lenp, loff_t *ppos)
4409{
4410 int *valp = ctl->data;
4411 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004412 loff_t pos = *ppos;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004413 ctl_table lctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 int ret;
4415
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004416 /*
4417 * ctl->data points to idev->cnf.forwarding, we should
4418 * not modify it until we get the rtnl lock.
4419 */
4420 lctl = *ctl;
4421 lctl.data = &val;
4422
4423 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
Pavel Emelyanovc8fecf22007-12-05 01:50:24 -08004425 if (write)
Stephen Hemmingerb325fdd2009-02-26 06:55:31 +00004426 ret = addrconf_fixup_forwarding(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00004427 if (ret)
4428 *ppos = pos;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004429 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430}
4431
Brian Haley56d417b2009-06-01 03:07:33 -07004432static void dev_disable_change(struct inet6_dev *idev)
4433{
4434 if (!idev || !idev->dev)
4435 return;
4436
4437 if (idev->cnf.disable_ipv6)
4438 addrconf_notify(NULL, NETDEV_DOWN, idev->dev);
4439 else
4440 addrconf_notify(NULL, NETDEV_UP, idev->dev);
4441}
4442
4443static void addrconf_disable_change(struct net *net, __s32 newf)
4444{
4445 struct net_device *dev;
4446 struct inet6_dev *idev;
4447
Eric Dumazetc6d14c82009-11-04 05:43:23 -08004448 rcu_read_lock();
4449 for_each_netdev_rcu(net, dev) {
Brian Haley56d417b2009-06-01 03:07:33 -07004450 idev = __in6_dev_get(dev);
4451 if (idev) {
4452 int changed = (!idev->cnf.disable_ipv6) ^ (!newf);
4453 idev->cnf.disable_ipv6 = newf;
4454 if (changed)
4455 dev_disable_change(idev);
4456 }
Brian Haley56d417b2009-06-01 03:07:33 -07004457 }
Eric Dumazetc6d14c82009-11-04 05:43:23 -08004458 rcu_read_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07004459}
4460
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004461static int addrconf_disable_ipv6(struct ctl_table *table, int *p, int newf)
Brian Haley56d417b2009-06-01 03:07:33 -07004462{
4463 struct net *net;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004464 int old;
4465
4466 if (!rtnl_trylock())
4467 return restart_syscall();
Brian Haley56d417b2009-06-01 03:07:33 -07004468
4469 net = (struct net *)table->extra2;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004470 old = *p;
4471 *p = newf;
Brian Haley56d417b2009-06-01 03:07:33 -07004472
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004473 if (p == &net->ipv6.devconf_dflt->disable_ipv6) {
4474 rtnl_unlock();
Brian Haley56d417b2009-06-01 03:07:33 -07004475 return 0;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004476 }
Brian Haley56d417b2009-06-01 03:07:33 -07004477
4478 if (p == &net->ipv6.devconf_all->disable_ipv6) {
Brian Haley56d417b2009-06-01 03:07:33 -07004479 net->ipv6.devconf_dflt->disable_ipv6 = newf;
4480 addrconf_disable_change(net, newf);
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004481 } else if ((!newf) ^ (!old))
Brian Haley56d417b2009-06-01 03:07:33 -07004482 dev_disable_change((struct inet6_dev *)table->extra1);
4483
4484 rtnl_unlock();
4485 return 0;
4486}
4487
4488static
Alexey Dobriyan8d65af72009-09-23 15:57:19 -07004489int addrconf_sysctl_disable(ctl_table *ctl, int write,
Brian Haley56d417b2009-06-01 03:07:33 -07004490 void __user *buffer, size_t *lenp, loff_t *ppos)
4491{
4492 int *valp = ctl->data;
4493 int val = *valp;
Eric W. Biederman88af1822010-02-19 13:22:59 +00004494 loff_t pos = *ppos;
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004495 ctl_table lctl;
Brian Haley56d417b2009-06-01 03:07:33 -07004496 int ret;
4497
Francesco Ruggeri013d97e2012-01-16 10:40:10 +00004498 /*
4499 * ctl->data points to idev->cnf.disable_ipv6, we should
4500 * not modify it until we get the rtnl lock.
4501 */
4502 lctl = *ctl;
4503 lctl.data = &val;
4504
4505 ret = proc_dointvec(&lctl, write, buffer, lenp, ppos);
Brian Haley56d417b2009-06-01 03:07:33 -07004506
4507 if (write)
4508 ret = addrconf_disable_ipv6(ctl, valp, val);
Eric W. Biederman88af1822010-02-19 13:22:59 +00004509 if (ret)
4510 *ppos = pos;
Brian Haley56d417b2009-06-01 03:07:33 -07004511 return ret;
4512}
4513
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514static struct addrconf_sysctl_table
4515{
4516 struct ctl_table_header *sysctl_header;
YOSHIFUJI Hideakif6a07b22008-03-25 00:25:11 +09004517 ctl_table addrconf_vars[DEVCONF_MAX+1];
Brian Haleyab32ea52006-09-22 14:15:41 -07004518} addrconf_sysctl __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 .sysctl_header = NULL,
4520 .addrconf_vars = {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004521 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004522 .procname = "forwarding",
4523 .data = &ipv6_devconf.forwarding,
4524 .maxlen = sizeof(int),
4525 .mode = 0644,
4526 .proc_handler = addrconf_sysctl_forward,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 },
4528 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004529 .procname = "hop_limit",
4530 .data = &ipv6_devconf.hop_limit,
4531 .maxlen = sizeof(int),
4532 .mode = 0644,
4533 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 },
4535 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004536 .procname = "mtu",
4537 .data = &ipv6_devconf.mtu6,
4538 .maxlen = sizeof(int),
4539 .mode = 0644,
4540 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 },
4542 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004543 .procname = "accept_ra",
4544 .data = &ipv6_devconf.accept_ra,
4545 .maxlen = sizeof(int),
4546 .mode = 0644,
4547 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 },
4549 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004550 .procname = "accept_redirects",
4551 .data = &ipv6_devconf.accept_redirects,
4552 .maxlen = sizeof(int),
4553 .mode = 0644,
4554 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 },
4556 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004557 .procname = "autoconf",
4558 .data = &ipv6_devconf.autoconf,
4559 .maxlen = sizeof(int),
4560 .mode = 0644,
4561 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 },
4563 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004564 .procname = "dad_transmits",
4565 .data = &ipv6_devconf.dad_transmits,
4566 .maxlen = sizeof(int),
4567 .mode = 0644,
4568 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 },
4570 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004571 .procname = "router_solicitations",
4572 .data = &ipv6_devconf.rtr_solicits,
4573 .maxlen = sizeof(int),
4574 .mode = 0644,
4575 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 },
4577 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004578 .procname = "router_solicitation_interval",
4579 .data = &ipv6_devconf.rtr_solicit_interval,
4580 .maxlen = sizeof(int),
4581 .mode = 0644,
4582 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 },
4584 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004585 .procname = "router_solicitation_delay",
4586 .data = &ipv6_devconf.rtr_solicit_delay,
4587 .maxlen = sizeof(int),
4588 .mode = 0644,
4589 .proc_handler = proc_dointvec_jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 },
4591 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004592 .procname = "force_mld_version",
4593 .data = &ipv6_devconf.force_mld_version,
4594 .maxlen = sizeof(int),
4595 .mode = 0644,
4596 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 },
4598#ifdef CONFIG_IPV6_PRIVACY
4599 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004600 .procname = "use_tempaddr",
4601 .data = &ipv6_devconf.use_tempaddr,
4602 .maxlen = sizeof(int),
4603 .mode = 0644,
4604 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 },
4606 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004607 .procname = "temp_valid_lft",
4608 .data = &ipv6_devconf.temp_valid_lft,
4609 .maxlen = sizeof(int),
4610 .mode = 0644,
4611 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 },
4613 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004614 .procname = "temp_prefered_lft",
4615 .data = &ipv6_devconf.temp_prefered_lft,
4616 .maxlen = sizeof(int),
4617 .mode = 0644,
4618 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 },
4620 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004621 .procname = "regen_max_retry",
4622 .data = &ipv6_devconf.regen_max_retry,
4623 .maxlen = sizeof(int),
4624 .mode = 0644,
4625 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 },
4627 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004628 .procname = "max_desync_factor",
4629 .data = &ipv6_devconf.max_desync_factor,
4630 .maxlen = sizeof(int),
4631 .mode = 0644,
4632 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 },
4634#endif
4635 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004636 .procname = "max_addresses",
4637 .data = &ipv6_devconf.max_addresses,
4638 .maxlen = sizeof(int),
4639 .mode = 0644,
4640 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 },
4642 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004643 .procname = "accept_ra_defrtr",
4644 .data = &ipv6_devconf.accept_ra_defrtr,
4645 .maxlen = sizeof(int),
4646 .mode = 0644,
4647 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki65f5c7c2006-03-20 16:55:08 -08004648 },
4649 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004650 .procname = "accept_ra_pinfo",
4651 .data = &ipv6_devconf.accept_ra_pinfo,
4652 .maxlen = sizeof(int),
4653 .mode = 0644,
4654 .proc_handler = proc_dointvec,
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004655 },
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004656#ifdef CONFIG_IPV6_ROUTER_PREF
4657 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004658 .procname = "accept_ra_rtr_pref",
4659 .data = &ipv6_devconf.accept_ra_rtr_pref,
4660 .maxlen = sizeof(int),
4661 .mode = 0644,
4662 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004663 },
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08004664 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004665 .procname = "router_probe_interval",
4666 .data = &ipv6_devconf.rtr_probe_interval,
4667 .maxlen = sizeof(int),
4668 .mode = 0644,
4669 .proc_handler = proc_dointvec_jiffies,
YOSHIFUJI Hideaki52e16352006-03-20 17:05:47 -08004670 },
Neil Hormanfa03ef32007-01-30 14:30:10 -08004671#ifdef CONFIG_IPV6_ROUTE_INFO
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004672 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004673 .procname = "accept_ra_rt_info_max_plen",
4674 .data = &ipv6_devconf.accept_ra_rt_info_max_plen,
4675 .maxlen = sizeof(int),
4676 .mode = 0644,
4677 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki09c884d2006-03-20 17:07:03 -08004678 },
4679#endif
YOSHIFUJI Hideaki930d6ff2006-03-20 17:05:30 -08004680#endif
YOSHIFUJI Hideakic4fd30e2006-03-20 16:55:26 -08004681 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004682 .procname = "proxy_ndp",
4683 .data = &ipv6_devconf.proxy_ndp,
4684 .maxlen = sizeof(int),
4685 .mode = 0644,
4686 .proc_handler = proc_dointvec,
YOSHIFUJI Hideakifbea49e2006-09-22 14:43:49 -07004687 },
4688 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004689 .procname = "accept_source_route",
4690 .data = &ipv6_devconf.accept_source_route,
4691 .maxlen = sizeof(int),
4692 .mode = 0644,
4693 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004694 },
Neil Horman95c385b2007-04-25 17:08:10 -07004695#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
4696 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004697 .procname = "optimistic_dad",
4698 .data = &ipv6_devconf.optimistic_dad,
4699 .maxlen = sizeof(int),
4700 .mode = 0644,
4701 .proc_handler = proc_dointvec,
Neil Horman95c385b2007-04-25 17:08:10 -07004702
4703 },
4704#endif
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004705#ifdef CONFIG_IPV6_MROUTE
4706 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004707 .procname = "mc_forwarding",
4708 .data = &ipv6_devconf.mc_forwarding,
4709 .maxlen = sizeof(int),
4710 .mode = 0444,
4711 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki7bc570c2008-04-03 09:22:53 +09004712 },
4713#endif
YOSHIFUJI Hideaki0bcbc922007-04-24 14:58:30 -07004714 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004715 .procname = "disable_ipv6",
4716 .data = &ipv6_devconf.disable_ipv6,
4717 .maxlen = sizeof(int),
4718 .mode = 0644,
4719 .proc_handler = addrconf_sysctl_disable,
YOSHIFUJI Hideaki778d80b2008-06-28 14:17:11 +09004720 },
4721 {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004722 .procname = "accept_dad",
4723 .data = &ipv6_devconf.accept_dad,
4724 .maxlen = sizeof(int),
4725 .mode = 0644,
4726 .proc_handler = proc_dointvec,
YOSHIFUJI Hideaki1b34be72008-06-28 14:18:38 +09004727 },
4728 {
Octavian Purdilaf7734fd2009-10-02 11:39:15 +00004729 .procname = "force_tllao",
4730 .data = &ipv6_devconf.force_tllao,
4731 .maxlen = sizeof(int),
4732 .mode = 0644,
4733 .proc_handler = proc_dointvec
4734 },
4735 {
Hannes Frederic Sowa5cb04432012-11-06 16:46:20 +00004736 .procname = "ndisc_notify",
4737 .data = &ipv6_devconf.ndisc_notify,
4738 .maxlen = sizeof(int),
4739 .mode = 0644,
4740 .proc_handler = proc_dointvec
4741 },
4742 {
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004743 /* sentinel */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 }
4745 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746};
4747
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004748static int __addrconf_sysctl_register(struct net *net, char *dev_name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004749 struct inet6_dev *idev, struct ipv6_devconf *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750{
4751 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 struct addrconf_sysctl_table *t;
Eric W. Biederman6105e292012-04-19 13:41:24 +00004753 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
Pavel Emelyanov1dab6222007-12-02 00:59:38 +11004754
Arnaldo Carvalho de Meloaf879cc2006-11-17 12:14:37 -02004755 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 if (t == NULL)
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004757 goto out;
4758
Stephen Hemmingerbcdd5532010-03-20 16:08:18 -07004759 for (i = 0; t->addrconf_vars[i].data; i++) {
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004760 t->addrconf_vars[i].data += (char *)p - (char *)&ipv6_devconf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
Pavel Emelyanovbff16c22008-01-10 17:42:13 -08004762 t->addrconf_vars[i].extra2 = net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764
Eric W. Biederman6105e292012-04-19 13:41:24 +00004765 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
Eric W. Biederman6105e292012-04-19 13:41:24 +00004767 t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 if (t->sysctl_header == NULL)
Eric W. Biederman6105e292012-04-19 13:41:24 +00004769 goto free;
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004770
4771 p->sysctl = t;
Pavel Emelyanov95897312008-01-10 17:41:45 -08004772 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004774free:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 kfree(t);
Pavel Emelyanovf68635e2007-12-02 00:21:52 +11004776out:
Pavel Emelyanov95897312008-01-10 17:41:45 -08004777 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778}
4779
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004780static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
4781{
4782 struct addrconf_sysctl_table *t;
4783
4784 if (p->sysctl == NULL)
4785 return;
4786
4787 t = p->sysctl;
4788 p->sysctl = NULL;
Lucian Adrian Grijincuff538812011-05-01 01:44:01 +00004789 unregister_net_sysctl_table(t->sysctl_header);
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004790 kfree(t);
4791}
4792
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004793static void addrconf_sysctl_register(struct inet6_dev *idev)
4794{
Eric W. Biederman54716e32010-02-14 03:27:03 +00004795 neigh_sysctl_register(idev->dev, idev->nd_parms, "ipv6",
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004796 &ndisc_ifinfo_sysctl_change);
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09004797 __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004798 idev, &idev->cnf);
Pavel Emelyanovf52295a2007-12-02 00:58:37 +11004799}
4800
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004801static void addrconf_sysctl_unregister(struct inet6_dev *idev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802{
Pavel Emelyanov408c4762008-01-10 17:41:21 -08004803 __addrconf_sysctl_unregister(&idev->cnf);
4804 neigh_sysctl_unregister(idev->nd_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805}
4806
4807
4808#endif
4809
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004810static int __net_init addrconf_init_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004811{
Hong Zhiguoa79ca222013-03-26 01:52:45 +08004812 int err = -ENOMEM;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004813 struct ipv6_devconf *all, *dflt;
4814
Hong Zhiguoa79ca222013-03-26 01:52:45 +08004815 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
4816 if (all == NULL)
4817 goto err_alloc_all;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004818
Hong Zhiguoa79ca222013-03-26 01:52:45 +08004819 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
4820 if (dflt == NULL)
4821 goto err_alloc_dflt;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004822
Hong Zhiguoa79ca222013-03-26 01:52:45 +08004823 /* these will be inherited by all namespaces */
4824 dflt->autoconf = ipv6_defaults.autoconf;
4825 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004826
4827 net->ipv6.devconf_all = all;
4828 net->ipv6.devconf_dflt = dflt;
4829
4830#ifdef CONFIG_SYSCTL
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004831 err = __addrconf_sysctl_register(net, "all", NULL, all);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004832 if (err < 0)
4833 goto err_reg_all;
4834
Eric W. Biedermanf8572d82009-11-05 13:32:03 -08004835 err = __addrconf_sysctl_register(net, "default", NULL, dflt);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004836 if (err < 0)
4837 goto err_reg_dflt;
4838#endif
4839 return 0;
4840
4841#ifdef CONFIG_SYSCTL
4842err_reg_dflt:
4843 __addrconf_sysctl_unregister(all);
4844err_reg_all:
4845 kfree(dflt);
4846#endif
4847err_alloc_dflt:
4848 kfree(all);
4849err_alloc_all:
4850 return err;
4851}
4852
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00004853static void __net_exit addrconf_exit_net(struct net *net)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004854{
4855#ifdef CONFIG_SYSCTL
4856 __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
4857 __addrconf_sysctl_unregister(net->ipv6.devconf_all);
4858#endif
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08004859 if (!net_eq(net, &init_net)) {
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004860 kfree(net->ipv6.devconf_dflt);
4861 kfree(net->ipv6.devconf_all);
4862 }
4863}
4864
4865static struct pernet_operations addrconf_ops = {
4866 .init = addrconf_init_net,
4867 .exit = addrconf_exit_net,
4868};
4869
Thomas Grafb382b192010-11-16 04:33:57 +00004870static struct rtnl_af_ops inet6_ops = {
4871 .family = AF_INET6,
4872 .fill_link_af = inet6_fill_link_af,
4873 .get_link_af_size = inet6_get_link_af_size,
Thomas Grafb382b192010-11-16 04:33:57 +00004874};
4875
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876/*
4877 * Init / cleanup code
4878 */
4879
4880int __init addrconf_init(void)
4881{
stephen hemmingerc2e21292010-03-17 20:31:10 +00004882 int i, err;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004883
Stephen Hemmingere21e8462010-03-20 16:09:01 -07004884 err = ipv6_addr_label_init();
4885 if (err < 0) {
Joe Perchesf3213832012-05-15 14:11:53 +00004886 pr_crit("%s: cannot initialize default policy table: %d\n",
4887 __func__, err);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004888 goto out;
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004891 err = register_pernet_subsys(&addrconf_ops);
4892 if (err < 0)
4893 goto out_addrlabel;
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004894
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 /* The addrconf netdev notifier requires that loopback_dev
4896 * has it's ipv6 private information allocated and setup
4897 * before it can bring up and give link-local addresses
4898 * to other devices which are up.
4899 *
4900 * Unfortunately, loopback_dev is not necessarily the first
4901 * entry in the global dev_base list of net devices. In fact,
4902 * it is likely to be the very last entry on that list.
4903 * So this causes the notifier registry below to try and
4904 * give link-local addresses to all devices besides loopback_dev
4905 * first, then loopback_dev, which cases all the non-loopback_dev
4906 * devices to fail to get a link-local address.
4907 *
4908 * So, as a temporary fix, allocate the ipv6 structure for
4909 * loopback_dev first by hand.
4910 * Longer term, all of the dependencies ipv6 has upon the loopback
4911 * device and it being up should be removed.
4912 */
4913 rtnl_lock();
Eric W. Biederman2774c7a2007-09-26 22:10:56 -07004914 if (!ipv6_add_dev(init_net.loopback_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 err = -ENOMEM;
4916 rtnl_unlock();
4917 if (err)
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004918 goto errlo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919
stephen hemmingerc2e21292010-03-17 20:31:10 +00004920 for (i = 0; i < IN6_ADDR_HSIZE; i++)
4921 INIT_HLIST_HEAD(&inet6_addr_lst[i]);
4922
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 register_netdevice_notifier(&ipv6_dev_notf);
4924
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 addrconf_verify(0);
Thomas Grafc127ea22007-03-22 11:58:32 -07004926
Thomas Grafb382b192010-11-16 04:33:57 +00004927 err = rtnl_af_register(&inet6_ops);
4928 if (err < 0)
4929 goto errout_af;
4930
Greg Rosec7ac8672011-06-10 01:27:09 +00004931 err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo,
4932 NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07004933 if (err < 0)
4934 goto errout;
4935
4936 /* Only the first call to __rtnl_register can fail */
Greg Rosec7ac8672011-06-10 01:27:09 +00004937 __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL, NULL);
4938 __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL, NULL);
4939 __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr,
4940 inet6_dump_ifaddr, NULL);
4941 __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL,
4942 inet6_dump_ifmcaddr, NULL);
4943 __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL,
4944 inet6_dump_ifacaddr, NULL);
Nicolas Dichtel76f8f6c2012-10-25 22:28:51 +00004945 __rtnl_register(PF_INET6, RTM_GETNETCONF, inet6_netconf_get_devconf,
4946 NULL, NULL);
Thomas Grafc127ea22007-03-22 11:58:32 -07004947
YOSHIFUJI Hideaki2a8cc6c2007-11-14 15:56:23 +09004948 ipv6_addr_label_rtnl_register();
4949
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 return 0;
Thomas Grafc127ea22007-03-22 11:58:32 -07004951errout:
Thomas Grafb382b192010-11-16 04:33:57 +00004952 rtnl_af_unregister(&inet6_ops);
4953errout_af:
Thomas Grafc127ea22007-03-22 11:58:32 -07004954 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004955errlo:
4956 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004957out_addrlabel:
4958 ipv6_addr_label_cleanup();
4959out:
Thomas Grafc127ea22007-03-22 11:58:32 -07004960 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961}
4962
Daniel Lezcano09f77092007-12-13 05:34:58 -08004963void addrconf_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964{
Daniel Lezcano176c39a2009-03-03 01:06:45 -08004965 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 int i;
4967
4968 unregister_netdevice_notifier(&ipv6_dev_notf);
Pavel Emelyanove0da5a42008-01-10 17:42:55 -08004969 unregister_pernet_subsys(&addrconf_ops);
Neil Horman2cc6d2b2010-09-24 09:55:52 +00004970 ipv6_addr_label_cleanup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
4972 rtnl_lock();
4973
Thomas Grafb382b192010-11-16 04:33:57 +00004974 __rtnl_af_unregister(&inet6_ops);
4975
Daniel Lezcano176c39a2009-03-03 01:06:45 -08004976 /* clean dev list */
4977 for_each_netdev(&init_net, dev) {
4978 if (__in6_dev_get(dev) == NULL)
4979 continue;
4980 addrconf_ifdown(dev, 1);
4981 }
4982 addrconf_ifdown(init_net.loopback_dev, 2);
4983
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 * Check hash table.
4986 */
stephen hemminger5c578ae2010-03-17 20:31:11 +00004987 spin_lock_bh(&addrconf_hash_lock);
stephen hemmingerc2e21292010-03-17 20:31:10 +00004988 for (i = 0; i < IN6_ADDR_HSIZE; i++)
4989 WARN_ON(!hlist_empty(&inet6_addr_lst[i]));
stephen hemminger5c578ae2010-03-17 20:31:11 +00004990 spin_unlock_bh(&addrconf_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991
4992 del_timer(&addr_chk_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994}