2 * net/tipc/socket.c: TIPC socket API
4 * Copyright (c) 2001-2006, Ericsson AB
5 * Copyright (c) 2004-2006, 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 (unlikely(protocol
!= 0))
173 return -EPROTONOSUPPORT
;
175 ref
= tipc_createport_raw(NULL
, &dispatch
, &wakeupdispatch
, TIPC_LOW_IMPORTANCE
);
179 sock
->state
= SS_UNCONNECTED
;
181 switch (sock
->type
) {
183 sock
->ops
= &stream_ops
;
186 sock
->ops
= &packet_ops
;
189 tipc_set_portunreliable(ref
, 1);
192 tipc_set_portunreturnable(ref
, 1);
193 sock
->ops
= &msg_ops
;
194 sock
->state
= SS_READY
;
197 tipc_deleteport(ref
);
201 sk
= sk_alloc(AF_TIPC
, GFP_KERNEL
, &tipc_proto
, 1);
203 tipc_deleteport(ref
);
207 sock_init_data(sock
, sk
);
208 init_waitqueue_head(sk
->sk_sleep
);
209 sk
->sk_rcvtimeo
= 8 * HZ
; /* default connect timeout = 8s */
212 port
= tipc_get_port(ref
);
215 port
->usr_handle
= tsock
;
217 init_MUTEX(&tsock
->sem
);
219 dbg("sock_create: %x\n",tsock
);
221 atomic_inc(&tipc_user_count
);
227 * release - destroy a TIPC socket
228 * @sock: socket to destroy
230 * This routine cleans up any messages that are still queued on the socket.
231 * For DGRAM and RDM socket types, all queued messages are rejected.
232 * For SEQPACKET and STREAM socket types, the first message is rejected
233 * and any others are discarded. (If the first message on a STREAM socket
234 * is partially-read, it is discarded and the next one is rejected instead.)
236 * NOTE: Rejected messages are not necessarily returned to the sender! They
237 * are returned or discarded according to the "destination droppable" setting
238 * specified for the message by the sender.
240 * Returns 0 on success, errno otherwise
243 static int release(struct socket
*sock
)
245 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
246 struct sock
*sk
= sock
->sk
;
250 dbg("sock_delete: %x\n",tsock
);
253 down_interruptible(&tsock
->sem
);
259 /* Reject unreceived messages, unless no longer connected */
261 while (sock
->state
!= SS_DISCONNECTING
) {
263 buf
= skb_dequeue(&sk
->sk_receive_queue
);
265 tsock
->p
->usr_handle
= NULL
;
269 if (TIPC_SKB_CB(buf
)->handle
!= msg_data(buf_msg(buf
)))
272 tipc_reject_msg(buf
, TIPC_ERR_NO_PORT
);
273 atomic_dec(&tipc_queue_size
);
276 /* Delete TIPC port */
278 res
= tipc_deleteport(tsock
->p
->ref
);
281 /* Discard any remaining messages */
283 while ((buf
= skb_dequeue(&sk
->sk_receive_queue
))) {
285 atomic_dec(&tipc_queue_size
);
292 atomic_dec(&tipc_user_count
);
297 * bind - associate or disassocate TIPC name(s) with a socket
298 * @sock: socket structure
299 * @uaddr: socket address describing name(s) and desired operation
300 * @uaddr_len: size of socket address data structure
302 * Name and name sequence binding is indicated using a positive scope value;
303 * a negative scope value unbinds the specified name. Specifying no name
304 * (i.e. a socket address length of 0) unbinds all names from the socket.
306 * Returns 0 on success, errno otherwise
309 static int bind(struct socket
*sock
, struct sockaddr
*uaddr
, int uaddr_len
)
311 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
312 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
315 if (down_interruptible(&tsock
->sem
))
318 if (unlikely(!uaddr_len
)) {
319 res
= tipc_withdraw(tsock
->p
->ref
, 0, NULL
);
323 if (uaddr_len
< sizeof(struct sockaddr_tipc
)) {
328 if (addr
->family
!= AF_TIPC
) {
332 if (addr
->addrtype
== TIPC_ADDR_NAME
)
333 addr
->addr
.nameseq
.upper
= addr
->addr
.nameseq
.lower
;
334 else if (addr
->addrtype
!= TIPC_ADDR_NAMESEQ
) {
340 res
= tipc_publish(tsock
->p
->ref
, addr
->scope
,
341 &addr
->addr
.nameseq
);
343 res
= tipc_withdraw(tsock
->p
->ref
, -addr
->scope
,
344 &addr
->addr
.nameseq
);
351 * get_name - get port ID of socket or peer socket
352 * @sock: socket structure
353 * @uaddr: area for returned socket address
354 * @uaddr_len: area for returned length of socket address
355 * @peer: 0 to obtain socket name, 1 to obtain peer socket name
357 * Returns 0 on success, errno otherwise
360 static int get_name(struct socket
*sock
, struct sockaddr
*uaddr
,
361 int *uaddr_len
, int peer
)
363 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
364 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
367 if (down_interruptible(&tsock
->sem
))
370 *uaddr_len
= sizeof(*addr
);
371 addr
->addrtype
= TIPC_ADDR_ID
;
372 addr
->family
= AF_TIPC
;
375 res
= tipc_peer(tsock
->p
->ref
, &addr
->addr
.id
);
377 res
= tipc_ownidentity(tsock
->p
->ref
, &addr
->addr
.id
);
378 addr
->addr
.name
.domain
= 0;
385 * poll - read and possibly block on pollmask
386 * @file: file structure associated with the socket
387 * @sock: socket for which to calculate the poll bits
390 * Returns the pollmask
393 static unsigned int poll(struct file
*file
, struct socket
*sock
,
396 poll_wait(file
, sock
->sk
->sk_sleep
, wait
);
397 /* NEED LOCK HERE? */
398 return pollmask(sock
);
402 * dest_name_check - verify user is permitted to send to specified port name
403 * @dest: destination address
404 * @m: descriptor for message to be sent
406 * Prevents restricted configuration commands from being issued by
407 * unauthorized users.
409 * Returns 0 if permission is granted, otherwise errno
412 static int dest_name_check(struct sockaddr_tipc
*dest
, struct msghdr
*m
)
414 struct tipc_cfg_msg_hdr hdr
;
416 if (likely(dest
->addr
.name
.name
.type
>= TIPC_RESERVED_TYPES
))
418 if (likely(dest
->addr
.name
.name
.type
== TIPC_TOP_SRV
))
421 if (likely(dest
->addr
.name
.name
.type
!= TIPC_CFG_SRV
))
424 if (copy_from_user(&hdr
, m
->msg_iov
[0].iov_base
, sizeof(hdr
)))
426 if ((ntohs(hdr
.tcm_type
) & 0xC000) && (!capable(CAP_NET_ADMIN
)))
433 * send_msg - send message in connectionless manner
435 * @sock: socket structure
436 * @m: message to send
437 * @total_len: length of message
439 * Message must have an destination specified explicitly.
440 * Used for SOCK_RDM and SOCK_DGRAM messages,
441 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
442 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
444 * Returns the number of bytes sent on success, or errno otherwise
447 static int send_msg(struct kiocb
*iocb
, struct socket
*sock
,
448 struct msghdr
*m
, size_t total_len
)
450 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
451 struct sockaddr_tipc
*dest
= (struct sockaddr_tipc
*)m
->msg_name
;
457 return -EDESTADDRREQ
;
458 if (unlikely((m
->msg_namelen
< sizeof(*dest
)) ||
459 (dest
->family
!= AF_TIPC
)))
462 needs_conn
= (sock
->state
!= SS_READY
);
463 if (unlikely(needs_conn
)) {
464 if (sock
->state
== SS_LISTENING
)
466 if (sock
->state
!= SS_UNCONNECTED
)
468 if ((tsock
->p
->published
) ||
469 ((sock
->type
== SOCK_STREAM
) && (total_len
!= 0)))
471 if (dest
->addrtype
== TIPC_ADDR_NAME
) {
472 tsock
->p
->conn_type
= dest
->addr
.name
.name
.type
;
473 tsock
->p
->conn_instance
= dest
->addr
.name
.name
.instance
;
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: length of message
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
)) {
567 if (unlikely(sock
->state
!= SS_CONNECTED
)) {
568 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 partial success),
603 * or errno if no data sent
607 static int send_stream(struct kiocb
*iocb
, struct socket
*sock
,
608 struct msghdr
*m
, size_t total_len
)
610 struct msghdr my_msg
;
612 struct iovec
*curr_iov
;
614 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
)
632 if (unlikely(m
->msg_name
))
636 * Send each iovec entry using one or more messages
638 * Note: This algorithm is good for the most likely case
639 * (i.e. one large iovec entry), but could be improved to pass sets
640 * of small iovec entries into send_packet().
643 curr_iov
= m
->msg_iov
;
644 curr_iovlen
= m
->msg_iovlen
;
645 my_msg
.msg_iov
= &my_iov
;
646 my_msg
.msg_iovlen
= 1;
647 my_msg
.msg_flags
= m
->msg_flags
;
648 my_msg
.msg_name
= NULL
;
651 while (curr_iovlen
--) {
652 curr_start
= curr_iov
->iov_base
;
653 curr_left
= curr_iov
->iov_len
;
656 bytes_to_send
= (curr_left
< TIPC_MAX_USER_MSG_SIZE
)
657 ? curr_left
: TIPC_MAX_USER_MSG_SIZE
;
658 my_iov
.iov_base
= curr_start
;
659 my_iov
.iov_len
= bytes_to_send
;
660 if ((res
= send_packet(iocb
, sock
, &my_msg
, 0)) < 0) {
661 return bytes_sent
? bytes_sent
: res
;
663 curr_left
-= bytes_to_send
;
664 curr_start
+= bytes_to_send
;
665 bytes_sent
+= bytes_to_send
;
675 * auto_connect - complete connection setup to a remote port
676 * @sock: socket structure
677 * @tsock: TIPC-specific socket structure
678 * @msg: peer's response message
680 * Returns 0 on success, errno otherwise
683 static int auto_connect(struct socket
*sock
, struct tipc_sock
*tsock
,
684 struct tipc_msg
*msg
)
686 struct tipc_portid peer
;
688 if (msg_errcode(msg
)) {
689 sock
->state
= SS_DISCONNECTING
;
690 return -ECONNREFUSED
;
693 peer
.ref
= msg_origport(msg
);
694 peer
.node
= msg_orignode(msg
);
695 tipc_connect2port(tsock
->p
->ref
, &peer
);
696 tipc_set_portimportance(tsock
->p
->ref
, msg_importance(msg
));
697 sock
->state
= SS_CONNECTED
;
702 * set_orig_addr - capture sender's address for received message
703 * @m: descriptor for message info
704 * @msg: received message header
706 * Note: Address is not captured if not requested by receiver.
709 static void set_orig_addr(struct msghdr
*m
, struct tipc_msg
*msg
)
711 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)m
->msg_name
;
714 addr
->family
= AF_TIPC
;
715 addr
->addrtype
= TIPC_ADDR_ID
;
716 addr
->addr
.id
.ref
= msg_origport(msg
);
717 addr
->addr
.id
.node
= msg_orignode(msg
);
718 addr
->addr
.name
.domain
= 0; /* could leave uninitialized */
719 addr
->scope
= 0; /* could leave uninitialized */
720 m
->msg_namelen
= sizeof(struct sockaddr_tipc
);
725 * anc_data_recv - optionally capture ancillary data for received message
726 * @m: descriptor for message info
727 * @msg: received message header
728 * @tport: TIPC port associated with message
730 * Note: Ancillary data is not captured if not requested by receiver.
732 * Returns 0 if successful, otherwise errno
735 static int anc_data_recv(struct msghdr
*m
, struct tipc_msg
*msg
,
736 struct tipc_port
*tport
)
744 if (likely(m
->msg_controllen
== 0))
747 /* Optionally capture errored message object(s) */
749 err
= msg
? msg_errcode(msg
) : 0;
752 anc_data
[1] = msg_data_sz(msg
);
753 if ((res
= put_cmsg(m
, SOL_TIPC
, TIPC_ERRINFO
, 8, anc_data
)))
756 (res
= put_cmsg(m
, SOL_TIPC
, TIPC_RETDATA
, anc_data
[1],
761 /* Optionally capture message destination object */
763 dest_type
= msg
? msg_type(msg
) : TIPC_DIRECT_MSG
;
767 anc_data
[0] = msg_nametype(msg
);
768 anc_data
[1] = msg_namelower(msg
);
769 anc_data
[2] = msg_namelower(msg
);
773 anc_data
[0] = msg_nametype(msg
);
774 anc_data
[1] = msg_namelower(msg
);
775 anc_data
[2] = msg_nameupper(msg
);
778 has_name
= (tport
->conn_type
!= 0);
779 anc_data
[0] = tport
->conn_type
;
780 anc_data
[1] = tport
->conn_instance
;
781 anc_data
[2] = tport
->conn_instance
;
787 (res
= put_cmsg(m
, SOL_TIPC
, TIPC_DESTNAME
, 12, anc_data
)))
794 * recv_msg - receive packet-oriented message
796 * @m: descriptor for message info
797 * @buf_len: total size of user buffer area
798 * @flags: receive flags
800 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
801 * If the complete message doesn't fit in user area, truncate it.
803 * Returns size of returned message data, errno otherwise
806 static int recv_msg(struct kiocb
*iocb
, struct socket
*sock
,
807 struct msghdr
*m
, size_t buf_len
, int flags
)
809 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
811 struct tipc_msg
*msg
;
817 /* Currently doesn't support receiving into multiple iovec entries */
819 if (m
->msg_iovlen
!= 1)
822 /* Catch invalid receive attempts */
824 if (unlikely(!buf_len
))
827 if (sock
->type
== SOCK_SEQPACKET
) {
828 if (unlikely(sock
->state
== SS_UNCONNECTED
))
830 if (unlikely((sock
->state
== SS_DISCONNECTING
) &&
831 (skb_queue_len(&sock
->sk
->sk_receive_queue
) == 0)))
835 /* Look for a message in receive queue; wait if necessary */
837 if (unlikely(down_interruptible(&tsock
->sem
)))
841 if (unlikely((skb_queue_len(&sock
->sk
->sk_receive_queue
) == 0) &&
842 (flags
& MSG_DONTWAIT
))) {
847 if ((res
= wait_event_interruptible(
849 ((q_len
= skb_queue_len(&sock
->sk
->sk_receive_queue
)) ||
850 (sock
->state
== SS_DISCONNECTING
))) )) {
854 /* Catch attempt to receive on an already terminated connection */
855 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
862 /* Get access to first message in receive queue */
864 buf
= skb_peek(&sock
->sk
->sk_receive_queue
);
866 sz
= msg_data_sz(msg
);
867 err
= msg_errcode(msg
);
869 /* Complete connection setup for an implied connect */
871 if (unlikely(sock
->state
== SS_CONNECTING
)) {
872 if ((res
= auto_connect(sock
, tsock
, msg
)))
876 /* Discard an empty non-errored message & try again */
878 if ((!sz
) && (!err
)) {
879 advance_queue(tsock
);
883 /* Capture sender's address (optional) */
885 set_orig_addr(m
, msg
);
887 /* Capture ancillary data (optional) */
889 if ((res
= anc_data_recv(m
, msg
, tsock
->p
)))
892 /* Capture message data (if valid) & compute return value (always) */
895 if (unlikely(buf_len
< sz
)) {
897 m
->msg_flags
|= MSG_TRUNC
;
899 if (unlikely(copy_to_user(m
->msg_iov
->iov_base
, msg_data(msg
),
906 if ((sock
->state
== SS_READY
) ||
907 ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
))
913 /* Consume received message (optional) */
915 if (likely(!(flags
& MSG_PEEK
))) {
916 if (unlikely(++tsock
->p
->conn_unacked
>= TIPC_FLOW_CONTROL_WIN
))
917 tipc_acknowledge(tsock
->p
->ref
, tsock
->p
->conn_unacked
);
918 advance_queue(tsock
);
926 * recv_stream - receive stream-oriented data
928 * @m: descriptor for message info
929 * @buf_len: total size of user buffer area
930 * @flags: receive flags
932 * Used for SOCK_STREAM messages only. If not enough data is available
933 * will optionally wait for more; never truncates data.
935 * Returns size of returned message data, errno otherwise
938 static int recv_stream(struct kiocb
*iocb
, struct socket
*sock
,
939 struct msghdr
*m
, size_t buf_len
, int flags
)
941 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
943 struct tipc_msg
*msg
;
949 char __user
*crs
= m
->msg_iov
->iov_base
;
950 unsigned char *buf_crs
;
954 /* Currently doesn't support receiving into multiple iovec entries */
956 if (m
->msg_iovlen
!= 1)
959 /* Catch invalid receive attempts */
961 if (unlikely(!buf_len
))
964 if (unlikely(sock
->state
== SS_DISCONNECTING
)) {
965 if (skb_queue_len(&sock
->sk
->sk_receive_queue
) == 0)
967 } else if (unlikely(sock
->state
!= SS_CONNECTED
))
970 /* Look for a message in receive queue; wait if necessary */
972 if (unlikely(down_interruptible(&tsock
->sem
)))
976 if (unlikely((skb_queue_len(&sock
->sk
->sk_receive_queue
) == 0) &&
977 (flags
& MSG_DONTWAIT
))) {
982 if ((res
= wait_event_interruptible(
984 ((q_len
= skb_queue_len(&sock
->sk
->sk_receive_queue
)) ||
985 (sock
->state
== SS_DISCONNECTING
))) )) {
989 /* Catch attempt to receive on an already terminated connection */
990 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
997 /* Get access to first message in receive queue */
999 buf
= skb_peek(&sock
->sk
->sk_receive_queue
);
1001 sz
= msg_data_sz(msg
);
1002 err
= msg_errcode(msg
);
1004 /* Discard an empty non-errored message & try again */
1006 if ((!sz
) && (!err
)) {
1007 advance_queue(tsock
);
1011 /* Optionally capture sender's address & ancillary data of first msg */
1013 if (sz_copied
== 0) {
1014 set_orig_addr(m
, msg
);
1015 if ((res
= anc_data_recv(m
, msg
, tsock
->p
)))
1019 /* Capture message data (if valid) & compute return value (always) */
1022 buf_crs
= (unsigned char *)(TIPC_SKB_CB(buf
)->handle
);
1023 sz
= buf
->tail
- buf_crs
;
1025 needed
= (buf_len
- sz_copied
);
1026 sz_to_copy
= (sz
<= needed
) ? sz
: needed
;
1027 if (unlikely(copy_to_user(crs
, buf_crs
, sz_to_copy
))) {
1031 sz_copied
+= sz_to_copy
;
1033 if (sz_to_copy
< sz
) {
1034 if (!(flags
& MSG_PEEK
))
1035 TIPC_SKB_CB(buf
)->handle
= buf_crs
+ sz_to_copy
;
1042 goto exit
; /* can't add error msg to valid data */
1044 if ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
)
1050 /* Consume received message (optional) */
1052 if (likely(!(flags
& MSG_PEEK
))) {
1053 if (unlikely(++tsock
->p
->conn_unacked
>= TIPC_FLOW_CONTROL_WIN
))
1054 tipc_acknowledge(tsock
->p
->ref
, tsock
->p
->conn_unacked
);
1055 advance_queue(tsock
);
1058 /* Loop around if more data is required */
1060 if ((sz_copied
< buf_len
) /* didn't get all requested data */
1061 && (flags
& MSG_WAITALL
) /* ... and need to wait for more */
1062 && (!(flags
& MSG_PEEK
)) /* ... and aren't just peeking at data */
1063 && (!err
) /* ... and haven't reached a FIN */
1069 return sz_copied
? sz_copied
: res
;
1073 * queue_overloaded - test if queue overload condition exists
1074 * @queue_size: current size of queue
1075 * @base: nominal maximum size of queue
1076 * @msg: message to be added to queue
1078 * Returns 1 if queue is currently overloaded, 0 otherwise
1081 static int queue_overloaded(u32 queue_size
, u32 base
, struct tipc_msg
*msg
)
1084 u32 imp
= msg_importance(msg
);
1086 if (imp
== TIPC_LOW_IMPORTANCE
)
1088 else if (imp
== TIPC_MEDIUM_IMPORTANCE
)
1089 threshold
= base
* 2;
1090 else if (imp
== TIPC_HIGH_IMPORTANCE
)
1091 threshold
= base
* 100;
1095 if (msg_connected(msg
))
1098 return (queue_size
> threshold
);
1102 * async_disconnect - wrapper function used to disconnect port
1103 * @portref: TIPC port reference (passed as pointer-sized value)
1106 static void async_disconnect(unsigned long portref
)
1108 tipc_disconnect((u32
)portref
);
1112 * dispatch - handle arriving message
1113 * @tport: TIPC port that received message
1116 * Called with port locked. Must not take socket lock to avoid deadlock risk.
1118 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1121 static u32
dispatch(struct tipc_port
*tport
, struct sk_buff
*buf
)
1123 struct tipc_msg
*msg
= buf_msg(buf
);
1124 struct tipc_sock
*tsock
= (struct tipc_sock
*)tport
->usr_handle
;
1125 struct socket
*sock
;
1128 /* Reject message if socket is closing */
1131 return TIPC_ERR_NO_PORT
;
1133 /* Reject message if it is wrong sort of message for socket */
1136 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1137 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1138 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1140 sock
= tsock
->sk
.sk_socket
;
1141 if (sock
->state
== SS_READY
) {
1142 if (msg_connected(msg
)) {
1143 msg_dbg(msg
, "dispatch filter 1\n");
1144 return TIPC_ERR_NO_PORT
;
1147 if (msg_mcast(msg
)) {
1148 msg_dbg(msg
, "dispatch filter 2\n");
1149 return TIPC_ERR_NO_PORT
;
1151 if (sock
->state
== SS_CONNECTED
) {
1152 if (!msg_connected(msg
)) {
1153 msg_dbg(msg
, "dispatch filter 3\n");
1154 return TIPC_ERR_NO_PORT
;
1157 else if (sock
->state
== SS_CONNECTING
) {
1158 if (!msg_connected(msg
) && (msg_errcode(msg
) == 0)) {
1159 msg_dbg(msg
, "dispatch filter 4\n");
1160 return TIPC_ERR_NO_PORT
;
1163 else if (sock
->state
== SS_LISTENING
) {
1164 if (msg_connected(msg
) || msg_errcode(msg
)) {
1165 msg_dbg(msg
, "dispatch filter 5\n");
1166 return TIPC_ERR_NO_PORT
;
1169 else if (sock
->state
== SS_DISCONNECTING
) {
1170 msg_dbg(msg
, "dispatch filter 6\n");
1171 return TIPC_ERR_NO_PORT
;
1173 else /* (sock->state == SS_UNCONNECTED) */ {
1174 if (msg_connected(msg
) || msg_errcode(msg
)) {
1175 msg_dbg(msg
, "dispatch filter 7\n");
1176 return TIPC_ERR_NO_PORT
;
1181 /* Reject message if there isn't room to queue it */
1183 if (unlikely((u32
)atomic_read(&tipc_queue_size
) >
1184 OVERLOAD_LIMIT_BASE
)) {
1185 if (queue_overloaded(atomic_read(&tipc_queue_size
),
1186 OVERLOAD_LIMIT_BASE
, msg
))
1187 return TIPC_ERR_OVERLOAD
;
1189 recv_q_len
= skb_queue_len(&tsock
->sk
.sk_receive_queue
);
1190 if (unlikely(recv_q_len
> (OVERLOAD_LIMIT_BASE
/ 2))) {
1191 if (queue_overloaded(recv_q_len
,
1192 OVERLOAD_LIMIT_BASE
/ 2, msg
))
1193 return TIPC_ERR_OVERLOAD
;
1196 /* Initiate connection termination for an incoming 'FIN' */
1198 if (unlikely(msg_errcode(msg
) && (sock
->state
== SS_CONNECTED
))) {
1199 sock
->state
= SS_DISCONNECTING
;
1200 /* Note: Use signal since port lock is already taken! */
1201 tipc_k_signal((Handler
)async_disconnect
, tport
->ref
);
1204 /* Enqueue message (finally!) */
1206 msg_dbg(msg
,"<DISP<: ");
1207 TIPC_SKB_CB(buf
)->handle
= msg_data(msg
);
1208 atomic_inc(&tipc_queue_size
);
1209 skb_queue_tail(&sock
->sk
->sk_receive_queue
, buf
);
1211 wake_up_interruptible(sock
->sk
->sk_sleep
);
1216 * wakeupdispatch - wake up port after congestion
1217 * @tport: port to wakeup
1219 * Called with port lock on.
1222 static void wakeupdispatch(struct tipc_port
*tport
)
1224 struct tipc_sock
*tsock
= (struct tipc_sock
*)tport
->usr_handle
;
1226 wake_up_interruptible(tsock
->sk
.sk_sleep
);
1230 * connect - establish a connection to another TIPC port
1231 * @sock: socket structure
1232 * @dest: socket address for destination port
1233 * @destlen: size of socket address data structure
1236 * Returns 0 on success, errno otherwise
1239 static int connect(struct socket
*sock
, struct sockaddr
*dest
, int destlen
,
1242 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
1243 struct sockaddr_tipc
*dst
= (struct sockaddr_tipc
*)dest
;
1244 struct msghdr m
= {NULL
,};
1245 struct sk_buff
*buf
;
1246 struct tipc_msg
*msg
;
1249 /* For now, TIPC does not allow use of connect() with DGRAM or RDM types */
1251 if (sock
->state
== SS_READY
)
1254 /* Issue Posix-compliant error code if socket is in the wrong state */
1256 if (sock
->state
== SS_LISTENING
)
1258 if (sock
->state
== SS_CONNECTING
)
1260 if (sock
->state
!= SS_UNCONNECTED
)
1264 * Reject connection attempt using multicast address
1266 * Note: send_msg() validates the rest of the address fields,
1267 * so there's no need to do it here
1270 if (dst
->addrtype
== TIPC_ADDR_MCAST
)
1273 /* Send a 'SYN-' to destination */
1276 m
.msg_namelen
= destlen
;
1277 if ((res
= send_msg(NULL
, sock
, &m
, 0)) < 0) {
1278 sock
->state
= SS_DISCONNECTING
;
1282 if (down_interruptible(&tsock
->sem
))
1283 return -ERESTARTSYS
;
1285 /* Wait for destination's 'ACK' response */
1287 res
= wait_event_interruptible_timeout(*sock
->sk
->sk_sleep
,
1288 skb_queue_len(&sock
->sk
->sk_receive_queue
),
1289 sock
->sk
->sk_rcvtimeo
);
1290 buf
= skb_peek(&sock
->sk
->sk_receive_queue
);
1293 res
= auto_connect(sock
, tsock
, msg
);
1295 if (!msg_data_sz(msg
))
1296 advance_queue(tsock
);
1302 { /* leave "res" unchanged */ }
1303 sock
->state
= SS_DISCONNECTING
;
1311 * listen - allow socket to listen for incoming connections
1312 * @sock: socket structure
1315 * Returns 0 on success, errno otherwise
1318 static int listen(struct socket
*sock
, int len
)
1320 /* REQUIRES SOCKET LOCKING OF SOME SORT? */
1322 if (sock
->state
== SS_READY
)
1324 if (sock
->state
!= SS_UNCONNECTED
)
1326 sock
->state
= SS_LISTENING
;
1331 * accept - wait for connection request
1332 * @sock: listening socket
1333 * @newsock: new socket that is to be connected
1334 * @flags: file-related flags associated with socket
1336 * Returns 0 on success, errno otherwise
1339 static int accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
1341 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
1342 struct sk_buff
*buf
;
1345 if (sock
->state
== SS_READY
)
1347 if (sock
->state
!= SS_LISTENING
)
1350 if (unlikely((skb_queue_len(&sock
->sk
->sk_receive_queue
) == 0) &&
1351 (flags
& O_NONBLOCK
)))
1352 return -EWOULDBLOCK
;
1354 if (down_interruptible(&tsock
->sem
))
1355 return -ERESTARTSYS
;
1357 if (wait_event_interruptible(*sock
->sk
->sk_sleep
,
1358 skb_queue_len(&sock
->sk
->sk_receive_queue
))) {
1362 buf
= skb_peek(&sock
->sk
->sk_receive_queue
);
1364 res
= tipc_create(newsock
, 0);
1366 struct tipc_sock
*new_tsock
= tipc_sk(newsock
->sk
);
1367 struct tipc_portid id
;
1368 struct tipc_msg
*msg
= buf_msg(buf
);
1369 u32 new_ref
= new_tsock
->p
->ref
;
1371 id
.ref
= msg_origport(msg
);
1372 id
.node
= msg_orignode(msg
);
1373 tipc_connect2port(new_ref
, &id
);
1374 newsock
->state
= SS_CONNECTED
;
1376 tipc_set_portimportance(new_ref
, msg_importance(msg
));
1377 if (msg_named(msg
)) {
1378 new_tsock
->p
->conn_type
= msg_nametype(msg
);
1379 new_tsock
->p
->conn_instance
= msg_nameinst(msg
);
1383 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1384 * Respond to 'SYN+' by queuing it on new socket.
1387 msg_dbg(msg
,"<ACC<: ");
1388 if (!msg_data_sz(msg
)) {
1389 struct msghdr m
= {NULL
,};
1391 send_packet(NULL
, newsock
, &m
, 0);
1392 advance_queue(tsock
);
1395 skb_dequeue(&sock
->sk
->sk_receive_queue
);
1397 skb_queue_head(&newsock
->sk
->sk_receive_queue
, buf
);
1406 * shutdown - shutdown socket connection
1407 * @sock: socket structure
1408 * @how: direction to close (unused; always treated as read + write)
1410 * Terminates connection (if necessary), then purges socket's receive queue.
1412 * Returns 0 on success, errno otherwise
1415 static int shutdown(struct socket
*sock
, int how
)
1417 struct tipc_sock
* tsock
= tipc_sk(sock
->sk
);
1418 struct sk_buff
*buf
;
1421 /* Could return -EINVAL for an invalid "how", but why bother? */
1423 if (down_interruptible(&tsock
->sem
))
1424 return -ERESTARTSYS
;
1428 switch (sock
->state
) {
1431 /* Send 'FIN+' or 'FIN-' message to peer */
1435 if ((buf
= skb_dequeue(&sock
->sk
->sk_receive_queue
))) {
1436 atomic_dec(&tipc_queue_size
);
1437 if (TIPC_SKB_CB(buf
)->handle
!= msg_data(buf_msg(buf
))) {
1441 tipc_reject_msg(buf
, TIPC_CONN_SHUTDOWN
);
1444 tipc_shutdown(tsock
->p
->ref
);
1450 case SS_DISCONNECTING
:
1452 /* Discard any unreceived messages */
1454 while ((buf
= skb_dequeue(&sock
->sk
->sk_receive_queue
))) {
1455 atomic_dec(&tipc_queue_size
);
1458 tsock
->p
->conn_unacked
= 0;
1463 sock
->state
= SS_DISCONNECTING
;
1478 * setsockopt - set socket option
1479 * @sock: socket structure
1480 * @lvl: option level
1481 * @opt: option identifier
1482 * @ov: pointer to new option value
1483 * @ol: length of option value
1485 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
1486 * (to ease compatibility).
1488 * Returns 0 on success, errno otherwise
1491 static int setsockopt(struct socket
*sock
,
1492 int lvl
, int opt
, char __user
*ov
, int ol
)
1494 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
1498 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
1500 if (lvl
!= SOL_TIPC
)
1501 return -ENOPROTOOPT
;
1502 if (ol
< sizeof(value
))
1504 if ((res
= get_user(value
, (u32 __user
*)ov
)))
1507 if (down_interruptible(&tsock
->sem
))
1508 return -ERESTARTSYS
;
1511 case TIPC_IMPORTANCE
:
1512 res
= tipc_set_portimportance(tsock
->p
->ref
, value
);
1514 case TIPC_SRC_DROPPABLE
:
1515 if (sock
->type
!= SOCK_STREAM
)
1516 res
= tipc_set_portunreliable(tsock
->p
->ref
, value
);
1520 case TIPC_DEST_DROPPABLE
:
1521 res
= tipc_set_portunreturnable(tsock
->p
->ref
, value
);
1523 case TIPC_CONN_TIMEOUT
:
1524 sock
->sk
->sk_rcvtimeo
= (value
* HZ
/ 1000);
1535 * getsockopt - get socket option
1536 * @sock: socket structure
1537 * @lvl: option level
1538 * @opt: option identifier
1539 * @ov: receptacle for option value
1540 * @ol: receptacle for length of option value
1542 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
1543 * (to ease compatibility).
1545 * Returns 0 on success, errno otherwise
1548 static int getsockopt(struct socket
*sock
,
1549 int lvl
, int opt
, char __user
*ov
, int __user
*ol
)
1551 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
1556 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
1557 return put_user(0, ol
);
1558 if (lvl
!= SOL_TIPC
)
1559 return -ENOPROTOOPT
;
1560 if ((res
= get_user(len
, ol
)))
1563 if (down_interruptible(&tsock
->sem
))
1564 return -ERESTARTSYS
;
1567 case TIPC_IMPORTANCE
:
1568 res
= tipc_portimportance(tsock
->p
->ref
, &value
);
1570 case TIPC_SRC_DROPPABLE
:
1571 res
= tipc_portunreliable(tsock
->p
->ref
, &value
);
1573 case TIPC_DEST_DROPPABLE
:
1574 res
= tipc_portunreturnable(tsock
->p
->ref
, &value
);
1576 case TIPC_CONN_TIMEOUT
:
1577 value
= (sock
->sk
->sk_rcvtimeo
* 1000) / HZ
;
1586 else if (len
< sizeof(value
)) {
1589 else if ((res
= copy_to_user(ov
, &value
, sizeof(value
)))) {
1590 /* couldn't return value */
1593 res
= put_user(sizeof(value
), ol
);
1601 * Placeholders for non-implemented functionality
1603 * Returns error code (POSIX-compliant where defined)
1606 static int ioctl(struct socket
*s
, u32 cmd
, unsigned long arg
)
1611 static int no_mmap(struct file
*file
, struct socket
*sock
,
1612 struct vm_area_struct
*vma
)
1616 static ssize_t
no_sendpage(struct socket
*sock
, struct page
*page
,
1617 int offset
, size_t size
, int flags
)
1622 static int no_skpair(struct socket
*s1
, struct socket
*s2
)
1628 * Protocol switches for the various types of TIPC sockets
1631 static struct proto_ops msg_ops
= {
1632 .owner
= THIS_MODULE
,
1637 .socketpair
= no_skpair
,
1639 .getname
= get_name
,
1643 .shutdown
= shutdown
,
1644 .setsockopt
= setsockopt
,
1645 .getsockopt
= getsockopt
,
1646 .sendmsg
= send_msg
,
1647 .recvmsg
= recv_msg
,
1649 .sendpage
= no_sendpage
1652 static struct proto_ops packet_ops
= {
1653 .owner
= THIS_MODULE
,
1658 .socketpair
= no_skpair
,
1660 .getname
= get_name
,
1664 .shutdown
= shutdown
,
1665 .setsockopt
= setsockopt
,
1666 .getsockopt
= getsockopt
,
1667 .sendmsg
= send_packet
,
1668 .recvmsg
= recv_msg
,
1670 .sendpage
= no_sendpage
1673 static struct proto_ops stream_ops
= {
1674 .owner
= THIS_MODULE
,
1679 .socketpair
= no_skpair
,
1681 .getname
= get_name
,
1685 .shutdown
= shutdown
,
1686 .setsockopt
= setsockopt
,
1687 .getsockopt
= getsockopt
,
1688 .sendmsg
= send_stream
,
1689 .recvmsg
= recv_stream
,
1691 .sendpage
= no_sendpage
1694 static struct net_proto_family tipc_family_ops
= {
1695 .owner
= THIS_MODULE
,
1697 .create
= tipc_create
1700 static struct proto tipc_proto
= {
1702 .owner
= THIS_MODULE
,
1703 .obj_size
= sizeof(struct tipc_sock
)
1707 * tipc_socket_init - initialize TIPC socket interface
1709 * Returns 0 on success, errno otherwise
1711 int tipc_socket_init(void)
1715 res
= proto_register(&tipc_proto
, 1);
1717 err("Failed to register TIPC protocol type\n");
1721 res
= sock_register(&tipc_family_ops
);
1723 err("Failed to register TIPC socket type\n");
1724 proto_unregister(&tipc_proto
);
1728 sockets_enabled
= 1;
1734 * tipc_socket_stop - stop TIPC socket interface
1736 void tipc_socket_stop(void)
1738 if (!sockets_enabled
)
1741 sockets_enabled
= 0;
1742 sock_unregister(tipc_family_ops
.family
);
1743 proto_unregister(&tipc_proto
);