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 * Redistributions of source code must retain the above copyright notice, this
13 * list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #include <linux/module.h>
35 #include <linux/types.h>
36 #include <linux/net.h>
37 #include <linux/socket.h>
38 #include <linux/errno.h>
40 #include <linux/slab.h>
41 #include <linux/poll.h>
42 #include <linux/version.h>
43 #include <linux/fcntl.h>
44 #include <linux/version.h>
45 #include <asm/semaphore.h>
46 #include <asm/string.h>
47 #include <asm/atomic.h>
50 #include <linux/tipc.h>
51 #include <linux/tipc_config.h>
52 #include <net/tipc/tipc_msg.h>
53 #include <net/tipc/tipc_port.h>
57 #define SS_LISTENING -1 /* socket is listening */
58 #define SS_READY -2 /* socket is connectionless */
60 #define OVERLOAD_LIMIT_BASE 5000
68 #define tipc_sk(sk) ((struct tipc_sock*)sk)
70 static u32
dispatch(struct tipc_port
*tport
, struct sk_buff
*buf
);
71 static void wakeupdispatch(struct tipc_port
*tport
);
73 static struct proto_ops packet_ops
;
74 static struct proto_ops stream_ops
;
75 static struct proto_ops msg_ops
;
77 static struct proto tipc_proto
;
79 static int sockets_enabled
= 0;
81 static atomic_t tipc_queue_size
= ATOMIC_INIT(0);
85 * sock_lock(): Lock a port/socket pair. lock_sock() can
86 * not be used here, since the same lock must protect ports
87 * with non-socket interfaces.
88 * See net.c for description of locking policy.
90 static inline void sock_lock(struct tipc_sock
* tsock
)
92 spin_lock_bh(tsock
->p
->lock
);
96 * sock_unlock(): Unlock a port/socket pair
98 static inline void sock_unlock(struct tipc_sock
* tsock
)
100 spin_unlock_bh(tsock
->p
->lock
);
104 * pollmask - determine the current set of poll() events for a socket
105 * @sock: socket structure
107 * TIPC sets the returned events as follows:
108 * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty
109 * or if a connection-oriented socket is does not have an active connection
110 * (i.e. a read operation will not block).
111 * b) POLLOUT is set except when a socket's connection has been terminated
112 * (i.e. a write operation will not block).
113 * c) POLLHUP is set when a socket's connection has been terminated.
115 * IMPORTANT: The fact that a read or write operation will not block does NOT
116 * imply that the operation will succeed!
118 * Returns pollmask value
121 static inline u32
pollmask(struct socket
*sock
)
125 if ((skb_queue_len(&sock
->sk
->sk_receive_queue
) != 0) ||
126 (sock
->state
== SS_UNCONNECTED
) ||
127 (sock
->state
== SS_DISCONNECTING
))
128 mask
= (POLLRDNORM
| POLLIN
);
132 if (sock
->state
== SS_DISCONNECTING
)
142 * advance_queue - discard first buffer in queue
143 * @tsock: TIPC socket
146 static inline void advance_queue(struct tipc_sock
*tsock
)
149 buf_discard(skb_dequeue(&tsock
->sk
.sk_receive_queue
));
151 atomic_dec(&tipc_queue_size
);
155 * tipc_create - create a TIPC socket
156 * @sock: pre-allocated socket structure
157 * @protocol: protocol indicator (must be 0)
159 * This routine creates and attaches a 'struct sock' to the 'struct socket',
160 * then create and attaches a TIPC port to the 'struct sock' part.
162 * Returns 0 on success, errno otherwise
164 static int tipc_create(struct socket
*sock
, int protocol
)
166 struct tipc_sock
*tsock
;
167 struct tipc_port
*port
;
171 if ((sock
->type
!= SOCK_STREAM
) &&
172 (sock
->type
!= SOCK_SEQPACKET
) &&
173 (sock
->type
!= SOCK_DGRAM
) &&
174 (sock
->type
!= SOCK_RDM
))
177 if (unlikely(protocol
!= 0))
178 return -EPROTONOSUPPORT
;
180 ref
= tipc_createport_raw(0, &dispatch
, &wakeupdispatch
, TIPC_LOW_IMPORTANCE
);
184 sock
->state
= SS_UNCONNECTED
;
186 switch (sock
->type
) {
188 sock
->ops
= &stream_ops
;
191 sock
->ops
= &packet_ops
;
194 tipc_set_portunreliable(ref
, 1);
197 tipc_set_portunreturnable(ref
, 1);
198 sock
->ops
= &msg_ops
;
199 sock
->state
= SS_READY
;
203 sk
= sk_alloc(AF_TIPC
, GFP_KERNEL
, &tipc_proto
, 1);
205 tipc_deleteport(ref
);
209 sock_init_data(sock
, sk
);
210 init_waitqueue_head(sk
->sk_sleep
);
211 sk
->sk_rcvtimeo
= 8 * HZ
; /* default connect timeout = 8s */
214 port
= tipc_get_port(ref
);
217 port
->usr_handle
= tsock
;
219 init_MUTEX(&tsock
->sem
);
221 dbg("sock_create: %x\n",tsock
);
223 atomic_inc(&tipc_user_count
);
229 * release - destroy a TIPC socket
230 * @sock: socket to destroy
232 * This routine cleans up any messages that are still queued on the socket.
233 * For DGRAM and RDM socket types, all queued messages are rejected.
234 * For SEQPACKET and STREAM socket types, the first message is rejected
235 * and any others are discarded. (If the first message on a STREAM socket
236 * is partially-read, it is discarded and the next one is rejected instead.)
238 * NOTE: Rejected messages are not necessarily returned to the sender! They
239 * are returned or discarded according to the "destination droppable" setting
240 * specified for the message by the sender.
242 * Returns 0 on success, errno otherwise
245 static int release(struct socket
*sock
)
247 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
248 struct sock
*sk
= sock
->sk
;
252 dbg("sock_delete: %x\n",tsock
);
255 down_interruptible(&tsock
->sem
);
261 /* Reject unreceived messages, unless no longer connected */
263 while (sock
->state
!= SS_DISCONNECTING
) {
265 buf
= skb_dequeue(&sk
->sk_receive_queue
);
267 tsock
->p
->usr_handle
= 0;
271 if (TIPC_SKB_CB(buf
)->handle
!= msg_data(buf_msg(buf
)))
274 tipc_reject_msg(buf
, TIPC_ERR_NO_PORT
);
275 atomic_dec(&tipc_queue_size
);
278 /* Delete TIPC port */
280 res
= tipc_deleteport(tsock
->p
->ref
);
283 /* Discard any remaining messages */
285 while ((buf
= skb_dequeue(&sk
->sk_receive_queue
))) {
287 atomic_dec(&tipc_queue_size
);
294 atomic_dec(&tipc_user_count
);
299 * bind - associate or disassocate TIPC name(s) with a socket
300 * @sock: socket structure
301 * @uaddr: socket address describing name(s) and desired operation
302 * @uaddr_len: size of socket address data structure
304 * Name and name sequence binding is indicated using a positive scope value;
305 * a negative scope value unbinds the specified name. Specifying no name
306 * (i.e. a socket address length of 0) unbinds all names from the socket.
308 * Returns 0 on success, errno otherwise
311 static int bind(struct socket
*sock
, struct sockaddr
*uaddr
, int uaddr_len
)
313 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
314 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
317 if (down_interruptible(&tsock
->sem
))
320 if (unlikely(!uaddr_len
)) {
321 res
= tipc_withdraw(tsock
->p
->ref
, 0, 0);
325 if (uaddr_len
< sizeof(struct sockaddr_tipc
)) {
330 if (addr
->family
!= AF_TIPC
) {
334 if (addr
->addrtype
== TIPC_ADDR_NAME
)
335 addr
->addr
.nameseq
.upper
= addr
->addr
.nameseq
.lower
;
336 else if (addr
->addrtype
!= TIPC_ADDR_NAMESEQ
) {
342 res
= tipc_publish(tsock
->p
->ref
, addr
->scope
,
343 &addr
->addr
.nameseq
);
345 res
= tipc_withdraw(tsock
->p
->ref
, -addr
->scope
,
346 &addr
->addr
.nameseq
);
353 * get_name - get port ID of socket or peer socket
354 * @sock: socket structure
355 * @uaddr: area for returned socket address
356 * @uaddr_len: area for returned length of socket address
357 * @peer: 0 to obtain socket name, 1 to obtain peer socket name
359 * Returns 0 on success, errno otherwise
362 static int get_name(struct socket
*sock
, struct sockaddr
*uaddr
,
363 int *uaddr_len
, int peer
)
365 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
366 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
369 if (down_interruptible(&tsock
->sem
))
372 *uaddr_len
= sizeof(*addr
);
373 addr
->addrtype
= TIPC_ADDR_ID
;
374 addr
->family
= AF_TIPC
;
377 res
= tipc_peer(tsock
->p
->ref
, &addr
->addr
.id
);
379 res
= tipc_ownidentity(tsock
->p
->ref
, &addr
->addr
.id
);
380 addr
->addr
.name
.domain
= 0;
387 * poll - read and possibly block on pollmask
388 * @file: file structure associated with the socket
389 * @sock: socket for which to calculate the poll bits
392 * Returns the pollmask
395 static unsigned int poll(struct file
*file
, struct socket
*sock
,
398 poll_wait(file
, sock
->sk
->sk_sleep
, wait
);
399 /* NEED LOCK HERE? */
400 return pollmask(sock
);
404 * dest_name_check - verify user is permitted to send to specified port name
405 * @dest: destination address
406 * @m: descriptor for message to be sent
408 * Prevents restricted configuration commands from being issued by
409 * unauthorized users.
411 * Returns 0 if permission is granted, otherwise errno
414 static inline int dest_name_check(struct sockaddr_tipc
*dest
, struct msghdr
*m
)
416 struct tipc_cfg_msg_hdr hdr
;
418 if (likely(dest
->addr
.name
.name
.type
>= TIPC_RESERVED_TYPES
))
420 if (likely(dest
->addr
.name
.name
.type
== TIPC_TOP_SRV
))
423 if (likely(dest
->addr
.name
.name
.type
!= TIPC_CFG_SRV
))
426 if (copy_from_user(&hdr
, m
->msg_iov
[0].iov_base
, sizeof(hdr
)))
428 if ((ntohs(hdr
.tcm_type
) & 0xC000) & (!capable(CAP_NET_ADMIN
)))
435 * send_msg - send message in connectionless manner
437 * @sock: socket structure
438 * @m: message to send
439 * @total_len: (unused)
441 * Message must have an destination specified explicitly.
442 * Used for SOCK_RDM and SOCK_DGRAM messages,
443 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
444 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
446 * Returns the number of bytes sent on success, or errno otherwise
449 static int send_msg(struct kiocb
*iocb
, struct socket
*sock
,
450 struct msghdr
*m
, size_t total_len
)
452 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
453 struct sockaddr_tipc
*dest
= (struct sockaddr_tipc
*)m
->msg_name
;
459 return -EDESTADDRREQ
;
460 if (unlikely(dest
->family
!= AF_TIPC
))
463 needs_conn
= (sock
->state
!= SS_READY
);
464 if (unlikely(needs_conn
)) {
465 if (sock
->state
== SS_LISTENING
)
467 if (sock
->state
!= SS_UNCONNECTED
)
469 if ((tsock
->p
->published
) ||
470 ((sock
->type
== SOCK_STREAM
) && (total_len
!= 0)))
474 if (down_interruptible(&tsock
->sem
))
479 /* Abort any pending connection attempts (very unlikely) */
481 while ((buf
= skb_dequeue(&sock
->sk
->sk_receive_queue
))) {
482 tipc_reject_msg(buf
, TIPC_ERR_NO_PORT
);
483 atomic_dec(&tipc_queue_size
);
486 sock
->state
= SS_CONNECTING
;
490 if (dest
->addrtype
== TIPC_ADDR_NAME
) {
491 if ((res
= dest_name_check(dest
, m
)))
493 res
= tipc_send2name(tsock
->p
->ref
,
494 &dest
->addr
.name
.name
,
495 dest
->addr
.name
.domain
,
499 else if (dest
->addrtype
== TIPC_ADDR_ID
) {
500 res
= tipc_send2port(tsock
->p
->ref
,
505 else if (dest
->addrtype
== TIPC_ADDR_MCAST
) {
510 if ((res
= dest_name_check(dest
, m
)))
512 res
= tipc_multicast(tsock
->p
->ref
,
518 if (likely(res
!= -ELINKCONG
)) {
523 if (m
->msg_flags
& MSG_DONTWAIT
) {
527 if (wait_event_interruptible(*sock
->sk
->sk_sleep
,
528 !tsock
->p
->congested
)) {
536 * send_packet - send a connection-oriented message
538 * @sock: socket structure
539 * @m: message to send
540 * @total_len: (unused)
542 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
544 * Returns the number of bytes sent on success, or errno otherwise
547 static int send_packet(struct kiocb
*iocb
, struct socket
*sock
,
548 struct msghdr
*m
, size_t total_len
)
550 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
551 struct sockaddr_tipc
*dest
= (struct sockaddr_tipc
*)m
->msg_name
;
554 /* Handle implied connection establishment */
557 return send_msg(iocb
, sock
, m
, total_len
);
559 if (down_interruptible(&tsock
->sem
)) {
563 if (unlikely(sock
->state
!= SS_CONNECTED
)) {
564 if (sock
->state
== SS_DISCONNECTING
)
572 res
= tipc_send(tsock
->p
->ref
, m
->msg_iovlen
, m
->msg_iov
);
573 if (likely(res
!= -ELINKCONG
)) {
578 if (m
->msg_flags
& MSG_DONTWAIT
) {
582 if (wait_event_interruptible(*sock
->sk
->sk_sleep
,
583 !tsock
->p
->congested
)) {
591 * send_stream - send stream-oriented data
593 * @sock: socket structure
595 * @total_len: total length of data to be sent
597 * Used for SOCK_STREAM data.
599 * Returns the number of bytes sent on success, or errno otherwise
603 static int send_stream(struct kiocb
*iocb
, struct socket
*sock
,
604 struct msghdr
*m
, size_t total_len
)
606 struct msghdr my_msg
;
608 struct iovec
*curr_iov
;
610 char __user
*curr_start
;
615 if (likely(total_len
<= TIPC_MAX_USER_MSG_SIZE
))
616 return send_packet(iocb
, sock
, m
, total_len
);
618 /* Can only send large data streams if already connected */
620 if (unlikely(sock
->state
!= SS_CONNECTED
)) {
621 if (sock
->state
== SS_DISCONNECTING
)
628 * Send each iovec entry using one or more messages
630 * Note: This algorithm is good for the most likely case
631 * (i.e. one large iovec entry), but could be improved to pass sets
632 * of small iovec entries into send_packet().
636 curr_iov
= my_msg
.msg_iov
;
637 curr_iovlen
= my_msg
.msg_iovlen
;
638 my_msg
.msg_iov
= &my_iov
;
639 my_msg
.msg_iovlen
= 1;
641 while (curr_iovlen
--) {
642 curr_start
= curr_iov
->iov_base
;
643 curr_left
= curr_iov
->iov_len
;
646 bytes_to_send
= (curr_left
< TIPC_MAX_USER_MSG_SIZE
)
647 ? curr_left
: TIPC_MAX_USER_MSG_SIZE
;
648 my_iov
.iov_base
= curr_start
;
649 my_iov
.iov_len
= bytes_to_send
;
650 if ((res
= send_packet(iocb
, sock
, &my_msg
, 0)) < 0)
652 curr_left
-= bytes_to_send
;
653 curr_start
+= bytes_to_send
;
663 * auto_connect - complete connection setup to a remote port
664 * @sock: socket structure
665 * @tsock: TIPC-specific socket structure
666 * @msg: peer's response message
668 * Returns 0 on success, errno otherwise
671 static int auto_connect(struct socket
*sock
, struct tipc_sock
*tsock
,
672 struct tipc_msg
*msg
)
674 struct tipc_portid peer
;
676 if (msg_errcode(msg
)) {
677 sock
->state
= SS_DISCONNECTING
;
678 return -ECONNREFUSED
;
681 peer
.ref
= msg_origport(msg
);
682 peer
.node
= msg_orignode(msg
);
683 tipc_connect2port(tsock
->p
->ref
, &peer
);
684 tipc_set_portimportance(tsock
->p
->ref
, msg_importance(msg
));
685 sock
->state
= SS_CONNECTED
;
690 * set_orig_addr - capture sender's address for received message
691 * @m: descriptor for message info
692 * @msg: received message header
694 * Note: Address is not captured if not requested by receiver.
697 static inline void set_orig_addr(struct msghdr
*m
, struct tipc_msg
*msg
)
699 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)m
->msg_name
;
702 addr
->family
= AF_TIPC
;
703 addr
->addrtype
= TIPC_ADDR_ID
;
704 addr
->addr
.id
.ref
= msg_origport(msg
);
705 addr
->addr
.id
.node
= msg_orignode(msg
);
706 addr
->addr
.name
.domain
= 0; /* could leave uninitialized */
707 addr
->scope
= 0; /* could leave uninitialized */
708 m
->msg_namelen
= sizeof(struct sockaddr_tipc
);
713 * anc_data_recv - optionally capture ancillary data for received message
714 * @m: descriptor for message info
715 * @msg: received message header
716 * @tport: TIPC port associated with message
718 * Note: Ancillary data is not captured if not requested by receiver.
720 * Returns 0 if successful, otherwise errno
723 static inline int anc_data_recv(struct msghdr
*m
, struct tipc_msg
*msg
,
724 struct tipc_port
*tport
)
731 if (likely(m
->msg_controllen
== 0))
734 /* Optionally capture errored message object(s) */
736 err
= msg
? msg_errcode(msg
) : 0;
739 anc_data
[1] = msg_data_sz(msg
);
740 if ((res
= put_cmsg(m
, SOL_SOCKET
, TIPC_ERRINFO
, 8, anc_data
)))
743 (res
= put_cmsg(m
, SOL_SOCKET
, TIPC_RETDATA
, anc_data
[1],
748 /* Optionally capture message destination object */
750 dest_type
= msg
? msg_type(msg
) : TIPC_DIRECT_MSG
;
753 anc_data
[0] = msg_nametype(msg
);
754 anc_data
[1] = msg_namelower(msg
);
755 anc_data
[2] = msg_namelower(msg
);
758 anc_data
[0] = msg_nametype(msg
);
759 anc_data
[1] = msg_namelower(msg
);
760 anc_data
[2] = msg_nameupper(msg
);
763 anc_data
[0] = tport
->conn_type
;
764 anc_data
[1] = tport
->conn_instance
;
765 anc_data
[2] = tport
->conn_instance
;
771 (res
= put_cmsg(m
, SOL_SOCKET
, TIPC_DESTNAME
, 12, anc_data
)))
778 * recv_msg - receive packet-oriented message
780 * @m: descriptor for message info
781 * @buf_len: total size of user buffer area
782 * @flags: receive flags
784 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
785 * If the complete message doesn't fit in user area, truncate it.
787 * Returns size of returned message data, errno otherwise
790 static int recv_msg(struct kiocb
*iocb
, struct socket
*sock
,
791 struct msghdr
*m
, size_t buf_len
, int flags
)
793 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
795 struct tipc_msg
*msg
;
801 /* Currently doesn't support receiving into multiple iovec entries */
803 if (m
->msg_iovlen
!= 1)
806 /* Catch invalid receive attempts */
808 if (unlikely(!buf_len
))
811 if (sock
->type
== SOCK_SEQPACKET
) {
812 if (unlikely(sock
->state
== SS_UNCONNECTED
))
814 if (unlikely((sock
->state
== SS_DISCONNECTING
) &&
815 (skb_queue_len(&sock
->sk
->sk_receive_queue
) == 0)))
819 /* Look for a message in receive queue; wait if necessary */
821 if (unlikely(down_interruptible(&tsock
->sem
)))
825 if (unlikely((skb_queue_len(&sock
->sk
->sk_receive_queue
) == 0) &&
826 (flags
& MSG_DONTWAIT
))) {
831 if ((res
= wait_event_interruptible(
833 ((q_len
= skb_queue_len(&sock
->sk
->sk_receive_queue
)) ||
834 (sock
->state
== SS_DISCONNECTING
))) )) {
838 /* Catch attempt to receive on an already terminated connection */
839 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
846 /* Get access to first message in receive queue */
848 buf
= skb_peek(&sock
->sk
->sk_receive_queue
);
850 sz
= msg_data_sz(msg
);
851 err
= msg_errcode(msg
);
853 /* Complete connection setup for an implied connect */
855 if (unlikely(sock
->state
== SS_CONNECTING
)) {
856 if ((res
= auto_connect(sock
, tsock
, msg
)))
860 /* Discard an empty non-errored message & try again */
862 if ((!sz
) && (!err
)) {
863 advance_queue(tsock
);
867 /* Capture sender's address (optional) */
869 set_orig_addr(m
, msg
);
871 /* Capture ancillary data (optional) */
873 if ((res
= anc_data_recv(m
, msg
, tsock
->p
)))
876 /* Capture message data (if valid) & compute return value (always) */
879 if (unlikely(buf_len
< sz
)) {
881 m
->msg_flags
|= MSG_TRUNC
;
883 if (unlikely(copy_to_user(m
->msg_iov
->iov_base
, msg_data(msg
),
890 if ((sock
->state
== SS_READY
) ||
891 ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
))
897 /* Consume received message (optional) */
899 if (likely(!(flags
& MSG_PEEK
))) {
900 if (unlikely(++tsock
->p
->conn_unacked
>= TIPC_FLOW_CONTROL_WIN
))
901 tipc_acknowledge(tsock
->p
->ref
, tsock
->p
->conn_unacked
);
902 advance_queue(tsock
);
910 * recv_stream - receive stream-oriented data
912 * @m: descriptor for message info
913 * @buf_len: total size of user buffer area
914 * @flags: receive flags
916 * Used for SOCK_STREAM messages only. If not enough data is available
917 * will optionally wait for more; never truncates data.
919 * Returns size of returned message data, errno otherwise
922 static int recv_stream(struct kiocb
*iocb
, struct socket
*sock
,
923 struct msghdr
*m
, size_t buf_len
, int flags
)
925 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
927 struct tipc_msg
*msg
;
933 char *crs
= m
->msg_iov
->iov_base
;
934 unsigned char *buf_crs
;
938 /* Currently doesn't support receiving into multiple iovec entries */
940 if (m
->msg_iovlen
!= 1)
943 /* Catch invalid receive attempts */
945 if (unlikely(!buf_len
))
948 if (unlikely(sock
->state
== SS_DISCONNECTING
)) {
949 if (skb_queue_len(&sock
->sk
->sk_receive_queue
) == 0)
951 } else if (unlikely(sock
->state
!= SS_CONNECTED
))
954 /* Look for a message in receive queue; wait if necessary */
956 if (unlikely(down_interruptible(&tsock
->sem
)))
960 if (unlikely((skb_queue_len(&sock
->sk
->sk_receive_queue
) == 0) &&
961 (flags
& MSG_DONTWAIT
))) {
962 res
= (sz_copied
== 0) ? -EWOULDBLOCK
: 0;
966 if ((res
= wait_event_interruptible(
968 ((q_len
= skb_queue_len(&sock
->sk
->sk_receive_queue
)) ||
969 (sock
->state
== SS_DISCONNECTING
))) )) {
973 /* Catch attempt to receive on an already terminated connection */
974 /* [THIS CHECK MAY OVERLAP WITH AN EARLIER CHECK] */
981 /* Get access to first message in receive queue */
983 buf
= skb_peek(&sock
->sk
->sk_receive_queue
);
985 sz
= msg_data_sz(msg
);
986 err
= msg_errcode(msg
);
988 /* Discard an empty non-errored message & try again */
990 if ((!sz
) && (!err
)) {
991 advance_queue(tsock
);
995 /* Optionally capture sender's address & ancillary data of first msg */
997 if (sz_copied
== 0) {
998 set_orig_addr(m
, msg
);
999 if ((res
= anc_data_recv(m
, msg
, tsock
->p
)))
1003 /* Capture message data (if valid) & compute return value (always) */
1006 buf_crs
= (unsigned char *)(TIPC_SKB_CB(buf
)->handle
);
1007 sz
= buf
->tail
- buf_crs
;
1009 needed
= (buf_len
- sz_copied
);
1010 sz_to_copy
= (sz
<= needed
) ? sz
: needed
;
1011 if (unlikely(copy_to_user(crs
, buf_crs
, sz_to_copy
))) {
1015 sz_copied
+= sz_to_copy
;
1017 if (sz_to_copy
< sz
) {
1018 if (!(flags
& MSG_PEEK
))
1019 TIPC_SKB_CB(buf
)->handle
= buf_crs
+ sz_to_copy
;
1026 goto exit
; /* can't add error msg to valid data */
1028 if ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
)
1034 /* Consume received message (optional) */
1036 if (likely(!(flags
& MSG_PEEK
))) {
1037 if (unlikely(++tsock
->p
->conn_unacked
>= TIPC_FLOW_CONTROL_WIN
))
1038 tipc_acknowledge(tsock
->p
->ref
, tsock
->p
->conn_unacked
);
1039 advance_queue(tsock
);
1042 /* Loop around if more data is required */
1044 if ((sz_copied
< buf_len
) /* didn't get all requested data */
1045 && (flags
& MSG_WAITALL
) /* ... and need to wait for more */
1046 && (!(flags
& MSG_PEEK
)) /* ... and aren't just peeking at data */
1047 && (!err
) /* ... and haven't reached a FIN */
1053 return res
? res
: sz_copied
;
1057 * queue_overloaded - test if queue overload condition exists
1058 * @queue_size: current size of queue
1059 * @base: nominal maximum size of queue
1060 * @msg: message to be added to queue
1062 * Returns 1 if queue is currently overloaded, 0 otherwise
1065 static int queue_overloaded(u32 queue_size
, u32 base
, struct tipc_msg
*msg
)
1068 u32 imp
= msg_importance(msg
);
1070 if (imp
== TIPC_LOW_IMPORTANCE
)
1072 else if (imp
== TIPC_MEDIUM_IMPORTANCE
)
1073 threshold
= base
* 2;
1074 else if (imp
== TIPC_HIGH_IMPORTANCE
)
1075 threshold
= base
* 100;
1079 if (msg_connected(msg
))
1082 return (queue_size
> threshold
);
1086 * async_disconnect - wrapper function used to disconnect port
1087 * @portref: TIPC port reference (passed as pointer-sized value)
1090 static void async_disconnect(unsigned long portref
)
1092 tipc_disconnect((u32
)portref
);
1096 * dispatch - handle arriving message
1097 * @tport: TIPC port that received message
1100 * Called with port locked. Must not take socket lock to avoid deadlock risk.
1102 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1105 static u32
dispatch(struct tipc_port
*tport
, struct sk_buff
*buf
)
1107 struct tipc_msg
*msg
= buf_msg(buf
);
1108 struct tipc_sock
*tsock
= (struct tipc_sock
*)tport
->usr_handle
;
1109 struct socket
*sock
;
1112 /* Reject message if socket is closing */
1115 return TIPC_ERR_NO_PORT
;
1117 /* Reject message if it is wrong sort of message for socket */
1120 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1121 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1122 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1124 sock
= tsock
->sk
.sk_socket
;
1125 if (sock
->state
== SS_READY
) {
1126 if (msg_connected(msg
)) {
1127 msg_dbg(msg
, "dispatch filter 1\n");
1128 return TIPC_ERR_NO_PORT
;
1131 if (msg_mcast(msg
)) {
1132 msg_dbg(msg
, "dispatch filter 2\n");
1133 return TIPC_ERR_NO_PORT
;
1135 if (sock
->state
== SS_CONNECTED
) {
1136 if (!msg_connected(msg
)) {
1137 msg_dbg(msg
, "dispatch filter 3\n");
1138 return TIPC_ERR_NO_PORT
;
1141 else if (sock
->state
== SS_CONNECTING
) {
1142 if (!msg_connected(msg
) && (msg_errcode(msg
) == 0)) {
1143 msg_dbg(msg
, "dispatch filter 4\n");
1144 return TIPC_ERR_NO_PORT
;
1147 else if (sock
->state
== SS_LISTENING
) {
1148 if (msg_connected(msg
) || msg_errcode(msg
)) {
1149 msg_dbg(msg
, "dispatch filter 5\n");
1150 return TIPC_ERR_NO_PORT
;
1153 else if (sock
->state
== SS_DISCONNECTING
) {
1154 msg_dbg(msg
, "dispatch filter 6\n");
1155 return TIPC_ERR_NO_PORT
;
1157 else /* (sock->state == SS_UNCONNECTED) */ {
1158 if (msg_connected(msg
) || msg_errcode(msg
)) {
1159 msg_dbg(msg
, "dispatch filter 7\n");
1160 return TIPC_ERR_NO_PORT
;
1165 /* Reject message if there isn't room to queue it */
1167 if (unlikely((u32
)atomic_read(&tipc_queue_size
) >
1168 OVERLOAD_LIMIT_BASE
)) {
1169 if (queue_overloaded(atomic_read(&tipc_queue_size
),
1170 OVERLOAD_LIMIT_BASE
, msg
))
1171 return TIPC_ERR_OVERLOAD
;
1173 recv_q_len
= skb_queue_len(&tsock
->sk
.sk_receive_queue
);
1174 if (unlikely(recv_q_len
> (OVERLOAD_LIMIT_BASE
/ 2))) {
1175 if (queue_overloaded(recv_q_len
,
1176 OVERLOAD_LIMIT_BASE
/ 2, msg
))
1177 return TIPC_ERR_OVERLOAD
;
1180 /* Initiate connection termination for an incoming 'FIN' */
1182 if (unlikely(msg_errcode(msg
) && (sock
->state
== SS_CONNECTED
))) {
1183 sock
->state
= SS_DISCONNECTING
;
1184 /* Note: Use signal since port lock is already taken! */
1185 k_signal((Handler
)async_disconnect
, tport
->ref
);
1188 /* Enqueue message (finally!) */
1190 msg_dbg(msg
,"<DISP<: ");
1191 TIPC_SKB_CB(buf
)->handle
= msg_data(msg
);
1192 atomic_inc(&tipc_queue_size
);
1193 skb_queue_tail(&sock
->sk
->sk_receive_queue
, buf
);
1195 wake_up_interruptible(sock
->sk
->sk_sleep
);
1200 * wakeupdispatch - wake up port after congestion
1201 * @tport: port to wakeup
1203 * Called with port lock on.
1206 static void wakeupdispatch(struct tipc_port
*tport
)
1208 struct tipc_sock
*tsock
= (struct tipc_sock
*)tport
->usr_handle
;
1210 wake_up_interruptible(tsock
->sk
.sk_sleep
);
1214 * connect - establish a connection to another TIPC port
1215 * @sock: socket structure
1216 * @dest: socket address for destination port
1217 * @destlen: size of socket address data structure
1220 * Returns 0 on success, errno otherwise
1223 static int connect(struct socket
*sock
, struct sockaddr
*dest
, int destlen
,
1226 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
1227 struct sockaddr_tipc
*dst
= (struct sockaddr_tipc
*)dest
;
1228 struct msghdr m
= {0,};
1229 struct sk_buff
*buf
;
1230 struct tipc_msg
*msg
;
1233 /* For now, TIPC does not allow use of connect() with DGRAM or RDM types */
1235 if (sock
->state
== SS_READY
)
1238 /* MOVE THE REST OF THIS ERROR CHECKING TO send_msg()? */
1239 if (sock
->state
== SS_LISTENING
)
1241 if (sock
->state
== SS_CONNECTING
)
1243 if (sock
->state
!= SS_UNCONNECTED
)
1246 if ((dst
->family
!= AF_TIPC
) ||
1247 ((dst
->addrtype
!= TIPC_ADDR_NAME
) && (dst
->addrtype
!= TIPC_ADDR_ID
)))
1250 /* Send a 'SYN-' to destination */
1253 if ((res
= send_msg(0, sock
, &m
, 0)) < 0) {
1254 sock
->state
= SS_DISCONNECTING
;
1258 if (down_interruptible(&tsock
->sem
))
1259 return -ERESTARTSYS
;
1261 /* Wait for destination's 'ACK' response */
1263 res
= wait_event_interruptible_timeout(*sock
->sk
->sk_sleep
,
1264 skb_queue_len(&sock
->sk
->sk_receive_queue
),
1265 sock
->sk
->sk_rcvtimeo
);
1266 buf
= skb_peek(&sock
->sk
->sk_receive_queue
);
1269 res
= auto_connect(sock
, tsock
, msg
);
1271 if (dst
->addrtype
== TIPC_ADDR_NAME
) {
1272 tsock
->p
->conn_type
= dst
->addr
.name
.name
.type
;
1273 tsock
->p
->conn_instance
= dst
->addr
.name
.name
.instance
;
1275 if (!msg_data_sz(msg
))
1276 advance_queue(tsock
);
1282 { /* leave "res" unchanged */ }
1283 sock
->state
= SS_DISCONNECTING
;
1291 * listen - allow socket to listen for incoming connections
1292 * @sock: socket structure
1295 * Returns 0 on success, errno otherwise
1298 static int listen(struct socket
*sock
, int len
)
1300 /* REQUIRES SOCKET LOCKING OF SOME SORT? */
1302 if (sock
->state
== SS_READY
)
1304 if (sock
->state
!= SS_UNCONNECTED
)
1306 sock
->state
= SS_LISTENING
;
1311 * accept - wait for connection request
1312 * @sock: listening socket
1313 * @newsock: new socket that is to be connected
1314 * @flags: file-related flags associated with socket
1316 * Returns 0 on success, errno otherwise
1319 static int accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
1321 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
1322 struct sk_buff
*buf
;
1325 if (sock
->state
== SS_READY
)
1327 if (sock
->state
!= SS_LISTENING
)
1330 if (unlikely((skb_queue_len(&sock
->sk
->sk_receive_queue
) == 0) &&
1331 (flags
& O_NONBLOCK
)))
1332 return -EWOULDBLOCK
;
1334 if (down_interruptible(&tsock
->sem
))
1335 return -ERESTARTSYS
;
1337 if (wait_event_interruptible(*sock
->sk
->sk_sleep
,
1338 skb_queue_len(&sock
->sk
->sk_receive_queue
))) {
1342 buf
= skb_peek(&sock
->sk
->sk_receive_queue
);
1344 res
= tipc_create(newsock
, 0);
1346 struct tipc_sock
*new_tsock
= tipc_sk(newsock
->sk
);
1347 struct tipc_portid id
;
1348 struct tipc_msg
*msg
= buf_msg(buf
);
1349 u32 new_ref
= new_tsock
->p
->ref
;
1351 id
.ref
= msg_origport(msg
);
1352 id
.node
= msg_orignode(msg
);
1353 tipc_connect2port(new_ref
, &id
);
1354 newsock
->state
= SS_CONNECTED
;
1356 tipc_set_portimportance(new_ref
, msg_importance(msg
));
1357 if (msg_named(msg
)) {
1358 new_tsock
->p
->conn_type
= msg_nametype(msg
);
1359 new_tsock
->p
->conn_instance
= msg_nameinst(msg
);
1363 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1364 * Respond to 'SYN+' by queuing it on new socket.
1367 msg_dbg(msg
,"<ACC<: ");
1368 if (!msg_data_sz(msg
)) {
1369 struct msghdr m
= {0,};
1371 send_packet(0, newsock
, &m
, 0);
1372 advance_queue(tsock
);
1375 skb_dequeue(&sock
->sk
->sk_receive_queue
);
1377 skb_queue_head(&newsock
->sk
->sk_receive_queue
, buf
);
1386 * shutdown - shutdown socket connection
1387 * @sock: socket structure
1388 * @how: direction to close (always treated as read + write)
1390 * Terminates connection (if necessary), then purges socket's receive queue.
1392 * Returns 0 on success, errno otherwise
1395 static int shutdown(struct socket
*sock
, int how
)
1397 struct tipc_sock
* tsock
= tipc_sk(sock
->sk
);
1398 struct sk_buff
*buf
;
1401 /* Could return -EINVAL for an invalid "how", but why bother? */
1403 if (down_interruptible(&tsock
->sem
))
1404 return -ERESTARTSYS
;
1408 switch (sock
->state
) {
1411 /* Send 'FIN+' or 'FIN-' message to peer */
1415 if ((buf
= skb_dequeue(&sock
->sk
->sk_receive_queue
))) {
1416 atomic_dec(&tipc_queue_size
);
1417 if (TIPC_SKB_CB(buf
)->handle
!= msg_data(buf_msg(buf
))) {
1421 tipc_reject_msg(buf
, TIPC_CONN_SHUTDOWN
);
1424 tipc_shutdown(tsock
->p
->ref
);
1430 case SS_DISCONNECTING
:
1432 /* Discard any unreceived messages */
1434 while ((buf
= skb_dequeue(&sock
->sk
->sk_receive_queue
))) {
1435 atomic_dec(&tipc_queue_size
);
1438 tsock
->p
->conn_unacked
= 0;
1443 sock
->state
= SS_DISCONNECTING
;
1458 * setsockopt - set socket option
1459 * @sock: socket structure
1460 * @lvl: option level
1461 * @opt: option identifier
1462 * @ov: pointer to new option value
1463 * @ol: length of option value
1465 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
1466 * (to ease compatibility).
1468 * Returns 0 on success, errno otherwise
1471 static int setsockopt(struct socket
*sock
, int lvl
, int opt
, char *ov
, int ol
)
1473 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
1477 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
1479 if (lvl
!= SOL_TIPC
)
1480 return -ENOPROTOOPT
;
1481 if (ol
< sizeof(value
))
1483 if ((res
= get_user(value
, (u32
*)ov
)))
1486 if (down_interruptible(&tsock
->sem
))
1487 return -ERESTARTSYS
;
1490 case TIPC_IMPORTANCE
:
1491 res
= tipc_set_portimportance(tsock
->p
->ref
, value
);
1493 case TIPC_SRC_DROPPABLE
:
1494 if (sock
->type
!= SOCK_STREAM
)
1495 res
= tipc_set_portunreliable(tsock
->p
->ref
, value
);
1499 case TIPC_DEST_DROPPABLE
:
1500 res
= tipc_set_portunreturnable(tsock
->p
->ref
, value
);
1502 case TIPC_CONN_TIMEOUT
:
1503 sock
->sk
->sk_rcvtimeo
= (value
* HZ
/ 1000);
1514 * getsockopt - get socket option
1515 * @sock: socket structure
1516 * @lvl: option level
1517 * @opt: option identifier
1518 * @ov: receptacle for option value
1519 * @ol: receptacle for length of option value
1521 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
1522 * (to ease compatibility).
1524 * Returns 0 on success, errno otherwise
1527 static int getsockopt(struct socket
*sock
, int lvl
, int opt
, char *ov
, int *ol
)
1529 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
1534 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
1535 return put_user(0, ol
);
1536 if (lvl
!= SOL_TIPC
)
1537 return -ENOPROTOOPT
;
1538 if ((res
= get_user(len
, ol
)))
1541 if (down_interruptible(&tsock
->sem
))
1542 return -ERESTARTSYS
;
1545 case TIPC_IMPORTANCE
:
1546 res
= tipc_portimportance(tsock
->p
->ref
, &value
);
1548 case TIPC_SRC_DROPPABLE
:
1549 res
= tipc_portunreliable(tsock
->p
->ref
, &value
);
1551 case TIPC_DEST_DROPPABLE
:
1552 res
= tipc_portunreturnable(tsock
->p
->ref
, &value
);
1554 case TIPC_CONN_TIMEOUT
:
1555 value
= (sock
->sk
->sk_rcvtimeo
* 1000) / HZ
;
1564 else if (len
< sizeof(value
)) {
1567 else if ((res
= copy_to_user(ov
, &value
, sizeof(value
)))) {
1568 /* couldn't return value */
1571 res
= put_user(sizeof(value
), ol
);
1579 * Placeholders for non-implemented functionality
1581 * Returns error code (POSIX-compliant where defined)
1584 static int ioctl(struct socket
*s
, u32 cmd
, unsigned long arg
)
1589 static int no_mmap(struct file
*file
, struct socket
*sock
,
1590 struct vm_area_struct
*vma
)
1594 static ssize_t
no_sendpage(struct socket
*sock
, struct page
*page
,
1595 int offset
, size_t size
, int flags
)
1600 static int no_skpair(struct socket
*s1
, struct socket
*s2
)
1606 * Protocol switches for the various types of TIPC sockets
1609 static struct proto_ops msg_ops
= {
1610 .owner
= THIS_MODULE
,
1615 .socketpair
= no_skpair
,
1617 .getname
= get_name
,
1621 .shutdown
= shutdown
,
1622 .setsockopt
= setsockopt
,
1623 .getsockopt
= getsockopt
,
1624 .sendmsg
= send_msg
,
1625 .recvmsg
= recv_msg
,
1627 .sendpage
= no_sendpage
1630 static struct proto_ops packet_ops
= {
1631 .owner
= THIS_MODULE
,
1636 .socketpair
= no_skpair
,
1638 .getname
= get_name
,
1642 .shutdown
= shutdown
,
1643 .setsockopt
= setsockopt
,
1644 .getsockopt
= getsockopt
,
1645 .sendmsg
= send_packet
,
1646 .recvmsg
= recv_msg
,
1648 .sendpage
= no_sendpage
1651 static struct proto_ops stream_ops
= {
1652 .owner
= THIS_MODULE
,
1657 .socketpair
= no_skpair
,
1659 .getname
= get_name
,
1663 .shutdown
= shutdown
,
1664 .setsockopt
= setsockopt
,
1665 .getsockopt
= getsockopt
,
1666 .sendmsg
= send_stream
,
1667 .recvmsg
= recv_stream
,
1669 .sendpage
= no_sendpage
1672 static struct net_proto_family tipc_family_ops
= {
1673 .owner
= THIS_MODULE
,
1675 .create
= tipc_create
1678 static struct proto tipc_proto
= {
1680 .owner
= THIS_MODULE
,
1681 .obj_size
= sizeof(struct tipc_sock
)
1685 * socket_init - initialize TIPC socket interface
1687 * Returns 0 on success, errno otherwise
1689 int socket_init(void)
1693 res
= proto_register(&tipc_proto
, 1);
1695 err("Unable to register TIPC protocol type\n");
1699 res
= sock_register(&tipc_family_ops
);
1701 err("Unable to register TIPC socket type\n");
1702 proto_unregister(&tipc_proto
);
1706 sockets_enabled
= 1;
1712 * sock_stop - stop TIPC socket interface
1714 void socket_stop(void)
1716 if (!sockets_enabled
)
1719 sockets_enabled
= 0;
1720 sock_unregister(tipc_family_ops
.family
);
1721 proto_unregister(&tipc_proto
);