2009-02-24 15:30:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006 Oracle. All rights reserved.
|
|
|
|
*
|
|
|
|
* This software is available to you under a choice of one of two
|
|
|
|
* licenses. You may choose to be licensed under the terms of the GNU
|
|
|
|
* General Public License (GPL) Version 2, available from the file
|
|
|
|
* COPYING in the main directory of this source tree, or the
|
|
|
|
* OpenIB.org BSD license below:
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or
|
|
|
|
* without modification, are permitted provided that the following
|
|
|
|
* conditions are met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer.
|
|
|
|
*
|
|
|
|
* - Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials
|
|
|
|
* provided with the distribution.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <net/sock.h>
|
|
|
|
#include <linux/in.h>
|
|
|
|
#include <linux/if_arp.h>
|
2010-05-11 22:09:45 +00:00
|
|
|
#include <linux/jhash.h>
|
2011-06-16 02:09:57 +00:00
|
|
|
#include <linux/ratelimit.h>
|
2009-02-24 15:30:18 +00:00
|
|
|
#include "rds.h"
|
|
|
|
|
2014-02-12 03:34:25 +00:00
|
|
|
struct bind_bucket {
|
|
|
|
rwlock_t lock;
|
|
|
|
struct hlist_head head;
|
|
|
|
};
|
|
|
|
|
2010-05-11 22:09:45 +00:00
|
|
|
#define BIND_HASH_SIZE 1024
|
2014-02-12 03:34:25 +00:00
|
|
|
static struct bind_bucket bind_hash_table[BIND_HASH_SIZE];
|
2010-05-11 22:09:45 +00:00
|
|
|
|
2014-02-12 03:34:25 +00:00
|
|
|
static struct bind_bucket *hash_to_bucket(__be32 addr, __be16 port)
|
2010-05-11 22:09:45 +00:00
|
|
|
{
|
|
|
|
return bind_hash_table + (jhash_2words((u32)addr, (u32)port, 0) &
|
|
|
|
(BIND_HASH_SIZE - 1));
|
|
|
|
}
|
2009-02-24 15:30:18 +00:00
|
|
|
|
2012-02-03 16:09:23 +00:00
|
|
|
/* must hold either read or write lock (write lock for insert != NULL) */
|
2014-02-12 03:34:25 +00:00
|
|
|
static struct rds_sock *rds_bind_lookup(struct bind_bucket *bucket,
|
|
|
|
__be32 addr, __be16 port,
|
2010-05-11 22:09:45 +00:00
|
|
|
struct rds_sock *insert)
|
2009-02-24 15:30:18 +00:00
|
|
|
{
|
|
|
|
struct rds_sock *rs;
|
2014-02-12 03:34:25 +00:00
|
|
|
struct hlist_head *head = &bucket->head;
|
2009-02-24 15:30:18 +00:00
|
|
|
u64 cmp;
|
|
|
|
u64 needle = ((u64)be32_to_cpu(addr) << 32) | be16_to_cpu(port);
|
|
|
|
|
2012-02-03 16:09:23 +00:00
|
|
|
hlist_for_each_entry(rs, head, rs_bound_node) {
|
2009-02-24 15:30:18 +00:00
|
|
|
cmp = ((u64)be32_to_cpu(rs->rs_bound_addr) << 32) |
|
|
|
|
be16_to_cpu(rs->rs_bound_port);
|
|
|
|
|
2012-02-03 16:08:50 +00:00
|
|
|
if (cmp == needle) {
|
|
|
|
rds_sock_addref(rs);
|
2009-02-24 15:30:18 +00:00
|
|
|
return rs;
|
2012-02-03 16:08:50 +00:00
|
|
|
}
|
2009-02-24 15:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (insert) {
|
2010-05-11 22:09:45 +00:00
|
|
|
/*
|
|
|
|
* make sure our addr and port are set before
|
2012-02-03 16:09:23 +00:00
|
|
|
* we are added to the list.
|
2010-05-11 22:09:45 +00:00
|
|
|
*/
|
|
|
|
insert->rs_bound_addr = addr;
|
|
|
|
insert->rs_bound_port = port;
|
|
|
|
rds_sock_addref(insert);
|
|
|
|
|
2012-02-03 16:09:23 +00:00
|
|
|
hlist_add_head(&insert->rs_bound_node, head);
|
2009-02-24 15:30:18 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the rds_sock bound at the given local address.
|
|
|
|
*
|
|
|
|
* The rx path can race with rds_release. We notice if rds_release() has
|
|
|
|
* marked this socket and don't return a rs ref to the rx path.
|
|
|
|
*/
|
|
|
|
struct rds_sock *rds_find_bound(__be32 addr, __be16 port)
|
|
|
|
{
|
|
|
|
struct rds_sock *rs;
|
2012-02-03 16:09:23 +00:00
|
|
|
unsigned long flags;
|
2014-02-12 03:34:25 +00:00
|
|
|
struct bind_bucket *bucket = hash_to_bucket(addr, port);
|
2009-02-24 15:30:18 +00:00
|
|
|
|
2014-02-12 03:34:25 +00:00
|
|
|
read_lock_irqsave(&bucket->lock, flags);
|
|
|
|
rs = rds_bind_lookup(bucket, addr, port, NULL);
|
|
|
|
read_unlock_irqrestore(&bucket->lock, flags);
|
2010-05-11 22:09:45 +00:00
|
|
|
|
2012-02-03 16:08:50 +00:00
|
|
|
if (rs && sock_flag(rds_rs_to_sk(rs), SOCK_DEAD)) {
|
|
|
|
rds_sock_put(rs);
|
2009-02-24 15:30:18 +00:00
|
|
|
rs = NULL;
|
2012-02-03 16:08:50 +00:00
|
|
|
}
|
2009-02-24 15:30:18 +00:00
|
|
|
|
|
|
|
rdsdebug("returning rs %p for %pI4:%u\n", rs, &addr,
|
|
|
|
ntohs(port));
|
2012-02-03 16:09:23 +00:00
|
|
|
|
2009-02-24 15:30:18 +00:00
|
|
|
return rs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns -ve errno or +ve port */
|
|
|
|
static int rds_add_bound(struct rds_sock *rs, __be32 addr, __be16 *port)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
int ret = -EADDRINUSE;
|
|
|
|
u16 rover, last;
|
2014-02-12 03:34:25 +00:00
|
|
|
struct bind_bucket *bucket;
|
2009-02-24 15:30:18 +00:00
|
|
|
|
|
|
|
if (*port != 0) {
|
|
|
|
rover = be16_to_cpu(*port);
|
|
|
|
last = rover;
|
|
|
|
} else {
|
2014-01-11 12:15:59 +00:00
|
|
|
rover = max_t(u16, prandom_u32(), 2);
|
2009-02-24 15:30:18 +00:00
|
|
|
last = rover - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2012-02-03 16:08:50 +00:00
|
|
|
struct rds_sock *rrs;
|
2009-02-24 15:30:18 +00:00
|
|
|
if (rover == 0)
|
|
|
|
rover++;
|
2014-02-12 03:34:25 +00:00
|
|
|
|
|
|
|
bucket = hash_to_bucket(addr, cpu_to_be16(rover));
|
|
|
|
write_lock_irqsave(&bucket->lock, flags);
|
|
|
|
rrs = rds_bind_lookup(bucket, addr, cpu_to_be16(rover), rs);
|
|
|
|
write_unlock_irqrestore(&bucket->lock, flags);
|
2012-02-03 16:08:50 +00:00
|
|
|
if (!rrs) {
|
2010-05-11 22:09:45 +00:00
|
|
|
*port = rs->rs_bound_port;
|
2009-02-24 15:30:18 +00:00
|
|
|
ret = 0;
|
2010-05-11 22:09:45 +00:00
|
|
|
rdsdebug("rs %p binding to %pI4:%d\n",
|
|
|
|
rs, &addr, (int)ntohs(*port));
|
2009-02-24 15:30:18 +00:00
|
|
|
break;
|
2012-02-03 16:08:50 +00:00
|
|
|
} else {
|
|
|
|
rds_sock_put(rrs);
|
2009-02-24 15:30:18 +00:00
|
|
|
}
|
|
|
|
} while (rover++ != last);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rds_remove_bound(struct rds_sock *rs)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
2014-02-12 03:34:25 +00:00
|
|
|
struct bind_bucket *bucket =
|
|
|
|
hash_to_bucket(rs->rs_bound_addr, rs->rs_bound_port);
|
2009-02-24 15:30:18 +00:00
|
|
|
|
2014-02-12 03:34:25 +00:00
|
|
|
write_lock_irqsave(&bucket->lock, flags);
|
2009-02-24 15:30:18 +00:00
|
|
|
|
|
|
|
if (rs->rs_bound_addr) {
|
|
|
|
rdsdebug("rs %p unbinding from %pI4:%d\n",
|
|
|
|
rs, &rs->rs_bound_addr,
|
|
|
|
ntohs(rs->rs_bound_port));
|
|
|
|
|
2012-02-03 16:09:23 +00:00
|
|
|
hlist_del_init(&rs->rs_bound_node);
|
2009-02-24 15:30:18 +00:00
|
|
|
rds_sock_put(rs);
|
|
|
|
rs->rs_bound_addr = 0;
|
|
|
|
}
|
|
|
|
|
2014-02-12 03:34:25 +00:00
|
|
|
write_unlock_irqrestore(&bucket->lock, flags);
|
2009-02-24 15:30:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int rds_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
|
|
|
|
{
|
|
|
|
struct sock *sk = sock->sk;
|
|
|
|
struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
|
|
|
|
struct rds_sock *rs = rds_sk_to_rs(sk);
|
|
|
|
struct rds_transport *trans;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
lock_sock(sk);
|
|
|
|
|
|
|
|
if (addr_len != sizeof(struct sockaddr_in) ||
|
|
|
|
sin->sin_family != AF_INET ||
|
|
|
|
rs->rs_bound_addr ||
|
|
|
|
sin->sin_addr.s_addr == htonl(INADDR_ANY)) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = rds_add_bound(rs, sin->sin_addr.s_addr, &sin->sin_port);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
2015-05-29 21:28:08 +00:00
|
|
|
if (rs->rs_transport) { /* previously bound */
|
2015-10-11 20:46:03 +00:00
|
|
|
trans = rs->rs_transport;
|
|
|
|
if (trans->laddr_check(sock_net(sock->sk),
|
|
|
|
sin->sin_addr.s_addr) != 0) {
|
|
|
|
ret = -ENOPROTOOPT;
|
|
|
|
rds_remove_bound(rs);
|
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
}
|
2015-05-29 21:28:08 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2015-08-05 05:43:25 +00:00
|
|
|
trans = rds_trans_get_preferred(sock_net(sock->sk),
|
|
|
|
sin->sin_addr.s_addr);
|
2010-01-12 19:56:44 +00:00
|
|
|
if (!trans) {
|
2009-02-24 15:30:18 +00:00
|
|
|
ret = -EADDRNOTAVAIL;
|
|
|
|
rds_remove_bound(rs);
|
2011-06-16 02:09:57 +00:00
|
|
|
printk_ratelimited(KERN_INFO "RDS: rds_bind() could not find a transport, "
|
2009-08-21 12:28:35 +00:00
|
|
|
"load rds_tcp or rds_rdma?\n");
|
2009-02-24 15:30:18 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
rs->rs_transport = trans;
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
out:
|
|
|
|
release_sock(sk);
|
|
|
|
return ret;
|
|
|
|
}
|
2014-02-12 03:34:25 +00:00
|
|
|
|
|
|
|
void rds_bind_lock_init(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < BIND_HASH_SIZE; i++)
|
|
|
|
rwlock_init(&bind_hash_table[i].lock);
|
|
|
|
}
|