1747 lines · 41.3 KB · C
RawDownload
/* * net/tipc/socket.c: TIPC socket API * * Copyright (c) 2001-2006, Ericsson AB * Copyright (c) 2004-2006, Wind River Systems * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * 2. 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. * 3. Neither the names of the copyright holders nor the names of its *    contributors may be used to endorse or promote products derived from *    this software without specific prior written permission. * * Alternatively, this software may be distributed under the terms of the * GNU General Public License ("GPL") version 2 as published by the Free * Software Foundation. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include <linux/module.h>#include <linux/types.h>#include <linux/net.h>#include <linux/socket.h>#include <linux/errno.h>#include <linux/mm.h>#include <linux/slab.h>#include <linux/poll.h>#include <linux/fcntl.h>#include <asm/semaphore.h>#include <asm/string.h>#include <asm/atomic.h>#include <net/sock.h> #include <linux/tipc.h>#include <linux/tipc_config.h>#include <net/tipc/tipc_msg.h>#include <net/tipc/tipc_port.h> #include "core.h" #define SS_LISTENING	-1	/* socket is listening */#define SS_READY	-2	/* socket is connectionless */ #define OVERLOAD_LIMIT_BASE    5000 struct tipc_sock {	struct sock sk;	struct tipc_port *p;	struct semaphore sem;}; #define tipc_sk(sk) ((struct tipc_sock*)sk) static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);static void wakeupdispatch(struct tipc_port *tport); static struct proto_ops packet_ops;static struct proto_ops stream_ops;static struct proto_ops msg_ops; static struct proto tipc_proto; static int sockets_enabled = 0; static atomic_t tipc_queue_size = ATOMIC_INIT(0);  /* * sock_lock(): Lock a port/socket pair. lock_sock() can * not be used here, since the same lock must protect ports * with non-socket interfaces. * See net.c for description of locking policy. */static void sock_lock(struct tipc_sock* tsock){	spin_lock_bh(tsock->p->lock);} /* * sock_unlock(): Unlock a port/socket pair */static void sock_unlock(struct tipc_sock* tsock){	spin_unlock_bh(tsock->p->lock);} /** * pollmask - determine the current set of poll() events for a socket * @sock: socket structure * * TIPC sets the returned events as follows: * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty *    or if a connection-oriented socket is does not have an active connection *    (i.e. a read operation will not block). * b) POLLOUT is set except when a socket's connection has been terminated *    (i.e. a write operation will not block). * c) POLLHUP is set when a socket's connection has been terminated. * * IMPORTANT: The fact that a read or write operation will not block does NOT * imply that the operation will succeed! * * Returns pollmask value */ static u32 pollmask(struct socket *sock){	u32 mask; 	if ((skb_queue_len(&sock->sk->sk_receive_queue) != 0) ||	    (sock->state == SS_UNCONNECTED) ||	    (sock->state == SS_DISCONNECTING))		mask = (POLLRDNORM | POLLIN);	else		mask = 0; 	if (sock->state == SS_DISCONNECTING)		mask |= POLLHUP;	else		mask |= POLLOUT; 	return mask;}  /** * advance_queue - discard first buffer in queue * @tsock: TIPC socket */ static void advance_queue(struct tipc_sock *tsock){	sock_lock(tsock);	buf_discard(skb_dequeue(&tsock->sk.sk_receive_queue));	sock_unlock(tsock);	atomic_dec(&tipc_queue_size);} /** * tipc_create - create a TIPC socket * @sock: pre-allocated socket structure * @protocol: protocol indicator (must be 0) * * This routine creates and attaches a 'struct sock' to the 'struct socket', * then create and attaches a TIPC port to the 'struct sock' part. * * Returns 0 on success, errno otherwise */static int tipc_create(struct socket *sock, int protocol){	struct tipc_sock *tsock;	struct tipc_port *port;	struct sock *sk;	u32 ref; 	if (unlikely(protocol != 0))		return -EPROTONOSUPPORT; 	ref = tipc_createport_raw(NULL, &dispatch, &wakeupdispatch, TIPC_LOW_IMPORTANCE);	if (unlikely(!ref))		return -ENOMEM; 	sock->state = SS_UNCONNECTED; 	switch (sock->type) {	case SOCK_STREAM:		sock->ops = &stream_ops;		break;	case SOCK_SEQPACKET:		sock->ops = &packet_ops;		break;	case SOCK_DGRAM:		tipc_set_portunreliable(ref, 1);		/* fall through */	case SOCK_RDM:		tipc_set_portunreturnable(ref, 1);		sock->ops = &msg_ops;		sock->state = SS_READY;		break;	default:		tipc_deleteport(ref);		return -EPROTOTYPE;	} 	sk = sk_alloc(AF_TIPC, GFP_KERNEL, &tipc_proto, 1);	if (!sk) {		tipc_deleteport(ref);		return -ENOMEM;	} 	sock_init_data(sock, sk);	init_waitqueue_head(sk->sk_sleep);	sk->sk_rcvtimeo = 8 * HZ;   /* default connect timeout = 8s */ 	tsock = tipc_sk(sk);	port = tipc_get_port(ref); 	tsock->p = port;	port->usr_handle = tsock; 	init_MUTEX(&tsock->sem); 	dbg("sock_create: %x\n",tsock); 	atomic_inc(&tipc_user_count); 	return 0;} /** * release - destroy a TIPC socket * @sock: socket to destroy * * This routine cleans up any messages that are still queued on the socket. * For DGRAM and RDM socket types, all queued messages are rejected. * For SEQPACKET and STREAM socket types, the first message is rejected * and any others are discarded.  (If the first message on a STREAM socket * is partially-read, it is discarded and the next one is rejected instead.) * * NOTE: Rejected messages are not necessarily returned to the sender!  They * are returned or discarded according to the "destination droppable" setting * specified for the message by the sender. * * Returns 0 on success, errno otherwise */ static int release(struct socket *sock){	struct tipc_sock *tsock = tipc_sk(sock->sk);	struct sock *sk = sock->sk;	int res = TIPC_OK;	struct sk_buff *buf; 	dbg("sock_delete: %x\n",tsock);	if (!tsock)		return 0;	down_interruptible(&tsock->sem);	if (!sock->sk) {		up(&tsock->sem);		return 0;	} 	/* Reject unreceived messages, unless no longer connected */ 	while (sock->state != SS_DISCONNECTING) {		sock_lock(tsock);		buf = skb_dequeue(&sk->sk_receive_queue);		if (!buf)			tsock->p->usr_handle = NULL;		sock_unlock(tsock);		if (!buf)			break;		if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf)))			buf_discard(buf);		else			tipc_reject_msg(buf, TIPC_ERR_NO_PORT);		atomic_dec(&tipc_queue_size);	} 	/* Delete TIPC port */ 	res = tipc_deleteport(tsock->p->ref);	sock->sk = NULL; 	/* Discard any remaining messages */ 	while ((buf = skb_dequeue(&sk->sk_receive_queue))) {		buf_discard(buf);		atomic_dec(&tipc_queue_size);	} 	up(&tsock->sem); 	sock_put(sk); 	atomic_dec(&tipc_user_count);	return res;} /** * bind - associate or disassocate TIPC name(s) with a socket * @sock: socket structure * @uaddr: socket address describing name(s) and desired operation * @uaddr_len: size of socket address data structure * * Name and name sequence binding is indicated using a positive scope value; * a negative scope value unbinds the specified name.  Specifying no name * (i.e. a socket address length of 0) unbinds all names from the socket. * * Returns 0 on success, errno otherwise */ 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;	int res; 	if (down_interruptible(&tsock->sem))		return -ERESTARTSYS; 	if (unlikely(!uaddr_len)) {		res = tipc_withdraw(tsock->p->ref, 0, NULL);		goto exit;	} 	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)		addr->addr.nameseq.upper = addr->addr.nameseq.lower;	else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {		res = -EAFNOSUPPORT;		goto exit;	} 	if (addr->scope > 0)		res = tipc_publish(tsock->p->ref, addr->scope,				   &addr->addr.nameseq);	else		res = tipc_withdraw(tsock->p->ref, -addr->scope,				    &addr->addr.nameseq);exit:	up(&tsock->sem);	return res;} /** * get_name - get port ID of socket or peer socket * @sock: socket structure * @uaddr: area for returned socket address * @uaddr_len: area for returned length of socket address * @peer: 0 to obtain socket name, 1 to obtain peer socket name * * Returns 0 on success, errno otherwise */ static int get_name(struct socket *sock, struct sockaddr *uaddr,		    int *uaddr_len, int peer){	struct tipc_sock *tsock = tipc_sk(sock->sk);	struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;	u32 res; 	if (down_interruptible(&tsock->sem))		return -ERESTARTSYS; 	*uaddr_len = sizeof(*addr);	addr->addrtype = TIPC_ADDR_ID;	addr->family = AF_TIPC;	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; 	up(&tsock->sem);	return res;} /** * poll - read and possibly block on pollmask * @file: file structure associated with the socket * @sock: socket for which to calculate the poll bits * @wait: ??? * * Returns the pollmask */ static unsigned int poll(struct file *file, struct socket *sock,			 poll_table *wait){	poll_wait(file, sock->sk->sk_sleep, wait);	/* NEED LOCK HERE? */	return pollmask(sock);} /** * dest_name_check - verify user is permitted to send to specified port name * @dest: destination address * @m: descriptor for message to be sent * * Prevents restricted configuration commands from being issued by * unauthorized users. * * Returns 0 if permission is granted, otherwise errno */ static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m){	struct tipc_cfg_msg_hdr hdr; 	if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))		return 0;	if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))		return 0; 	if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))		return -EACCES; 	if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))		return -EFAULT;	if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))		return -EACCES; 	return 0;} /** * send_msg - send message in connectionless manner * @iocb: (unused) * @sock: socket structure * @m: message to send * @total_len: length of message * * Message must have an destination specified explicitly. * Used for SOCK_RDM and SOCK_DGRAM messages, * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections. * (Note: 'SYN+' is prohibited on SOCK_STREAM.) * * Returns the number of bytes sent on success, or errno otherwise */ static int send_msg(struct kiocb *iocb, struct socket *sock,		    struct msghdr *m, size_t total_len){	struct tipc_sock *tsock = tipc_sk(sock->sk);	struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;	struct sk_buff *buf;	int needs_conn;	int res = -EINVAL; 	if (unlikely(!dest))		return -EDESTADDRREQ;	if (unlikely((m->msg_namelen < sizeof(*dest)) ||		     (dest->family != AF_TIPC)))		return -EINVAL; 	needs_conn = (sock->state != SS_READY);	if (unlikely(needs_conn)) {		if (sock->state == SS_LISTENING)			return -EPIPE;		if (sock->state != SS_UNCONNECTED)			return -EISCONN;		if ((tsock->p->published) ||		    ((sock->type == SOCK_STREAM) && (total_len != 0)))			return -EOPNOTSUPP;		if (dest->addrtype == TIPC_ADDR_NAME) {			tsock->p->conn_type = dest->addr.name.name.type;			tsock->p->conn_instance = dest->addr.name.name.instance;		}	} 	if (down_interruptible(&tsock->sem))		return -ERESTARTSYS; 	if (needs_conn) { 		/* Abort any pending connection attempts (very unlikely) */ 		while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {			tipc_reject_msg(buf, TIPC_ERR_NO_PORT);			atomic_dec(&tipc_queue_size);		} 		sock->state = SS_CONNECTING;	} 	do {		if (dest->addrtype == TIPC_ADDR_NAME) {			if ((res = dest_name_check(dest, m)))				goto exit;			res = tipc_send2name(tsock->p->ref,					     &dest->addr.name.name,					     dest->addr.name.domain,					     m->msg_iovlen,					     m->msg_iov);

Lines 1–500 of 1747.