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/version.h>
46 #include <linux/fcntl.h>
47 #include <linux/version.h>
48 #include <asm/semaphore.h>
49 #include <asm/string.h>
50 #include <asm/atomic.h>
53 #include <linux/tipc.h>
54 #include <linux/tipc_config.h>
55 #include <net/tipc/tipc_msg.h>
56 #include <net/tipc/tipc_port.h>
60 #define SS_LISTENING -1 /* socket is listening */
61 #define SS_READY -2 /* socket is connectionless */
63 #define OVERLOAD_LIMIT_BASE 5000
71 #define tipc_sk(sk) ((struct tipc_sock*)sk)
73 static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
74 static void wakeupdispatch(struct tipc_port *tport);
76 static struct proto_ops packet_ops;
77 static struct proto_ops stream_ops;
78 static struct proto_ops msg_ops;
80 static struct proto tipc_proto;
82 static int sockets_enabled = 0;
84 static atomic_t tipc_queue_size = ATOMIC_INIT(0);
88 * sock_lock(): Lock a port/socket pair. lock_sock() can
89 * not be used here, since the same lock must protect ports
90 * with non-socket interfaces.
91 * See net.c for description of locking policy.
93 static inline void sock_lock(struct tipc_sock* tsock)
95 spin_lock_bh(tsock->p->lock);
99 * sock_unlock(): Unlock a port/socket pair
101 static inline void sock_unlock(struct tipc_sock* tsock)
103 spin_unlock_bh(tsock->p->lock);
107 * pollmask - determine the current set of poll() events for a socket
108 * @sock: socket structure
110 * TIPC sets the returned events as follows:
111 * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty
112 * or if a connection-oriented socket is does not have an active connection
113 * (i.e. a read operation will not block).
114 * b) POLLOUT is set except when a socket's connection has been terminated
115 * (i.e. a write operation will not block).
116 * c) POLLHUP is set when a socket's connection has been terminated.
118 * IMPORTANT: The fact that a read or write operation will not block does NOT
119 * imply that the operation will succeed!
121 * Returns pollmask value
124 static inline u32 pollmask(struct socket *sock)
128 if ((skb_queue_len(&sock->sk->sk_receive_queue) != 0) ||
129 (sock->state == SS_UNCONNECTED) ||
130 (sock->state == SS_DISCONNECTING))
131 mask = (POLLRDNORM | POLLIN);
135 if (sock->state == SS_DISCONNECTING)
145 * advance_queue - discard first buffer in queue
146 * @tsock: TIPC socket
149 static inline void advance_queue(struct tipc_sock *tsock)
152 buf_discard(skb_dequeue(&tsock->sk.sk_receive_queue));
154 atomic_dec(&tipc_queue_size);
158 * tipc_create - create a TIPC socket
159 * @sock: pre-allocated socket structure
160 * @protocol: protocol indicator (must be 0)
162 * This routine creates and attaches a 'struct sock' to the 'struct socket',
163 * then create and attaches a TIPC port to the 'struct sock' part.
165 * Returns 0 on success, errno otherwise
167 static int tipc_create(struct socket *sock, int protocol)
169 struct tipc_sock *tsock;
170 struct tipc_port *port;
174 if ((sock->type != SOCK_STREAM) &&
175 (sock->type != SOCK_SEQPACKET) &&
176 (sock->type != SOCK_DGRAM) &&
177 (sock->type != SOCK_RDM))
180 if (unlikely(protocol != 0))
181 return -EPROTONOSUPPORT;
183 ref = tipc_createport_raw(0, &dispatch, &wakeupdispatch, TIPC_LOW_IMPORTANCE);
187 sock->state = SS_UNCONNECTED;
189 switch (sock->type) {
191 sock->ops = &stream_ops;
194 sock->ops = &packet_ops;
197 tipc_set_portunreliable(ref, 1);
200 tipc_set_portunreturnable(ref, 1);
201 sock->ops = &msg_ops;
202 sock->state = SS_READY;
206 sk = sk_alloc(AF_TIPC, GFP_KERNEL, &tipc_proto, 1);
208 tipc_deleteport(ref);
212 sock_init_data(sock, sk);
213 init_waitqueue_head(sk->sk_sleep);
214 sk->sk_rcvtimeo = 8 * HZ; /* default connect timeout = 8s */
217 port = tipc_get_port(ref);
220 port->usr_handle = tsock;
222 init_MUTEX(&tsock->sem);
224 dbg("sock_create: %x\n",tsock);
226 atomic_inc(&tipc_user_count);
232 * release - destroy a TIPC socket
233 * @sock: socket to destroy
235 * This routine cleans up any messages that are still queued on the socket.
236 * For DGRAM and RDM socket types, all queued messages are rejected.
237 * For SEQPACKET and STREAM socket types, the first message is rejected
238 * and any others are discarded. (If the first message on a STREAM socket
239 * is partially-read, it is discarded and the next one is rejected instead.)
241 * NOTE: Rejected messages are not necessarily returned to the sender! They
242 * are returned or discarded according to the "destination droppable" setting
243 * specified for the message by the sender.
245 * Returns 0 on success, errno otherwise
248 static int release(struct socket *sock)
250 struct tipc_sock *tsock = tipc_sk(sock->sk);
251 struct sock *sk = sock->sk;
255 dbg("sock_delete: %x\n",tsock);
258 down_interruptible(&tsock->sem);
264 /* Reject unreceived messages, unless no longer connected */
266 while (sock->state != SS_DISCONNECTING) {
268 buf = skb_dequeue(&sk->sk_receive_queue);
270 tsock->p->usr_handle = 0;
274 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf)))
277 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
278 atomic_dec(&tipc_queue_size);
281 /* Delete TIPC port */
283 res = tipc_deleteport(tsock->p->ref);
286 /* Discard any remaining messages */
288 while ((buf = skb_dequeue(&sk->sk_receive_queue))) {
290 atomic_dec(&tipc_queue_size);
297 atomic_dec(&tipc_user_count);
302 * bind - associate or disassocate TIPC name(s) with a socket
303 * @sock: socket structure
304 * @uaddr: socket address describing name(s) and desired operation
305 * @uaddr_len: size of socket address data structure
307 * Name and name sequence binding is indicated using a positive scope value;
308 * a negative scope value unbinds the specified name. Specifying no name
309 * (i.e. a socket address length of 0) unbinds all names from the socket.
311 * Returns 0 on success, errno otherwise
314 static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
316 struct tipc_sock *tsock = tipc_sk(sock->sk);
317 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
320 if (down_interruptible(&tsock->sem))
323 if (unlikely(!uaddr_len)) {
324 res = tipc_withdraw(tsock->p->ref, 0, 0);
328 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
333 if (addr->family != AF_TIPC) {
337 if (addr->addrtype == TIPC_ADDR_NAME)
338 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
339 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
345 res = tipc_publish(tsock->p->ref, addr->scope,
346 &addr->addr.nameseq);
348 res = tipc_withdraw(tsock->p->ref, -addr->scope,
349 &addr->addr.nameseq);
356 * get_name - get port ID of socket or peer socket
357 * @sock: socket structure
358 * @uaddr: area for returned socket address
359 * @uaddr_len: area for returned length of socket address
360 * @peer: 0 to obtain socket name, 1 to obtain peer socket name
362 * Returns 0 on success, errno otherwise
365 static int get_name(struct socket *sock, struct sockaddr *uaddr,
366 int *uaddr_len, int peer)
368 struct tipc_sock *tsock = tipc_sk(sock->sk);
369 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
372 if (down_interruptible(&tsock->sem))
375 *uaddr_len = sizeof(*addr);
376 addr->addrtype = TIPC_ADDR_ID;
377 addr->family = AF_TIPC;
380 res = tipc_peer(tsock->p->ref, &addr->addr.id);
382 res = tipc_ownidentity(tsock->p->ref, &addr->addr.id);
383 addr->addr.name.domain = 0;
390 * poll - read and possibly block on pollmask
391 * @file: file structure associated with the socket
392 * @sock: socket for which to calculate the poll bits
395 * Returns the pollmask
398 static unsigned int poll(struct file *file, struct socket *sock,
401 poll_wait(file, sock->sk->sk_sleep, wait);
402 /* NEED LOCK HERE? */
403 return pollmask(sock);
407 * dest_name_check - verify user is permitted to send to specified port name
408 * @dest: destination address
409 * @m: descriptor for message to be sent
411 * Prevents restricted configuration commands from being issued by
412 * unauthorized users.
414 * Returns 0 if permission is granted, otherwise errno
417 static inline int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
419 struct tipc_cfg_msg_hdr hdr;
421 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
423 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
426 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
429 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
431 if ((ntohs(hdr.tcm_type) & 0xC000) & (!capable(CAP_NET_ADMIN)))
438 * send_msg - send message in connectionless manner
440 * @sock: socket structure
441 * @m: message to send
442 * @total_len: (unused)
444 * Message must have an destination specified explicitly.
445 * Used for SOCK_RDM and SOCK_DGRAM messages,
446 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
447 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
449 * Returns the number of bytes sent on success, or errno otherwise
452 static int send_msg(struct kiocb *iocb, struct socket *sock,
453 struct msghdr *m, size_t total_len)
455 struct tipc_sock *tsock = tipc_sk(sock->sk);
456 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
462 return -EDESTADDRREQ;
463 if (unlikely(dest->family != AF_TIPC))
466 needs_conn = (sock->state != SS_READY);
467 if (unlikely(needs_conn)) {
468 if (sock->state == SS_LISTENING)
470 if (sock->state != SS_UNCONNECTED)
472 if ((tsock->p->published) ||
473 ((sock->type == SOCK_STREAM) && (total_len != 0)))
477 if (down_interruptible(&tsock->sem))
482 /* Abort any pending connection attempts (very unlikely) */
484 while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
485 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
486 atomic_dec(&tipc_queue_size);
489 sock->state = SS_CONNECTING;
493 if (dest->addrtype == TIPC_ADDR_NAME) {
494 if ((res = dest_name_check(dest, m)))
496 res = tipc_send2name(tsock->p->ref,
497 &dest->addr.name.name,
498 dest->addr.name.domain,
502 else if (dest->addrtype == TIPC_ADDR_ID) {
503 res = tipc_send2port(tsock->p->ref,
508 else if (dest->addrtype == TIPC_ADDR_MCAST) {
513 if ((res = dest_name_check(dest, m)))
515 res = tipc_multicast(tsock->p->ref,
521 if (likely(res != -ELINKCONG)) {
526 if (m->msg_flags & MSG_DONTWAIT) {
530 if (wait_event_interruptible(*sock->sk->sk_sleep,
531 !tsock->p->congested)) {
539 * send_packet - send a connection-oriented message
541 * @sock: socket structure
542 * @m: message to send
543 * @total_len: (unused)
545 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
547 * Returns the number of bytes sent on success, or errno otherwise
550 static int send_packet(struct kiocb *iocb, struct socket *sock,
551 struct msghdr *m, size_t total_len)
553 struct tipc_sock *tsock = tipc_sk(sock->sk);
554 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
557 /* Handle implied connection establishment */
560 return send_msg(iocb, sock, m, total_len);
562 if (down_interruptible(&tsock->sem)) {
566 if (unlikely(sock->state != SS_CONNECTED)) {
567 if (sock->state == SS_DISCONNECTING)
575 res = tipc_send(tsock->p->ref, m->msg_iovlen, m->msg_iov);
576 if (likely(res != -ELINKCONG)) {
581 if (m->msg_flags & MSG_DONTWAIT) {
585 if (wait_event_interruptible(*sock->sk->sk_sleep,
586 !tsock->p->congested)) {
594 * send_stream - send stream-oriented data
596 * @sock: socket structure
598 * @total_len: total length of data to be sent
600 * Used for SOCK_STREAM data.
602 * Returns the number of bytes sent on success, or errno otherwise
606 static int send_stream(struct kiocb *iocb, struct socket *sock,
607 struct msghdr *m, size_t total_len)
609 struct msghdr my_msg;
611 struct iovec *curr_iov;
613 char __user *curr_start;
618 if (likely(total_len <= TIPC_MAX_USER_MSG_SIZE))
619 return send_packet(iocb, sock, m, total_len);
621 /* Can only send large data streams if already connected */
623 if (unlikely(sock->state != SS_CONNECTED)) {
624 if (sock->state == SS_DISCONNECTING)
631 * Send each iovec entry using one or more messages
633 * Note: This algorithm is good for the most likely case
634 * (i.e. one large iovec entry), but could be improved to pass sets
635 * of small iovec entries into send_packet().
639 curr_iov = my_msg.msg_iov;
640 curr_iovlen = my_msg.msg_iovlen;
641 my_msg.msg_iov = &my_iov;
642 my_msg.msg_iovlen = 1;
644 while (curr_iovlen--) {
645 curr_start = curr_iov->iov_base;
646 curr_left = curr_iov->iov_len;
649 bytes_to_send = (curr_left < TIPC_MAX_USER_MSG_SIZE)
650 ? curr_left : TIPC_MAX_USER_MSG_SIZE;
651 my_iov.iov_base = curr_start;
652 my_iov.iov_len = bytes_to_send;
653 if ((res = send_packet(iocb, sock, &my_msg, 0)) < 0)
655 curr_left -= bytes_to_send;
656 curr_start += bytes_to_send;
666 * auto_connect - complete connection setup to a remote port
667 * @sock: socket structure
668 * @tsock: TIPC-specific socket structure
669 * @msg: peer's response message
671 * Returns 0 on success, errno otherwise
674 static int auto_connect(struct socket *sock, struct tipc_sock *tsock,
675 struct tipc_msg *msg)
677 struct tipc_portid peer;
679 if (msg_errcode(msg)) {
680 sock->state = SS_DISCONNECTING;
681 return -ECONNREFUSED;
684 peer.ref = msg_origport(msg);
685 peer.node = msg_orignode(msg);
686 tipc_connect2port(tsock->p->ref, &peer);
687 tipc_set_portimportance(tsock->p->ref, msg_importance(msg));
688 sock->state = SS_CONNECTED;
693 * set_orig_addr - capture sender's address for received message
694 * @m: descriptor for message info
695 * @msg: received message header
697 * Note: Address is not captured if not requested by receiver.
700 static inline void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
702 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
705 addr->family = AF_TIPC;
706 addr->addrtype = TIPC_ADDR_ID;
707 addr->addr.id.ref = msg_origport(msg);
708 addr->addr.id.node = msg_orignode(msg);
709 addr->addr.name.domain = 0; /* could leave uninitialized */
710 addr->scope = 0; /* could leave uninitialized */
711 m->msg_namelen = sizeof(struct sockaddr_tipc);
716 * anc_data_recv - optionally capture ancillary data for received message
717 * @m: descriptor for message info
718 * @msg: received message header
719 * @tport: TIPC port associated with message
721 * Note: Ancillary data is not captured if not requested by receiver.
723 * Returns 0 if successful, otherwise errno
726 static inline int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
727 struct tipc_port *tport)
734 if (likely(m->msg_controllen == 0))
737 /* Optionally capture errored message object(s) */
739 err = msg ? msg_errcode(msg) : 0;
742 anc_data[1] = msg_data_sz(msg);
743 if ((res = put_cmsg(m, SOL_SOCKET, TIPC_ERRINFO, 8, anc_data)))
746 (res = put_cmsg(m, SOL_SOCKET, TIPC_RETDATA, anc_data[1],
751 /* Optionally capture message destination object */
753 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
756 anc_data[0] = msg_nametype(msg);
757 anc_data[1] = msg_namelower(msg);
758 anc_data[2] = msg_namelower(msg);
761 anc_data[0] = msg_nametype(msg);
762 anc_data[1] = msg_namelower(msg);
763 anc_data[2] = msg_nameupper(msg);
766 anc_data[0] = tport->conn_type;
767 anc_data[1] = tport->conn_instance;
768 anc_data[2] = tport->conn_instance;
774 (res = put_cmsg(m, SOL_SOCKET, TIPC_DESTNAME, 12, anc_data)))
781 * recv_msg - receive packet-oriented message
783 * @m: descriptor for message info
784 * @buf_len: total size of user buffer area
785 * @flags: receive flags
787 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
788 * If the complete message doesn't fit in user area, truncate it.
790 * Returns size of returned message data, errno otherwise
793 static int recv_msg(struct kiocb *iocb, struct socket *sock,
794 struct msghdr *m, size_t buf_len, int flags)
796 struct tipc_sock *tsock = tipc_sk(sock->sk);
798 struct tipc_msg *msg;
804 /* Currently doesn't support receiving into multiple iovec entries */
806 if (m->msg_iovlen != 1)
809 /* Catch invalid receive attempts */
811 if (unlikely(!buf_len))
814 if (sock->type == SOCK_SEQPACKET) {
815 if (unlikely(sock->state == SS_UNCONNECTED))
817 if (unlikely((sock->state == SS_DISCONNECTING) &&
818 (skb_queue_len(&sock->sk->sk_receive_queue) == 0)))
822 /* Look for a message in receive queue; wait if necessary */
824 if (unlikely(down_interruptible(&tsock->sem)))
828 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
829 (flags & MSG_DONTWAIT))) {
834 if ((res = wait_event_interruptible(
836 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
837 (sock->state == SS_DISCONNECTING))) )) {
841 /* Catch attempt to receive on an already terminated connection */
842 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
849 /* Get access to first message in receive queue */
851 buf = skb_peek(&sock->sk->sk_receive_queue);
853 sz = msg_data_sz(msg);
854 err = msg_errcode(msg);
856 /* Complete connection setup for an implied connect */
858 if (unlikely(sock->state == SS_CONNECTING)) {
859 if ((res = auto_connect(sock, tsock, msg)))
863 /* Discard an empty non-errored message & try again */
865 if ((!sz) && (!err)) {
866 advance_queue(tsock);
870 /* Capture sender's address (optional) */
872 set_orig_addr(m, msg);
874 /* Capture ancillary data (optional) */
876 if ((res = anc_data_recv(m, msg, tsock->p)))
879 /* Capture message data (if valid) & compute return value (always) */
882 if (unlikely(buf_len < sz)) {
884 m->msg_flags |= MSG_TRUNC;
886 if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg),
893 if ((sock->state == SS_READY) ||
894 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
900 /* Consume received message (optional) */
902 if (likely(!(flags & MSG_PEEK))) {
903 if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
904 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
905 advance_queue(tsock);
913 * recv_stream - receive stream-oriented data
915 * @m: descriptor for message info
916 * @buf_len: total size of user buffer area
917 * @flags: receive flags
919 * Used for SOCK_STREAM messages only. If not enough data is available
920 * will optionally wait for more; never truncates data.
922 * Returns size of returned message data, errno otherwise
925 static int recv_stream(struct kiocb *iocb, struct socket *sock,
926 struct msghdr *m, size_t buf_len, int flags)
928 struct tipc_sock *tsock = tipc_sk(sock->sk);
930 struct tipc_msg *msg;
936 char *crs = m->msg_iov->iov_base;
937 unsigned char *buf_crs;
941 /* Currently doesn't support receiving into multiple iovec entries */
943 if (m->msg_iovlen != 1)
946 /* Catch invalid receive attempts */
948 if (unlikely(!buf_len))
951 if (unlikely(sock->state == SS_DISCONNECTING)) {
952 if (skb_queue_len(&sock->sk->sk_receive_queue) == 0)
954 } else if (unlikely(sock->state != SS_CONNECTED))
957 /* Look for a message in receive queue; wait if necessary */
959 if (unlikely(down_interruptible(&tsock->sem)))
963 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
964 (flags & MSG_DONTWAIT))) {
965 res = (sz_copied == 0) ? -EWOULDBLOCK : 0;
969 if ((res = wait_event_interruptible(
971 ((q_len = skb_queue_len(&sock->sk->sk_receive_queue)) ||
972 (sock->state == SS_DISCONNECTING))) )) {
976 /* Catch attempt to receive on an already terminated connection */
977 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
984 /* Get access to first message in receive queue */
986 buf = skb_peek(&sock->sk->sk_receive_queue);
988 sz = msg_data_sz(msg);
989 err = msg_errcode(msg);
991 /* Discard an empty non-errored message & try again */
993 if ((!sz) && (!err)) {
994 advance_queue(tsock);
998 /* Optionally capture sender's address & ancillary data of first msg */
1000 if (sz_copied == 0) {
1001 set_orig_addr(m, msg);
1002 if ((res = anc_data_recv(m, msg, tsock->p)))
1006 /* Capture message data (if valid) & compute return value (always) */
1009 buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
1010 sz = buf->tail - buf_crs;
1012 needed = (buf_len - sz_copied);
1013 sz_to_copy = (sz <= needed) ? sz : needed;
1014 if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) {
1018 sz_copied += sz_to_copy;
1020 if (sz_to_copy < sz) {
1021 if (!(flags & MSG_PEEK))
1022 TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy;
1029 goto exit; /* can't add error msg to valid data */
1031 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1037 /* Consume received message (optional) */
1039 if (likely(!(flags & MSG_PEEK))) {
1040 if (unlikely(++tsock->p->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1041 tipc_acknowledge(tsock->p->ref, tsock->p->conn_unacked);
1042 advance_queue(tsock);
1045 /* Loop around if more data is required */
1047 if ((sz_copied < buf_len) /* didn't get all requested data */
1048 && (flags & MSG_WAITALL) /* ... and need to wait for more */
1049 && (!(flags & MSG_PEEK)) /* ... and aren't just peeking at data */
1050 && (!err) /* ... and haven't reached a FIN */
1056 return res ? res : sz_copied;
1060 * queue_overloaded - test if queue overload condition exists
1061 * @queue_size: current size of queue
1062 * @base: nominal maximum size of queue
1063 * @msg: message to be added to queue
1065 * Returns 1 if queue is currently overloaded, 0 otherwise
1068 static int queue_overloaded(u32 queue_size, u32 base, struct tipc_msg *msg)
1071 u32 imp = msg_importance(msg);
1073 if (imp == TIPC_LOW_IMPORTANCE)
1075 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1076 threshold = base * 2;
1077 else if (imp == TIPC_HIGH_IMPORTANCE)
1078 threshold = base * 100;
1082 if (msg_connected(msg))
1085 return (queue_size > threshold);
1089 * async_disconnect - wrapper function used to disconnect port
1090 * @portref: TIPC port reference (passed as pointer-sized value)
1093 static void async_disconnect(unsigned long portref)
1095 tipc_disconnect((u32)portref);
1099 * dispatch - handle arriving message
1100 * @tport: TIPC port that received message
1103 * Called with port locked. Must not take socket lock to avoid deadlock risk.
1105 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1108 static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1110 struct tipc_msg *msg = buf_msg(buf);
1111 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
1112 struct socket *sock;
1115 /* Reject message if socket is closing */
1118 return TIPC_ERR_NO_PORT;
1120 /* Reject message if it is wrong sort of message for socket */
1123 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1124 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1125 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1127 sock = tsock->sk.sk_socket;
1128 if (sock->state == SS_READY) {
1129 if (msg_connected(msg)) {
1130 msg_dbg(msg, "dispatch filter 1\n");
1131 return TIPC_ERR_NO_PORT;
1134 if (msg_mcast(msg)) {
1135 msg_dbg(msg, "dispatch filter 2\n");
1136 return TIPC_ERR_NO_PORT;
1138 if (sock->state == SS_CONNECTED) {
1139 if (!msg_connected(msg)) {
1140 msg_dbg(msg, "dispatch filter 3\n");
1141 return TIPC_ERR_NO_PORT;
1144 else if (sock->state == SS_CONNECTING) {
1145 if (!msg_connected(msg) && (msg_errcode(msg) == 0)) {
1146 msg_dbg(msg, "dispatch filter 4\n");
1147 return TIPC_ERR_NO_PORT;
1150 else if (sock->state == SS_LISTENING) {
1151 if (msg_connected(msg) || msg_errcode(msg)) {
1152 msg_dbg(msg, "dispatch filter 5\n");
1153 return TIPC_ERR_NO_PORT;
1156 else if (sock->state == SS_DISCONNECTING) {
1157 msg_dbg(msg, "dispatch filter 6\n");
1158 return TIPC_ERR_NO_PORT;
1160 else /* (sock->state == SS_UNCONNECTED) */ {
1161 if (msg_connected(msg) || msg_errcode(msg)) {
1162 msg_dbg(msg, "dispatch filter 7\n");
1163 return TIPC_ERR_NO_PORT;
1168 /* Reject message if there isn't room to queue it */
1170 if (unlikely((u32)atomic_read(&tipc_queue_size) >
1171 OVERLOAD_LIMIT_BASE)) {
1172 if (queue_overloaded(atomic_read(&tipc_queue_size),
1173 OVERLOAD_LIMIT_BASE, msg))
1174 return TIPC_ERR_OVERLOAD;
1176 recv_q_len = skb_queue_len(&tsock->sk.sk_receive_queue);
1177 if (unlikely(recv_q_len > (OVERLOAD_LIMIT_BASE / 2))) {
1178 if (queue_overloaded(recv_q_len,
1179 OVERLOAD_LIMIT_BASE / 2, msg))
1180 return TIPC_ERR_OVERLOAD;
1183 /* Initiate connection termination for an incoming 'FIN' */
1185 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1186 sock->state = SS_DISCONNECTING;
1187 /* Note: Use signal since port lock is already taken! */
1188 k_signal((Handler)async_disconnect, tport->ref);
1191 /* Enqueue message (finally!) */
1193 msg_dbg(msg,"<DISP<: ");
1194 TIPC_SKB_CB(buf)->handle = msg_data(msg);
1195 atomic_inc(&tipc_queue_size);
1196 skb_queue_tail(&sock->sk->sk_receive_queue, buf);
1198 wake_up_interruptible(sock->sk->sk_sleep);
1203 * wakeupdispatch - wake up port after congestion
1204 * @tport: port to wakeup
1206 * Called with port lock on.
1209 static void wakeupdispatch(struct tipc_port *tport)
1211 struct tipc_sock *tsock = (struct tipc_sock *)tport->usr_handle;
1213 wake_up_interruptible(tsock->sk.sk_sleep);
1217 * connect - establish a connection to another TIPC port
1218 * @sock: socket structure
1219 * @dest: socket address for destination port
1220 * @destlen: size of socket address data structure
1223 * Returns 0 on success, errno otherwise
1226 static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
1229 struct tipc_sock *tsock = tipc_sk(sock->sk);
1230 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1231 struct msghdr m = {0,};
1232 struct sk_buff *buf;
1233 struct tipc_msg *msg;
1236 /* For now, TIPC does not allow use of connect() with DGRAM or RDM types */
1238 if (sock->state == SS_READY)
1241 /* MOVE THE REST OF THIS ERROR CHECKING TO send_msg()? */
1242 if (sock->state == SS_LISTENING)
1244 if (sock->state == SS_CONNECTING)
1246 if (sock->state != SS_UNCONNECTED)
1249 if ((dst->family != AF_TIPC) ||
1250 ((dst->addrtype != TIPC_ADDR_NAME) && (dst->addrtype != TIPC_ADDR_ID)))
1253 /* Send a 'SYN-' to destination */
1256 if ((res = send_msg(0, sock, &m, 0)) < 0) {
1257 sock->state = SS_DISCONNECTING;
1261 if (down_interruptible(&tsock->sem))
1262 return -ERESTARTSYS;
1264 /* Wait for destination's 'ACK' response */
1266 res = wait_event_interruptible_timeout(*sock->sk->sk_sleep,
1267 skb_queue_len(&sock->sk->sk_receive_queue),
1268 sock->sk->sk_rcvtimeo);
1269 buf = skb_peek(&sock->sk->sk_receive_queue);
1272 res = auto_connect(sock, tsock, msg);
1274 if (dst->addrtype == TIPC_ADDR_NAME) {
1275 tsock->p->conn_type = dst->addr.name.name.type;
1276 tsock->p->conn_instance = dst->addr.name.name.instance;
1278 if (!msg_data_sz(msg))
1279 advance_queue(tsock);
1285 { /* leave "res" unchanged */ }
1286 sock->state = SS_DISCONNECTING;
1294 * listen - allow socket to listen for incoming connections
1295 * @sock: socket structure
1298 * Returns 0 on success, errno otherwise
1301 static int listen(struct socket *sock, int len)
1303 /* REQUIRES SOCKET LOCKING OF SOME SORT? */
1305 if (sock->state == SS_READY)
1307 if (sock->state != SS_UNCONNECTED)
1309 sock->state = SS_LISTENING;
1314 * accept - wait for connection request
1315 * @sock: listening socket
1316 * @newsock: new socket that is to be connected
1317 * @flags: file-related flags associated with socket
1319 * Returns 0 on success, errno otherwise
1322 static int accept(struct socket *sock, struct socket *newsock, int flags)
1324 struct tipc_sock *tsock = tipc_sk(sock->sk);
1325 struct sk_buff *buf;
1328 if (sock->state == SS_READY)
1330 if (sock->state != SS_LISTENING)
1333 if (unlikely((skb_queue_len(&sock->sk->sk_receive_queue) == 0) &&
1334 (flags & O_NONBLOCK)))
1335 return -EWOULDBLOCK;
1337 if (down_interruptible(&tsock->sem))
1338 return -ERESTARTSYS;
1340 if (wait_event_interruptible(*sock->sk->sk_sleep,
1341 skb_queue_len(&sock->sk->sk_receive_queue))) {
1345 buf = skb_peek(&sock->sk->sk_receive_queue);
1347 res = tipc_create(newsock, 0);
1349 struct tipc_sock *new_tsock = tipc_sk(newsock->sk);
1350 struct tipc_portid id;
1351 struct tipc_msg *msg = buf_msg(buf);
1352 u32 new_ref = new_tsock->p->ref;
1354 id.ref = msg_origport(msg);
1355 id.node = msg_orignode(msg);
1356 tipc_connect2port(new_ref, &id);
1357 newsock->state = SS_CONNECTED;
1359 tipc_set_portimportance(new_ref, msg_importance(msg));
1360 if (msg_named(msg)) {
1361 new_tsock->p->conn_type = msg_nametype(msg);
1362 new_tsock->p->conn_instance = msg_nameinst(msg);
1366 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1367 * Respond to 'SYN+' by queuing it on new socket.
1370 msg_dbg(msg,"<ACC<: ");
1371 if (!msg_data_sz(msg)) {
1372 struct msghdr m = {0,};
1374 send_packet(0, newsock, &m, 0);
1375 advance_queue(tsock);
1378 skb_dequeue(&sock->sk->sk_receive_queue);
1380 skb_queue_head(&newsock->sk->sk_receive_queue, buf);
1389 * shutdown - shutdown socket connection
1390 * @sock: socket structure
1391 * @how: direction to close (always treated as read + write)
1393 * Terminates connection (if necessary), then purges socket's receive queue.
1395 * Returns 0 on success, errno otherwise
1398 static int shutdown(struct socket *sock, int how)
1400 struct tipc_sock* tsock = tipc_sk(sock->sk);
1401 struct sk_buff *buf;
1404 /* Could return -EINVAL for an invalid "how", but why bother? */
1406 if (down_interruptible(&tsock->sem))
1407 return -ERESTARTSYS;
1411 switch (sock->state) {
1414 /* Send 'FIN+' or 'FIN-' message to peer */
1418 if ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
1419 atomic_dec(&tipc_queue_size);
1420 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) {
1424 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
1427 tipc_shutdown(tsock->p->ref);
1433 case SS_DISCONNECTING:
1435 /* Discard any unreceived messages */
1437 while ((buf = skb_dequeue(&sock->sk->sk_receive_queue))) {
1438 atomic_dec(&tipc_queue_size);
1441 tsock->p->conn_unacked = 0;
1446 sock->state = SS_DISCONNECTING;
1461 * setsockopt - set socket option
1462 * @sock: socket structure
1463 * @lvl: option level
1464 * @opt: option identifier
1465 * @ov: pointer to new option value
1466 * @ol: length of option value
1468 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
1469 * (to ease compatibility).
1471 * Returns 0 on success, errno otherwise
1474 static int setsockopt(struct socket *sock, int lvl, int opt, char *ov, int ol)
1476 struct tipc_sock *tsock = tipc_sk(sock->sk);
1480 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1482 if (lvl != SOL_TIPC)
1483 return -ENOPROTOOPT;
1484 if (ol < sizeof(value))
1486 if ((res = get_user(value, (u32 *)ov)))
1489 if (down_interruptible(&tsock->sem))
1490 return -ERESTARTSYS;
1493 case TIPC_IMPORTANCE:
1494 res = tipc_set_portimportance(tsock->p->ref, value);
1496 case TIPC_SRC_DROPPABLE:
1497 if (sock->type != SOCK_STREAM)
1498 res = tipc_set_portunreliable(tsock->p->ref, value);
1502 case TIPC_DEST_DROPPABLE:
1503 res = tipc_set_portunreturnable(tsock->p->ref, value);
1505 case TIPC_CONN_TIMEOUT:
1506 sock->sk->sk_rcvtimeo = (value * HZ / 1000);
1517 * getsockopt - get socket option
1518 * @sock: socket structure
1519 * @lvl: option level
1520 * @opt: option identifier
1521 * @ov: receptacle for option value
1522 * @ol: receptacle for length of option value
1524 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
1525 * (to ease compatibility).
1527 * Returns 0 on success, errno otherwise
1530 static int getsockopt(struct socket *sock, int lvl, int opt, char *ov, int *ol)
1532 struct tipc_sock *tsock = tipc_sk(sock->sk);
1537 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1538 return put_user(0, ol);
1539 if (lvl != SOL_TIPC)
1540 return -ENOPROTOOPT;
1541 if ((res = get_user(len, ol)))
1544 if (down_interruptible(&tsock->sem))
1545 return -ERESTARTSYS;
1548 case TIPC_IMPORTANCE:
1549 res = tipc_portimportance(tsock->p->ref, &value);
1551 case TIPC_SRC_DROPPABLE:
1552 res = tipc_portunreliable(tsock->p->ref, &value);
1554 case TIPC_DEST_DROPPABLE:
1555 res = tipc_portunreturnable(tsock->p->ref, &value);
1557 case TIPC_CONN_TIMEOUT:
1558 value = (sock->sk->sk_rcvtimeo * 1000) / HZ;
1567 else if (len < sizeof(value)) {
1570 else if ((res = copy_to_user(ov, &value, sizeof(value)))) {
1571 /* couldn't return value */
1574 res = put_user(sizeof(value), ol);
1582 * Placeholders for non-implemented functionality
1584 * Returns error code (POSIX-compliant where defined)
1587 static int ioctl(struct socket *s, u32 cmd, unsigned long arg)
1592 static int no_mmap(struct file *file, struct socket *sock,
1593 struct vm_area_struct *vma)
1597 static ssize_t no_sendpage(struct socket *sock, struct page *page,
1598 int offset, size_t size, int flags)
1603 static int no_skpair(struct socket *s1, struct socket *s2)
1609 * Protocol switches for the various types of TIPC sockets
1612 static struct proto_ops msg_ops = {
1613 .owner = THIS_MODULE,
1618 .socketpair = no_skpair,
1620 .getname = get_name,
1624 .shutdown = shutdown,
1625 .setsockopt = setsockopt,
1626 .getsockopt = getsockopt,
1627 .sendmsg = send_msg,
1628 .recvmsg = recv_msg,
1630 .sendpage = no_sendpage
1633 static struct proto_ops packet_ops = {
1634 .owner = THIS_MODULE,
1639 .socketpair = no_skpair,
1641 .getname = get_name,
1645 .shutdown = shutdown,
1646 .setsockopt = setsockopt,
1647 .getsockopt = getsockopt,
1648 .sendmsg = send_packet,
1649 .recvmsg = recv_msg,
1651 .sendpage = no_sendpage
1654 static struct proto_ops stream_ops = {
1655 .owner = THIS_MODULE,
1660 .socketpair = no_skpair,
1662 .getname = get_name,
1666 .shutdown = shutdown,
1667 .setsockopt = setsockopt,
1668 .getsockopt = getsockopt,
1669 .sendmsg = send_stream,
1670 .recvmsg = recv_stream,
1672 .sendpage = no_sendpage
1675 static struct net_proto_family tipc_family_ops = {
1676 .owner = THIS_MODULE,
1678 .create = tipc_create
1681 static struct proto tipc_proto = {
1683 .owner = THIS_MODULE,
1684 .obj_size = sizeof(struct tipc_sock)
1688 * socket_init - initialize TIPC socket interface
1690 * Returns 0 on success, errno otherwise
1692 int socket_init(void)
1696 res = proto_register(&tipc_proto, 1);
1698 err("Failed to register TIPC protocol type\n");
1702 res = sock_register(&tipc_family_ops);
1704 err("Failed to register TIPC socket type\n");
1705 proto_unregister(&tipc_proto);
1709 sockets_enabled = 1;
1715 * sock_stop - stop TIPC socket interface
1717 void socket_stop(void)
1719 if (!sockets_enabled)
1722 sockets_enabled = 0;
1723 sock_unregister(tipc_family_ops.family);
1724 proto_unregister(&tipc_proto);