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 errno otherwise
608 static int send_stream(struct kiocb *iocb, struct socket *sock,
609 struct msghdr *m, size_t total_len)
611 struct msghdr my_msg;
613 struct iovec *curr_iov;
615 char __user *curr_start;
620 if (likely(total_len <= TIPC_MAX_USER_MSG_SIZE))
621 return send_packet(iocb, sock, m, total_len);
623 /* Can only send large data streams if already connected */
625 if (unlikely(sock->state != SS_CONNECTED)) {
626 if (sock->state == SS_DISCONNECTING)
633 * Send each iovec entry using one or more messages
635 * Note: This algorithm is good for the most likely case
636 * (i.e. one large iovec entry), but could be improved to pass sets
637 * of small iovec entries into send_packet().
641 curr_iov = my_msg.msg_iov;
642 curr_iovlen = my_msg.msg_iovlen;
643 my_msg.msg_iov = &my_iov;
644 my_msg.msg_iovlen = 1;
646 while (curr_iovlen--) {
647 curr_start = curr_iov->iov_base;
648 curr_left = curr_iov->iov_len;
651 bytes_to_send = (curr_left < TIPC_MAX_USER_MSG_SIZE)
652 ? curr_left : TIPC_MAX_USER_MSG_SIZE;
653 my_iov.iov_base = curr_start;
654 my_iov.iov_len = bytes_to_send;
655 if ((res = send_packet(iocb, sock, &my_msg, 0)) < 0)
657 curr_left -= bytes_to_send;
658 curr_start += bytes_to_send;
668 * auto_connect - complete connection setup to a remote port
669 * @sock: socket structure
670 * @tsock: TIPC-specific socket structure
671 * @msg: peer's response message
673 * Returns 0 on success, errno otherwise
676 static int auto_connect(struct socket *sock, struct tipc_sock *tsock,
677 struct tipc_msg *msg)
679 struct tipc_portid peer;
681 if (msg_errcode(msg)) {
682 sock->state = SS_DISCONNECTING;
683 return -ECONNREFUSED;
686 peer.ref = msg_origport(msg);
687 peer.node = msg_orignode(msg);
688 tipc_connect2port(tsock->p->ref, &peer);
689 tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
690 sock->state = SS_CONNECTED;
695 * set_orig_addr - capture sender's address for received message
696 * @m: descriptor for message info
697 * @msg: received message header
699 * Note: Address is not captured if not requested by receiver.
702 static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
704 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
707 addr->family = AF_TIPC;
708 addr->addrtype = TIPC_ADDR_ID;
709 addr->addr.id.ref = msg_origport(msg);
710 addr->addr.id.node = msg_orignode(msg);
711 addr->addr.name.domain = 0; /* could leave uninitialized */
712 addr->scope = 0; /* could leave uninitialized */
713 m->msg_namelen = sizeof(struct sockaddr_tipc);
718 * anc_data_recv - optionally capture ancillary data for received message
719 * @m: descriptor for message info
720 * @msg: received message header
721 * @tport: TIPC port associated with message
723 * Note: Ancillary data is not captured if not requested by receiver.
725 * Returns 0 if successful, otherwise errno
728 static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
729 struct tipc_port *tport)
737 if (likely(m->msg_controllen == 0))
740 /* Optionally capture errored message object(s) */
742 err = msg ? msg_errcode(msg) : 0;
745 anc_data[1] = msg_data_sz(msg);
746 if ((res = put_cmsg(m, SOL_SOCKET, TIPC_ERRINFO, 8, anc_data)))
749 (res = put_cmsg(m, SOL_SOCKET, TIPC_RETDATA, anc_data[1],
754 /* Optionally capture message destination object */
756 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
760 anc_data[0] = msg_nametype(msg);
761 anc_data[1] = msg_namelower(msg);
762 anc_data[2] = msg_namelower(msg);
766 anc_data[0] = msg_nametype(msg);
767 anc_data[1] = msg_namelower(msg);
768 anc_data[2] = msg_nameupper(msg);
771 has_name = (tport->conn_type != 0);
772 anc_data[0] = tport->conn_type;
773 anc_data[1] = tport->conn_instance;
774 anc_data[2] = tport->conn_instance;
780 (res = put_cmsg(m, SOL_SOCKET, TIPC_DESTNAME, 12, anc_data)))
787 * recv_msg - receive packet-oriented message
789 * @m: descriptor for message info
790 * @buf_len: total size of user buffer area
791 * @flags: receive flags
793 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
794 * If the complete message doesn't fit in user area, truncate it.
796 * Returns size of returned message data, errno otherwise
799 static int recv_msg(struct kiocb *iocb, struct socket *sock,
800 struct msghdr *m, size_t buf_len, int flags)
802 struct tipc_sock *tsock = tipc_sk(sock->sk);
804 struct tipc_msg *msg;
810 /* Currently doesn't support receiving into multiple iovec entries */
812 if (m->msg_iovlen != 1)
815 /* Catch invalid receive attempts */
817 if (unlikely(!buf_len))
820 if (sock->type == SOCK_SEQPACKET) {
821 if (unlikely(sock->state == SS_UNCONNECTED))
823 if (unlikely((sock->state == SS_DISCONNECTING) &&
824 (skb_queue_len(&sock->sk->sk_receive_queue) == 0)))
828 /* Look for a message in receive queue; wait if necessary */
830 if (unlikely(down_interruptible(&tsock->sem)))
834 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
835 (flags & MSG_DONTWAIT))) {
840 if ((res = wait_event_interruptible(
842 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
843 (sock->state == SS_DISCONNECTING))) )) {
847 /* Catch attempt to receive on an already terminated connection */
848 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
855 /* Get access to first message in receive queue */
857 buf = skb_peek(&sock->sk->sk_receive_queue);
859 sz = msg_data_sz(msg);
860 err = msg_errcode(msg);
862 /* Complete connection setup for an implied connect */
864 if (unlikely(sock->state == SS_CONNECTING)) {
865 if ((res = auto_connect(sock, tsock, msg)))
869 /* Discard an empty non-errored message & try again */
871 if ((!sz) && (!err)) {
872 advance_queue(tsock);
876 /* Capture sender's address (optional) */
878 set_orig_addr(m, msg);
880 /* Capture ancillary data (optional) */
882 if ((res = anc_data_recv(m, msg, tsock->p)))
885 /* Capture message data (if valid) & compute return value (always) */
888 if (unlikely(buf_len < sz)) {
890 m->msg_flags |= MSG_TRUNC;
892 if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg),
899 if ((sock->state == SS_READY) ||
900 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
906 /* Consume received message (optional) */
908 if (likely(!(flags & MSG_PEEK))) {
909 if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
910 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
911 advance_queue(tsock);
919 * recv_stream - receive stream-oriented data
921 * @m: descriptor for message info
922 * @buf_len: total size of user buffer area
923 * @flags: receive flags
925 * Used for SOCK_STREAM messages only. If not enough data is available
926 * will optionally wait for more; never truncates data.
928 * Returns size of returned message data, errno otherwise
931 static int recv_stream(struct kiocb *iocb, struct socket *sock,
932 struct msghdr *m, size_t buf_len, int flags)
934 struct tipc_sock *tsock = tipc_sk(sock->sk);
936 struct tipc_msg *msg;
942 char *crs = m->msg_iov->iov_base;
943 unsigned char *buf_crs;
947 /* Currently doesn't support receiving into multiple iovec entries */
949 if (m->msg_iovlen != 1)
952 /* Catch invalid receive attempts */
954 if (unlikely(!buf_len))
957 if (unlikely(sock->state == SS_DISCONNECTING)) {
958 if (skb_queue_len(&sock->sk->sk_receive_queue) == 0)
960 } else if (unlikely(sock->state != SS_CONNECTED))
963 /* Look for a message in receive queue; wait if necessary */
965 if (unlikely(down_interruptible(&tsock->sem)))
969 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
970 (flags & MSG_DONTWAIT))) {
971 res = (sz_copied == 0) ? -EWOULDBLOCK : 0;
975 if ((res = wait_event_interruptible(
977 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
978 (sock->state == SS_DISCONNECTING))) )) {
982 /* Catch attempt to receive on an already terminated connection */
983 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
990 /* Get access to first message in receive queue */
992 buf = skb_peek(&sock->sk->sk_receive_queue);
994 sz = msg_data_sz(msg);
995 err = msg_errcode(msg);
997 /* Discard an empty non-errored message & try again */
999 if ((!sz) && (!err)) {
1000 advance_queue(tsock);
1004 /* Optionally capture sender's address & ancillary data of first msg */
1006 if (sz_copied == 0) {
1007 set_orig_addr(m, msg);
1008 if ((res = anc_data_recv(m, msg, tsock->p)))
1012 /* Capture message data (if valid) & compute return value (always) */
1015 buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
1016 sz = buf->tail - buf_crs;
1018 needed = (buf_len - sz_copied);
1019 sz_to_copy = (sz <= needed) ? sz : needed;
1020 if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) {
1024 sz_copied += sz_to_copy;
1026 if (sz_to_copy < sz) {
1027 if (!(flags & MSG_PEEK))
1028 TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy;
1035 goto exit; /* can't add error msg to valid data */
1037 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1043 /* Consume received message (optional) */
1045 if (likely(!(flags & MSG_PEEK))) {
1046 if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1047 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
1048 advance_queue(tsock);
1051 /* Loop around if more data is required */
1053 if ((sz_copied < buf_len) /* didn't get all requested data */
1054 && (flags & MSG_WAITALL) /* ... and need to wait for more */
1055 && (!(flags & MSG_PEEK)) /* ... and aren't just peeking at data */
1056 && (!err) /* ... and haven't reached a FIN */
1062 return res ? res : sz_copied;
1066 * queue_overloaded - test if queue overload condition exists
1067 * @queue_size: current size of queue
1068 * @base: nominal maximum size of queue
1069 * @msg: message to be added to queue
1071 * Returns 1 if queue is currently overloaded, 0 otherwise
1074 static int queue_overloaded(u32 queue_size, u32 base, struct tipc_msg *msg)
1077 u32 imp = msg_importance(msg);
1079 if (imp == TIPC_LOW_IMPORTANCE)
1081 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1082 threshold = base * 2;
1083 else if (imp == TIPC_HIGH_IMPORTANCE)
1084 threshold = base * 100;
1088 if (msg_connected(msg))
1091 return (queue_size > threshold);
1095 * async_disconnect - wrapper function used to disconnect port
1096 * @portref: TIPC port reference (passed as pointer-sized value)
1099 static void async_disconnect(unsigned long portref)
1101 tipc_disconnect((u32)portref);
1105 * dispatch - handle arriving message
1106 * @tport: TIPC port that received message
1109 * Called with port locked. Must not take socket lock to avoid deadlock risk.
1111 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1114 static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1116 struct tipc_msg *msg = buf_msg(buf);
1117 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
1118 struct socket *sock;
1121 /* Reject message if socket is closing */
1124 return TIPC_ERR_NO_PORT;
1126 /* Reject message if it is wrong sort of message for socket */
1129 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1130 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1131 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1133 sock = tsock->sk.sk_socket;
1134 if (sock->state == SS_READY) {
1135 if (msg_connected(msg)) {
1136 msg_dbg(msg, "dispatch filter 1\n");
1137 return TIPC_ERR_NO_PORT;
1140 if (msg_mcast(msg)) {
1141 msg_dbg(msg, "dispatch filter 2\n");
1142 return TIPC_ERR_NO_PORT;
1144 if (sock->state == SS_CONNECTED) {
1145 if (!msg_connected(msg)) {
1146 msg_dbg(msg, "dispatch filter 3\n");
1147 return TIPC_ERR_NO_PORT;
1150 else if (sock->state == SS_CONNECTING) {
1151 if (!msg_connected(msg) && (msg_errcode(msg) == 0)) {
1152 msg_dbg(msg, "dispatch filter 4\n");
1153 return TIPC_ERR_NO_PORT;
1156 else if (sock->state == SS_LISTENING) {
1157 if (msg_connected(msg) || msg_errcode(msg)) {
1158 msg_dbg(msg, "dispatch filter 5\n");
1159 return TIPC_ERR_NO_PORT;
1162 else if (sock->state == SS_DISCONNECTING) {
1163 msg_dbg(msg, "dispatch filter 6\n");
1164 return TIPC_ERR_NO_PORT;
1166 else /* (sock->state == SS_UNCONNECTED) */ {
1167 if (msg_connected(msg) || msg_errcode(msg)) {
1168 msg_dbg(msg, "dispatch filter 7\n");
1169 return TIPC_ERR_NO_PORT;
1174 /* Reject message if there isn't room to queue it */
1176 if (unlikely((u32)atomic_read(&tipc_queue_size) >
1177 OVERLOAD_LIMIT_BASE)) {
1178 if (queue_overloaded(atomic_read(&tipc_queue_size),
1179 OVERLOAD_LIMIT_BASE, msg))
1180 return TIPC_ERR_OVERLOAD;
1182 recv_q_len = skb_queue_len(&tsock->sk.sk_receive_queue);
1183 if (unlikely(recv_q_len > (OVERLOAD_LIMIT_BASE / 2))) {
1184 if (queue_overloaded(recv_q_len,
1185 OVERLOAD_LIMIT_BASE / 2, msg))
1186 return TIPC_ERR_OVERLOAD;
1189 /* Initiate connection termination for an incoming 'FIN' */
1191 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1192 sock->state = SS_DISCONNECTING;
1193 /* Note: Use signal since port lock is already taken! */
1194 tipc_k_signal((Handler)async_disconnect, tport->ref);
1197 /* Enqueue message (finally!) */
1199 msg_dbg(msg,"<DISP<: ");
1200 TIPC_SKB_CB(buf)->handle = msg_data(msg);
1201 atomic_inc(&tipc_queue_size);
1202 skb_queue_tail(&sock->sk->sk_receive_queue, buf);
1204 wake_up_interruptible(sock->sk->sk_sleep);
1209 * wakeupdispatch - wake up port after congestion
1210 * @tport: port to wakeup
1212 * Called with port lock on.
1215 static void wakeupdispatch(struct tipc_port *tport)
1217 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
1219 wake_up_interruptible(tsock->sk.sk_sleep);
1223 * connect - establish a connection to another TIPC port
1224 * @sock: socket structure
1225 * @dest: socket address for destination port
1226 * @destlen: size of socket address data structure
1229 * Returns 0 on success, errno otherwise
1232 static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
1235 struct tipc_sock *tsock = tipc_sk(sock->sk);
1236 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1237 struct msghdr m = {NULL,};
1238 struct sk_buff *buf;
1239 struct tipc_msg *msg;
1242 /* For now, TIPC does not allow use of connect() with DGRAM or RDM types */
1244 if (sock->state == SS_READY)
1247 /* MOVE THE REST OF THIS ERROR CHECKING TO send_msg()? */
1248 if (sock->state == SS_LISTENING)
1250 if (sock->state == SS_CONNECTING)
1252 if (sock->state != SS_UNCONNECTED)
1255 if ((destlen < sizeof(*dst)) || (dst->family != AF_TIPC) ||
1256 ((dst->addrtype != TIPC_ADDR_NAME) && (dst->addrtype != TIPC_ADDR_ID)))
1259 /* Send a 'SYN-' to destination */
1262 if ((res = send_msg(NULL, sock, &m, 0)) < 0) {
1263 sock->state = SS_DISCONNECTING;
1267 if (down_interruptible(&tsock->sem))
1268 return -ERESTARTSYS;
1270 /* Wait for destination's 'ACK' response */
1272 res = wait_event_interruptible_timeout(*sock->sk->sk_sleep,
1273 skb_queue_len(&sock->sk->sk_receive_queue),
1274 sock->sk->sk_rcvtimeo);
1275 buf = skb_peek(&sock->sk->sk_receive_queue);
1278 res = auto_connect(sock, tsock, msg);
1280 if (!msg_data_sz(msg))
1281 advance_queue(tsock);
1287 { /* leave "res" unchanged */ }
1288 sock->state = SS_DISCONNECTING;
1296 * listen - allow socket to listen for incoming connections
1297 * @sock: socket structure
1300 * Returns 0 on success, errno otherwise
1303 static int listen(struct socket *sock, int len)
1305 /* REQUIRES SOCKET LOCKING OF SOME SORT? */
1307 if (sock->state == SS_READY)
1309 if (sock->state != SS_UNCONNECTED)
1311 sock->state = SS_LISTENING;
1316 * accept - wait for connection request
1317 * @sock: listening socket
1318 * @newsock: new socket that is to be connected
1319 * @flags: file-related flags associated with socket
1321 * Returns 0 on success, errno otherwise
1324 static int accept(struct socket *sock, struct socket *newsock, int flags)
1326 struct tipc_sock *tsock = tipc_sk(sock->sk);
1327 struct sk_buff *buf;
1330 if (sock->state == SS_READY)
1332 if (sock->state != SS_LISTENING)
1335 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
1336 (flags & O_NONBLOCK)))
1337 return -EWOULDBLOCK;
1339 if (down_interruptible(&tsock->sem))
1340 return -ERESTARTSYS;
1342 if (wait_event_interruptible(*sock->sk->sk_sleep,
1343 skb_queue_len(&sock->sk->sk_receive_queue))) {
1347 buf = skb_peek(&sock->sk->sk_receive_queue);
1349 res = tipc_create(newsock, 0);
1351 struct tipc_sock *new_tsock = tipc_sk(newsock->sk);
1352 struct tipc_portid id;
1353 struct tipc_msg *msg = buf_msg(buf);
1354 u32 new_ref = new_tsock->p->ref;
1356 id.ref = msg_origport(msg);
1357 id.node = msg_orignode(msg);
1358 tipc_connect2port(new_ref, &id);
1359 newsock->state = SS_CONNECTED;
1361 tipc_set_portimportance(new_ref, msg_importance(msg));
1362 if (msg_named(msg)) {
1363 new_tsock->p->conn_type = msg_nametype(msg);
1364 new_tsock->p->conn_instance = msg_nameinst(msg);
1368 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1369 * Respond to 'SYN+' by queuing it on new socket.
1372 msg_dbg(msg,"<ACC<: ");
1373 if (!msg_data_sz(msg)) {
1374 struct msghdr m = {NULL,};
1376 send_packet(NULL, newsock, &m, 0);
1377 advance_queue(tsock);
1380 skb_dequeue(&sock->sk->sk_receive_queue);
1382 skb_queue_head(&newsock->sk->sk_receive_queue, buf);
1391 * shutdown - shutdown socket connection
1392 * @sock: socket structure
1393 * @how: direction to close (unused; always treated as read + write)
1395 * Terminates connection (if necessary), then purges socket's receive queue.
1397 * Returns 0 on success, errno otherwise
1400 static int shutdown(struct socket *sock, int how)
1402 struct tipc_sock* tsock = tipc_sk(sock->sk);
1403 struct sk_buff *buf;
1406 /* Could return -EINVAL for an invalid "how", but why bother? */
1408 if (down_interruptible(&tsock->sem))
1409 return -ERESTARTSYS;
1413 switch (sock->state) {
1416 /* Send 'FIN+' or 'FIN-' message to peer */
1420 if ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
1421 atomic_dec(&tipc_queue_size);
1422 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) {
1426 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
1429 tipc_shutdown(tsock->p->ref);
1435 case SS_DISCONNECTING:
1437 /* Discard any unreceived messages */
1439 while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
1440 atomic_dec(&tipc_queue_size);
1443 tsock->p->conn_unacked = 0;
1448 sock->state = SS_DISCONNECTING;
1463 * setsockopt - set socket option
1464 * @sock: socket structure
1465 * @lvl: option level
1466 * @opt: option identifier
1467 * @ov: pointer to new option value
1468 * @ol: length of option value
1470 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
1471 * (to ease compatibility).
1473 * Returns 0 on success, errno otherwise
1476 static int setsockopt(struct socket *sock,
1477 int lvl, int opt, char __user *ov, int ol)
1479 struct tipc_sock *tsock = tipc_sk(sock->sk);
1483 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1485 if (lvl != SOL_TIPC)
1486 return -ENOPROTOOPT;
1487 if (ol < sizeof(value))
1489 if ((res = get_user(value, (u32 *)ov)))
1492 if (down_interruptible(&tsock->sem))
1493 return -ERESTARTSYS;
1496 case TIPC_IMPORTANCE:
1497 res = tipc_set_portimportance(tsock->p->ref, value);
1499 case TIPC_SRC_DROPPABLE:
1500 if (sock->type != SOCK_STREAM)
1501 res = tipc_set_portunreliable(tsock->p->ref, value);
1505 case TIPC_DEST_DROPPABLE:
1506 res = tipc_set_portunreturnable(tsock->p->ref, value);
1508 case TIPC_CONN_TIMEOUT:
1509 sock->sk->sk_rcvtimeo = (value * HZ / 1000);
1520 * getsockopt - get socket option
1521 * @sock: socket structure
1522 * @lvl: option level
1523 * @opt: option identifier
1524 * @ov: receptacle for option value
1525 * @ol: receptacle for length of option value
1527 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
1528 * (to ease compatibility).
1530 * Returns 0 on success, errno otherwise
1533 static int getsockopt(struct socket *sock,
1534 int lvl, int opt, char __user *ov, int *ol)
1536 struct tipc_sock *tsock = tipc_sk(sock->sk);
1541 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1542 return put_user(0, ol);
1543 if (lvl != SOL_TIPC)
1544 return -ENOPROTOOPT;
1545 if ((res = get_user(len, ol)))
1548 if (down_interruptible(&tsock->sem))
1549 return -ERESTARTSYS;
1552 case TIPC_IMPORTANCE:
1553 res = tipc_portimportance(tsock->p->ref, &value);
1555 case TIPC_SRC_DROPPABLE:
1556 res = tipc_portunreliable(tsock->p->ref, &value);
1558 case TIPC_DEST_DROPPABLE:
1559 res = tipc_portunreturnable(tsock->p->ref, &value);
1561 case TIPC_CONN_TIMEOUT:
1562 value = (sock->sk->sk_rcvtimeo * 1000) / HZ;
1571 else if (len < sizeof(value)) {
1574 else if ((res = copy_to_user(ov, &value, sizeof(value)))) {
1575 /* couldn't return value */
1578 res = put_user(sizeof(value), ol);
1586 * Placeholders for non-implemented functionality
1588 * Returns error code (POSIX-compliant where defined)
1591 static int ioctl(struct socket *s, u32 cmd, unsigned long arg)
1596 static int no_mmap(struct file *file, struct socket *sock,
1597 struct vm_area_struct *vma)
1601 static ssize_t no_sendpage(struct socket *sock, struct page *page,
1602 int offset, size_t size, int flags)
1607 static int no_skpair(struct socket *s1, struct socket *s2)
1613 * Protocol switches for the various types of TIPC sockets
1616 static struct proto_ops msg_ops = {
1617 .owner = THIS_MODULE,
1622 .socketpair = no_skpair,
1624 .getname = get_name,
1628 .shutdown = shutdown,
1629 .setsockopt = setsockopt,
1630 .getsockopt = getsockopt,
1631 .sendmsg = send_msg,
1632 .recvmsg = recv_msg,
1634 .sendpage = no_sendpage
1637 static struct proto_ops packet_ops = {
1638 .owner = THIS_MODULE,
1643 .socketpair = no_skpair,
1645 .getname = get_name,
1649 .shutdown = shutdown,
1650 .setsockopt = setsockopt,
1651 .getsockopt = getsockopt,
1652 .sendmsg = send_packet,
1653 .recvmsg = recv_msg,
1655 .sendpage = no_sendpage
1658 static struct proto_ops stream_ops = {
1659 .owner = THIS_MODULE,
1664 .socketpair = no_skpair,
1666 .getname = get_name,
1670 .shutdown = shutdown,
1671 .setsockopt = setsockopt,
1672 .getsockopt = getsockopt,
1673 .sendmsg = send_stream,
1674 .recvmsg = recv_stream,
1676 .sendpage = no_sendpage
1679 static struct net_proto_family tipc_family_ops = {
1680 .owner = THIS_MODULE,
1682 .create = tipc_create
1685 static struct proto tipc_proto = {
1687 .owner = THIS_MODULE,
1688 .obj_size = sizeof(struct tipc_sock)
1692 * tipc_socket_init - initialize TIPC socket interface
1694 * Returns 0 on success, errno otherwise
1696 int tipc_socket_init(void)
1700 res = proto_register(&tipc_proto, 1);
1702 err("Failed to register TIPC protocol type\n");
1706 res = sock_register(&tipc_family_ops);
1708 err("Failed to register TIPC socket type\n");
1709 proto_unregister(&tipc_proto);
1713 sockets_enabled = 1;
1719 * tipc_socket_stop - stop TIPC socket interface
1721 void tipc_socket_stop(void)
1723 if (!sockets_enabled)
1726 sockets_enabled = 0;
1727 sock_unregister(tipc_family_ops.family);
1728 proto_unregister(&tipc_proto);