2 * net/tipc/socket.c: TIPC socket API
4 * Copyright (c) 2001-2006, Ericsson AB
5 * Copyright (c) 2004-2005, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
37 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/net.h>
40 #include <linux/socket.h>
41 #include <linux/errno.h>
43 #include <linux/slab.h>
44 #include <linux/poll.h>
45 #include <linux/fcntl.h>
46 #include <asm/semaphore.h>
47 #include <asm/string.h>
48 #include <asm/atomic.h>
51 #include <linux/tipc.h>
52 #include <linux/tipc_config.h>
53 #include <net/tipc/tipc_msg.h>
54 #include <net/tipc/tipc_port.h>
58 #define SS_LISTENING -1 /* socket is listening */
59 #define SS_READY -2 /* socket is connectionless */
61 #define OVERLOAD_LIMIT_BASE 5000
69 #define tipc_sk(sk) ((struct tipc_sock*)sk)
71 static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
72 static void wakeupdispatch(struct tipc_port *tport);
74 static struct proto_ops packet_ops;
75 static struct proto_ops stream_ops;
76 static struct proto_ops msg_ops;
78 static struct proto tipc_proto;
80 static int sockets_enabled = 0;
82 static atomic_t tipc_queue_size = ATOMIC_INIT(0);
86 * sock_lock(): Lock a port/socket pair. lock_sock() can
87 * not be used here, since the same lock must protect ports
88 * with non-socket interfaces.
89 * See net.c for description of locking policy.
91 static void sock_lock(struct tipc_sock* tsock)
93 spin_lock_bh(tsock->p->lock);
97 * sock_unlock(): Unlock a port/socket pair
99 static void sock_unlock(struct tipc_sock* tsock)
101 spin_unlock_bh(tsock->p->lock);
105 * pollmask - determine the current set of poll() events for a socket
106 * @sock: socket structure
108 * TIPC sets the returned events as follows:
109 * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty
110 * or if a connection-oriented socket is does not have an active connection
111 * (i.e. a read operation will not block).
112 * b) POLLOUT is set except when a socket's connection has been terminated
113 * (i.e. a write operation will not block).
114 * c) POLLHUP is set when a socket's connection has been terminated.
116 * IMPORTANT: The fact that a read or write operation will not block does NOT
117 * imply that the operation will succeed!
119 * Returns pollmask value
122 static u32 pollmask(struct socket *sock)
126 if ((skb_queue_len(&sock->sk->sk_receive_queue) != 0) ||
127 (sock->state == SS_UNCONNECTED) ||
128 (sock->state == SS_DISCONNECTING))
129 mask = (POLLRDNORM | POLLIN);
133 if (sock->state == SS_DISCONNECTING)
143 * advance_queue - discard first buffer in queue
144 * @tsock: TIPC socket
147 static void advance_queue(struct tipc_sock *tsock)
150 buf_discard(skb_dequeue(&tsock->sk.sk_receive_queue));
152 atomic_dec(&tipc_queue_size);
156 * tipc_create - create a TIPC socket
157 * @sock: pre-allocated socket structure
158 * @protocol: protocol indicator (must be 0)
160 * This routine creates and attaches a 'struct sock' to the 'struct socket',
161 * then create and attaches a TIPC port to the 'struct sock' part.
163 * Returns 0 on success, errno otherwise
165 static int tipc_create(struct socket *sock, int protocol)
167 struct tipc_sock *tsock;
168 struct tipc_port *port;
172 if ((sock->type != SOCK_STREAM) &&
173 (sock->type != SOCK_SEQPACKET) &&
174 (sock->type != SOCK_DGRAM) &&
175 (sock->type != SOCK_RDM))
178 if (unlikely(protocol != 0))
179 return -EPROTONOSUPPORT;
181 ref = tipc_createport_raw(NULL, &dispatch, &wakeupdispatch, TIPC_LOW_IMPORTANCE);
185 sock->state = SS_UNCONNECTED;
187 switch (sock->type) {
189 sock->ops = &stream_ops;
192 sock->ops = &packet_ops;
195 tipc_set_portunreliable(ref, 1);
198 tipc_set_portunreturnable(ref, 1);
199 sock->ops = &msg_ops;
200 sock->state = SS_READY;
204 sk = sk_alloc(AF_TIPC, GFP_KERNEL, &tipc_proto, 1);
206 tipc_deleteport(ref);
210 sock_init_data(sock, sk);
211 init_waitqueue_head(sk->sk_sleep);
212 sk->sk_rcvtimeo = 8 * HZ; /* default connect timeout = 8s */
215 port = tipc_get_port(ref);
218 port->usr_handle = tsock;
220 init_MUTEX(&tsock->sem);
222 dbg("sock_create: %x\n",tsock);
224 atomic_inc(&tipc_user_count);
230 * release - destroy a TIPC socket
231 * @sock: socket to destroy
233 * This routine cleans up any messages that are still queued on the socket.
234 * For DGRAM and RDM socket types, all queued messages are rejected.
235 * For SEQPACKET and STREAM socket types, the first message is rejected
236 * and any others are discarded. (If the first message on a STREAM socket
237 * is partially-read, it is discarded and the next one is rejected instead.)
239 * NOTE: Rejected messages are not necessarily returned to the sender! They
240 * are returned or discarded according to the "destination droppable" setting
241 * specified for the message by the sender.
243 * Returns 0 on success, errno otherwise
246 static int release(struct socket *sock)
248 struct tipc_sock *tsock = tipc_sk(sock->sk);
249 struct sock *sk = sock->sk;
253 dbg("sock_delete: %x\n",tsock);
256 down_interruptible(&tsock->sem);
262 /* Reject unreceived messages, unless no longer connected */
264 while (sock->state != SS_DISCONNECTING) {
266 buf = skb_dequeue(&sk->sk_receive_queue);
268 tsock->p->usr_handle = NULL;
272 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf)))
275 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
276 atomic_dec(&tipc_queue_size);
279 /* Delete TIPC port */
281 res = tipc_deleteport(tsock->p->ref);
284 /* Discard any remaining messages */
286 while ((buf = skb_dequeue(&sk->sk_receive_queue))) {
288 atomic_dec(&tipc_queue_size);
295 atomic_dec(&tipc_user_count);
300 * bind - associate or disassocate TIPC name(s) with a socket
301 * @sock: socket structure
302 * @uaddr: socket address describing name(s) and desired operation
303 * @uaddr_len: size of socket address data structure
305 * Name and name sequence binding is indicated using a positive scope value;
306 * a negative scope value unbinds the specified name. Specifying no name
307 * (i.e. a socket address length of 0) unbinds all names from the socket.
309 * Returns 0 on success, errno otherwise
312 static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
314 struct tipc_sock *tsock = tipc_sk(sock->sk);
315 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
318 if (down_interruptible(&tsock->sem))
321 if (unlikely(!uaddr_len)) {
322 res = tipc_withdraw(tsock->p->ref, 0, NULL);
326 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
331 if (addr->family != AF_TIPC) {
335 if (addr->addrtype == TIPC_ADDR_NAME)
336 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
337 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
343 res = tipc_publish(tsock->p->ref, addr->scope,
344 &addr->addr.nameseq);
346 res = tipc_withdraw(tsock->p->ref, -addr->scope,
347 &addr->addr.nameseq);
354 * get_name - get port ID of socket or peer socket
355 * @sock: socket structure
356 * @uaddr: area for returned socket address
357 * @uaddr_len: area for returned length of socket address
358 * @peer: 0 to obtain socket name, 1 to obtain peer socket name
360 * Returns 0 on success, errno otherwise
363 static int get_name(struct socket *sock, struct sockaddr *uaddr,
364 int *uaddr_len, int peer)
366 struct tipc_sock *tsock = tipc_sk(sock->sk);
367 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
370 if (down_interruptible(&tsock->sem))
373 *uaddr_len = sizeof(*addr);
374 addr->addrtype = TIPC_ADDR_ID;
375 addr->family = AF_TIPC;
378 res = tipc_peer(tsock->p->ref, &addr->addr.id);
380 res = tipc_ownidentity(tsock->p->ref, &addr->addr.id);
381 addr->addr.name.domain = 0;
388 * poll - read and possibly block on pollmask
389 * @file: file structure associated with the socket
390 * @sock: socket for which to calculate the poll bits
393 * Returns the pollmask
396 static unsigned int poll(struct file *file, struct socket *sock,
399 poll_wait(file, sock->sk->sk_sleep, wait);
400 /* NEED LOCK HERE? */
401 return pollmask(sock);
405 * dest_name_check - verify user is permitted to send to specified port name
406 * @dest: destination address
407 * @m: descriptor for message to be sent
409 * Prevents restricted configuration commands from being issued by
410 * unauthorized users.
412 * Returns 0 if permission is granted, otherwise errno
415 static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
417 struct tipc_cfg_msg_hdr hdr;
419 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
421 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
424 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
427 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
429 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
436 * send_msg - send message in connectionless manner
438 * @sock: socket structure
439 * @m: message to send
440 * @total_len: length of message
442 * Message must have an destination specified explicitly.
443 * Used for SOCK_RDM and SOCK_DGRAM messages,
444 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
445 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
447 * Returns the number of bytes sent on success, or errno otherwise
450 static int send_msg(struct kiocb *iocb, struct socket *sock,
451 struct msghdr *m, size_t total_len)
453 struct tipc_sock *tsock = tipc_sk(sock->sk);
454 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
460 return -EDESTADDRREQ;
461 if (unlikely(dest->family != AF_TIPC))
464 needs_conn = (sock->state != SS_READY);
465 if (unlikely(needs_conn)) {
466 if (sock->state == SS_LISTENING)
468 if (sock->state != SS_UNCONNECTED)
470 if ((tsock->p->published) ||
471 ((sock->type == SOCK_STREAM) && (total_len != 0)))
473 if (dest->addrtype == TIPC_ADDR_NAME) {
474 tsock->p->conn_type = dest->addr.name.name.type;
475 tsock->p->conn_instance = dest->addr.name.name.instance;
479 if (down_interruptible(&tsock->sem))
484 /* Abort any pending connection attempts (very unlikely) */
486 while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
487 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
488 atomic_dec(&tipc_queue_size);
491 sock->state = SS_CONNECTING;
495 if (dest->addrtype == TIPC_ADDR_NAME) {
496 if ((res = dest_name_check(dest, m)))
498 res = tipc_send2name(tsock->p->ref,
499 &dest->addr.name.name,
500 dest->addr.name.domain,
504 else if (dest->addrtype == TIPC_ADDR_ID) {
505 res = tipc_send2port(tsock->p->ref,
510 else if (dest->addrtype == TIPC_ADDR_MCAST) {
515 if ((res = dest_name_check(dest, m)))
517 res = tipc_multicast(tsock->p->ref,
523 if (likely(res != -ELINKCONG)) {
528 if (m->msg_flags & MSG_DONTWAIT) {
532 if (wait_event_interruptible(*sock->sk->sk_sleep,
533 !tsock->p->congested)) {
541 * send_packet - send a connection-oriented message
543 * @sock: socket structure
544 * @m: message to send
545 * @total_len: length of message
547 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
549 * Returns the number of bytes sent on success, or errno otherwise
552 static int send_packet(struct kiocb *iocb, struct socket *sock,
553 struct msghdr *m, size_t total_len)
555 struct tipc_sock *tsock = tipc_sk(sock->sk);
556 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
559 /* Handle implied connection establishment */
562 return send_msg(iocb, sock, m, total_len);
564 if (down_interruptible(&tsock->sem)) {
569 if (unlikely(sock->state != SS_CONNECTED)) {
570 if (sock->state == SS_DISCONNECTING)
577 res = tipc_send(tsock->p->ref, m->msg_iovlen, m->msg_iov);
578 if (likely(res != -ELINKCONG)) {
583 if (m->msg_flags & MSG_DONTWAIT) {
587 if (wait_event_interruptible(*sock->sk->sk_sleep,
588 !tsock->p->congested)) {
596 * send_stream - send stream-oriented data
598 * @sock: socket structure
600 * @total_len: total length of data to be sent
602 * Used for SOCK_STREAM data.
604 * Returns the number of bytes sent on success (or partial success),
605 * or errno if no data sent
609 static int send_stream(struct kiocb *iocb, struct socket *sock,
610 struct msghdr *m, size_t total_len)
612 struct msghdr my_msg;
614 struct iovec *curr_iov;
616 char __user *curr_start;
622 if (likely(total_len <= TIPC_MAX_USER_MSG_SIZE))
623 return send_packet(iocb, sock, m, total_len);
625 /* Can only send large data streams if already connected */
627 if (unlikely(sock->state != SS_CONNECTED)) {
628 if (sock->state == SS_DISCONNECTING)
635 * Send each iovec entry using one or more messages
637 * Note: This algorithm is good for the most likely case
638 * (i.e. one large iovec entry), but could be improved to pass sets
639 * of small iovec entries into send_packet().
642 curr_iov = m->msg_iov;
643 curr_iovlen = m->msg_iovlen;
644 my_msg.msg_iov = &my_iov;
645 my_msg.msg_iovlen = 1;
648 while (curr_iovlen--) {
649 curr_start = curr_iov->iov_base;
650 curr_left = curr_iov->iov_len;
653 bytes_to_send = (curr_left < TIPC_MAX_USER_MSG_SIZE)
654 ? curr_left : TIPC_MAX_USER_MSG_SIZE;
655 my_iov.iov_base = curr_start;
656 my_iov.iov_len = bytes_to_send;
657 if ((res = send_packet(iocb, sock, &my_msg, 0)) < 0) {
658 return bytes_sent ? bytes_sent : res;
660 curr_left -= bytes_to_send;
661 curr_start += bytes_to_send;
662 bytes_sent += bytes_to_send;
672 * auto_connect - complete connection setup to a remote port
673 * @sock: socket structure
674 * @tsock: TIPC-specific socket structure
675 * @msg: peer's response message
677 * Returns 0 on success, errno otherwise
680 static int auto_connect(struct socket *sock, struct tipc_sock *tsock,
681 struct tipc_msg *msg)
683 struct tipc_portid peer;
685 if (msg_errcode(msg)) {
686 sock->state = SS_DISCONNECTING;
687 return -ECONNREFUSED;
690 peer.ref = msg_origport(msg);
691 peer.node = msg_orignode(msg);
692 tipc_connect2port(tsock->p->ref, &peer);
693 tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
694 sock->state = SS_CONNECTED;
699 * set_orig_addr - capture sender's address for received message
700 * @m: descriptor for message info
701 * @msg: received message header
703 * Note: Address is not captured if not requested by receiver.
706 static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
708 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
711 addr->family = AF_TIPC;
712 addr->addrtype = TIPC_ADDR_ID;
713 addr->addr.id.ref = msg_origport(msg);
714 addr->addr.id.node = msg_orignode(msg);
715 addr->addr.name.domain = 0; /* could leave uninitialized */
716 addr->scope = 0; /* could leave uninitialized */
717 m->msg_namelen = sizeof(struct sockaddr_tipc);
722 * anc_data_recv - optionally capture ancillary data for received message
723 * @m: descriptor for message info
724 * @msg: received message header
725 * @tport: TIPC port associated with message
727 * Note: Ancillary data is not captured if not requested by receiver.
729 * Returns 0 if successful, otherwise errno
732 static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
733 struct tipc_port *tport)
741 if (likely(m->msg_controllen == 0))
744 /* Optionally capture errored message object(s) */
746 err = msg ? msg_errcode(msg) : 0;
749 anc_data[1] = msg_data_sz(msg);
750 if ((res = put_cmsg(m, SOL_SOCKET, TIPC_ERRINFO, 8, anc_data)))
753 (res = put_cmsg(m, SOL_SOCKET, TIPC_RETDATA, anc_data[1],
758 /* Optionally capture message destination object */
760 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
764 anc_data[0] = msg_nametype(msg);
765 anc_data[1] = msg_namelower(msg);
766 anc_data[2] = msg_namelower(msg);
770 anc_data[0] = msg_nametype(msg);
771 anc_data[1] = msg_namelower(msg);
772 anc_data[2] = msg_nameupper(msg);
775 has_name = (tport->conn_type != 0);
776 anc_data[0] = tport->conn_type;
777 anc_data[1] = tport->conn_instance;
778 anc_data[2] = tport->conn_instance;
784 (res = put_cmsg(m, SOL_SOCKET, TIPC_DESTNAME, 12, anc_data)))
791 * recv_msg - receive packet-oriented message
793 * @m: descriptor for message info
794 * @buf_len: total size of user buffer area
795 * @flags: receive flags
797 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
798 * If the complete message doesn't fit in user area, truncate it.
800 * Returns size of returned message data, errno otherwise
803 static int recv_msg(struct kiocb *iocb, struct socket *sock,
804 struct msghdr *m, size_t buf_len, int flags)
806 struct tipc_sock *tsock = tipc_sk(sock->sk);
808 struct tipc_msg *msg;
814 /* Currently doesn't support receiving into multiple iovec entries */
816 if (m->msg_iovlen != 1)
819 /* Catch invalid receive attempts */
821 if (unlikely(!buf_len))
824 if (sock->type == SOCK_SEQPACKET) {
825 if (unlikely(sock->state == SS_UNCONNECTED))
827 if (unlikely((sock->state == SS_DISCONNECTING) &&
828 (skb_queue_len(&sock->sk->sk_receive_queue) == 0)))
832 /* Look for a message in receive queue; wait if necessary */
834 if (unlikely(down_interruptible(&tsock->sem)))
838 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
839 (flags & MSG_DONTWAIT))) {
844 if ((res = wait_event_interruptible(
846 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
847 (sock->state == SS_DISCONNECTING))) )) {
851 /* Catch attempt to receive on an already terminated connection */
852 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
859 /* Get access to first message in receive queue */
861 buf = skb_peek(&sock->sk->sk_receive_queue);
863 sz = msg_data_sz(msg);
864 err = msg_errcode(msg);
866 /* Complete connection setup for an implied connect */
868 if (unlikely(sock->state == SS_CONNECTING)) {
869 if ((res = auto_connect(sock, tsock, msg)))
873 /* Discard an empty non-errored message & try again */
875 if ((!sz) && (!err)) {
876 advance_queue(tsock);
880 /* Capture sender's address (optional) */
882 set_orig_addr(m, msg);
884 /* Capture ancillary data (optional) */
886 if ((res = anc_data_recv(m, msg, tsock->p)))
889 /* Capture message data (if valid) & compute return value (always) */
892 if (unlikely(buf_len < sz)) {
894 m->msg_flags |= MSG_TRUNC;
896 if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg),
903 if ((sock->state == SS_READY) ||
904 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
910 /* Consume received message (optional) */
912 if (likely(!(flags & MSG_PEEK))) {
913 if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
914 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
915 advance_queue(tsock);
923 * recv_stream - receive stream-oriented data
925 * @m: descriptor for message info
926 * @buf_len: total size of user buffer area
927 * @flags: receive flags
929 * Used for SOCK_STREAM messages only. If not enough data is available
930 * will optionally wait for more; never truncates data.
932 * Returns size of returned message data, errno otherwise
935 static int recv_stream(struct kiocb *iocb, struct socket *sock,
936 struct msghdr *m, size_t buf_len, int flags)
938 struct tipc_sock *tsock = tipc_sk(sock->sk);
940 struct tipc_msg *msg;
946 char *crs = m->msg_iov->iov_base;
947 unsigned char *buf_crs;
951 /* Currently doesn't support receiving into multiple iovec entries */
953 if (m->msg_iovlen != 1)
956 /* Catch invalid receive attempts */
958 if (unlikely(!buf_len))
961 if (unlikely(sock->state == SS_DISCONNECTING)) {
962 if (skb_queue_len(&sock->sk->sk_receive_queue) == 0)
964 } else if (unlikely(sock->state != SS_CONNECTED))
967 /* Look for a message in receive queue; wait if necessary */
969 if (unlikely(down_interruptible(&tsock->sem)))
973 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
974 (flags & MSG_DONTWAIT))) {
975 res = (sz_copied == 0) ? -EWOULDBLOCK : 0;
979 if ((res = wait_event_interruptible(
981 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
982 (sock->state == SS_DISCONNECTING))) )) {
986 /* Catch attempt to receive on an already terminated connection */
987 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
994 /* Get access to first message in receive queue */
996 buf = skb_peek(&sock->sk->sk_receive_queue);
998 sz = msg_data_sz(msg);
999 err = msg_errcode(msg);
1001 /* Discard an empty non-errored message & try again */
1003 if ((!sz) && (!err)) {
1004 advance_queue(tsock);
1008 /* Optionally capture sender's address & ancillary data of first msg */
1010 if (sz_copied == 0) {
1011 set_orig_addr(m, msg);
1012 if ((res = anc_data_recv(m, msg, tsock->p)))
1016 /* Capture message data (if valid) & compute return value (always) */
1019 buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
1020 sz = buf->tail - buf_crs;
1022 needed = (buf_len - sz_copied);
1023 sz_to_copy = (sz <= needed) ? sz : needed;
1024 if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) {
1028 sz_copied += sz_to_copy;
1030 if (sz_to_copy < sz) {
1031 if (!(flags & MSG_PEEK))
1032 TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy;
1039 goto exit; /* can't add error msg to valid data */
1041 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1047 /* Consume received message (optional) */
1049 if (likely(!(flags & MSG_PEEK))) {
1050 if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1051 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
1052 advance_queue(tsock);
1055 /* Loop around if more data is required */
1057 if ((sz_copied < buf_len) /* didn't get all requested data */
1058 && (flags & MSG_WAITALL) /* ... and need to wait for more */
1059 && (!(flags & MSG_PEEK)) /* ... and aren't just peeking at data */
1060 && (!err) /* ... and haven't reached a FIN */
1066 return res ? res : sz_copied;
1070 * queue_overloaded - test if queue overload condition exists
1071 * @queue_size: current size of queue
1072 * @base: nominal maximum size of queue
1073 * @msg: message to be added to queue
1075 * Returns 1 if queue is currently overloaded, 0 otherwise
1078 static int queue_overloaded(u32 queue_size, u32 base, struct tipc_msg *msg)
1081 u32 imp = msg_importance(msg);
1083 if (imp == TIPC_LOW_IMPORTANCE)
1085 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1086 threshold = base * 2;
1087 else if (imp == TIPC_HIGH_IMPORTANCE)
1088 threshold = base * 100;
1092 if (msg_connected(msg))
1095 return (queue_size > threshold);
1099 * async_disconnect - wrapper function used to disconnect port
1100 * @portref: TIPC port reference (passed as pointer-sized value)
1103 static void async_disconnect(unsigned long portref)
1105 tipc_disconnect((u32)portref);
1109 * dispatch - handle arriving message
1110 * @tport: TIPC port that received message
1113 * Called with port locked. Must not take socket lock to avoid deadlock risk.
1115 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1118 static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1120 struct tipc_msg *msg = buf_msg(buf);
1121 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
1122 struct socket *sock;
1125 /* Reject message if socket is closing */
1128 return TIPC_ERR_NO_PORT;
1130 /* Reject message if it is wrong sort of message for socket */
1133 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1134 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1135 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1137 sock = tsock->sk.sk_socket;
1138 if (sock->state == SS_READY) {
1139 if (msg_connected(msg)) {
1140 msg_dbg(msg, "dispatch filter 1\n");
1141 return TIPC_ERR_NO_PORT;
1144 if (msg_mcast(msg)) {
1145 msg_dbg(msg, "dispatch filter 2\n");
1146 return TIPC_ERR_NO_PORT;
1148 if (sock->state == SS_CONNECTED) {
1149 if (!msg_connected(msg)) {
1150 msg_dbg(msg, "dispatch filter 3\n");
1151 return TIPC_ERR_NO_PORT;
1154 else if (sock->state == SS_CONNECTING) {
1155 if (!msg_connected(msg) && (msg_errcode(msg) == 0)) {
1156 msg_dbg(msg, "dispatch filter 4\n");
1157 return TIPC_ERR_NO_PORT;
1160 else if (sock->state == SS_LISTENING) {
1161 if (msg_connected(msg) || msg_errcode(msg)) {
1162 msg_dbg(msg, "dispatch filter 5\n");
1163 return TIPC_ERR_NO_PORT;
1166 else if (sock->state == SS_DISCONNECTING) {
1167 msg_dbg(msg, "dispatch filter 6\n");
1168 return TIPC_ERR_NO_PORT;
1170 else /* (sock->state == SS_UNCONNECTED) */ {
1171 if (msg_connected(msg) || msg_errcode(msg)) {
1172 msg_dbg(msg, "dispatch filter 7\n");
1173 return TIPC_ERR_NO_PORT;
1178 /* Reject message if there isn't room to queue it */
1180 if (unlikely((u32)atomic_read(&tipc_queue_size) >
1181 OVERLOAD_LIMIT_BASE)) {
1182 if (queue_overloaded(atomic_read(&tipc_queue_size),
1183 OVERLOAD_LIMIT_BASE, msg))
1184 return TIPC_ERR_OVERLOAD;
1186 recv_q_len = skb_queue_len(&tsock->sk.sk_receive_queue);
1187 if (unlikely(recv_q_len > (OVERLOAD_LIMIT_BASE / 2))) {
1188 if (queue_overloaded(recv_q_len,
1189 OVERLOAD_LIMIT_BASE / 2, msg))
1190 return TIPC_ERR_OVERLOAD;
1193 /* Initiate connection termination for an incoming 'FIN' */
1195 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1196 sock->state = SS_DISCONNECTING;
1197 /* Note: Use signal since port lock is already taken! */
1198 tipc_k_signal((Handler)async_disconnect, tport->ref);
1201 /* Enqueue message (finally!) */
1203 msg_dbg(msg,"<DISP<: ");
1204 TIPC_SKB_CB(buf)->handle = msg_data(msg);
1205 atomic_inc(&tipc_queue_size);
1206 skb_queue_tail(&sock->sk->sk_receive_queue, buf);
1208 wake_up_interruptible(sock->sk->sk_sleep);
1213 * wakeupdispatch - wake up port after congestion
1214 * @tport: port to wakeup
1216 * Called with port lock on.
1219 static void wakeupdispatch(struct tipc_port *tport)
1221 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
1223 wake_up_interruptible(tsock->sk.sk_sleep);
1227 * connect - establish a connection to another TIPC port
1228 * @sock: socket structure
1229 * @dest: socket address for destination port
1230 * @destlen: size of socket address data structure
1233 * Returns 0 on success, errno otherwise
1236 static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
1239 struct tipc_sock *tsock = tipc_sk(sock->sk);
1240 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1241 struct msghdr m = {NULL,};
1242 struct sk_buff *buf;
1243 struct tipc_msg *msg;
1246 /* For now, TIPC does not allow use of connect() with DGRAM or RDM types */
1248 if (sock->state == SS_READY)
1251 /* MOVE THE REST OF THIS ERROR CHECKING TO send_msg()? */
1252 if (sock->state == SS_LISTENING)
1254 if (sock->state == SS_CONNECTING)
1256 if (sock->state != SS_UNCONNECTED)
1259 if ((destlen < sizeof(*dst)) || (dst->family != AF_TIPC) ||
1260 ((dst->addrtype != TIPC_ADDR_NAME) && (dst->addrtype != TIPC_ADDR_ID)))
1263 /* Send a 'SYN-' to destination */
1266 if ((res = send_msg(NULL, sock, &m, 0)) < 0) {
1267 sock->state = SS_DISCONNECTING;
1271 if (down_interruptible(&tsock->sem))
1272 return -ERESTARTSYS;
1274 /* Wait for destination's 'ACK' response */
1276 res = wait_event_interruptible_timeout(*sock->sk->sk_sleep,
1277 skb_queue_len(&sock->sk->sk_receive_queue),
1278 sock->sk->sk_rcvtimeo);
1279 buf = skb_peek(&sock->sk->sk_receive_queue);
1282 res = auto_connect(sock, tsock, msg);
1284 if (!msg_data_sz(msg))
1285 advance_queue(tsock);
1291 { /* leave "res" unchanged */ }
1292 sock->state = SS_DISCONNECTING;
1300 * listen - allow socket to listen for incoming connections
1301 * @sock: socket structure
1304 * Returns 0 on success, errno otherwise
1307 static int listen(struct socket *sock, int len)
1309 /* REQUIRES SOCKET LOCKING OF SOME SORT? */
1311 if (sock->state == SS_READY)
1313 if (sock->state != SS_UNCONNECTED)
1315 sock->state = SS_LISTENING;
1320 * accept - wait for connection request
1321 * @sock: listening socket
1322 * @newsock: new socket that is to be connected
1323 * @flags: file-related flags associated with socket
1325 * Returns 0 on success, errno otherwise
1328 static int accept(struct socket *sock, struct socket *newsock, int flags)
1330 struct tipc_sock *tsock = tipc_sk(sock->sk);
1331 struct sk_buff *buf;
1334 if (sock->state == SS_READY)
1336 if (sock->state != SS_LISTENING)
1339 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
1340 (flags & O_NONBLOCK)))
1341 return -EWOULDBLOCK;
1343 if (down_interruptible(&tsock->sem))
1344 return -ERESTARTSYS;
1346 if (wait_event_interruptible(*sock->sk->sk_sleep,
1347 skb_queue_len(&sock->sk->sk_receive_queue))) {
1351 buf = skb_peek(&sock->sk->sk_receive_queue);
1353 res = tipc_create(newsock, 0);
1355 struct tipc_sock *new_tsock = tipc_sk(newsock->sk);
1356 struct tipc_portid id;
1357 struct tipc_msg *msg = buf_msg(buf);
1358 u32 new_ref = new_tsock->p->ref;
1360 id.ref = msg_origport(msg);
1361 id.node = msg_orignode(msg);
1362 tipc_connect2port(new_ref, &id);
1363 newsock->state = SS_CONNECTED;
1365 tipc_set_portimportance(new_ref, msg_importance(msg));
1366 if (msg_named(msg)) {
1367 new_tsock->p->conn_type = msg_nametype(msg);
1368 new_tsock->p->conn_instance = msg_nameinst(msg);
1372 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1373 * Respond to 'SYN+' by queuing it on new socket.
1376 msg_dbg(msg,"<ACC<: ");
1377 if (!msg_data_sz(msg)) {
1378 struct msghdr m = {NULL,};
1380 send_packet(NULL, newsock, &m, 0);
1381 advance_queue(tsock);
1384 skb_dequeue(&sock->sk->sk_receive_queue);
1386 skb_queue_head(&newsock->sk->sk_receive_queue, buf);
1395 * shutdown - shutdown socket connection
1396 * @sock: socket structure
1397 * @how: direction to close (unused; always treated as read + write)
1399 * Terminates connection (if necessary), then purges socket's receive queue.
1401 * Returns 0 on success, errno otherwise
1404 static int shutdown(struct socket *sock, int how)
1406 struct tipc_sock* tsock = tipc_sk(sock->sk);
1407 struct sk_buff *buf;
1410 /* Could return -EINVAL for an invalid "how", but why bother? */
1412 if (down_interruptible(&tsock->sem))
1413 return -ERESTARTSYS;
1417 switch (sock->state) {
1420 /* Send 'FIN+' or 'FIN-' message to peer */
1424 if ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
1425 atomic_dec(&tipc_queue_size);
1426 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) {
1430 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
1433 tipc_shutdown(tsock->p->ref);
1439 case SS_DISCONNECTING:
1441 /* Discard any unreceived messages */
1443 while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
1444 atomic_dec(&tipc_queue_size);
1447 tsock->p->conn_unacked = 0;
1452 sock->state = SS_DISCONNECTING;
1467 * setsockopt - set socket option
1468 * @sock: socket structure
1469 * @lvl: option level
1470 * @opt: option identifier
1471 * @ov: pointer to new option value
1472 * @ol: length of option value
1474 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
1475 * (to ease compatibility).
1477 * Returns 0 on success, errno otherwise
1480 static int setsockopt(struct socket *sock,
1481 int lvl, int opt, char __user *ov, int ol)
1483 struct tipc_sock *tsock = tipc_sk(sock->sk);
1487 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1489 if (lvl != SOL_TIPC)
1490 return -ENOPROTOOPT;
1491 if (ol < sizeof(value))
1493 if ((res = get_user(value, (u32 *)ov)))
1496 if (down_interruptible(&tsock->sem))
1497 return -ERESTARTSYS;
1500 case TIPC_IMPORTANCE:
1501 res = tipc_set_portimportance(tsock->p->ref, value);
1503 case TIPC_SRC_DROPPABLE:
1504 if (sock->type != SOCK_STREAM)
1505 res = tipc_set_portunreliable(tsock->p->ref, value);
1509 case TIPC_DEST_DROPPABLE:
1510 res = tipc_set_portunreturnable(tsock->p->ref, value);
1512 case TIPC_CONN_TIMEOUT:
1513 sock->sk->sk_rcvtimeo = (value * HZ / 1000);
1524 * getsockopt - get socket option
1525 * @sock: socket structure
1526 * @lvl: option level
1527 * @opt: option identifier
1528 * @ov: receptacle for option value
1529 * @ol: receptacle for length of option value
1531 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
1532 * (to ease compatibility).
1534 * Returns 0 on success, errno otherwise
1537 static int getsockopt(struct socket *sock,
1538 int lvl, int opt, char __user *ov, int *ol)
1540 struct tipc_sock *tsock = tipc_sk(sock->sk);
1545 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1546 return put_user(0, ol);
1547 if (lvl != SOL_TIPC)
1548 return -ENOPROTOOPT;
1549 if ((res = get_user(len, ol)))
1552 if (down_interruptible(&tsock->sem))
1553 return -ERESTARTSYS;
1556 case TIPC_IMPORTANCE:
1557 res = tipc_portimportance(tsock->p->ref, &value);
1559 case TIPC_SRC_DROPPABLE:
1560 res = tipc_portunreliable(tsock->p->ref, &value);
1562 case TIPC_DEST_DROPPABLE:
1563 res = tipc_portunreturnable(tsock->p->ref, &value);
1565 case TIPC_CONN_TIMEOUT:
1566 value = (sock->sk->sk_rcvtimeo * 1000) / HZ;
1575 else if (len < sizeof(value)) {
1578 else if ((res = copy_to_user(ov, &value, sizeof(value)))) {
1579 /* couldn't return value */
1582 res = put_user(sizeof(value), ol);
1590 * Placeholders for non-implemented functionality
1592 * Returns error code (POSIX-compliant where defined)
1595 static int ioctl(struct socket *s, u32 cmd, unsigned long arg)
1600 static int no_mmap(struct file *file, struct socket *sock,
1601 struct vm_area_struct *vma)
1605 static ssize_t no_sendpage(struct socket *sock, struct page *page,
1606 int offset, size_t size, int flags)
1611 static int no_skpair(struct socket *s1, struct socket *s2)
1617 * Protocol switches for the various types of TIPC sockets
1620 static struct proto_ops msg_ops = {
1621 .owner = THIS_MODULE,
1626 .socketpair = no_skpair,
1628 .getname = get_name,
1632 .shutdown = shutdown,
1633 .setsockopt = setsockopt,
1634 .getsockopt = getsockopt,
1635 .sendmsg = send_msg,
1636 .recvmsg = recv_msg,
1638 .sendpage = no_sendpage
1641 static struct proto_ops packet_ops = {
1642 .owner = THIS_MODULE,
1647 .socketpair = no_skpair,
1649 .getname = get_name,
1653 .shutdown = shutdown,
1654 .setsockopt = setsockopt,
1655 .getsockopt = getsockopt,
1656 .sendmsg = send_packet,
1657 .recvmsg = recv_msg,
1659 .sendpage = no_sendpage
1662 static struct proto_ops stream_ops = {
1663 .owner = THIS_MODULE,
1668 .socketpair = no_skpair,
1670 .getname = get_name,
1674 .shutdown = shutdown,
1675 .setsockopt = setsockopt,
1676 .getsockopt = getsockopt,
1677 .sendmsg = send_stream,
1678 .recvmsg = recv_stream,
1680 .sendpage = no_sendpage
1683 static struct net_proto_family tipc_family_ops = {
1684 .owner = THIS_MODULE,
1686 .create = tipc_create
1689 static struct proto tipc_proto = {
1691 .owner = THIS_MODULE,
1692 .obj_size = sizeof(struct tipc_sock)
1696 * tipc_socket_init - initialize TIPC socket interface
1698 * Returns 0 on success, errno otherwise
1700 int tipc_socket_init(void)
1704 res = proto_register(&tipc_proto, 1);
1706 err("Failed to register TIPC protocol type\n");
1710 res = sock_register(&tipc_family_ops);
1712 err("Failed to register TIPC socket type\n");
1713 proto_unregister(&tipc_proto);
1717 sockets_enabled = 1;
1723 * tipc_socket_stop - stop TIPC socket interface
1725 void tipc_socket_stop(void)
1727 if (!sockets_enabled)
1730 sockets_enabled = 0;
1731 sock_unregister(tipc_family_ops.family);
1732 proto_unregister(&tipc_proto);