2019-05-27 14:55:01 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Anycast support for IPv6
|
2007-02-09 22:24:49 +08:00
|
|
|
* Linux INET6 implementation
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* David L Stevens (dlstevens@us.ibm.com)
|
|
|
|
*
|
|
|
|
* based heavily on net/ipv6/mcast.c
|
|
|
|
*/
|
|
|
|
|
2006-01-12 04:17:47 +08:00
|
|
|
#include <linux/capability.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/random.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/socket.h>
|
|
|
|
#include <linux/sockios.h>
|
|
|
|
#include <linux/net.h>
|
|
|
|
#include <linux/in6.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/if_arp.h>
|
|
|
|
#include <linux/route.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/proc_fs.h>
|
|
|
|
#include <linux/seq_file.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-09-12 18:01:34 +08:00
|
|
|
#include <net/net_namespace.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <net/sock.h>
|
|
|
|
#include <net/snmp.h>
|
|
|
|
|
|
|
|
#include <net/ipv6.h>
|
|
|
|
#include <net/protocol.h>
|
|
|
|
#include <net/if_inet6.h>
|
|
|
|
#include <net/ndisc.h>
|
|
|
|
#include <net/addrconf.h>
|
|
|
|
#include <net/ip6_route.h>
|
|
|
|
|
|
|
|
#include <net/checksum.h>
|
|
|
|
|
2018-11-03 04:23:57 +08:00
|
|
|
#define IN6_ADDR_HSIZE_SHIFT 8
|
|
|
|
#define IN6_ADDR_HSIZE BIT(IN6_ADDR_HSIZE_SHIFT)
|
|
|
|
/* anycast address hash table
|
|
|
|
*/
|
|
|
|
static struct hlist_head inet6_acaddr_lst[IN6_ADDR_HSIZE];
|
|
|
|
static DEFINE_SPINLOCK(acaddr_hash_lock);
|
|
|
|
|
2011-04-22 12:53:02 +08:00
|
|
|
static int ipv6_dev_ac_dec(struct net_device *dev, const struct in6_addr *addr);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2018-11-03 04:23:57 +08:00
|
|
|
static u32 inet6_acaddr_hash(struct net *net, const struct in6_addr *addr)
|
|
|
|
{
|
|
|
|
u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net);
|
|
|
|
|
|
|
|
return hash_32(val, IN6_ADDR_HSIZE_SHIFT);
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* socket join an anycast group
|
|
|
|
*/
|
|
|
|
|
2011-04-22 12:53:02 +08:00
|
|
|
int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct ipv6_pinfo *np = inet6_sk(sk);
|
|
|
|
struct net_device *dev = NULL;
|
|
|
|
struct inet6_dev *idev;
|
|
|
|
struct ipv6_ac_socklist *pac;
|
2008-03-27 07:52:32 +08:00
|
|
|
struct net *net = sock_net(sk);
|
2008-07-20 13:35:03 +08:00
|
|
|
int ishost = !net->ipv6.devconf_all->forwarding;
|
2005-04-17 06:20:36 +08:00
|
|
|
int err = 0;
|
|
|
|
|
2015-03-20 22:37:17 +08:00
|
|
|
ASSERT_RTNL();
|
|
|
|
|
net: Allow userns root to control ipv6
Allow an unpriviled user who has created a user namespace, and then
created a network namespace to effectively use the new network
namespace, by reducing capable(CAP_NET_ADMIN) and
capable(CAP_NET_RAW) calls to be ns_capable(net->user_ns,
CAP_NET_ADMIN), or capable(net->user_ns, CAP_NET_RAW) calls.
Settings that merely control a single network device are allowed.
Either the network device is a logical network device where
restrictions make no difference or the network device is hardware NIC
that has been explicity moved from the initial network namespace.
In general policy and network stack state changes are allowed while
resource control is left unchanged.
Allow the SIOCSIFADDR ioctl to add ipv6 addresses.
Allow the SIOCDIFADDR ioctl to delete ipv6 addresses.
Allow the SIOCADDRT ioctl to add ipv6 routes.
Allow the SIOCDELRT ioctl to delete ipv6 routes.
Allow creation of ipv6 raw sockets.
Allow setting the IPV6_JOIN_ANYCAST socket option.
Allow setting the IPV6_FL_A_RENEW parameter of the IPV6_FLOWLABEL_MGR
socket option.
Allow setting the IPV6_TRANSPARENT socket option.
Allow setting the IPV6_HOPOPTS socket option.
Allow setting the IPV6_RTHDRDSTOPTS socket option.
Allow setting the IPV6_DSTOPTS socket option.
Allow setting the IPV6_IPSEC_POLICY socket option.
Allow setting the IPV6_XFRM_POLICY socket option.
Allow sending packets with the IPV6_2292HOPOPTS control message.
Allow sending packets with the IPV6_2292DSTOPTS control message.
Allow sending packets with the IPV6_RTHDRDSTOPTS control message.
Allow setting the multicast routing socket options on non multicast
routing sockets.
Allow the SIOCADDTUNNEL, SIOCCHGTUNNEL, and SIOCDELTUNNEL ioctls for
setting up, changing and deleting tunnels over ipv6.
Allow the SIOCADDTUNNEL, SIOCCHGTUNNEL, SIOCDELTUNNEL ioctls for
setting up, changing and deleting ipv6 over ipv4 tunnels.
Allow the SIOCADDPRL, SIOCDELPRL, SIOCCHGPRL ioctls for adding,
deleting, and changing the potential router list for ISATAP tunnels.
Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2012-11-16 11:03:06 +08:00
|
|
|
if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EPERM;
|
|
|
|
if (ipv6_addr_is_multicast(addr))
|
|
|
|
return -EINVAL;
|
2018-03-13 23:29:37 +08:00
|
|
|
|
|
|
|
if (ifindex)
|
|
|
|
dev = __dev_get_by_index(net, ifindex);
|
|
|
|
|
|
|
|
if (ipv6_chk_addr_and_flags(net, addr, dev, true, 0, IFA_F_TENTATIVE))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
pac = sock_kmalloc(sk, sizeof(struct ipv6_ac_socklist), GFP_KERNEL);
|
2015-03-29 21:00:04 +08:00
|
|
|
if (!pac)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
pac->acl_next = NULL;
|
2011-11-21 11:39:03 +08:00
|
|
|
pac->acl_addr = *addr;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (ifindex == 0) {
|
|
|
|
struct rt6_info *rt;
|
|
|
|
|
2018-03-03 00:32:17 +08:00
|
|
|
rt = rt6_lookup(net, addr, NULL, 0, NULL, 0);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (rt) {
|
2011-12-29 09:19:20 +08:00
|
|
|
dev = rt->dst.dev;
|
2012-10-29 08:13:19 +08:00
|
|
|
ip6_rt_put(rt);
|
2005-04-17 06:20:36 +08:00
|
|
|
} else if (ishost) {
|
|
|
|
err = -EADDRNOTAVAIL;
|
2010-06-07 19:42:13 +08:00
|
|
|
goto error;
|
2005-04-17 06:20:36 +08:00
|
|
|
} else {
|
|
|
|
/* router, no matching interface: just pick one */
|
2014-09-12 06:35:09 +08:00
|
|
|
dev = __dev_get_by_flags(net, IFF_UP,
|
|
|
|
IFF_UP | IFF_LOOPBACK);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2018-03-13 23:29:37 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2015-03-29 21:00:04 +08:00
|
|
|
if (!dev) {
|
2005-04-17 06:20:36 +08:00
|
|
|
err = -ENODEV;
|
2010-06-07 19:42:13 +08:00
|
|
|
goto error;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2010-06-07 19:42:13 +08:00
|
|
|
idev = __in6_dev_get(dev);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!idev) {
|
|
|
|
if (ifindex)
|
|
|
|
err = -ENODEV;
|
|
|
|
else
|
|
|
|
err = -EADDRNOTAVAIL;
|
2010-06-07 19:42:13 +08:00
|
|
|
goto error;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
/* reset ishost, now that we have a specific device */
|
|
|
|
ishost = !idev->cnf.forwarding;
|
|
|
|
|
|
|
|
pac->acl_ifindex = dev->ifindex;
|
|
|
|
|
|
|
|
/* XXX
|
|
|
|
* For hosts, allow link-local or matching prefix anycasts.
|
|
|
|
* This obviates the need for propagating anycast routes while
|
|
|
|
* still allowing some non-router anycast participation.
|
|
|
|
*/
|
2008-03-16 10:54:23 +08:00
|
|
|
if (!ipv6_chk_prefix(addr, dev)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
if (ishost)
|
|
|
|
err = -EADDRNOTAVAIL;
|
|
|
|
if (err)
|
2010-06-07 19:42:13 +08:00
|
|
|
goto error;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2014-09-12 06:35:11 +08:00
|
|
|
err = __ipv6_dev_ac_inc(idev, addr);
|
2010-06-07 19:42:13 +08:00
|
|
|
if (!err) {
|
|
|
|
pac->acl_next = np->ipv6_ac_list;
|
|
|
|
np->ipv6_ac_list = pac;
|
|
|
|
pac = NULL;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-06-07 19:42:13 +08:00
|
|
|
error:
|
|
|
|
if (pac)
|
|
|
|
sock_kfree_s(sk, pac, sizeof(*pac));
|
2005-04-17 06:20:36 +08:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* socket leave an anycast group
|
|
|
|
*/
|
2011-04-22 12:53:02 +08:00
|
|
|
int ipv6_sock_ac_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct ipv6_pinfo *np = inet6_sk(sk);
|
|
|
|
struct net_device *dev;
|
|
|
|
struct ipv6_ac_socklist *pac, *prev_pac;
|
2008-03-27 07:52:32 +08:00
|
|
|
struct net *net = sock_net(sk);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2015-03-20 22:37:17 +08:00
|
|
|
ASSERT_RTNL();
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
prev_pac = NULL;
|
|
|
|
for (pac = np->ipv6_ac_list; pac; pac = pac->acl_next) {
|
|
|
|
if ((ifindex == 0 || pac->acl_ifindex == ifindex) &&
|
|
|
|
ipv6_addr_equal(&pac->acl_addr, addr))
|
|
|
|
break;
|
|
|
|
prev_pac = pac;
|
|
|
|
}
|
2015-03-20 22:37:17 +08:00
|
|
|
if (!pac)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENOENT;
|
|
|
|
if (prev_pac)
|
|
|
|
prev_pac->acl_next = pac->acl_next;
|
|
|
|
else
|
|
|
|
np->ipv6_ac_list = pac->acl_next;
|
|
|
|
|
2014-09-12 06:35:09 +08:00
|
|
|
dev = __dev_get_by_index(net, pac->acl_ifindex);
|
2010-06-07 19:42:13 +08:00
|
|
|
if (dev)
|
2005-04-17 06:20:36 +08:00
|
|
|
ipv6_dev_ac_dec(dev, &pac->acl_addr);
|
2010-06-07 19:42:13 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
sock_kfree_s(sk, pac, sizeof(*pac));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-06-11 20:26:44 +08:00
|
|
|
void __ipv6_sock_ac_close(struct sock *sk)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct ipv6_pinfo *np = inet6_sk(sk);
|
|
|
|
struct net_device *dev = NULL;
|
|
|
|
struct ipv6_ac_socklist *pac;
|
2008-03-27 07:52:32 +08:00
|
|
|
struct net *net = sock_net(sk);
|
2005-04-17 06:20:36 +08:00
|
|
|
int prev_index;
|
|
|
|
|
2024-06-11 20:26:44 +08:00
|
|
|
ASSERT_RTNL();
|
2005-04-17 06:20:36 +08:00
|
|
|
pac = np->ipv6_ac_list;
|
|
|
|
np->ipv6_ac_list = NULL;
|
|
|
|
|
|
|
|
prev_index = 0;
|
|
|
|
while (pac) {
|
|
|
|
struct ipv6_ac_socklist *next = pac->acl_next;
|
|
|
|
|
|
|
|
if (pac->acl_ifindex != prev_index) {
|
2014-09-12 06:35:09 +08:00
|
|
|
dev = __dev_get_by_index(net, pac->acl_ifindex);
|
2005-04-17 06:20:36 +08:00
|
|
|
prev_index = pac->acl_ifindex;
|
|
|
|
}
|
|
|
|
if (dev)
|
|
|
|
ipv6_dev_ac_dec(dev, &pac->acl_addr);
|
|
|
|
sock_kfree_s(sk, pac, sizeof(*pac));
|
|
|
|
pac = next;
|
|
|
|
}
|
2024-06-11 20:26:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ipv6_sock_ac_close(struct sock *sk)
|
|
|
|
{
|
|
|
|
struct ipv6_pinfo *np = inet6_sk(sk);
|
|
|
|
|
|
|
|
if (!np->ipv6_ac_list)
|
|
|
|
return;
|
|
|
|
rtnl_lock();
|
|
|
|
__ipv6_sock_ac_close(sk);
|
2014-09-02 16:29:29 +08:00
|
|
|
rtnl_unlock();
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2018-11-03 04:23:57 +08:00
|
|
|
static void ipv6_add_acaddr_hash(struct net *net, struct ifacaddr6 *aca)
|
|
|
|
{
|
|
|
|
unsigned int hash = inet6_acaddr_hash(net, &aca->aca_addr);
|
|
|
|
|
|
|
|
spin_lock(&acaddr_hash_lock);
|
|
|
|
hlist_add_head_rcu(&aca->aca_addr_lst, &inet6_acaddr_lst[hash]);
|
|
|
|
spin_unlock(&acaddr_hash_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ipv6_del_acaddr_hash(struct ifacaddr6 *aca)
|
|
|
|
{
|
|
|
|
spin_lock(&acaddr_hash_lock);
|
|
|
|
hlist_del_init_rcu(&aca->aca_addr_lst);
|
|
|
|
spin_unlock(&acaddr_hash_lock);
|
|
|
|
}
|
|
|
|
|
2014-09-12 06:35:12 +08:00
|
|
|
static void aca_get(struct ifacaddr6 *aca)
|
|
|
|
{
|
2017-07-04 14:34:58 +08:00
|
|
|
refcount_inc(&aca->aca_refcnt);
|
2014-09-12 06:35:12 +08:00
|
|
|
}
|
|
|
|
|
2018-11-03 04:23:57 +08:00
|
|
|
static void aca_free_rcu(struct rcu_head *h)
|
|
|
|
{
|
|
|
|
struct ifacaddr6 *aca = container_of(h, struct ifacaddr6, rcu);
|
|
|
|
|
|
|
|
fib6_info_release(aca->aca_rt);
|
|
|
|
kfree(aca);
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static void aca_put(struct ifacaddr6 *ac)
|
|
|
|
{
|
2017-07-04 14:34:58 +08:00
|
|
|
if (refcount_dec_and_test(&ac->aca_refcnt)) {
|
2018-11-03 04:23:57 +08:00
|
|
|
call_rcu(&ac->rcu, aca_free_rcu);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 06:38:59 +08:00
|
|
|
static struct ifacaddr6 *aca_alloc(struct fib6_info *f6i,
|
2014-09-12 06:35:12 +08:00
|
|
|
const struct in6_addr *addr)
|
|
|
|
{
|
|
|
|
struct ifacaddr6 *aca;
|
|
|
|
|
|
|
|
aca = kzalloc(sizeof(*aca), GFP_ATOMIC);
|
2015-03-29 21:00:04 +08:00
|
|
|
if (!aca)
|
2014-09-12 06:35:12 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
aca->aca_addr = *addr;
|
2018-04-19 06:38:59 +08:00
|
|
|
fib6_info_hold(f6i);
|
|
|
|
aca->aca_rt = f6i;
|
2018-11-03 04:23:57 +08:00
|
|
|
INIT_HLIST_NODE(&aca->aca_addr_lst);
|
2014-09-12 06:35:12 +08:00
|
|
|
aca->aca_users = 1;
|
|
|
|
/* aca_tstamp should be updated upon changes */
|
|
|
|
aca->aca_cstamp = aca->aca_tstamp = jiffies;
|
2017-07-04 14:34:58 +08:00
|
|
|
refcount_set(&aca->aca_refcnt, 1);
|
2014-09-12 06:35:12 +08:00
|
|
|
|
|
|
|
return aca;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* device anycast group inc (add if not found)
|
|
|
|
*/
|
2014-09-12 06:35:11 +08:00
|
|
|
int __ipv6_dev_ac_inc(struct inet6_dev *idev, const struct in6_addr *addr)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct ifacaddr6 *aca;
|
2018-04-19 06:39:00 +08:00
|
|
|
struct fib6_info *f6i;
|
2018-04-18 08:33:11 +08:00
|
|
|
struct net *net;
|
2005-04-17 06:20:36 +08:00
|
|
|
int err;
|
|
|
|
|
2014-09-02 16:29:29 +08:00
|
|
|
ASSERT_RTNL();
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
write_lock_bh(&idev->lock);
|
|
|
|
if (idev->dead) {
|
|
|
|
err = -ENODEV;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (aca = idev->ac_list; aca; aca = aca->aca_next) {
|
|
|
|
if (ipv6_addr_equal(&aca->aca_addr, addr)) {
|
|
|
|
aca->aca_users++;
|
|
|
|
err = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-18 08:33:11 +08:00
|
|
|
net = dev_net(idev->dev);
|
2018-04-19 06:39:00 +08:00
|
|
|
f6i = addrconf_f6i_alloc(net, idev, addr, true, GFP_ATOMIC);
|
|
|
|
if (IS_ERR(f6i)) {
|
|
|
|
err = PTR_ERR(f6i);
|
2005-04-17 06:20:36 +08:00
|
|
|
goto out;
|
|
|
|
}
|
2018-04-19 06:39:00 +08:00
|
|
|
aca = aca_alloc(f6i, addr);
|
2015-03-29 21:00:04 +08:00
|
|
|
if (!aca) {
|
2018-04-19 06:39:00 +08:00
|
|
|
fib6_info_release(f6i);
|
2014-09-12 06:35:12 +08:00
|
|
|
err = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
aca->aca_next = idev->ac_list;
|
|
|
|
idev->ac_list = aca;
|
2014-09-12 06:35:12 +08:00
|
|
|
|
|
|
|
/* Hold this for addrconf_join_solict() below before we unlock,
|
|
|
|
* it is already exposed via idev->ac_list.
|
|
|
|
*/
|
|
|
|
aca_get(aca);
|
2005-04-17 06:20:36 +08:00
|
|
|
write_unlock_bh(&idev->lock);
|
|
|
|
|
2018-11-03 04:23:57 +08:00
|
|
|
ipv6_add_acaddr_hash(net, aca);
|
|
|
|
|
2018-04-19 06:39:00 +08:00
|
|
|
ip6_ins_rt(net, f6i);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-09-12 06:35:11 +08:00
|
|
|
addrconf_join_solict(idev->dev, &aca->aca_addr);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
aca_put(aca);
|
|
|
|
return 0;
|
|
|
|
out:
|
|
|
|
write_unlock_bh(&idev->lock);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* device anycast group decrement
|
|
|
|
*/
|
2011-04-22 12:53:02 +08:00
|
|
|
int __ipv6_dev_ac_dec(struct inet6_dev *idev, const struct in6_addr *addr)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct ifacaddr6 *aca, *prev_aca;
|
|
|
|
|
2014-09-02 16:29:29 +08:00
|
|
|
ASSERT_RTNL();
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
write_lock_bh(&idev->lock);
|
|
|
|
prev_aca = NULL;
|
|
|
|
for (aca = idev->ac_list; aca; aca = aca->aca_next) {
|
|
|
|
if (ipv6_addr_equal(&aca->aca_addr, addr))
|
|
|
|
break;
|
|
|
|
prev_aca = aca;
|
|
|
|
}
|
|
|
|
if (!aca) {
|
|
|
|
write_unlock_bh(&idev->lock);
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
if (--aca->aca_users > 0) {
|
|
|
|
write_unlock_bh(&idev->lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (prev_aca)
|
|
|
|
prev_aca->aca_next = aca->aca_next;
|
|
|
|
else
|
|
|
|
idev->ac_list = aca->aca_next;
|
|
|
|
write_unlock_bh(&idev->lock);
|
2018-11-03 04:23:57 +08:00
|
|
|
ipv6_del_acaddr_hash(aca);
|
2005-04-17 06:20:36 +08:00
|
|
|
addrconf_leave_solict(idev, &aca->aca_addr);
|
|
|
|
|
2018-04-18 08:33:11 +08:00
|
|
|
ip6_del_rt(dev_net(idev->dev), aca->aca_rt);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
aca_put(aca);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-12 06:35:09 +08:00
|
|
|
/* called with rtnl_lock() */
|
2011-04-22 12:53:02 +08:00
|
|
|
static int ipv6_dev_ac_dec(struct net_device *dev, const struct in6_addr *addr)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2010-06-07 19:42:13 +08:00
|
|
|
struct inet6_dev *idev = __in6_dev_get(dev);
|
|
|
|
|
2015-03-29 21:00:04 +08:00
|
|
|
if (!idev)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENODEV;
|
2010-06-07 19:42:13 +08:00
|
|
|
return __ipv6_dev_ac_dec(idev, addr);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2007-02-09 22:24:49 +08:00
|
|
|
|
2014-09-11 05:23:02 +08:00
|
|
|
void ipv6_ac_destroy_dev(struct inet6_dev *idev)
|
|
|
|
{
|
|
|
|
struct ifacaddr6 *aca;
|
|
|
|
|
|
|
|
write_lock_bh(&idev->lock);
|
|
|
|
while ((aca = idev->ac_list) != NULL) {
|
|
|
|
idev->ac_list = aca->aca_next;
|
|
|
|
write_unlock_bh(&idev->lock);
|
|
|
|
|
2018-11-03 04:23:57 +08:00
|
|
|
ipv6_del_acaddr_hash(aca);
|
|
|
|
|
2014-09-11 05:23:02 +08:00
|
|
|
addrconf_leave_solict(idev, &aca->aca_addr);
|
|
|
|
|
2018-04-18 08:33:11 +08:00
|
|
|
ip6_del_rt(dev_net(idev->dev), aca->aca_rt);
|
2014-09-11 05:23:02 +08:00
|
|
|
|
|
|
|
aca_put(aca);
|
|
|
|
|
|
|
|
write_lock_bh(&idev->lock);
|
|
|
|
}
|
|
|
|
write_unlock_bh(&idev->lock);
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* check if the interface has this anycast address
|
2010-06-07 19:42:13 +08:00
|
|
|
* called with rcu_read_lock()
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2012-05-19 02:57:34 +08:00
|
|
|
static bool ipv6_chk_acast_dev(struct net_device *dev, const struct in6_addr *addr)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct inet6_dev *idev;
|
|
|
|
struct ifacaddr6 *aca;
|
|
|
|
|
2010-06-07 19:42:13 +08:00
|
|
|
idev = __in6_dev_get(dev);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (idev) {
|
|
|
|
read_lock_bh(&idev->lock);
|
|
|
|
for (aca = idev->ac_list; aca; aca = aca->aca_next)
|
|
|
|
if (ipv6_addr_equal(&aca->aca_addr, addr))
|
|
|
|
break;
|
|
|
|
read_unlock_bh(&idev->lock);
|
2007-10-09 16:59:42 +08:00
|
|
|
return aca != NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2012-05-19 02:57:34 +08:00
|
|
|
return false;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check if given interface (or any, if dev==0) has this anycast address
|
|
|
|
*/
|
2012-05-19 02:57:34 +08:00
|
|
|
bool ipv6_chk_acast_addr(struct net *net, struct net_device *dev,
|
|
|
|
const struct in6_addr *addr)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2018-11-03 04:23:57 +08:00
|
|
|
struct net_device *nh_dev;
|
|
|
|
struct ifacaddr6 *aca;
|
2012-05-19 02:57:34 +08:00
|
|
|
bool found = false;
|
2007-05-04 06:13:45 +08:00
|
|
|
|
2009-11-04 21:43:23 +08:00
|
|
|
rcu_read_lock();
|
2010-06-07 19:42:13 +08:00
|
|
|
if (dev)
|
|
|
|
found = ipv6_chk_acast_dev(dev, addr);
|
2018-11-08 14:58:07 +08:00
|
|
|
else {
|
|
|
|
unsigned int hash = inet6_acaddr_hash(net, addr);
|
|
|
|
|
2018-11-03 04:23:57 +08:00
|
|
|
hlist_for_each_entry_rcu(aca, &inet6_acaddr_lst[hash],
|
|
|
|
aca_addr_lst) {
|
|
|
|
nh_dev = fib6_info_nh_dev(aca->aca_rt);
|
|
|
|
if (!nh_dev || !net_eq(dev_net(nh_dev), net))
|
|
|
|
continue;
|
|
|
|
if (ipv6_addr_equal(&aca->aca_addr, addr)) {
|
2012-05-19 02:57:34 +08:00
|
|
|
found = true;
|
2010-06-07 19:42:13 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-11-03 04:23:57 +08:00
|
|
|
}
|
2018-11-08 14:58:07 +08:00
|
|
|
}
|
2009-11-04 21:43:23 +08:00
|
|
|
rcu_read_unlock();
|
2007-05-04 06:13:45 +08:00
|
|
|
return found;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2014-01-22 14:42:37 +08:00
|
|
|
/* check if this anycast address is link-local on given interface or
|
|
|
|
* is global
|
|
|
|
*/
|
|
|
|
bool ipv6_chk_acast_addr_src(struct net *net, struct net_device *dev,
|
|
|
|
const struct in6_addr *addr)
|
|
|
|
{
|
|
|
|
return ipv6_chk_acast_addr(net,
|
|
|
|
(ipv6_addr_type(addr) & IPV6_ADDR_LINKLOCAL ?
|
|
|
|
dev : NULL),
|
|
|
|
addr);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
struct ac6_iter_state {
|
2008-03-27 07:52:32 +08:00
|
|
|
struct seq_net_private p;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct net_device *dev;
|
|
|
|
struct inet6_dev *idev;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ac6_seq_private(seq) ((struct ac6_iter_state *)(seq)->private)
|
|
|
|
|
|
|
|
static inline struct ifacaddr6 *ac6_get_first(struct seq_file *seq)
|
|
|
|
{
|
|
|
|
struct ifacaddr6 *im = NULL;
|
|
|
|
struct ac6_iter_state *state = ac6_seq_private(seq);
|
2008-03-27 07:52:32 +08:00
|
|
|
struct net *net = seq_file_net(seq);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2007-05-04 06:13:45 +08:00
|
|
|
state->idev = NULL;
|
2009-11-12 01:34:30 +08:00
|
|
|
for_each_netdev_rcu(net, state->dev) {
|
2005-04-17 06:20:36 +08:00
|
|
|
struct inet6_dev *idev;
|
2009-11-12 01:34:30 +08:00
|
|
|
idev = __in6_dev_get(state->dev);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!idev)
|
|
|
|
continue;
|
|
|
|
read_lock_bh(&idev->lock);
|
|
|
|
im = idev->ac_list;
|
|
|
|
if (im) {
|
|
|
|
state->idev = idev;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
read_unlock_bh(&idev->lock);
|
|
|
|
}
|
|
|
|
return im;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ifacaddr6 *ac6_get_next(struct seq_file *seq, struct ifacaddr6 *im)
|
|
|
|
{
|
|
|
|
struct ac6_iter_state *state = ac6_seq_private(seq);
|
|
|
|
|
|
|
|
im = im->aca_next;
|
|
|
|
while (!im) {
|
2009-11-12 01:34:30 +08:00
|
|
|
if (likely(state->idev != NULL))
|
2005-04-17 06:20:36 +08:00
|
|
|
read_unlock_bh(&state->idev->lock);
|
2009-11-12 01:34:30 +08:00
|
|
|
|
|
|
|
state->dev = next_net_device_rcu(state->dev);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!state->dev) {
|
|
|
|
state->idev = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2009-11-12 01:34:30 +08:00
|
|
|
state->idev = __in6_dev_get(state->dev);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!state->idev)
|
|
|
|
continue;
|
|
|
|
read_lock_bh(&state->idev->lock);
|
|
|
|
im = state->idev->ac_list;
|
|
|
|
}
|
|
|
|
return im;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ifacaddr6 *ac6_get_idx(struct seq_file *seq, loff_t pos)
|
|
|
|
{
|
|
|
|
struct ifacaddr6 *im = ac6_get_first(seq);
|
|
|
|
if (im)
|
|
|
|
while (pos && (im = ac6_get_next(seq, im)) != NULL)
|
|
|
|
--pos;
|
|
|
|
return pos ? NULL : im;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *ac6_seq_start(struct seq_file *seq, loff_t *pos)
|
2009-11-12 01:34:30 +08:00
|
|
|
__acquires(RCU)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2009-11-12 01:34:30 +08:00
|
|
|
rcu_read_lock();
|
2005-04-17 06:20:36 +08:00
|
|
|
return ac6_get_idx(seq, *pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *ac6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
|
|
|
|
{
|
2009-11-12 01:34:30 +08:00
|
|
|
struct ifacaddr6 *im = ac6_get_next(seq, v);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
++*pos;
|
|
|
|
return im;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ac6_seq_stop(struct seq_file *seq, void *v)
|
2009-11-12 01:34:30 +08:00
|
|
|
__releases(RCU)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct ac6_iter_state *state = ac6_seq_private(seq);
|
2009-11-12 01:34:30 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (likely(state->idev != NULL)) {
|
|
|
|
read_unlock_bh(&state->idev->lock);
|
2009-11-12 01:34:30 +08:00
|
|
|
state->idev = NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2009-11-12 01:34:30 +08:00
|
|
|
rcu_read_unlock();
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ac6_seq_show(struct seq_file *seq, void *v)
|
|
|
|
{
|
|
|
|
struct ifacaddr6 *im = (struct ifacaddr6 *)v;
|
|
|
|
struct ac6_iter_state *state = ac6_seq_private(seq);
|
|
|
|
|
2008-10-30 03:50:24 +08:00
|
|
|
seq_printf(seq, "%-4d %-15s %pi6 %5d\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
state->dev->ifindex, state->dev->name,
|
2008-10-29 07:05:40 +08:00
|
|
|
&im->aca_addr, im->aca_users);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-11 14:07:31 +08:00
|
|
|
static const struct seq_operations ac6_seq_ops = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.start = ac6_seq_start,
|
|
|
|
.next = ac6_seq_next,
|
|
|
|
.stop = ac6_seq_stop,
|
|
|
|
.show = ac6_seq_show,
|
|
|
|
};
|
|
|
|
|
2010-01-17 11:35:32 +08:00
|
|
|
int __net_init ac6_proc_init(struct net *net)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2018-04-11 01:42:55 +08:00
|
|
|
if (!proc_create_net("anycast6", 0444, net->proc_net, &ac6_seq_ops,
|
|
|
|
sizeof(struct ac6_iter_state)))
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-27 07:52:32 +08:00
|
|
|
void ac6_proc_exit(struct net *net)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2013-02-18 09:34:56 +08:00
|
|
|
remove_proc_entry("anycast6", net->proc_net);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2018-11-06 04:36:45 +08:00
|
|
|
#endif
|
2018-11-03 04:23:57 +08:00
|
|
|
|
|
|
|
/* Init / cleanup code
|
|
|
|
*/
|
|
|
|
int __init ipv6_anycast_init(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < IN6_ADDR_HSIZE; i++)
|
|
|
|
INIT_HLIST_HEAD(&inet6_acaddr_lst[i]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ipv6_anycast_cleanup(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
spin_lock(&acaddr_hash_lock);
|
|
|
|
for (i = 0; i < IN6_ADDR_HSIZE; i++)
|
|
|
|
WARN_ON(!hlist_empty(&inet6_acaddr_lst[i]));
|
|
|
|
spin_unlock(&acaddr_hash_lock);
|
|
|
|
}
|