2 * net/tipc/socket.c: TIPC socket API
4 * Copyright (c) 2003-2005, Ericsson Research Canada
5 * Copyright (c) 2004-2005, Wind River Systems
6 * Copyright (c) 2005-2006, Ericsson AB
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
21 * Alternatively, this software may be distributed under the terms of the
22 * GNU General Public License ("GPL") version 2 as published by the Free
23 * Software Foundation.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
38 #include <linux/module.h>
39 #include <linux/types.h>
40 #include <linux/net.h>
41 #include <linux/socket.h>
42 #include <linux/errno.h>
44 #include <linux/slab.h>
45 #include <linux/poll.h>
46 #include <linux/version.h>
47 #include <linux/fcntl.h>
48 #include <linux/version.h>
49 #include <asm/semaphore.h>
50 #include <asm/string.h>
51 #include <asm/atomic.h>
54 #include <linux/tipc.h>
55 #include <linux/tipc_config.h>
56 #include <net/tipc/tipc_msg.h>
57 #include <net/tipc/tipc_port.h>
61 #define SS_LISTENING -1 /* socket is listening */
62 #define SS_READY -2 /* socket is connectionless */
64 #define OVERLOAD_LIMIT_BASE 5000
72 #define tipc_sk(sk) ((struct tipc_sock*)sk)
74 static u32
dispatch(struct tipc_port
*tport
, struct sk_buff
*buf
);
75 static void wakeupdispatch(struct tipc_port
*tport
);
77 static struct proto_ops packet_ops
;
78 static struct proto_ops stream_ops
;
79 static struct proto_ops msg_ops
;
81 static struct proto tipc_proto
;
83 static int sockets_enabled
= 0;
85 static atomic_t tipc_queue_size
= ATOMIC_INIT(0);
89 * sock_lock(): Lock a port/socket pair. lock_sock() can
90 * not be used here, since the same lock must protect ports
91 * with non-socket interfaces.
92 * See net.c for description of locking policy.
94 static inline void sock_lock(struct tipc_sock
* tsock
)
96 spin_lock_bh(tsock
->p
->lock
);
100 * sock_unlock(): Unlock a port/socket pair
102 static inline void sock_unlock(struct tipc_sock
* tsock
)
104 spin_unlock_bh(tsock
->p
->lock
);
108 * pollmask - determine the current set of poll() events for a socket
109 * @sock: socket structure
111 * TIPC sets the returned events as follows:
112 * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty
113 * or if a connection-oriented socket is does not have an active connection
114 * (i.e. a read operation will not block).
115 * b) POLLOUT is set except when a socket's connection has been terminated
116 * (i.e. a write operation will not block).
117 * c) POLLHUP is set when a socket's connection has been terminated.
119 * IMPORTANT: The fact that a read or write operation will not block does NOT
120 * imply that the operation will succeed!
122 * Returns pollmask value
125 static inline u32
pollmask(struct socket
*sock
)
129 if ((skb_queue_len(&sock
->sk
->sk_receive_queue
) != 0) ||
130 (sock
->state
== SS_UNCONNECTED
) ||
131 (sock
->state
== SS_DISCONNECTING
))
132 mask
= (POLLRDNORM
| POLLIN
);
136 if (sock
->state
== SS_DISCONNECTING
)
146 * advance_queue - discard first buffer in queue
147 * @tsock: TIPC socket
150 static inline void advance_queue(struct tipc_sock
*tsock
)
153 buf_discard(skb_dequeue(&tsock
->sk
.sk_receive_queue
));
155 atomic_dec(&tipc_queue_size
);
159 * tipc_create - create a TIPC socket
160 * @sock: pre-allocated socket structure
161 * @protocol: protocol indicator (must be 0)
163 * This routine creates and attaches a 'struct sock' to the 'struct socket',
164 * then create and attaches a TIPC port to the 'struct sock' part.
166 * Returns 0 on success, errno otherwise
168 static int tipc_create(struct socket
*sock
, int protocol
)
170 struct tipc_sock
*tsock
;
171 struct tipc_port
*port
;
175 if ((sock
->type
!= SOCK_STREAM
) &&
176 (sock
->type
!= SOCK_SEQPACKET
) &&
177 (sock
->type
!= SOCK_DGRAM
) &&
178 (sock
->type
!= SOCK_RDM
))
181 if (unlikely(protocol
!= 0))
182 return -EPROTONOSUPPORT
;
184 ref
= tipc_createport_raw(0, &dispatch
, &wakeupdispatch
, TIPC_LOW_IMPORTANCE
);
188 sock
->state
= SS_UNCONNECTED
;
190 switch (sock
->type
) {
192 sock
->ops
= &stream_ops
;
195 sock
->ops
= &packet_ops
;
198 tipc_set_portunreliable(ref
, 1);
201 tipc_set_portunreturnable(ref
, 1);
202 sock
->ops
= &msg_ops
;
203 sock
->state
= SS_READY
;
207 sk
= sk_alloc(AF_TIPC
, GFP_KERNEL
, &tipc_proto
, 1);
209 tipc_deleteport(ref
);
213 sock_init_data(sock
, sk
);
214 init_waitqueue_head(sk
->sk_sleep
);
215 sk
->sk_rcvtimeo
= 8 * HZ
; /* default connect timeout = 8s */
218 port
= tipc_get_port(ref
);
221 port
->usr_handle
= tsock
;
223 init_MUTEX(&tsock
->sem
);
225 dbg("sock_create: %x\n",tsock
);
227 atomic_inc(&tipc_user_count
);
233 * release - destroy a TIPC socket
234 * @sock: socket to destroy
236 * This routine cleans up any messages that are still queued on the socket.
237 * For DGRAM and RDM socket types, all queued messages are rejected.
238 * For SEQPACKET and STREAM socket types, the first message is rejected
239 * and any others are discarded. (If the first message on a STREAM socket
240 * is partially-read, it is discarded and the next one is rejected instead.)
242 * NOTE: Rejected messages are not necessarily returned to the sender! They
243 * are returned or discarded according to the "destination droppable" setting
244 * specified for the message by the sender.
246 * Returns 0 on success, errno otherwise
249 static int release(struct socket
*sock
)
251 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
252 struct sock
*sk
= sock
->sk
;
256 dbg("sock_delete: %x\n",tsock
);
259 down_interruptible(&tsock
->sem
);
265 /* Reject unreceived messages, unless no longer connected */
267 while (sock
->state
!= SS_DISCONNECTING
) {
269 buf
= skb_dequeue(&sk
->sk_receive_queue
);
271 tsock
->p
->usr_handle
= 0;
275 if (TIPC_SKB_CB(buf
)->handle
!= msg_data(buf_msg(buf
)))
278 tipc_reject_msg(buf
, TIPC_ERR_NO_PORT
);
279 atomic_dec(&tipc_queue_size
);
282 /* Delete TIPC port */
284 res
= tipc_deleteport(tsock
->p
->ref
);
287 /* Discard any remaining messages */
289 while ((buf
= skb_dequeue(&sk
->sk_receive_queue
))) {
291 atomic_dec(&tipc_queue_size
);
298 atomic_dec(&tipc_user_count
);
303 * bind - associate or disassocate TIPC name(s) with a socket
304 * @sock: socket structure
305 * @uaddr: socket address describing name(s) and desired operation
306 * @uaddr_len: size of socket address data structure
308 * Name and name sequence binding is indicated using a positive scope value;
309 * a negative scope value unbinds the specified name. Specifying no name
310 * (i.e. a socket address length of 0) unbinds all names from the socket.
312 * Returns 0 on success, errno otherwise
315 static int bind(struct socket
*sock
, struct sockaddr
*uaddr
, int uaddr_len
)
317 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
318 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
321 if (down_interruptible(&tsock
->sem
))
324 if (unlikely(!uaddr_len
)) {
325 res
= tipc_withdraw(tsock
->p
->ref
, 0, 0);
329 if (uaddr_len
< sizeof(struct sockaddr_tipc
)) {
334 if (addr
->family
!= AF_TIPC
) {
338 if (addr
->addrtype
== TIPC_ADDR_NAME
)
339 addr
->addr
.nameseq
.upper
= addr
->addr
.nameseq
.lower
;
340 else if (addr
->addrtype
!= TIPC_ADDR_NAMESEQ
) {
346 res
= tipc_publish(tsock
->p
->ref
, addr
->scope
,
347 &addr
->addr
.nameseq
);
349 res
= tipc_withdraw(tsock
->p
->ref
, -addr
->scope
,
350 &addr
->addr
.nameseq
);
357 * get_name - get port ID of socket or peer socket
358 * @sock: socket structure
359 * @uaddr: area for returned socket address
360 * @uaddr_len: area for returned length of socket address
361 * @peer: 0 to obtain socket name, 1 to obtain peer socket name
363 * Returns 0 on success, errno otherwise
366 static int get_name(struct socket
*sock
, struct sockaddr
*uaddr
,
367 int *uaddr_len
, int peer
)
369 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
370 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
373 if (down_interruptible(&tsock
->sem
))
376 *uaddr_len
= sizeof(*addr
);
377 addr
->addrtype
= TIPC_ADDR_ID
;
378 addr
->family
= AF_TIPC
;
381 res
= tipc_peer(tsock
->p
->ref
, &addr
->addr
.id
);
383 res
= tipc_ownidentity(tsock
->p
->ref
, &addr
->addr
.id
);
384 addr
->addr
.name
.domain
= 0;
391 * poll - read and possibly block on pollmask
392 * @file: file structure associated with the socket
393 * @sock: socket for which to calculate the poll bits
396 * Returns the pollmask
399 static unsigned int poll(struct file
*file
, struct socket
*sock
,
402 poll_wait(file
, sock
->sk
->sk_sleep
, wait
);
403 /* NEED LOCK HERE? */
404 return pollmask(sock
);
408 * dest_name_check - verify user is permitted to send to specified port name
409 * @dest: destination address
410 * @m: descriptor for message to be sent
412 * Prevents restricted configuration commands from being issued by
413 * unauthorized users.
415 * Returns 0 if permission is granted, otherwise errno
418 static inline int dest_name_check(struct sockaddr_tipc
*dest
, struct msghdr
*m
)
420 struct tipc_cfg_msg_hdr hdr
;
422 if (likely(dest
->addr
.name
.name
.type
>= TIPC_RESERVED_TYPES
))
424 if (likely(dest
->addr
.name
.name
.type
== TIPC_TOP_SRV
))
427 if (likely(dest
->addr
.name
.name
.type
!= TIPC_CFG_SRV
))
430 if (copy_from_user(&hdr
, m
->msg_iov
[0].iov_base
, sizeof(hdr
)))
432 if ((ntohs(hdr
.tcm_type
) & 0xC000) & (!capable(CAP_NET_ADMIN
)))
439 * send_msg - send message in connectionless manner
441 * @sock: socket structure
442 * @m: message to send
443 * @total_len: (unused)
445 * Message must have an destination specified explicitly.
446 * Used for SOCK_RDM and SOCK_DGRAM messages,
447 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
448 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
450 * Returns the number of bytes sent on success, or errno otherwise
453 static int send_msg(struct kiocb
*iocb
, struct socket
*sock
,
454 struct msghdr
*m
, size_t total_len
)
456 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
457 struct sockaddr_tipc
*dest
= (struct sockaddr_tipc
*)m
->msg_name
;
463 return -EDESTADDRREQ
;
464 if (unlikely(dest
->family
!= AF_TIPC
))
467 needs_conn
= (sock
->state
!= SS_READY
);
468 if (unlikely(needs_conn
)) {
469 if (sock
->state
== SS_LISTENING
)
471 if (sock
->state
!= SS_UNCONNECTED
)
473 if ((tsock
->p
->published
) ||
474 ((sock
->type
== SOCK_STREAM
) && (total_len
!= 0)))
478 if (down_interruptible(&tsock
->sem
))
483 /* Abort any pending connection attempts (very unlikely) */
485 while ((buf
= skb_dequeue(&sock
->sk
->sk_receive_queue
))) {
486 tipc_reject_msg(buf
, TIPC_ERR_NO_PORT
);
487 atomic_dec(&tipc_queue_size
);
490 sock
->state
= SS_CONNECTING
;
494 if (dest
->addrtype
== TIPC_ADDR_NAME
) {
495 if ((res
= dest_name_check(dest
, m
)))
497 res
= tipc_send2name(tsock
->p
->ref
,
498 &dest
->addr
.name
.name
,
499 dest
->addr
.name
.domain
,
503 else if (dest
->addrtype
== TIPC_ADDR_ID
) {
504 res
= tipc_send2port(tsock
->p
->ref
,
509 else if (dest
->addrtype
== TIPC_ADDR_MCAST
) {
514 if ((res
= dest_name_check(dest
, m
)))
516 res
= tipc_multicast(tsock
->p
->ref
,
522 if (likely(res
!= -ELINKCONG
)) {
527 if (m
->msg_flags
& MSG_DONTWAIT
) {
531 if (wait_event_interruptible(*sock
->sk
->sk_sleep
,
532 !tsock
->p
->congested
)) {
540 * send_packet - send a connection-oriented message
542 * @sock: socket structure
543 * @m: message to send
544 * @total_len: (unused)
546 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
548 * Returns the number of bytes sent on success, or errno otherwise
551 static int send_packet(struct kiocb
*iocb
, struct socket
*sock
,
552 struct msghdr
*m
, size_t total_len
)
554 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
555 struct sockaddr_tipc
*dest
= (struct sockaddr_tipc
*)m
->msg_name
;
558 /* Handle implied connection establishment */
561 return send_msg(iocb
, sock
, m
, total_len
);
563 if (down_interruptible(&tsock
->sem
)) {
567 if (unlikely(sock
->state
!= SS_CONNECTED
)) {
568 if (sock
->state
== SS_DISCONNECTING
)
576 res
= tipc_send(tsock
->p
->ref
, m
->msg_iovlen
, m
->msg_iov
);
577 if (likely(res
!= -ELINKCONG
)) {
582 if (m
->msg_flags
& MSG_DONTWAIT
) {
586 if (wait_event_interruptible(*sock
->sk
->sk_sleep
,
587 !tsock
->p
->congested
)) {
595 * send_stream - send stream-oriented data
597 * @sock: socket structure
599 * @total_len: total length of data to be sent
601 * Used for SOCK_STREAM data.
603 * Returns the number of bytes sent on success, or errno otherwise
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
;
619 if (likely(total_len
<= TIPC_MAX_USER_MSG_SIZE
))
620 return send_packet(iocb
, sock
, m
, total_len
);
622 /* Can only send large data streams if already connected */
624 if (unlikely(sock
->state
!= SS_CONNECTED
)) {
625 if (sock
->state
== SS_DISCONNECTING
)
632 * Send each iovec entry using one or more messages
634 * Note: This algorithm is good for the most likely case
635 * (i.e. one large iovec entry), but could be improved to pass sets
636 * of small iovec entries into send_packet().
640 curr_iov
= my_msg
.msg_iov
;
641 curr_iovlen
= my_msg
.msg_iovlen
;
642 my_msg
.msg_iov
= &my_iov
;
643 my_msg
.msg_iovlen
= 1;
645 while (curr_iovlen
--) {
646 curr_start
= curr_iov
->iov_base
;
647 curr_left
= curr_iov
->iov_len
;
650 bytes_to_send
= (curr_left
< TIPC_MAX_USER_MSG_SIZE
)
651 ? curr_left
: TIPC_MAX_USER_MSG_SIZE
;
652 my_iov
.iov_base
= curr_start
;
653 my_iov
.iov_len
= bytes_to_send
;
654 if ((res
= send_packet(iocb
, sock
, &my_msg
, 0)) < 0)
656 curr_left
-= bytes_to_send
;
657 curr_start
+= bytes_to_send
;
667 * auto_connect - complete connection setup to a remote port
668 * @sock: socket structure
669 * @tsock: TIPC-specific socket structure
670 * @msg: peer's response message
672 * Returns 0 on success, errno otherwise
675 static int auto_connect(struct socket
*sock
, struct tipc_sock
*tsock
,
676 struct tipc_msg
*msg
)
678 struct tipc_portid peer
;
680 if (msg_errcode(msg
)) {
681 sock
->state
= SS_DISCONNECTING
;
682 return -ECONNREFUSED
;
685 peer
.ref
= msg_origport(msg
);
686 peer
.node
= msg_orignode(msg
);
687 tipc_connect2port(tsock
->p
->ref
, &peer
);
688 tipc_set_portimportance(tsock
->p
->ref
, msg_importance(msg
));
689 sock
->state
= SS_CONNECTED
;
694 * set_orig_addr - capture sender's address for received message
695 * @m: descriptor for message info
696 * @msg: received message header
698 * Note: Address is not captured if not requested by receiver.
701 static inline void set_orig_addr(struct msghdr
*m
, struct tipc_msg
*msg
)
703 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)m
->msg_name
;
706 addr
->family
= AF_TIPC
;
707 addr
->addrtype
= TIPC_ADDR_ID
;
708 addr
->addr
.id
.ref
= msg_origport(msg
);
709 addr
->addr
.id
.node
= msg_orignode(msg
);
710 addr
->addr
.name
.domain
= 0; /* could leave uninitialized */
711 addr
->scope
= 0; /* could leave uninitialized */
712 m
->msg_namelen
= sizeof(struct sockaddr_tipc
);
717 * anc_data_recv - optionally capture ancillary data for received message
718 * @m: descriptor for message info
719 * @msg: received message header
720 * @tport: TIPC port associated with message
722 * Note: Ancillary data is not captured if not requested by receiver.
724 * Returns 0 if successful, otherwise errno
727 static inline int anc_data_recv(struct msghdr
*m
, struct tipc_msg
*msg
,
728 struct tipc_port
*tport
)
735 if (likely(m
->msg_controllen
== 0))
738 /* Optionally capture errored message object(s) */
740 err
= msg
? msg_errcode(msg
) : 0;
743 anc_data
[1] = msg_data_sz(msg
);
744 if ((res
= put_cmsg(m
, SOL_SOCKET
, TIPC_ERRINFO
, 8, anc_data
)))
747 (res
= put_cmsg(m
, SOL_SOCKET
, TIPC_RETDATA
, anc_data
[1],
752 /* Optionally capture message destination object */
754 dest_type
= msg
? msg_type(msg
) : TIPC_DIRECT_MSG
;
757 anc_data
[0] = msg_nametype(msg
);
758 anc_data
[1] = msg_namelower(msg
);
759 anc_data
[2] = msg_namelower(msg
);
762 anc_data
[0] = msg_nametype(msg
);
763 anc_data
[1] = msg_namelower(msg
);
764 anc_data
[2] = msg_nameupper(msg
);
767 anc_data
[0] = tport
->conn_type
;
768 anc_data
[1] = tport
->conn_instance
;
769 anc_data
[2] = tport
->conn_instance
;
775 (res
= put_cmsg(m
, SOL_SOCKET
, TIPC_DESTNAME
, 12, anc_data
)))
782 * recv_msg - receive packet-oriented message
784 * @m: descriptor for message info
785 * @buf_len: total size of user buffer area
786 * @flags: receive flags
788 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
789 * If the complete message doesn't fit in user area, truncate it.
791 * Returns size of returned message data, errno otherwise
794 static int recv_msg(struct kiocb
*iocb
, struct socket
*sock
,
795 struct msghdr
*m
, size_t buf_len
, int flags
)
797 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
799 struct tipc_msg
*msg
;
805 /* Currently doesn't support receiving into multiple iovec entries */
807 if (m
->msg_iovlen
!= 1)
810 /* Catch invalid receive attempts */
812 if (unlikely(!buf_len
))
815 if (sock
->type
== SOCK_SEQPACKET
) {
816 if (unlikely(sock
->state
== SS_UNCONNECTED
))
818 if (unlikely((sock
->state
== SS_DISCONNECTING
) &&
819 (skb_queue_len(&sock
->sk
->sk_receive_queue
) == 0)))
823 /* Look for a message in receive queue; wait if necessary */
825 if (unlikely(down_interruptible(&tsock
->sem
)))
829 if (unlikely((skb_queue_len(&sock
->sk
->sk_receive_queue
) == 0) &&
830 (flags
& MSG_DONTWAIT
))) {
835 if ((res
= wait_event_interruptible(
837 ((q_len
= skb_queue_len(&sock
->sk
->sk_receive_queue
)) ||
838 (sock
->state
== SS_DISCONNECTING
))) )) {
842 /* Catch attempt to receive on an already terminated connection */
843 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
850 /* Get access to first message in receive queue */
852 buf
= skb_peek(&sock
->sk
->sk_receive_queue
);
854 sz
= msg_data_sz(msg
);
855 err
= msg_errcode(msg
);
857 /* Complete connection setup for an implied connect */
859 if (unlikely(sock
->state
== SS_CONNECTING
)) {
860 if ((res
= auto_connect(sock
, tsock
, msg
)))
864 /* Discard an empty non-errored message & try again */
866 if ((!sz
) && (!err
)) {
867 advance_queue(tsock
);
871 /* Capture sender's address (optional) */
873 set_orig_addr(m
, msg
);
875 /* Capture ancillary data (optional) */
877 if ((res
= anc_data_recv(m
, msg
, tsock
->p
)))
880 /* Capture message data (if valid) & compute return value (always) */
883 if (unlikely(buf_len
< sz
)) {
885 m
->msg_flags
|= MSG_TRUNC
;
887 if (unlikely(copy_to_user(m
->msg_iov
->iov_base
, msg_data(msg
),
894 if ((sock
->state
== SS_READY
) ||
895 ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
))
901 /* Consume received message (optional) */
903 if (likely(!(flags
& MSG_PEEK
))) {
904 if (unlikely(++tsock
->p
->conn_unacked
>= TIPC_FLOW_CONTROL_WIN
))
905 tipc_acknowledge(tsock
->p
->ref
, tsock
->p
->conn_unacked
);
906 advance_queue(tsock
);
914 * recv_stream - receive stream-oriented data
916 * @m: descriptor for message info
917 * @buf_len: total size of user buffer area
918 * @flags: receive flags
920 * Used for SOCK_STREAM messages only. If not enough data is available
921 * will optionally wait for more; never truncates data.
923 * Returns size of returned message data, errno otherwise
926 static int recv_stream(struct kiocb
*iocb
, struct socket
*sock
,
927 struct msghdr
*m
, size_t buf_len
, int flags
)
929 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
931 struct tipc_msg
*msg
;
937 char *crs
= m
->msg_iov
->iov_base
;
938 unsigned char *buf_crs
;
942 /* Currently doesn't support receiving into multiple iovec entries */
944 if (m
->msg_iovlen
!= 1)
947 /* Catch invalid receive attempts */
949 if (unlikely(!buf_len
))
952 if (unlikely(sock
->state
== SS_DISCONNECTING
)) {
953 if (skb_queue_len(&sock
->sk
->sk_receive_queue
) == 0)
955 } else if (unlikely(sock
->state
!= SS_CONNECTED
))
958 /* Look for a message in receive queue; wait if necessary */
960 if (unlikely(down_interruptible(&tsock
->sem
)))
964 if (unlikely((skb_queue_len(&sock
->sk
->sk_receive_queue
) == 0) &&
965 (flags
& MSG_DONTWAIT
))) {
966 res
= (sz_copied
== 0) ? -EWOULDBLOCK
: 0;
970 if ((res
= wait_event_interruptible(
972 ((q_len
= skb_queue_len(&sock
->sk
->sk_receive_queue
)) ||
973 (sock
->state
== SS_DISCONNECTING
))) )) {
977 /* Catch attempt to receive on an already terminated connection */
978 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
985 /* Get access to first message in receive queue */
987 buf
= skb_peek(&sock
->sk
->sk_receive_queue
);
989 sz
= msg_data_sz(msg
);
990 err
= msg_errcode(msg
);
992 /* Discard an empty non-errored message & try again */
994 if ((!sz
) && (!err
)) {
995 advance_queue(tsock
);
999 /* Optionally capture sender's address & ancillary data of first msg */
1001 if (sz_copied
== 0) {
1002 set_orig_addr(m
, msg
);
1003 if ((res
= anc_data_recv(m
, msg
, tsock
->p
)))
1007 /* Capture message data (if valid) & compute return value (always) */
1010 buf_crs
= (unsigned char *)(TIPC_SKB_CB(buf
)->handle
);
1011 sz
= buf
->tail
- buf_crs
;
1013 needed
= (buf_len
- sz_copied
);
1014 sz_to_copy
= (sz
<= needed
) ? sz
: needed
;
1015 if (unlikely(copy_to_user(crs
, buf_crs
, sz_to_copy
))) {
1019 sz_copied
+= sz_to_copy
;
1021 if (sz_to_copy
< sz
) {
1022 if (!(flags
& MSG_PEEK
))
1023 TIPC_SKB_CB(buf
)->handle
= buf_crs
+ sz_to_copy
;
1030 goto exit
; /* can't add error msg to valid data */
1032 if ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
)
1038 /* Consume received message (optional) */
1040 if (likely(!(flags
& MSG_PEEK
))) {
1041 if (unlikely(++tsock
->p
->conn_unacked
>= TIPC_FLOW_CONTROL_WIN
))
1042 tipc_acknowledge(tsock
->p
->ref
, tsock
->p
->conn_unacked
);
1043 advance_queue(tsock
);
1046 /* Loop around if more data is required */
1048 if ((sz_copied
< buf_len
) /* didn't get all requested data */
1049 && (flags
& MSG_WAITALL
) /* ... and need to wait for more */
1050 && (!(flags
& MSG_PEEK
)) /* ... and aren't just peeking at data */
1051 && (!err
) /* ... and haven't reached a FIN */
1057 return res
? res
: sz_copied
;
1061 * queue_overloaded - test if queue overload condition exists
1062 * @queue_size: current size of queue
1063 * @base: nominal maximum size of queue
1064 * @msg: message to be added to queue
1066 * Returns 1 if queue is currently overloaded, 0 otherwise
1069 static int queue_overloaded(u32 queue_size
, u32 base
, struct tipc_msg
*msg
)
1072 u32 imp
= msg_importance(msg
);
1074 if (imp
== TIPC_LOW_IMPORTANCE
)
1076 else if (imp
== TIPC_MEDIUM_IMPORTANCE
)
1077 threshold
= base
* 2;
1078 else if (imp
== TIPC_HIGH_IMPORTANCE
)
1079 threshold
= base
* 100;
1083 if (msg_connected(msg
))
1086 return (queue_size
> threshold
);
1090 * async_disconnect - wrapper function used to disconnect port
1091 * @portref: TIPC port reference (passed as pointer-sized value)
1094 static void async_disconnect(unsigned long portref
)
1096 tipc_disconnect((u32
)portref
);
1100 * dispatch - handle arriving message
1101 * @tport: TIPC port that received message
1104 * Called with port locked. Must not take socket lock to avoid deadlock risk.
1106 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1109 static u32
dispatch(struct tipc_port
*tport
, struct sk_buff
*buf
)
1111 struct tipc_msg
*msg
= buf_msg(buf
);
1112 struct tipc_sock
*tsock
= (struct tipc_sock
*)tport
->usr_handle
;
1113 struct socket
*sock
;
1116 /* Reject message if socket is closing */
1119 return TIPC_ERR_NO_PORT
;
1121 /* Reject message if it is wrong sort of message for socket */
1124 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1125 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1126 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1128 sock
= tsock
->sk
.sk_socket
;
1129 if (sock
->state
== SS_READY
) {
1130 if (msg_connected(msg
)) {
1131 msg_dbg(msg
, "dispatch filter 1\n");
1132 return TIPC_ERR_NO_PORT
;
1135 if (msg_mcast(msg
)) {
1136 msg_dbg(msg
, "dispatch filter 2\n");
1137 return TIPC_ERR_NO_PORT
;
1139 if (sock
->state
== SS_CONNECTED
) {
1140 if (!msg_connected(msg
)) {
1141 msg_dbg(msg
, "dispatch filter 3\n");
1142 return TIPC_ERR_NO_PORT
;
1145 else if (sock
->state
== SS_CONNECTING
) {
1146 if (!msg_connected(msg
) && (msg_errcode(msg
) == 0)) {
1147 msg_dbg(msg
, "dispatch filter 4\n");
1148 return TIPC_ERR_NO_PORT
;
1151 else if (sock
->state
== SS_LISTENING
) {
1152 if (msg_connected(msg
) || msg_errcode(msg
)) {
1153 msg_dbg(msg
, "dispatch filter 5\n");
1154 return TIPC_ERR_NO_PORT
;
1157 else if (sock
->state
== SS_DISCONNECTING
) {
1158 msg_dbg(msg
, "dispatch filter 6\n");
1159 return TIPC_ERR_NO_PORT
;
1161 else /* (sock->state == SS_UNCONNECTED) */ {
1162 if (msg_connected(msg
) || msg_errcode(msg
)) {
1163 msg_dbg(msg
, "dispatch filter 7\n");
1164 return TIPC_ERR_NO_PORT
;
1169 /* Reject message if there isn't room to queue it */
1171 if (unlikely((u32
)atomic_read(&tipc_queue_size
) >
1172 OVERLOAD_LIMIT_BASE
)) {
1173 if (queue_overloaded(atomic_read(&tipc_queue_size
),
1174 OVERLOAD_LIMIT_BASE
, msg
))
1175 return TIPC_ERR_OVERLOAD
;
1177 recv_q_len
= skb_queue_len(&tsock
->sk
.sk_receive_queue
);
1178 if (unlikely(recv_q_len
> (OVERLOAD_LIMIT_BASE
/ 2))) {
1179 if (queue_overloaded(recv_q_len
,
1180 OVERLOAD_LIMIT_BASE
/ 2, msg
))
1181 return TIPC_ERR_OVERLOAD
;
1184 /* Initiate connection termination for an incoming 'FIN' */
1186 if (unlikely(msg_errcode(msg
) && (sock
->state
== SS_CONNECTED
))) {
1187 sock
->state
= SS_DISCONNECTING
;
1188 /* Note: Use signal since port lock is already taken! */
1189 k_signal((Handler
)async_disconnect
, tport
->ref
);
1192 /* Enqueue message (finally!) */
1194 msg_dbg(msg
,"<DISP<: ");
1195 TIPC_SKB_CB(buf
)->handle
= msg_data(msg
);
1196 atomic_inc(&tipc_queue_size
);
1197 skb_queue_tail(&sock
->sk
->sk_receive_queue
, buf
);
1199 wake_up_interruptible(sock
->sk
->sk_sleep
);
1204 * wakeupdispatch - wake up port after congestion
1205 * @tport: port to wakeup
1207 * Called with port lock on.
1210 static void wakeupdispatch(struct tipc_port
*tport
)
1212 struct tipc_sock
*tsock
= (struct tipc_sock
*)tport
->usr_handle
;
1214 wake_up_interruptible(tsock
->sk
.sk_sleep
);
1218 * connect - establish a connection to another TIPC port
1219 * @sock: socket structure
1220 * @dest: socket address for destination port
1221 * @destlen: size of socket address data structure
1224 * Returns 0 on success, errno otherwise
1227 static int connect(struct socket
*sock
, struct sockaddr
*dest
, int destlen
,
1230 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
1231 struct sockaddr_tipc
*dst
= (struct sockaddr_tipc
*)dest
;
1232 struct msghdr m
= {0,};
1233 struct sk_buff
*buf
;
1234 struct tipc_msg
*msg
;
1237 /* For now, TIPC does not allow use of connect() with DGRAM or RDM types */
1239 if (sock
->state
== SS_READY
)
1242 /* MOVE THE REST OF THIS ERROR CHECKING TO send_msg()? */
1243 if (sock
->state
== SS_LISTENING
)
1245 if (sock
->state
== SS_CONNECTING
)
1247 if (sock
->state
!= SS_UNCONNECTED
)
1250 if ((dst
->family
!= AF_TIPC
) ||
1251 ((dst
->addrtype
!= TIPC_ADDR_NAME
) && (dst
->addrtype
!= TIPC_ADDR_ID
)))
1254 /* Send a 'SYN-' to destination */
1257 if ((res
= send_msg(0, sock
, &m
, 0)) < 0) {
1258 sock
->state
= SS_DISCONNECTING
;
1262 if (down_interruptible(&tsock
->sem
))
1263 return -ERESTARTSYS
;
1265 /* Wait for destination's 'ACK' response */
1267 res
= wait_event_interruptible_timeout(*sock
->sk
->sk_sleep
,
1268 skb_queue_len(&sock
->sk
->sk_receive_queue
),
1269 sock
->sk
->sk_rcvtimeo
);
1270 buf
= skb_peek(&sock
->sk
->sk_receive_queue
);
1273 res
= auto_connect(sock
, tsock
, msg
);
1275 if (dst
->addrtype
== TIPC_ADDR_NAME
) {
1276 tsock
->p
->conn_type
= dst
->addr
.name
.name
.type
;
1277 tsock
->p
->conn_instance
= dst
->addr
.name
.name
.instance
;
1279 if (!msg_data_sz(msg
))
1280 advance_queue(tsock
);
1286 { /* leave "res" unchanged */ }
1287 sock
->state
= SS_DISCONNECTING
;
1295 * listen - allow socket to listen for incoming connections
1296 * @sock: socket structure
1299 * Returns 0 on success, errno otherwise
1302 static int listen(struct socket
*sock
, int len
)
1304 /* REQUIRES SOCKET LOCKING OF SOME SORT? */
1306 if (sock
->state
== SS_READY
)
1308 if (sock
->state
!= SS_UNCONNECTED
)
1310 sock
->state
= SS_LISTENING
;
1315 * accept - wait for connection request
1316 * @sock: listening socket
1317 * @newsock: new socket that is to be connected
1318 * @flags: file-related flags associated with socket
1320 * Returns 0 on success, errno otherwise
1323 static int accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
1325 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
1326 struct sk_buff
*buf
;
1329 if (sock
->state
== SS_READY
)
1331 if (sock
->state
!= SS_LISTENING
)
1334 if (unlikely((skb_queue_len(&sock
->sk
->sk_receive_queue
) == 0) &&
1335 (flags
& O_NONBLOCK
)))
1336 return -EWOULDBLOCK
;
1338 if (down_interruptible(&tsock
->sem
))
1339 return -ERESTARTSYS
;
1341 if (wait_event_interruptible(*sock
->sk
->sk_sleep
,
1342 skb_queue_len(&sock
->sk
->sk_receive_queue
))) {
1346 buf
= skb_peek(&sock
->sk
->sk_receive_queue
);
1348 res
= tipc_create(newsock
, 0);
1350 struct tipc_sock
*new_tsock
= tipc_sk(newsock
->sk
);
1351 struct tipc_portid id
;
1352 struct tipc_msg
*msg
= buf_msg(buf
);
1353 u32 new_ref
= new_tsock
->p
->ref
;
1355 id
.ref
= msg_origport(msg
);
1356 id
.node
= msg_orignode(msg
);
1357 tipc_connect2port(new_ref
, &id
);
1358 newsock
->state
= SS_CONNECTED
;
1360 tipc_set_portimportance(new_ref
, msg_importance(msg
));
1361 if (msg_named(msg
)) {
1362 new_tsock
->p
->conn_type
= msg_nametype(msg
);
1363 new_tsock
->p
->conn_instance
= msg_nameinst(msg
);
1367 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1368 * Respond to 'SYN+' by queuing it on new socket.
1371 msg_dbg(msg
,"<ACC<: ");
1372 if (!msg_data_sz(msg
)) {
1373 struct msghdr m
= {0,};
1375 send_packet(0, newsock
, &m
, 0);
1376 advance_queue(tsock
);
1379 skb_dequeue(&sock
->sk
->sk_receive_queue
);
1381 skb_queue_head(&newsock
->sk
->sk_receive_queue
, buf
);
1390 * shutdown - shutdown socket connection
1391 * @sock: socket structure
1392 * @how: direction to close (always treated as read + write)
1394 * Terminates connection (if necessary), then purges socket's receive queue.
1396 * Returns 0 on success, errno otherwise
1399 static int shutdown(struct socket
*sock
, int how
)
1401 struct tipc_sock
* tsock
= tipc_sk(sock
->sk
);
1402 struct sk_buff
*buf
;
1405 /* Could return -EINVAL for an invalid "how", but why bother? */
1407 if (down_interruptible(&tsock
->sem
))
1408 return -ERESTARTSYS
;
1412 switch (sock
->state
) {
1415 /* Send 'FIN+' or 'FIN-' message to peer */
1419 if ((buf
= skb_dequeue(&sock
->sk
->sk_receive_queue
))) {
1420 atomic_dec(&tipc_queue_size
);
1421 if (TIPC_SKB_CB(buf
)->handle
!= msg_data(buf_msg(buf
))) {
1425 tipc_reject_msg(buf
, TIPC_CONN_SHUTDOWN
);
1428 tipc_shutdown(tsock
->p
->ref
);
1434 case SS_DISCONNECTING
:
1436 /* Discard any unreceived messages */
1438 while ((buf
= skb_dequeue(&sock
->sk
->sk_receive_queue
))) {
1439 atomic_dec(&tipc_queue_size
);
1442 tsock
->p
->conn_unacked
= 0;
1447 sock
->state
= SS_DISCONNECTING
;
1462 * setsockopt - set socket option
1463 * @sock: socket structure
1464 * @lvl: option level
1465 * @opt: option identifier
1466 * @ov: pointer to new option value
1467 * @ol: length of option value
1469 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
1470 * (to ease compatibility).
1472 * Returns 0 on success, errno otherwise
1475 static int setsockopt(struct socket
*sock
, int lvl
, int opt
, char *ov
, int ol
)
1477 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
1481 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
1483 if (lvl
!= SOL_TIPC
)
1484 return -ENOPROTOOPT
;
1485 if (ol
< sizeof(value
))
1487 if ((res
= get_user(value
, (u32
*)ov
)))
1490 if (down_interruptible(&tsock
->sem
))
1491 return -ERESTARTSYS
;
1494 case TIPC_IMPORTANCE
:
1495 res
= tipc_set_portimportance(tsock
->p
->ref
, value
);
1497 case TIPC_SRC_DROPPABLE
:
1498 if (sock
->type
!= SOCK_STREAM
)
1499 res
= tipc_set_portunreliable(tsock
->p
->ref
, value
);
1503 case TIPC_DEST_DROPPABLE
:
1504 res
= tipc_set_portunreturnable(tsock
->p
->ref
, value
);
1506 case TIPC_CONN_TIMEOUT
:
1507 sock
->sk
->sk_rcvtimeo
= (value
* HZ
/ 1000);
1518 * getsockopt - get socket option
1519 * @sock: socket structure
1520 * @lvl: option level
1521 * @opt: option identifier
1522 * @ov: receptacle for option value
1523 * @ol: receptacle for length of option value
1525 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
1526 * (to ease compatibility).
1528 * Returns 0 on success, errno otherwise
1531 static int getsockopt(struct socket
*sock
, int lvl
, int opt
, char *ov
, int *ol
)
1533 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
1538 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
1539 return put_user(0, ol
);
1540 if (lvl
!= SOL_TIPC
)
1541 return -ENOPROTOOPT
;
1542 if ((res
= get_user(len
, ol
)))
1545 if (down_interruptible(&tsock
->sem
))
1546 return -ERESTARTSYS
;
1549 case TIPC_IMPORTANCE
:
1550 res
= tipc_portimportance(tsock
->p
->ref
, &value
);
1552 case TIPC_SRC_DROPPABLE
:
1553 res
= tipc_portunreliable(tsock
->p
->ref
, &value
);
1555 case TIPC_DEST_DROPPABLE
:
1556 res
= tipc_portunreturnable(tsock
->p
->ref
, &value
);
1558 case TIPC_CONN_TIMEOUT
:
1559 value
= (sock
->sk
->sk_rcvtimeo
* 1000) / HZ
;
1568 else if (len
< sizeof(value
)) {
1571 else if ((res
= copy_to_user(ov
, &value
, sizeof(value
)))) {
1572 /* couldn't return value */
1575 res
= put_user(sizeof(value
), ol
);
1583 * Placeholders for non-implemented functionality
1585 * Returns error code (POSIX-compliant where defined)
1588 static int ioctl(struct socket
*s
, u32 cmd
, unsigned long arg
)
1593 static int no_mmap(struct file
*file
, struct socket
*sock
,
1594 struct vm_area_struct
*vma
)
1598 static ssize_t
no_sendpage(struct socket
*sock
, struct page
*page
,
1599 int offset
, size_t size
, int flags
)
1604 static int no_skpair(struct socket
*s1
, struct socket
*s2
)
1610 * Protocol switches for the various types of TIPC sockets
1613 static struct proto_ops msg_ops
= {
1614 .owner
= THIS_MODULE
,
1619 .socketpair
= no_skpair
,
1621 .getname
= get_name
,
1625 .shutdown
= shutdown
,
1626 .setsockopt
= setsockopt
,
1627 .getsockopt
= getsockopt
,
1628 .sendmsg
= send_msg
,
1629 .recvmsg
= recv_msg
,
1631 .sendpage
= no_sendpage
1634 static struct proto_ops packet_ops
= {
1635 .owner
= THIS_MODULE
,
1640 .socketpair
= no_skpair
,
1642 .getname
= get_name
,
1646 .shutdown
= shutdown
,
1647 .setsockopt
= setsockopt
,
1648 .getsockopt
= getsockopt
,
1649 .sendmsg
= send_packet
,
1650 .recvmsg
= recv_msg
,
1652 .sendpage
= no_sendpage
1655 static struct proto_ops stream_ops
= {
1656 .owner
= THIS_MODULE
,
1661 .socketpair
= no_skpair
,
1663 .getname
= get_name
,
1667 .shutdown
= shutdown
,
1668 .setsockopt
= setsockopt
,
1669 .getsockopt
= getsockopt
,
1670 .sendmsg
= send_stream
,
1671 .recvmsg
= recv_stream
,
1673 .sendpage
= no_sendpage
1676 static struct net_proto_family tipc_family_ops
= {
1677 .owner
= THIS_MODULE
,
1679 .create
= tipc_create
1682 static struct proto tipc_proto
= {
1684 .owner
= THIS_MODULE
,
1685 .obj_size
= sizeof(struct tipc_sock
)
1689 * socket_init - initialize TIPC socket interface
1691 * Returns 0 on success, errno otherwise
1693 int socket_init(void)
1697 res
= proto_register(&tipc_proto
, 1);
1699 err("Failed to register TIPC protocol type\n");
1703 res
= sock_register(&tipc_family_ops
);
1705 err("Failed to register TIPC socket type\n");
1706 proto_unregister(&tipc_proto
);
1710 sockets_enabled
= 1;
1716 * sock_stop - stop TIPC socket interface
1718 void socket_stop(void)
1720 if (!sockets_enabled
)
1723 sockets_enabled
= 0;
1724 sock_unregister(tipc_family_ops
.family
);
1725 proto_unregister(&tipc_proto
);