Commit 0c3141e9 authored by Allan Stephens's avatar Allan Stephens Committed by David S. Miller

[TIPC]: Overhaul of socket locking logic

This patch modifies TIPC's socket code to follow the same approach
used by other protocols.  This change eliminates the need for a
mutex in the TIPC-specific portion of the socket protocol data
structure -- in its place, the standard Linux socket backlog queue
and associated locking routines are utilized.  These changes fix
a long-standing receive queue bug on SMP systems, and also enable
individual read and write threads to utilize a socket without
unnecessarily interfering with each other.
Signed-off-by: default avatarAllan Stephens <allan.stephens@windriver.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent b89741a0
...@@ -96,6 +96,12 @@ struct tipc_port *tipc_get_port(const u32 ref); ...@@ -96,6 +96,12 @@ struct tipc_port *tipc_get_port(const u32 ref);
void *tipc_get_handle(const u32 ref); void *tipc_get_handle(const u32 ref);
/*
* The following routines require that the port be locked on entry
*/
int tipc_disconnect_port(struct tipc_port *tp_ptr);
#endif #endif
......
...@@ -1240,6 +1240,28 @@ int tipc_connect2port(u32 ref, struct tipc_portid const *peer) ...@@ -1240,6 +1240,28 @@ int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
return res; return res;
} }
/**
* tipc_disconnect_port - disconnect port from peer
*
* Port must be locked.
*/
int tipc_disconnect_port(struct tipc_port *tp_ptr)
{
int res;
if (tp_ptr->connected) {
tp_ptr->connected = 0;
/* let timer expire on it's own to avoid deadlock! */
tipc_nodesub_unsubscribe(
&((struct port *)tp_ptr)->subscription);
res = TIPC_OK;
} else {
res = -ENOTCONN;
}
return res;
}
/* /*
* tipc_disconnect(): Disconnect port form peer. * tipc_disconnect(): Disconnect port form peer.
* This is a node local operation. * This is a node local operation.
...@@ -1248,17 +1270,12 @@ int tipc_connect2port(u32 ref, struct tipc_portid const *peer) ...@@ -1248,17 +1270,12 @@ int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
int tipc_disconnect(u32 ref) int tipc_disconnect(u32 ref)
{ {
struct port *p_ptr; struct port *p_ptr;
int res = -ENOTCONN; int res;
p_ptr = tipc_port_lock(ref); p_ptr = tipc_port_lock(ref);
if (!p_ptr) if (!p_ptr)
return -EINVAL; return -EINVAL;
if (p_ptr->publ.connected) { res = tipc_disconnect_port((struct tipc_port *)p_ptr);
p_ptr->publ.connected = 0;
/* let timer expire on it's own to avoid deadlock! */
tipc_nodesub_unsubscribe(&p_ptr->subscription);
res = TIPC_OK;
}
tipc_port_unlock(p_ptr); tipc_port_unlock(p_ptr);
return res; return res;
} }
......
...@@ -43,7 +43,6 @@ ...@@ -43,7 +43,6 @@
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/poll.h> #include <linux/poll.h>
#include <linux/fcntl.h> #include <linux/fcntl.h>
#include <linux/mutex.h>
#include <asm/string.h> #include <asm/string.h>
#include <asm/atomic.h> #include <asm/atomic.h>
#include <net/sock.h> #include <net/sock.h>
...@@ -64,11 +63,12 @@ ...@@ -64,11 +63,12 @@
struct tipc_sock { struct tipc_sock {
struct sock sk; struct sock sk;
struct tipc_port *p; struct tipc_port *p;
struct mutex lock;
}; };
#define tipc_sk(sk) ((struct tipc_sock*)sk) #define tipc_sk(sk) ((struct tipc_sock *)(sk))
#define tipc_sk_port(sk) ((struct tipc_port *)(tipc_sk(sk)->p))
static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf); static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
static void wakeupdispatch(struct tipc_port *tport); static void wakeupdispatch(struct tipc_port *tport);
...@@ -82,55 +82,115 @@ static int sockets_enabled = 0; ...@@ -82,55 +82,115 @@ static int sockets_enabled = 0;
static atomic_t tipc_queue_size = ATOMIC_INIT(0); static atomic_t tipc_queue_size = ATOMIC_INIT(0);
/* /*
* sock_lock(): Lock a port/socket pair. lock_sock() can * Revised TIPC socket locking policy:
* not be used here, since the same lock must protect ports *
* with non-socket interfaces. * Most socket operations take the standard socket lock when they start
* See net.c for description of locking policy. * and hold it until they finish (or until they need to sleep). Acquiring
* this lock grants the owner exclusive access to the fields of the socket
* data structures, with the exception of the backlog queue. A few socket
* operations can be done without taking the socket lock because they only
* read socket information that never changes during the life of the socket.
*
* Socket operations may acquire the lock for the associated TIPC port if they
* need to perform an operation on the port. If any routine needs to acquire
* both the socket lock and the port lock it must take the socket lock first
* to avoid the risk of deadlock.
*
* The dispatcher handling incoming messages cannot grab the socket lock in
* the standard fashion, since invoked it runs at the BH level and cannot block.
* Instead, it checks to see if the socket lock is currently owned by someone,
* and either handles the message itself or adds it to the socket's backlog
* queue; in the latter case the queued message is processed once the process
* owning the socket lock releases it.
*
* NOTE: Releasing the socket lock while an operation is sleeping overcomes
* the problem of a blocked socket operation preventing any other operations
* from occurring. However, applications must be careful if they have
* multiple threads trying to send (or receive) on the same socket, as these
* operations might interfere with each other. For example, doing a connect
* and a receive at the same time might allow the receive to consume the
* ACK message meant for the connect. While additional work could be done
* to try and overcome this, it doesn't seem to be worthwhile at the present.
*
* NOTE: Releasing the socket lock while an operation is sleeping also ensures
* that another operation that must be performed in a non-blocking manner is
* not delayed for very long because the lock has already been taken.
*
* NOTE: This code assumes that certain fields of a port/socket pair are
* constant over its lifetime; such fields can be examined without taking
* the socket lock and/or port lock, and do not need to be re-read even
* after resuming processing after waiting. These fields include:
* - socket type
* - pointer to socket sk structure (aka tipc_sock structure)
* - pointer to port structure
* - port reference
*/
/**
* advance_rx_queue - discard first buffer in socket receive queue
*
* Caller must hold socket lock
*/ */
static void sock_lock(struct tipc_sock* tsock)
static void advance_rx_queue(struct sock *sk)
{ {
spin_lock_bh(tsock->p->lock); buf_discard(__skb_dequeue(&sk->sk_receive_queue));
atomic_dec(&tipc_queue_size);
} }
/* /**
* sock_unlock(): Unlock a port/socket pair * discard_rx_queue - discard all buffers in socket receive queue
*
* Caller must hold socket lock
*/ */
static void sock_unlock(struct tipc_sock* tsock)
static void discard_rx_queue(struct sock *sk)
{ {
spin_unlock_bh(tsock->p->lock); struct sk_buff *buf;
while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
atomic_dec(&tipc_queue_size);
buf_discard(buf);
}
} }
/** /**
* advance_queue - discard first buffer in queue * reject_rx_queue - reject all buffers in socket receive queue
* @tsock: TIPC socket *
* Caller must hold socket lock
*/ */
static void advance_queue(struct tipc_sock *tsock) static void reject_rx_queue(struct sock *sk)
{ {
sock_lock(tsock); struct sk_buff *buf;
buf_discard(skb_dequeue(&tsock->sk.sk_receive_queue));
sock_unlock(tsock); while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
atomic_dec(&tipc_queue_size); tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
atomic_dec(&tipc_queue_size);
}
} }
/** /**
* tipc_create - create a TIPC socket * tipc_create - create a TIPC socket
* @net: network namespace (must be default network)
* @sock: pre-allocated socket structure * @sock: pre-allocated socket structure
* @protocol: protocol indicator (must be 0) * @protocol: protocol indicator (must be 0)
* *
* This routine creates and attaches a 'struct sock' to the 'struct socket', * This routine creates additional data structures used by the TIPC socket,
* then create and attaches a TIPC port to the 'struct sock' part. * initializes them, and links them together.
* *
* Returns 0 on success, errno otherwise * Returns 0 on success, errno otherwise
*/ */
static int tipc_create(struct net *net, struct socket *sock, int protocol) static int tipc_create(struct net *net, struct socket *sock, int protocol)
{ {
struct tipc_sock *tsock; const struct proto_ops *ops;
struct tipc_port *port; socket_state state;
struct sock *sk; struct sock *sk;
u32 ref; u32 portref;
/* Validate arguments */
if (net != &init_net) if (net != &init_net)
return -EAFNOSUPPORT; return -EAFNOSUPPORT;
...@@ -138,53 +198,56 @@ static int tipc_create(struct net *net, struct socket *sock, int protocol) ...@@ -138,53 +198,56 @@ static int tipc_create(struct net *net, struct socket *sock, int protocol)
if (unlikely(protocol != 0)) if (unlikely(protocol != 0))
return -EPROTONOSUPPORT; return -EPROTONOSUPPORT;
ref = tipc_createport_raw(NULL, &dispatch, &wakeupdispatch, TIPC_LOW_IMPORTANCE);
if (unlikely(!ref))
return -ENOMEM;
sock->state = SS_UNCONNECTED;
switch (sock->type) { switch (sock->type) {
case SOCK_STREAM: case SOCK_STREAM:
sock->ops = &stream_ops; ops = &stream_ops;
state = SS_UNCONNECTED;
break; break;
case SOCK_SEQPACKET: case SOCK_SEQPACKET:
sock->ops = &packet_ops; ops = &packet_ops;
state = SS_UNCONNECTED;
break; break;
case SOCK_DGRAM: case SOCK_DGRAM:
tipc_set_portunreliable(ref, 1);
/* fall through */
case SOCK_RDM: case SOCK_RDM:
tipc_set_portunreturnable(ref, 1); ops = &msg_ops;
sock->ops = &msg_ops; state = SS_READY;
sock->state = SS_READY;
break; break;
default: default:
tipc_deleteport(ref);
return -EPROTOTYPE; return -EPROTOTYPE;
} }
/* Allocate socket's protocol area */
sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto); sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
if (!sk) { if (sk == NULL)
tipc_deleteport(ref);
return -ENOMEM; return -ENOMEM;
}
sock_init_data(sock, sk); /* Allocate TIPC port for socket to use */
sk->sk_rcvtimeo = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
tsock = tipc_sk(sk); portref = tipc_createport_raw(sk, &dispatch, &wakeupdispatch,
port = tipc_get_port(ref); TIPC_LOW_IMPORTANCE);
if (unlikely(portref == 0)) {
sk_free(sk);
return -ENOMEM;
}
tsock->p = port; /* Finish initializing socket data structures */
port->usr_handle = tsock;
mutex_init(&tsock->lock); sock->ops = ops;
sock->state = state;
dbg("sock_create: %x\n",tsock); sock_init_data(sock, sk);
sk->sk_rcvtimeo = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
sk->sk_backlog_rcv = backlog_rcv;
tipc_sk(sk)->p = tipc_get_port(portref);
atomic_inc(&tipc_user_count); if (sock->state == SS_READY) {
tipc_set_portunreturnable(portref, 1);
if (sock->type == SOCK_DGRAM)
tipc_set_portunreliable(portref, 1);
}
atomic_inc(&tipc_user_count);
return 0; return 0;
} }
...@@ -207,52 +270,62 @@ static int tipc_create(struct net *net, struct socket *sock, int protocol) ...@@ -207,52 +270,62 @@ static int tipc_create(struct net *net, struct socket *sock, int protocol)
static int release(struct socket *sock) static int release(struct socket *sock)
{ {
struct tipc_sock *tsock = tipc_sk(sock->sk);
struct sock *sk = sock->sk; struct sock *sk = sock->sk;
int res = TIPC_OK; struct tipc_port *tport;
struct sk_buff *buf; struct sk_buff *buf;
int res;
dbg("sock_delete: %x\n",tsock); /*
if (!tsock) * Exit if socket isn't fully initialized (occurs when a failed accept()
return 0; * releases a pre-allocated child socket that was never used)
mutex_lock(&tsock->lock); */
if (!sock->sk) {
mutex_unlock(&tsock->lock); if (sk == NULL)
return 0; return 0;
}
/* Reject unreceived messages, unless no longer connected */ tport = tipc_sk_port(sk);
lock_sock(sk);
/*
* Reject all unreceived messages, except on an active connection
* (which disconnects locally & sends a 'FIN+' to peer)
*/
while (sock->state != SS_DISCONNECTING) { while (sock->state != SS_DISCONNECTING) {
sock_lock(tsock); buf = __skb_dequeue(&sk->sk_receive_queue);
buf = skb_dequeue(&sk->sk_receive_queue); if (buf == NULL)
if (!buf)
tsock->p->usr_handle = NULL;
sock_unlock(tsock);
if (!buf)
break; break;
atomic_dec(&tipc_queue_size);
if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf)))
buf_discard(buf); buf_discard(buf);
else else {
if ((sock->state == SS_CONNECTING) ||
(sock->state == SS_CONNECTED)) {
sock->state = SS_DISCONNECTING;
tipc_disconnect(tport->ref);
}
tipc_reject_msg(buf, TIPC_ERR_NO_PORT); tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
atomic_dec(&tipc_queue_size); }
} }
/* Delete TIPC port */ /*
* Delete TIPC port; this ensures no more messages are queued
* (also disconnects an active connection & sends a 'FIN-' to peer)
*/
res = tipc_deleteport(tsock->p->ref); res = tipc_deleteport(tport->ref);
sock->sk = NULL;
/* Discard any remaining messages */ /* Discard any remaining (connection-based) messages in receive queue */
while ((buf = skb_dequeue(&sk->sk_receive_queue))) { discard_rx_queue(sk);
buf_discard(buf);
atomic_dec(&tipc_queue_size);
}
mutex_unlock(&tsock->lock); /* Reject any messages that accumulated in backlog queue */
sock->state = SS_DISCONNECTING;
release_sock(sk);
sock_put(sk); sock_put(sk);
sock->sk = NULL;
atomic_dec(&tipc_user_count); atomic_dec(&tipc_user_count);
return res; return res;
...@@ -269,47 +342,32 @@ static int release(struct socket *sock) ...@@ -269,47 +342,32 @@ static int release(struct socket *sock)
* (i.e. a socket address length of 0) unbinds all names from the socket. * (i.e. a socket address length of 0) unbinds all names from the socket.
* *
* Returns 0 on success, errno otherwise * Returns 0 on success, errno otherwise
*
* NOTE: This routine doesn't need to take the socket lock since it doesn't
* access any non-constant socket information.
*/ */
static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len) static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
{ {
struct tipc_sock *tsock = tipc_sk(sock->sk);
struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
int res; u32 portref = tipc_sk_port(sock->sk)->ref;
if (mutex_lock_interruptible(&tsock->lock)) if (unlikely(!uaddr_len))
return -ERESTARTSYS; return tipc_withdraw(portref, 0, NULL);
if (unlikely(!uaddr_len)) { if (uaddr_len < sizeof(struct sockaddr_tipc))
res = tipc_withdraw(tsock->p->ref, 0, NULL); return -EINVAL;
goto exit; if (addr->family != AF_TIPC)
} return -EAFNOSUPPORT;
if (uaddr_len < sizeof(struct sockaddr_tipc)) {
res = -EINVAL;
goto exit;
}
if (addr->family != AF_TIPC) {
res = -EAFNOSUPPORT;
goto exit;
}
if (addr->addrtype == TIPC_ADDR_NAME) if (addr->addrtype == TIPC_ADDR_NAME)
addr->addr.nameseq.upper = addr->addr.nameseq.lower; addr->addr.nameseq.upper = addr->addr.nameseq.lower;
else if (addr->addrtype != TIPC_ADDR_NAMESEQ) { else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
res = -EAFNOSUPPORT; return -EAFNOSUPPORT;
goto exit;
}
if (addr->scope > 0) return (addr->scope > 0) ?
res = tipc_publish(tsock->p->ref, addr->scope, tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
&addr->addr.nameseq); tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
else
res = tipc_withdraw(tsock->p->ref, -addr->scope,
&addr->addr.nameseq);
exit:
mutex_unlock(&tsock->lock);
return res;
} }
/** /**
...@@ -320,30 +378,33 @@ static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len) ...@@ -320,30 +378,33 @@ static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
* @peer: 0 to obtain socket name, 1 to obtain peer socket name * @peer: 0 to obtain socket name, 1 to obtain peer socket name
* *
* Returns 0 on success, errno otherwise * Returns 0 on success, errno otherwise
*
* NOTE: This routine doesn't need to take the socket lock since it doesn't
* access any non-constant socket information.
*/ */
static int get_name(struct socket *sock, struct sockaddr *uaddr, static int get_name(struct socket *sock, struct sockaddr *uaddr,
int *uaddr_len, int peer) int *uaddr_len, int peer)
{ {
struct tipc_sock *tsock = tipc_sk(sock->sk);
struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
u32 portref = tipc_sk_port(sock->sk)->ref;
u32 res; u32 res;
if (mutex_lock_interruptible(&tsock->lock)) if (peer) {
return -ERESTARTSYS; res = tipc_peer(portref, &addr->addr.id);
if (res)
return res;
} else {
tipc_ownidentity(portref, &addr->addr.id);
}
*uaddr_len = sizeof(*addr); *uaddr_len = sizeof(*addr);
addr->addrtype = TIPC_ADDR_ID; addr->addrtype = TIPC_ADDR_ID;
addr->family = AF_TIPC; addr->family = AF_TIPC;
addr->scope = 0; addr->scope = 0;
if (peer)
res = tipc_peer(tsock->p->ref, &addr->addr.id);
else
res = tipc_ownidentity(tsock->p->ref, &addr->addr.id);
addr->addr.name.domain = 0; addr->addr.name.domain = 0;
mutex_unlock(&tsock->lock); return 0;
return res;
} }
/** /**
...@@ -414,7 +475,6 @@ static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m) ...@@ -414,7 +475,6 @@ static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
return 0; return 0;
if (likely(dest->addr.name.name.type == TIPC_TOP_SRV)) if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
return 0; return 0;
if (likely(dest->addr.name.name.type != TIPC_CFG_SRV)) if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
return -EACCES; return -EACCES;
...@@ -428,7 +488,7 @@ static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m) ...@@ -428,7 +488,7 @@ static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
/** /**
* send_msg - send message in connectionless manner * send_msg - send message in connectionless manner
* @iocb: (unused) * @iocb: if NULL, indicates that socket lock is already held
* @sock: socket structure * @sock: socket structure
* @m: message to send * @m: message to send
* @total_len: length of message * @total_len: length of message
...@@ -444,9 +504,9 @@ static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m) ...@@ -444,9 +504,9 @@ static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
static int send_msg(struct kiocb *iocb, struct socket *sock, static int send_msg(struct kiocb *iocb, struct socket *sock,
struct msghdr *m, size_t total_len) struct msghdr *m, size_t total_len)
{ {
struct tipc_sock *tsock = tipc_sk(sock->sk); struct sock *sk = sock->sk;
struct tipc_port *tport = tipc_sk_port(sk);
struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name; struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
struct sk_buff *buf;
int needs_conn; int needs_conn;
int res = -EINVAL; int res = -EINVAL;
...@@ -456,48 +516,46 @@ static int send_msg(struct kiocb *iocb, struct socket *sock, ...@@ -456,48 +516,46 @@ static int send_msg(struct kiocb *iocb, struct socket *sock,
(dest->family != AF_TIPC))) (dest->family != AF_TIPC)))
return -EINVAL; return -EINVAL;
if (iocb)
lock_sock(sk);
needs_conn = (sock->state != SS_READY); needs_conn = (sock->state != SS_READY);
if (unlikely(needs_conn)) { if (unlikely(needs_conn)) {
if (sock->state == SS_LISTENING) if (sock->state == SS_LISTENING) {
return -EPIPE; res = -EPIPE;
if (sock->state != SS_UNCONNECTED) goto exit;
return -EISCONN; }
if ((tsock->p->published) || if (sock->state != SS_UNCONNECTED) {
((sock->type == SOCK_STREAM) && (total_len != 0))) res = -EISCONN;
return -EOPNOTSUPP; goto exit;
}
if ((tport->published) ||
((sock->type == SOCK_STREAM) && (total_len != 0))) {
res = -EOPNOTSUPP;
goto exit;
}
if (dest->addrtype == TIPC_ADDR_NAME) { if (dest->addrtype == TIPC_ADDR_NAME) {
tsock->p->conn_type = dest->addr.name.name.type; tport->conn_type = dest->addr.name.name.type;
tsock->p->conn_instance = dest->addr.name.name.instance; tport->conn_instance = dest->addr.name.name.instance;
} }
}
if (mutex_lock_interruptible(&tsock->lock))
return -ERESTARTSYS;
if (needs_conn) {
/* Abort any pending connection attempts (very unlikely) */ /* Abort any pending connection attempts (very unlikely) */
while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) { reject_rx_queue(sk);
tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
atomic_dec(&tipc_queue_size);
}
sock->state = SS_CONNECTING;
} }
do { do {
if (dest->addrtype == TIPC_ADDR_NAME) { if (dest->addrtype == TIPC_ADDR_NAME) {
if ((res = dest_name_check(dest, m))) if ((res = dest_name_check(dest, m)))
goto exit; break;
res = tipc_send2name(tsock->p->ref, res = tipc_send2name(tport->ref,
&dest->addr.name.name, &dest->addr.name.name,
dest->addr.name.domain, dest->addr.name.domain,
m->msg_iovlen, m->msg_iovlen,
m->msg_iov); m->msg_iov);
} }
else if (dest->addrtype == TIPC_ADDR_ID) { else if (dest->addrtype == TIPC_ADDR_ID) {
res = tipc_send2port(tsock->p->ref, res = tipc_send2port(tport->ref,
&dest->addr.id, &dest->addr.id,
m->msg_iovlen, m->msg_iovlen,
m->msg_iov); m->msg_iov);
...@@ -505,36 +563,43 @@ static int send_msg(struct kiocb *iocb, struct socket *sock, ...@@ -505,36 +563,43 @@ static int send_msg(struct kiocb *iocb, struct socket *sock,
else if (dest->addrtype == TIPC_ADDR_MCAST) { else if (dest->addrtype == TIPC_ADDR_MCAST) {
if (needs_conn) { if (needs_conn) {
res = -EOPNOTSUPP; res = -EOPNOTSUPP;
goto exit; break;
} }
if ((res = dest_name_check(dest, m))) if ((res = dest_name_check(dest, m)))
goto exit; break;
res = tipc_multicast(tsock->p->ref, res = tipc_multicast(tport->ref,
&dest->addr.nameseq, &dest->addr.nameseq,
0, 0,
m->msg_iovlen, m->msg_iovlen,
m->msg_iov); m->msg_iov);
} }
if (likely(res != -ELINKCONG)) { if (likely(res != -ELINKCONG)) {
exit: if (needs_conn && (res >= 0)) {
mutex_unlock(&tsock->lock); sock->state = SS_CONNECTING;
return res; }
break;
} }
if (m->msg_flags & MSG_DONTWAIT) { if (m->msg_flags & MSG_DONTWAIT) {
res = -EWOULDBLOCK; res = -EWOULDBLOCK;
goto exit; break;
}
if (wait_event_interruptible(*sock->sk->sk_sleep,
!tsock->p->congested)) {
res = -ERESTARTSYS;
goto exit;
} }
release_sock(sk);
res = wait_event_interruptible(*sk->sk_sleep,
!tport->congested);
lock_sock(sk);
if (res)
break;
} while (1); } while (1);
exit:
if (iocb)
release_sock(sk);
return res;
} }
/** /**
* send_packet - send a connection-oriented message * send_packet - send a connection-oriented message
* @iocb: (unused) * @iocb: if NULL, indicates that socket lock is already held
* @sock: socket structure * @sock: socket structure
* @m: message to send * @m: message to send
* @total_len: length of message * @total_len: length of message
...@@ -547,7 +612,8 @@ static int send_msg(struct kiocb *iocb, struct socket *sock, ...@@ -547,7 +612,8 @@ static int send_msg(struct kiocb *iocb, struct socket *sock,
static int send_packet(struct kiocb *iocb, struct socket *sock, static int send_packet(struct kiocb *iocb, struct socket *sock,
struct msghdr *m, size_t total_len) struct msghdr *m, size_t total_len)
{ {
struct tipc_sock *tsock = tipc_sk(sock->sk); struct sock *sk = sock->sk;
struct tipc_port *tport = tipc_sk_port(sk);
struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name; struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
int res; int res;
...@@ -556,9 +622,8 @@ static int send_packet(struct kiocb *iocb, struct socket *sock, ...@@ -556,9 +622,8 @@ static int send_packet(struct kiocb *iocb, struct socket *sock,
if (unlikely(dest)) if (unlikely(dest))
return send_msg(iocb, sock, m, total_len); return send_msg(iocb, sock, m, total_len);
if (mutex_lock_interruptible(&tsock->lock)) { if (iocb)
return -ERESTARTSYS; lock_sock(sk);
}
do { do {
if (unlikely(sock->state != SS_CONNECTED)) { if (unlikely(sock->state != SS_CONNECTED)) {
...@@ -566,25 +631,28 @@ static int send_packet(struct kiocb *iocb, struct socket *sock, ...@@ -566,25 +631,28 @@ static int send_packet(struct kiocb *iocb, struct socket *sock,
res = -EPIPE; res = -EPIPE;
else else
res = -ENOTCONN; res = -ENOTCONN;
goto exit; break;
} }
res = tipc_send(tsock->p->ref, m->msg_iovlen, m->msg_iov); res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov);
if (likely(res != -ELINKCONG)) { if (likely(res != -ELINKCONG)) {
exit: break;
mutex_unlock(&tsock->lock);
return res;
} }
if (m->msg_flags & MSG_DONTWAIT) { if (m->msg_flags & MSG_DONTWAIT) {
res = -EWOULDBLOCK; res = -EWOULDBLOCK;
goto exit; break;
}
if (wait_event_interruptible(*sock->sk->sk_sleep,
!tsock->p->congested)) {
res = -ERESTARTSYS;
goto exit;
} }
release_sock(sk);
res = wait_event_interruptible(*sk->sk_sleep,
(!tport->congested || !tport->connected));
lock_sock(sk);
if (res)
break;
} while (1); } while (1);
if (iocb)
release_sock(sk);
return res;
} }
/** /**
...@@ -600,11 +668,11 @@ static int send_packet(struct kiocb *iocb, struct socket *sock, ...@@ -600,11 +668,11 @@ static int send_packet(struct kiocb *iocb, struct socket *sock,
* or errno if no data sent * or errno if no data sent
*/ */
static int send_stream(struct kiocb *iocb, struct socket *sock, static int send_stream(struct kiocb *iocb, struct socket *sock,
struct msghdr *m, size_t total_len) struct msghdr *m, size_t total_len)
{ {
struct tipc_port *tport; struct sock *sk = sock->sk;
struct tipc_port *tport = tipc_sk_port(sk);
struct msghdr my_msg; struct msghdr my_msg;
struct iovec my_iov; struct iovec my_iov;
struct iovec *curr_iov; struct iovec *curr_iov;
...@@ -616,19 +684,27 @@ static int send_stream(struct kiocb *iocb, struct socket *sock, ...@@ -616,19 +684,27 @@ static int send_stream(struct kiocb *iocb, struct socket *sock,
int bytes_sent; int bytes_sent;
int res; int res;
lock_sock(sk);
/* Handle special cases where there is no connection */ /* Handle special cases where there is no connection */
if (unlikely(sock->state != SS_CONNECTED)) { if (unlikely(sock->state != SS_CONNECTED)) {
if (sock->state == SS_UNCONNECTED) if (sock->state == SS_UNCONNECTED) {
return send_packet(iocb, sock, m, total_len); res = send_packet(NULL, sock, m, total_len);
else if (sock->state == SS_DISCONNECTING) goto exit;
return -EPIPE; } else if (sock->state == SS_DISCONNECTING) {
else res = -EPIPE;
return -ENOTCONN; goto exit;
} else {
res = -ENOTCONN;
goto exit;
}
} }
if (unlikely(m->msg_name)) if (unlikely(m->msg_name)) {
return -EISCONN; res = -EISCONN;
goto exit;
}
/* /*
* Send each iovec entry using one or more messages * Send each iovec entry using one or more messages
...@@ -646,7 +722,6 @@ static int send_stream(struct kiocb *iocb, struct socket *sock, ...@@ -646,7 +722,6 @@ static int send_stream(struct kiocb *iocb, struct socket *sock,
my_msg.msg_name = NULL; my_msg.msg_name = NULL;
bytes_sent = 0; bytes_sent = 0;
tport = tipc_sk(sock->sk)->p;
hdr_size = msg_hdr_sz(&tport->phdr); hdr_size = msg_hdr_sz(&tport->phdr);
while (curr_iovlen--) { while (curr_iovlen--) {
...@@ -661,10 +736,10 @@ static int send_stream(struct kiocb *iocb, struct socket *sock, ...@@ -661,10 +736,10 @@ static int send_stream(struct kiocb *iocb, struct socket *sock,
bytes_to_send = curr_left; bytes_to_send = curr_left;
my_iov.iov_base = curr_start; my_iov.iov_base = curr_start;
my_iov.iov_len = bytes_to_send; my_iov.iov_len = bytes_to_send;
if ((res = send_packet(iocb, sock, &my_msg, 0)) < 0) { if ((res = send_packet(NULL, sock, &my_msg, 0)) < 0) {
if (bytes_sent != 0) if (bytes_sent)
res = bytes_sent; res = bytes_sent;
return res; goto exit;
} }
curr_left -= bytes_to_send; curr_left -= bytes_to_send;
curr_start += bytes_to_send; curr_start += bytes_to_send;
...@@ -673,22 +748,23 @@ static int send_stream(struct kiocb *iocb, struct socket *sock, ...@@ -673,22 +748,23 @@ static int send_stream(struct kiocb *iocb, struct socket *sock,
curr_iov++; curr_iov++;
} }
res = bytes_sent;
return bytes_sent; exit:
release_sock(sk);
return res;
} }
/** /**
* auto_connect - complete connection setup to a remote port * auto_connect - complete connection setup to a remote port
* @sock: socket structure * @sock: socket structure
* @tsock: TIPC-specific socket structure
* @msg: peer's response message * @msg: peer's response message
* *
* Returns 0 on success, errno otherwise * Returns 0 on success, errno otherwise
*/ */
static int auto_connect(struct socket *sock, struct tipc_sock *tsock, static int auto_connect(struct socket *sock, struct tipc_msg *msg)
struct tipc_msg *msg)
{ {
struct tipc_port *tport = tipc_sk_port(sock->sk);
struct tipc_portid peer; struct tipc_portid peer;
if (msg_errcode(msg)) { if (msg_errcode(msg)) {
...@@ -698,8 +774,8 @@ static int auto_connect(struct socket *sock, struct tipc_sock *tsock, ...@@ -698,8 +774,8 @@ static int auto_connect(struct socket *sock, struct tipc_sock *tsock,
peer.ref = msg_origport(msg); peer.ref = msg_origport(msg);
peer.node = msg_orignode(msg); peer.node = msg_orignode(msg);
tipc_connect2port(tsock->p->ref, &peer); tipc_connect2port(tport->ref, &peer);
tipc_set_portimportance(tsock->p->ref, msg_importance(msg)); tipc_set_portimportance(tport->ref, msg_importance(msg));
sock->state = SS_CONNECTED; sock->state = SS_CONNECTED;
return 0; return 0;
} }
...@@ -812,62 +888,54 @@ static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg, ...@@ -812,62 +888,54 @@ static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
static int recv_msg(struct kiocb *iocb, struct socket *sock, static int recv_msg(struct kiocb *iocb, struct socket *sock,
struct msghdr *m, size_t buf_len, int flags) struct msghdr *m, size_t buf_len, int flags)
{ {
struct tipc_sock *tsock = tipc_sk(sock->sk); struct sock *sk = sock->sk;
struct tipc_port *tport = tipc_sk_port(sk);
struct sk_buff *buf; struct sk_buff *buf;
struct tipc_msg *msg; struct tipc_msg *msg;
unsigned int q_len;
unsigned int sz; unsigned int sz;
u32 err; u32 err;
int res; int res;
/* Currently doesn't support receiving into multiple iovec entries */ /* Catch invalid receive requests */
if (m->msg_iovlen != 1) if (m->msg_iovlen != 1)
return -EOPNOTSUPP; return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
/* Catch invalid receive attempts */
if (unlikely(!buf_len)) if (unlikely(!buf_len))
return -EINVAL; return -EINVAL;
if (sock->type == SOCK_SEQPACKET) { lock_sock(sk);
if (unlikely(sock->state == SS_UNCONNECTED))
return -ENOTCONN;
if (unlikely((sock->state == SS_DISCONNECTING) &&
(skb_queue_len(&sock->sk->sk_receive_queue) == 0)))
return -ENOTCONN;
}
/* Look for a message in receive queue; wait if necessary */ if (unlikely(sock->state == SS_UNCONNECTED)) {
res = -ENOTCONN;
if (unlikely(mutex_lock_interruptible(&tsock->lock)))
return -ERESTARTSYS;
restart:
if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
(flags & MSG_DONTWAIT))) {
res = -EWOULDBLOCK;
goto exit; goto exit;
} }
if ((res = wait_event_interruptible( restart:
*sock->sk->sk_sleep,
((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
(sock->state == SS_DISCONNECTING))) )) {
goto exit;
}
/* Catch attempt to receive on an already terminated connection */ /* Look for a message in receive queue; wait if necessary */
/* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
if (!q_len) { while (skb_queue_empty(&sk->sk_receive_queue)) {
res = -ENOTCONN; if (sock->state == SS_DISCONNECTING) {
goto exit; res = -ENOTCONN;
goto exit;
}
if (flags & MSG_DONTWAIT) {
res = -EWOULDBLOCK;
goto exit;
}
release_sock(sk);
res = wait_event_interruptible(*sk->sk_sleep,
(!skb_queue_empty(&sk->sk_receive_queue) ||
(sock->state == SS_DISCONNECTING)));
lock_sock(sk);
if (res)
goto exit;
} }
/* Get access to first message in receive queue */ /* Look at first message in receive queue */
buf = skb_peek(&sock->sk->sk_receive_queue); buf = skb_peek(&sk->sk_receive_queue);
msg = buf_msg(buf); msg = buf_msg(buf);
sz = msg_data_sz(msg); sz = msg_data_sz(msg);
err = msg_errcode(msg); err = msg_errcode(msg);
...@@ -875,14 +943,15 @@ static int recv_msg(struct kiocb *iocb, struct socket *sock, ...@@ -875,14 +943,15 @@ static int recv_msg(struct kiocb *iocb, struct socket *sock,
/* Complete connection setup for an implied connect */ /* Complete connection setup for an implied connect */
if (unlikely(sock->state == SS_CONNECTING)) { if (unlikely(sock->state == SS_CONNECTING)) {
if ((res = auto_connect(sock, tsock, msg))) res = auto_connect(sock, msg);
if (res)
goto exit; goto exit;
} }
/* Discard an empty non-errored message & try again */ /* Discard an empty non-errored message & try again */
if ((!sz) && (!err)) { if ((!sz) && (!err)) {
advance_queue(tsock); advance_rx_queue(sk);
goto restart; goto restart;
} }
...@@ -892,7 +961,8 @@ static int recv_msg(struct kiocb *iocb, struct socket *sock, ...@@ -892,7 +961,8 @@ static int recv_msg(struct kiocb *iocb, struct socket *sock,
/* Capture ancillary data (optional) */ /* Capture ancillary data (optional) */
if ((res = anc_data_recv(m, msg, tsock->p))) res = anc_data_recv(m, msg, tport);
if (res)
goto exit; goto exit;
/* Capture message data (if valid) & compute return value (always) */ /* Capture message data (if valid) & compute return value (always) */
...@@ -920,12 +990,12 @@ static int recv_msg(struct kiocb *iocb, struct socket *sock, ...@@ -920,12 +990,12 @@ static int recv_msg(struct kiocb *iocb, struct socket *sock,
if (likely(!(flags & MSG_PEEK))) { if (likely(!(flags & MSG_PEEK))) {
if ((sock->state != SS_READY) && if ((sock->state != SS_READY) &&
(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN)) (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked); tipc_acknowledge(tport->ref, tport->conn_unacked);
advance_queue(tsock); advance_rx_queue(sk);
} }
exit: exit:
mutex_unlock(&tsock->lock); release_sock(sk);
return res; return res;
} }
...@@ -945,10 +1015,10 @@ static int recv_msg(struct kiocb *iocb, struct socket *sock, ...@@ -945,10 +1015,10 @@ static int recv_msg(struct kiocb *iocb, struct socket *sock,
static int recv_stream(struct kiocb *iocb, struct socket *sock, static int recv_stream(struct kiocb *iocb, struct socket *sock,
struct msghdr *m, size_t buf_len, int flags) struct msghdr *m, size_t buf_len, int flags)
{ {
struct tipc_sock *tsock = tipc_sk(sock->sk); struct sock *sk = sock->sk;
struct tipc_port *tport = tipc_sk_port(sk);
struct sk_buff *buf; struct sk_buff *buf;
struct tipc_msg *msg; struct tipc_msg *msg;
unsigned int q_len;
unsigned int sz; unsigned int sz;
int sz_to_copy; int sz_to_copy;
int sz_copied = 0; int sz_copied = 0;
...@@ -956,54 +1026,49 @@ static int recv_stream(struct kiocb *iocb, struct socket *sock, ...@@ -956,54 +1026,49 @@ static int recv_stream(struct kiocb *iocb, struct socket *sock,
char __user *crs = m->msg_iov->iov_base; char __user *crs = m->msg_iov->iov_base;
unsigned char *buf_crs; unsigned char *buf_crs;
u32 err; u32 err;
int res; int res = 0;
/* Currently doesn't support receiving into multiple iovec entries */ /* Catch invalid receive attempts */
if (m->msg_iovlen != 1) if (m->msg_iovlen != 1)
return -EOPNOTSUPP; return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
/* Catch invalid receive attempts */
if (unlikely(!buf_len)) if (unlikely(!buf_len))
return -EINVAL; return -EINVAL;
if (unlikely(sock->state == SS_DISCONNECTING)) { lock_sock(sk);
if (skb_queue_len(&sock->sk->sk_receive_queue) == 0)
return -ENOTCONN;
} else if (unlikely(sock->state != SS_CONNECTED))
return -ENOTCONN;
/* Look for a message in receive queue; wait if necessary */
if (unlikely(mutex_lock_interruptible(&tsock->lock)))
return -ERESTARTSYS;
restart: if (unlikely((sock->state == SS_UNCONNECTED) ||
if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) && (sock->state == SS_CONNECTING))) {
(flags & MSG_DONTWAIT))) { res = -ENOTCONN;
res = -EWOULDBLOCK;
goto exit; goto exit;
} }
if ((res = wait_event_interruptible( restart:
*sock->sk->sk_sleep,
((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
(sock->state == SS_DISCONNECTING))) )) {
goto exit;
}
/* Catch attempt to receive on an already terminated connection */ /* Look for a message in receive queue; wait if necessary */
/* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
if (!q_len) { while (skb_queue_empty(&sk->sk_receive_queue)) {
res = -ENOTCONN; if (sock->state == SS_DISCONNECTING) {
goto exit; res = -ENOTCONN;
goto exit;
}
if (flags & MSG_DONTWAIT) {
res = -EWOULDBLOCK;
goto exit;
}
release_sock(sk);
res = wait_event_interruptible(*sk->sk_sleep,
(!skb_queue_empty(&sk->sk_receive_queue) ||
(sock->state == SS_DISCONNECTING)));
lock_sock(sk);
if (res)
goto exit;
} }
/* Get access to first message in receive queue */ /* Look at first message in receive queue */
buf = skb_peek(&sock->sk->sk_receive_queue); buf = skb_peek(&sk->sk_receive_queue);
msg = buf_msg(buf); msg = buf_msg(buf);
sz = msg_data_sz(msg); sz = msg_data_sz(msg);
err = msg_errcode(msg); err = msg_errcode(msg);
...@@ -1011,7 +1076,7 @@ static int recv_stream(struct kiocb *iocb, struct socket *sock, ...@@ -1011,7 +1076,7 @@ static int recv_stream(struct kiocb *iocb, struct socket *sock,
/* Discard an empty non-errored message & try again */ /* Discard an empty non-errored message & try again */
if ((!sz) && (!err)) { if ((!sz) && (!err)) {
advance_queue(tsock); advance_rx_queue(sk);
goto restart; goto restart;
} }
...@@ -1019,7 +1084,8 @@ static int recv_stream(struct kiocb *iocb, struct socket *sock, ...@@ -1019,7 +1084,8 @@ static int recv_stream(struct kiocb *iocb, struct socket *sock,
if (sz_copied == 0) { if (sz_copied == 0) {
set_orig_addr(m, msg); set_orig_addr(m, msg);
if ((res = anc_data_recv(m, msg, tsock->p))) res = anc_data_recv(m, msg, tport);
if (res)
goto exit; goto exit;
} }
...@@ -1057,9 +1123,9 @@ static int recv_stream(struct kiocb *iocb, struct socket *sock, ...@@ -1057,9 +1123,9 @@ static int recv_stream(struct kiocb *iocb, struct socket *sock,
/* Consume received message (optional) */ /* Consume received message (optional) */
if (likely(!(flags & MSG_PEEK))) { if (likely(!(flags & MSG_PEEK))) {
if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN)) if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked); tipc_acknowledge(tport->ref, tport->conn_unacked);
advance_queue(tsock); advance_rx_queue(sk);
} }
/* Loop around if more data is required */ /* Loop around if more data is required */
...@@ -1074,7 +1140,7 @@ static int recv_stream(struct kiocb *iocb, struct socket *sock, ...@@ -1074,7 +1140,7 @@ static int recv_stream(struct kiocb *iocb, struct socket *sock,
goto restart; goto restart;
exit: exit:
mutex_unlock(&tsock->lock); release_sock(sk);
return sz_copied ? sz_copied : res; return sz_copied ? sz_copied : res;
} }
...@@ -1108,37 +1174,24 @@ static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base) ...@@ -1108,37 +1174,24 @@ static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base)
} }
/** /**
* async_disconnect - wrapper function used to disconnect port * filter_rcv - validate incoming message
* @portref: TIPC port reference (passed as pointer-sized value) * @sk: socket
*/
static void async_disconnect(unsigned long portref)
{
tipc_disconnect((u32)portref);
}
/**
* dispatch - handle arriving message
* @tport: TIPC port that received message
* @buf: message * @buf: message
* *
* Called with port locked. Must not take socket lock to avoid deadlock risk. * Enqueues message on receive queue if acceptable; optionally handles
* disconnect indication for a connected socket.
*
* Called with socket lock already taken; port lock may also be taken.
* *
* Returns TIPC error status code (TIPC_OK if message is not to be rejected) * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
*/ */
static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf) static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
{ {
struct socket *sock = sk->sk_socket;
struct tipc_msg *msg = buf_msg(buf); struct tipc_msg *msg = buf_msg(buf);
struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
struct socket *sock;
u32 recv_q_len; u32 recv_q_len;
/* Reject message if socket is closing */
if (!tsock)
return TIPC_ERR_NO_PORT;
/* Reject message if it is wrong sort of message for socket */ /* Reject message if it is wrong sort of message for socket */
/* /*
...@@ -1146,7 +1199,7 @@ static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf) ...@@ -1146,7 +1199,7 @@ static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
* "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
* BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
*/ */
sock = tsock->sk.sk_socket;
if (sock->state == SS_READY) { if (sock->state == SS_READY) {
if (msg_connected(msg)) { if (msg_connected(msg)) {
msg_dbg(msg, "dispatch filter 1\n"); msg_dbg(msg, "dispatch filter 1\n");
...@@ -1194,45 +1247,98 @@ static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf) ...@@ -1194,45 +1247,98 @@ static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE)) if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE))
return TIPC_ERR_OVERLOAD; return TIPC_ERR_OVERLOAD;
} }
recv_q_len = skb_queue_len(&tsock->sk.sk_receive_queue); recv_q_len = skb_queue_len(&sk->sk_receive_queue);
if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) { if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) {
if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2)) if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2))
return TIPC_ERR_OVERLOAD; return TIPC_ERR_OVERLOAD;
} }
/* Enqueue message (finally!) */
msg_dbg(msg, "<DISP<: ");
TIPC_SKB_CB(buf)->handle = msg_data(msg);
atomic_inc(&tipc_queue_size);
__skb_queue_tail(&sk->sk_receive_queue, buf);
/* Initiate connection termination for an incoming 'FIN' */ /* Initiate connection termination for an incoming 'FIN' */
if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) { if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
sock->state = SS_DISCONNECTING; sock->state = SS_DISCONNECTING;
/* Note: Use signal since port lock is already taken! */ tipc_disconnect_port(tipc_sk_port(sk));
tipc_k_signal((Handler)async_disconnect, tport->ref);
} }
/* Enqueue message (finally!) */ if (waitqueue_active(sk->sk_sleep))
wake_up_interruptible(sk->sk_sleep);
return TIPC_OK;
}
msg_dbg(msg,"<DISP<: "); /**
TIPC_SKB_CB(buf)->handle = msg_data(msg); * backlog_rcv - handle incoming message from backlog queue
atomic_inc(&tipc_queue_size); * @sk: socket
skb_queue_tail(&sock->sk->sk_receive_queue, buf); * @buf: message
*
* Caller must hold socket lock, but not port lock.
*
* Returns 0
*/
if (waitqueue_active(sock->sk->sk_sleep)) static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
wake_up_interruptible(sock->sk->sk_sleep); {
return TIPC_OK; u32 res;
res = filter_rcv(sk, buf);
if (res)
tipc_reject_msg(buf, res);
return 0;
}
/**
* dispatch - handle incoming message
* @tport: TIPC port that received message
* @buf: message
*
* Called with port lock already taken.
*
* Returns TIPC error status code (TIPC_OK if message is not to be rejected)
*/
static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
{
struct sock *sk = (struct sock *)tport->usr_handle;
u32 res;
/*
* Process message if socket is unlocked; otherwise add to backlog queue
*
* This code is based on sk_receive_skb(), but must be distinct from it
* since a TIPC-specific filter/reject mechanism is utilized
*/
bh_lock_sock(sk);
if (!sock_owned_by_user(sk)) {
res = filter_rcv(sk, buf);
} else {
sk_add_backlog(sk, buf);
res = TIPC_OK;
}
bh_unlock_sock(sk);
return res;
} }
/** /**
* wakeupdispatch - wake up port after congestion * wakeupdispatch - wake up port after congestion
* @tport: port to wakeup * @tport: port to wakeup
* *
* Called with port lock on. * Called with port lock already taken.
*/ */
static void wakeupdispatch(struct tipc_port *tport) static void wakeupdispatch(struct tipc_port *tport)
{ {
struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle; struct sock *sk = (struct sock *)tport->usr_handle;
if (waitqueue_active(tsock->sk.sk_sleep)) if (waitqueue_active(sk->sk_sleep))
wake_up_interruptible(tsock->sk.sk_sleep); wake_up_interruptible(sk->sk_sleep);
} }
/** /**
...@@ -1240,7 +1346,7 @@ static void wakeupdispatch(struct tipc_port *tport) ...@@ -1240,7 +1346,7 @@ static void wakeupdispatch(struct tipc_port *tport)
* @sock: socket structure * @sock: socket structure
* @dest: socket address for destination port * @dest: socket address for destination port
* @destlen: size of socket address data structure * @destlen: size of socket address data structure
* @flags: (unused) * @flags: file-related flags associated with socket
* *
* Returns 0 on success, errno otherwise * Returns 0 on success, errno otherwise
*/ */
...@@ -1248,31 +1354,43 @@ static void wakeupdispatch(struct tipc_port *tport) ...@@ -1248,31 +1354,43 @@ static void wakeupdispatch(struct tipc_port *tport)
static int connect(struct socket *sock, struct sockaddr *dest, int destlen, static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
int flags) int flags)
{ {
struct tipc_sock *tsock = tipc_sk(sock->sk); struct sock *sk = sock->sk;
struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest; struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
struct msghdr m = {NULL,}; struct msghdr m = {NULL,};
struct sk_buff *buf; struct sk_buff *buf;
struct tipc_msg *msg; struct tipc_msg *msg;
int res; int res;
lock_sock(sk);
/* For now, TIPC does not allow use of connect() with DGRAM/RDM types */ /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
if (sock->state == SS_READY) if (sock->state == SS_READY) {
return -EOPNOTSUPP; res = -EOPNOTSUPP;
goto exit;
}
/* For now, TIPC does not support the non-blocking form of connect() */ /* For now, TIPC does not support the non-blocking form of connect() */
if (flags & O_NONBLOCK) if (flags & O_NONBLOCK) {
return -EWOULDBLOCK; res = -EWOULDBLOCK;
goto exit;
}
/* Issue Posix-compliant error code if socket is in the wrong state */ /* Issue Posix-compliant error code if socket is in the wrong state */
if (sock->state == SS_LISTENING) if (sock->state == SS_LISTENING) {
return -EOPNOTSUPP; res = -EOPNOTSUPP;
if (sock->state == SS_CONNECTING) goto exit;
return -EALREADY; }
if (sock->state != SS_UNCONNECTED) if (sock->state == SS_CONNECTING) {
return -EISCONN; res = -EALREADY;
goto exit;
}
if (sock->state != SS_UNCONNECTED) {
res = -EISCONN;
goto exit;
}
/* /*
* Reject connection attempt using multicast address * Reject connection attempt using multicast address
...@@ -1281,8 +1399,14 @@ static int connect(struct socket *sock, struct sockaddr *dest, int destlen, ...@@ -1281,8 +1399,14 @@ static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
* so there's no need to do it here * so there's no need to do it here
*/ */
if (dst->addrtype == TIPC_ADDR_MCAST) if (dst->addrtype == TIPC_ADDR_MCAST) {
return -EINVAL; res = -EINVAL;
goto exit;
}
/* Reject any messages already in receive queue (very unlikely) */
reject_rx_queue(sk);
/* Send a 'SYN-' to destination */ /* Send a 'SYN-' to destination */
...@@ -1290,25 +1414,33 @@ static int connect(struct socket *sock, struct sockaddr *dest, int destlen, ...@@ -1290,25 +1414,33 @@ static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
m.msg_namelen = destlen; m.msg_namelen = destlen;
res = send_msg(NULL, sock, &m, 0); res = send_msg(NULL, sock, &m, 0);
if (res < 0) { if (res < 0) {
sock->state = SS_DISCONNECTING; goto exit;
return res;
} }
if (mutex_lock_interruptible(&tsock->lock)) /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
return -ERESTARTSYS;
/* Wait for destination's 'ACK' response */ release_sock(sk);
res = wait_event_interruptible_timeout(*sk->sk_sleep,
(!skb_queue_empty(&sk->sk_receive_queue) ||
(sock->state != SS_CONNECTING)),
sk->sk_rcvtimeo);
lock_sock(sk);
res = wait_event_interruptible_timeout(*sock->sk->sk_sleep,
skb_queue_len(&sock->sk->sk_receive_queue),
sock->sk->sk_rcvtimeo);
buf = skb_peek(&sock->sk->sk_receive_queue);
if (res > 0) { if (res > 0) {
msg = buf_msg(buf); buf = skb_peek(&sk->sk_receive_queue);
res = auto_connect(sock, tsock, msg); if (buf != NULL) {
if (!res) { msg = buf_msg(buf);
if (!msg_data_sz(msg)) res = auto_connect(sock, msg);
advance_queue(tsock); if (!res) {
if (!msg_data_sz(msg))
advance_rx_queue(sk);
}
} else {
if (sock->state == SS_CONNECTED) {
res = -EISCONN;
} else {
res = -ECONNREFUSED;
}
} }
} else { } else {
if (res == 0) if (res == 0)
...@@ -1318,7 +1450,8 @@ static int connect(struct socket *sock, struct sockaddr *dest, int destlen, ...@@ -1318,7 +1450,8 @@ static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
sock->state = SS_DISCONNECTING; sock->state = SS_DISCONNECTING;
} }
mutex_unlock(&tsock->lock); exit:
release_sock(sk);
return res; return res;
} }
...@@ -1332,14 +1465,22 @@ static int connect(struct socket *sock, struct sockaddr *dest, int destlen, ...@@ -1332,14 +1465,22 @@ static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
static int listen(struct socket *sock, int len) static int listen(struct socket *sock, int len)
{ {
/* REQUIRES SOCKET LOCKING OF SOME SORT? */ struct sock *sk = sock->sk;
int res;
lock_sock(sk);
if (sock->state == SS_READY) if (sock->state == SS_READY)
return -EOPNOTSUPP; res = -EOPNOTSUPP;
if (sock->state != SS_UNCONNECTED) else if (sock->state != SS_UNCONNECTED)
return -EINVAL; res = -EINVAL;
sock->state = SS_LISTENING; else {
return 0; sock->state = SS_LISTENING;
res = 0;
}
release_sock(sk);
return res;
} }
/** /**
...@@ -1351,50 +1492,69 @@ static int listen(struct socket *sock, int len) ...@@ -1351,50 +1492,69 @@ static int listen(struct socket *sock, int len)
* Returns 0 on success, errno otherwise * Returns 0 on success, errno otherwise
*/ */
static int accept(struct socket *sock, struct socket *newsock, int flags) static int accept(struct socket *sock, struct socket *new_sock, int flags)
{ {
struct tipc_sock *tsock = tipc_sk(sock->sk); struct sock *sk = sock->sk;
struct sk_buff *buf; struct sk_buff *buf;
int res = -EFAULT; int res;
if (sock->state == SS_READY)
return -EOPNOTSUPP;
if (sock->state != SS_LISTENING)
return -EINVAL;
if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
(flags & O_NONBLOCK)))
return -EWOULDBLOCK;
if (mutex_lock_interruptible(&tsock->lock)) lock_sock(sk);
return -ERESTARTSYS;
if (wait_event_interruptible(*sock->sk->sk_sleep, if (sock->state == SS_READY) {
skb_queue_len(&sock->sk->sk_receive_queue))) { res = -EOPNOTSUPP;
res = -ERESTARTSYS; goto exit;
}
if (sock->state != SS_LISTENING) {
res = -EINVAL;
goto exit; goto exit;
} }
buf = skb_peek(&sock->sk->sk_receive_queue);
res = tipc_create(sock_net(sock->sk), newsock, 0); while (skb_queue_empty(&sk->sk_receive_queue)) {
if (flags & O_NONBLOCK) {
res = -EWOULDBLOCK;
goto exit;
}
release_sock(sk);
res = wait_event_interruptible(*sk->sk_sleep,
(!skb_queue_empty(&sk->sk_receive_queue)));
lock_sock(sk);
if (res)
goto exit;
}
buf = skb_peek(&sk->sk_receive_queue);
res = tipc_create(sock_net(sock->sk), new_sock, 0);
if (!res) { if (!res) {
struct tipc_sock *new_tsock = tipc_sk(newsock->sk); struct sock *new_sk = new_sock->sk;
struct tipc_port *new_tport = tipc_sk_port(new_sk);
u32 new_ref = new_tport->ref;
struct tipc_portid id; struct tipc_portid id;
struct tipc_msg *msg = buf_msg(buf); struct tipc_msg *msg = buf_msg(buf);
u32 new_ref = new_tsock->p->ref;
lock_sock(new_sk);
/*
* Reject any stray messages received by new socket
* before the socket lock was taken (very, very unlikely)
*/
reject_rx_queue(new_sk);
/* Connect new socket to it's peer */
id.ref = msg_origport(msg); id.ref = msg_origport(msg);
id.node = msg_orignode(msg); id.node = msg_orignode(msg);
tipc_connect2port(new_ref, &id); tipc_connect2port(new_ref, &id);
newsock->state = SS_CONNECTED; new_sock->state = SS_CONNECTED;
tipc_set_portimportance(new_ref, msg_importance(msg)); tipc_set_portimportance(new_ref, msg_importance(msg));
if (msg_named(msg)) { if (msg_named(msg)) {
new_tsock->p->conn_type = msg_nametype(msg); new_tport->conn_type = msg_nametype(msg);
new_tsock->p->conn_instance = msg_nameinst(msg); new_tport->conn_instance = msg_nameinst(msg);
} }
/* /*
* Respond to 'SYN-' by discarding it & returning 'ACK'-. * Respond to 'SYN-' by discarding it & returning 'ACK'-.
* Respond to 'SYN+' by queuing it on new socket. * Respond to 'SYN+' by queuing it on new socket.
*/ */
...@@ -1403,17 +1563,16 @@ static int accept(struct socket *sock, struct socket *newsock, int flags) ...@@ -1403,17 +1563,16 @@ static int accept(struct socket *sock, struct socket *newsock, int flags)
if (!msg_data_sz(msg)) { if (!msg_data_sz(msg)) {
struct msghdr m = {NULL,}; struct msghdr m = {NULL,};
send_packet(NULL, newsock, &m, 0); advance_rx_queue(sk);
advance_queue(tsock); send_packet(NULL, new_sock, &m, 0);
} else { } else {
sock_lock(tsock); __skb_dequeue(&sk->sk_receive_queue);
skb_dequeue(&sock->sk->sk_receive_queue); __skb_queue_head(&new_sk->sk_receive_queue, buf);
sock_unlock(tsock);
skb_queue_head(&newsock->sk->sk_receive_queue, buf);
} }
release_sock(new_sk);
} }
exit: exit:
mutex_unlock(&tsock->lock); release_sock(sk);
return res; return res;
} }
...@@ -1429,54 +1588,46 @@ static int accept(struct socket *sock, struct socket *newsock, int flags) ...@@ -1429,54 +1588,46 @@ static int accept(struct socket *sock, struct socket *newsock, int flags)
static int shutdown(struct socket *sock, int how) static int shutdown(struct socket *sock, int how)
{ {
struct tipc_sock* tsock = tipc_sk(sock->sk); struct sock *sk = sock->sk;
struct tipc_port *tport = tipc_sk_port(sk);
struct sk_buff *buf; struct sk_buff *buf;
int res; int res;
if (how != SHUT_RDWR) if (how != SHUT_RDWR)
return -EINVAL; return -EINVAL;
if (mutex_lock_interruptible(&tsock->lock)) lock_sock(sk);
return -ERESTARTSYS;
sock_lock(tsock);
switch (sock->state) { switch (sock->state) {
case SS_CONNECTING:
case SS_CONNECTED: case SS_CONNECTED:
/* Send 'FIN+' or 'FIN-' message to peer */ /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
sock_unlock(tsock);
restart: restart:
if ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) { buf = __skb_dequeue(&sk->sk_receive_queue);
if (buf) {
atomic_dec(&tipc_queue_size); atomic_dec(&tipc_queue_size);
if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) { if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) {
buf_discard(buf); buf_discard(buf);
goto restart; goto restart;
} }
tipc_disconnect(tport->ref);
tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN); tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
} else {
tipc_shutdown(tport->ref);
} }
else {
tipc_shutdown(tsock->p->ref); sock->state = SS_DISCONNECTING;
}
sock_lock(tsock);
/* fall through */ /* fall through */
case SS_DISCONNECTING: case SS_DISCONNECTING:
/* Discard any unreceived messages */ /* Discard any unreceived messages; wake up sleeping tasks */
while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
atomic_dec(&tipc_queue_size);
buf_discard(buf);
}
tsock->p->conn_unacked = 0;
/* fall through */ discard_rx_queue(sk);
if (waitqueue_active(sk->sk_sleep))
case SS_CONNECTING: wake_up_interruptible(sk->sk_sleep);
sock->state = SS_DISCONNECTING;
res = 0; res = 0;
break; break;
...@@ -1484,9 +1635,7 @@ static int shutdown(struct socket *sock, int how) ...@@ -1484,9 +1635,7 @@ static int shutdown(struct socket *sock, int how)
res = -ENOTCONN; res = -ENOTCONN;
} }
sock_unlock(tsock); release_sock(sk);
mutex_unlock(&tsock->lock);
return res; return res;
} }
...@@ -1507,7 +1656,8 @@ static int shutdown(struct socket *sock, int how) ...@@ -1507,7 +1656,8 @@ static int shutdown(struct socket *sock, int how)
static int setsockopt(struct socket *sock, static int setsockopt(struct socket *sock,
int lvl, int opt, char __user *ov, int ol) int lvl, int opt, char __user *ov, int ol)
{ {
struct tipc_sock *tsock = tipc_sk(sock->sk); struct sock *sk = sock->sk;
struct tipc_port *tport = tipc_sk_port(sk);
u32 value; u32 value;
int res; int res;
...@@ -1520,30 +1670,31 @@ static int setsockopt(struct socket *sock, ...@@ -1520,30 +1670,31 @@ static int setsockopt(struct socket *sock,
if ((res = get_user(value, (u32 __user *)ov))) if ((res = get_user(value, (u32 __user *)ov)))
return res; return res;
if (mutex_lock_interruptible(&tsock->lock)) lock_sock(sk);
return -ERESTARTSYS;
switch (opt) { switch (opt) {
case TIPC_IMPORTANCE: case TIPC_IMPORTANCE:
res = tipc_set_portimportance(tsock->p->ref, value); res = tipc_set_portimportance(tport->ref, value);
break; break;
case TIPC_SRC_DROPPABLE: case TIPC_SRC_DROPPABLE:
if (sock->type != SOCK_STREAM) if (sock->type != SOCK_STREAM)
res = tipc_set_portunreliable(tsock->p->ref, value); res = tipc_set_portunreliable(tport->ref, value);
else else
res = -ENOPROTOOPT; res = -ENOPROTOOPT;
break; break;
case TIPC_DEST_DROPPABLE: case TIPC_DEST_DROPPABLE:
res = tipc_set_portunreturnable(tsock->p->ref, value); res = tipc_set_portunreturnable(tport->ref, value);
break; break;
case TIPC_CONN_TIMEOUT: case TIPC_CONN_TIMEOUT:
sock->sk->sk_rcvtimeo = msecs_to_jiffies(value); sk->sk_rcvtimeo = msecs_to_jiffies(value);
/* no need to set "res", since already 0 at this point */
break; break;
default: default:
res = -EINVAL; res = -EINVAL;
} }
mutex_unlock(&tsock->lock); release_sock(sk);
return res; return res;
} }
...@@ -1564,7 +1715,8 @@ static int setsockopt(struct socket *sock, ...@@ -1564,7 +1715,8 @@ static int setsockopt(struct socket *sock,
static int getsockopt(struct socket *sock, static int getsockopt(struct socket *sock,
int lvl, int opt, char __user *ov, int __user *ol) int lvl, int opt, char __user *ov, int __user *ol)
{ {
struct tipc_sock *tsock = tipc_sk(sock->sk); struct sock *sk = sock->sk;
struct tipc_port *tport = tipc_sk_port(sk);
int len; int len;
u32 value; u32 value;
int res; int res;
...@@ -1576,26 +1728,28 @@ static int getsockopt(struct socket *sock, ...@@ -1576,26 +1728,28 @@ static int getsockopt(struct socket *sock,
if ((res = get_user(len, ol))) if ((res = get_user(len, ol)))
return res; return res;
if (mutex_lock_interruptible(&tsock->lock)) lock_sock(sk);
return -ERESTARTSYS;
switch (opt) { switch (opt) {
case TIPC_IMPORTANCE: case TIPC_IMPORTANCE:
res = tipc_portimportance(tsock->p->ref, &value); res = tipc_portimportance(tport->ref, &value);
break; break;
case TIPC_SRC_DROPPABLE: case TIPC_SRC_DROPPABLE:
res = tipc_portunreliable(tsock->p->ref, &value); res = tipc_portunreliable(tport->ref, &value);
break; break;
case TIPC_DEST_DROPPABLE: case TIPC_DEST_DROPPABLE:
res = tipc_portunreturnable(tsock->p->ref, &value); res = tipc_portunreturnable(tport->ref, &value);
break; break;
case TIPC_CONN_TIMEOUT: case TIPC_CONN_TIMEOUT:
value = jiffies_to_msecs(sock->sk->sk_rcvtimeo); value = jiffies_to_msecs(sk->sk_rcvtimeo);
/* no need to set "res", since already 0 at this point */
break; break;
default: default:
res = -EINVAL; res = -EINVAL;
} }
release_sock(sk);
if (res) { if (res) {
/* "get" failed */ /* "get" failed */
} }
...@@ -1609,7 +1763,6 @@ static int getsockopt(struct socket *sock, ...@@ -1609,7 +1763,6 @@ static int getsockopt(struct socket *sock,
res = put_user(sizeof(value), ol); res = put_user(sizeof(value), ol);
} }
mutex_unlock(&tsock->lock);
return res; return res;
} }
...@@ -1722,6 +1875,7 @@ int tipc_socket_init(void) ...@@ -1722,6 +1875,7 @@ int tipc_socket_init(void)
/** /**
* tipc_socket_stop - stop TIPC socket interface * tipc_socket_stop - stop TIPC socket interface
*/ */
void tipc_socket_stop(void) void tipc_socket_stop(void)
{ {
if (!sockets_enabled) if (!sockets_enabled)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment