2 * net/tipc/socket.c: TIPC socket API
4 * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
5 * Copyright (c) 2004-2008, 2010-2013, 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.
41 #include <linux/export.h>
43 #define SS_LISTENING -1 /* socket is listening */
44 #define SS_READY -2 /* socket is connectionless */
46 #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
48 static int tipc_backlog_rcv(struct sock
*sk
, struct sk_buff
*skb
);
49 static void tipc_data_ready(struct sock
*sk
);
50 static void tipc_write_space(struct sock
*sk
);
51 static int tipc_release(struct socket
*sock
);
52 static int tipc_accept(struct socket
*sock
, struct socket
*new_sock
, int flags
);
54 static const struct proto_ops packet_ops
;
55 static const struct proto_ops stream_ops
;
56 static const struct proto_ops msg_ops
;
58 static struct proto tipc_proto
;
59 static struct proto tipc_proto_kern
;
62 * Revised TIPC socket locking policy:
64 * Most socket operations take the standard socket lock when they start
65 * and hold it until they finish (or until they need to sleep). Acquiring
66 * this lock grants the owner exclusive access to the fields of the socket
67 * data structures, with the exception of the backlog queue. A few socket
68 * operations can be done without taking the socket lock because they only
69 * read socket information that never changes during the life of the socket.
71 * Socket operations may acquire the lock for the associated TIPC port if they
72 * need to perform an operation on the port. If any routine needs to acquire
73 * both the socket lock and the port lock it must take the socket lock first
74 * to avoid the risk of deadlock.
76 * The dispatcher handling incoming messages cannot grab the socket lock in
77 * the standard fashion, since invoked it runs at the BH level and cannot block.
78 * Instead, it checks to see if the socket lock is currently owned by someone,
79 * and either handles the message itself or adds it to the socket's backlog
80 * queue; in the latter case the queued message is processed once the process
81 * owning the socket lock releases it.
83 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
84 * the problem of a blocked socket operation preventing any other operations
85 * from occurring. However, applications must be careful if they have
86 * multiple threads trying to send (or receive) on the same socket, as these
87 * operations might interfere with each other. For example, doing a connect
88 * and a receive at the same time might allow the receive to consume the
89 * ACK message meant for the connect. While additional work could be done
90 * to try and overcome this, it doesn't seem to be worthwhile at the present.
92 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
93 * that another operation that must be performed in a non-blocking manner is
94 * not delayed for very long because the lock has already been taken.
96 * NOTE: This code assumes that certain fields of a port/socket pair are
97 * constant over its lifetime; such fields can be examined without taking
98 * the socket lock and/or port lock, and do not need to be re-read even
99 * after resuming processing after waiting. These fields include:
101 * - pointer to socket sk structure (aka tipc_sock structure)
102 * - pointer to port structure
109 * advance_rx_queue - discard first buffer in socket receive queue
111 * Caller must hold socket lock
113 static void advance_rx_queue(struct sock
*sk
)
115 kfree_skb(__skb_dequeue(&sk
->sk_receive_queue
));
119 * reject_rx_queue - reject all buffers in socket receive queue
121 * Caller must hold socket lock
123 static void reject_rx_queue(struct sock
*sk
)
127 while ((buf
= __skb_dequeue(&sk
->sk_receive_queue
)))
128 tipc_reject_msg(buf
, TIPC_ERR_NO_PORT
);
132 * tipc_sk_create - create a TIPC socket
133 * @net: network namespace (must be default network)
134 * @sock: pre-allocated socket structure
135 * @protocol: protocol indicator (must be 0)
136 * @kern: caused by kernel or by userspace?
138 * This routine creates additional data structures used by the TIPC socket,
139 * initializes them, and links them together.
141 * Returns 0 on success, errno otherwise
143 static int tipc_sk_create(struct net
*net
, struct socket
*sock
,
144 int protocol
, int kern
)
146 const struct proto_ops
*ops
;
149 struct tipc_sock
*tsk
;
150 struct tipc_port
*port
;
153 /* Validate arguments */
154 if (unlikely(protocol
!= 0))
155 return -EPROTONOSUPPORT
;
157 switch (sock
->type
) {
160 state
= SS_UNCONNECTED
;
164 state
= SS_UNCONNECTED
;
175 /* Allocate socket's protocol area */
177 sk
= sk_alloc(net
, AF_TIPC
, GFP_KERNEL
, &tipc_proto
);
179 sk
= sk_alloc(net
, AF_TIPC
, GFP_KERNEL
, &tipc_proto_kern
);
187 ref
= tipc_port_init(port
, TIPC_LOW_IMPORTANCE
);
189 pr_warn("Socket registration failed, ref. table exhausted\n");
194 /* Finish initializing socket data structures */
198 sock_init_data(sock
, sk
);
199 sk
->sk_backlog_rcv
= tipc_backlog_rcv
;
200 sk
->sk_rcvbuf
= sysctl_tipc_rmem
[1];
201 sk
->sk_data_ready
= tipc_data_ready
;
202 sk
->sk_write_space
= tipc_write_space
;
203 tsk
->conn_timeout
= CONN_TIMEOUT_DEFAULT
;
204 atomic_set(&tsk
->dupl_rcvcnt
, 0);
205 tipc_port_unlock(port
);
207 if (sock
->state
== SS_READY
) {
208 tipc_port_set_unreturnable(port
, true);
209 if (sock
->type
== SOCK_DGRAM
)
210 tipc_port_set_unreliable(port
, true);
216 * tipc_sock_create_local - create TIPC socket from inside TIPC module
217 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
219 * We cannot use sock_creat_kern here because it bumps module user count.
220 * Since socket owner and creator is the same module we must make sure
221 * that module count remains zero for module local sockets, otherwise
222 * we cannot do rmmod.
224 * Returns 0 on success, errno otherwise
226 int tipc_sock_create_local(int type
, struct socket
**res
)
230 rc
= sock_create_lite(AF_TIPC
, type
, 0, res
);
232 pr_err("Failed to create kernel socket\n");
235 tipc_sk_create(&init_net
, *res
, 0, 1);
241 * tipc_sock_release_local - release socket created by tipc_sock_create_local
242 * @sock: the socket to be released.
244 * Module reference count is not incremented when such sockets are created,
245 * so we must keep it from being decremented when they are released.
247 void tipc_sock_release_local(struct socket
*sock
)
255 * tipc_sock_accept_local - accept a connection on a socket created
256 * with tipc_sock_create_local. Use this function to avoid that
257 * module reference count is inadvertently incremented.
259 * @sock: the accepting socket
260 * @newsock: reference to the new socket to be created
261 * @flags: socket flags
264 int tipc_sock_accept_local(struct socket
*sock
, struct socket
**newsock
,
267 struct sock
*sk
= sock
->sk
;
270 ret
= sock_create_lite(sk
->sk_family
, sk
->sk_type
,
271 sk
->sk_protocol
, newsock
);
275 ret
= tipc_accept(sock
, *newsock
, flags
);
277 sock_release(*newsock
);
280 (*newsock
)->ops
= sock
->ops
;
285 * tipc_release - destroy a TIPC socket
286 * @sock: socket to destroy
288 * This routine cleans up any messages that are still queued on the socket.
289 * For DGRAM and RDM socket types, all queued messages are rejected.
290 * For SEQPACKET and STREAM socket types, the first message is rejected
291 * and any others are discarded. (If the first message on a STREAM socket
292 * is partially-read, it is discarded and the next one is rejected instead.)
294 * NOTE: Rejected messages are not necessarily returned to the sender! They
295 * are returned or discarded according to the "destination droppable" setting
296 * specified for the message by the sender.
298 * Returns 0 on success, errno otherwise
300 static int tipc_release(struct socket
*sock
)
302 struct sock
*sk
= sock
->sk
;
303 struct tipc_sock
*tsk
;
304 struct tipc_port
*port
;
308 * Exit if socket isn't fully initialized (occurs when a failed accept()
309 * releases a pre-allocated child socket that was never used)
319 * Reject all unreceived messages, except on an active connection
320 * (which disconnects locally & sends a 'FIN+' to peer)
322 while (sock
->state
!= SS_DISCONNECTING
) {
323 buf
= __skb_dequeue(&sk
->sk_receive_queue
);
326 if (TIPC_SKB_CB(buf
)->handle
!= NULL
)
329 if ((sock
->state
== SS_CONNECTING
) ||
330 (sock
->state
== SS_CONNECTED
)) {
331 sock
->state
= SS_DISCONNECTING
;
332 tipc_port_disconnect(port
->ref
);
334 tipc_reject_msg(buf
, TIPC_ERR_NO_PORT
);
338 /* Destroy TIPC port; also disconnects an active connection and
339 * sends a 'FIN-' to peer.
341 tipc_port_destroy(port
);
343 /* Discard any remaining (connection-based) messages in receive queue */
344 __skb_queue_purge(&sk
->sk_receive_queue
);
346 /* Reject any messages that accumulated in backlog queue */
347 sock
->state
= SS_DISCONNECTING
;
357 * tipc_bind - associate or disassocate TIPC name(s) with a socket
358 * @sock: socket structure
359 * @uaddr: socket address describing name(s) and desired operation
360 * @uaddr_len: size of socket address data structure
362 * Name and name sequence binding is indicated using a positive scope value;
363 * a negative scope value unbinds the specified name. Specifying no name
364 * (i.e. a socket address length of 0) unbinds all names from the socket.
366 * Returns 0 on success, errno otherwise
368 * NOTE: This routine doesn't need to take the socket lock since it doesn't
369 * access any non-constant socket information.
371 static int tipc_bind(struct socket
*sock
, struct sockaddr
*uaddr
,
374 struct sock
*sk
= sock
->sk
;
375 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
376 struct tipc_sock
*tsk
= tipc_sk(sk
);
380 if (unlikely(!uaddr_len
)) {
381 res
= tipc_withdraw(&tsk
->port
, 0, NULL
);
385 if (uaddr_len
< sizeof(struct sockaddr_tipc
)) {
389 if (addr
->family
!= AF_TIPC
) {
394 if (addr
->addrtype
== TIPC_ADDR_NAME
)
395 addr
->addr
.nameseq
.upper
= addr
->addr
.nameseq
.lower
;
396 else if (addr
->addrtype
!= TIPC_ADDR_NAMESEQ
) {
401 if ((addr
->addr
.nameseq
.type
< TIPC_RESERVED_TYPES
) &&
402 (addr
->addr
.nameseq
.type
!= TIPC_TOP_SRV
) &&
403 (addr
->addr
.nameseq
.type
!= TIPC_CFG_SRV
)) {
408 res
= (addr
->scope
> 0) ?
409 tipc_publish(&tsk
->port
, addr
->scope
, &addr
->addr
.nameseq
) :
410 tipc_withdraw(&tsk
->port
, -addr
->scope
, &addr
->addr
.nameseq
);
417 * tipc_getname - get port ID of socket or peer socket
418 * @sock: socket structure
419 * @uaddr: area for returned socket address
420 * @uaddr_len: area for returned length of socket address
421 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
423 * Returns 0 on success, errno otherwise
425 * NOTE: This routine doesn't need to take the socket lock since it only
426 * accesses socket information that is unchanging (or which changes in
427 * a completely predictable manner).
429 static int tipc_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
430 int *uaddr_len
, int peer
)
432 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
433 struct tipc_sock
*tsk
= tipc_sk(sock
->sk
);
435 memset(addr
, 0, sizeof(*addr
));
437 if ((sock
->state
!= SS_CONNECTED
) &&
438 ((peer
!= 2) || (sock
->state
!= SS_DISCONNECTING
)))
440 addr
->addr
.id
.ref
= tipc_port_peerport(&tsk
->port
);
441 addr
->addr
.id
.node
= tipc_port_peernode(&tsk
->port
);
443 addr
->addr
.id
.ref
= tsk
->port
.ref
;
444 addr
->addr
.id
.node
= tipc_own_addr
;
447 *uaddr_len
= sizeof(*addr
);
448 addr
->addrtype
= TIPC_ADDR_ID
;
449 addr
->family
= AF_TIPC
;
451 addr
->addr
.name
.domain
= 0;
457 * tipc_poll - read and possibly block on pollmask
458 * @file: file structure associated with the socket
459 * @sock: socket for which to calculate the poll bits
462 * Returns pollmask value
465 * It appears that the usual socket locking mechanisms are not useful here
466 * since the pollmask info is potentially out-of-date the moment this routine
467 * exits. TCP and other protocols seem to rely on higher level poll routines
468 * to handle any preventable race conditions, so TIPC will do the same ...
470 * TIPC sets the returned events as follows:
472 * socket state flags set
473 * ------------ ---------
474 * unconnected no read flags
475 * POLLOUT if port is not congested
477 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
480 * connected POLLIN/POLLRDNORM if data in rx queue
481 * POLLOUT if port is not congested
483 * disconnecting POLLIN/POLLRDNORM/POLLHUP
486 * listening POLLIN if SYN in rx queue
489 * ready POLLIN/POLLRDNORM if data in rx queue
490 * [connectionless] POLLOUT (since port cannot be congested)
492 * IMPORTANT: The fact that a read or write operation is indicated does NOT
493 * imply that the operation will succeed, merely that it should be performed
494 * and will not block.
496 static unsigned int tipc_poll(struct file
*file
, struct socket
*sock
,
499 struct sock
*sk
= sock
->sk
;
500 struct tipc_sock
*tsk
= tipc_sk(sk
);
503 sock_poll_wait(file
, sk_sleep(sk
), wait
);
505 switch ((int)sock
->state
) {
507 if (!tsk
->port
.congested
)
512 if (!tsk
->port
.congested
)
517 if (!skb_queue_empty(&sk
->sk_receive_queue
))
518 mask
|= (POLLIN
| POLLRDNORM
);
520 case SS_DISCONNECTING
:
521 mask
= (POLLIN
| POLLRDNORM
| POLLHUP
);
529 * dest_name_check - verify user is permitted to send to specified port name
530 * @dest: destination address
531 * @m: descriptor for message to be sent
533 * Prevents restricted configuration commands from being issued by
534 * unauthorized users.
536 * Returns 0 if permission is granted, otherwise errno
538 static int dest_name_check(struct sockaddr_tipc
*dest
, struct msghdr
*m
)
540 struct tipc_cfg_msg_hdr hdr
;
542 if (likely(dest
->addr
.name
.name
.type
>= TIPC_RESERVED_TYPES
))
544 if (likely(dest
->addr
.name
.name
.type
== TIPC_TOP_SRV
))
546 if (likely(dest
->addr
.name
.name
.type
!= TIPC_CFG_SRV
))
549 if (!m
->msg_iovlen
|| (m
->msg_iov
[0].iov_len
< sizeof(hdr
)))
551 if (copy_from_user(&hdr
, m
->msg_iov
[0].iov_base
, sizeof(hdr
)))
553 if ((ntohs(hdr
.tcm_type
) & 0xC000) && (!capable(CAP_NET_ADMIN
)))
559 static int tipc_wait_for_sndmsg(struct socket
*sock
, long *timeo_p
)
561 struct sock
*sk
= sock
->sk
;
562 struct tipc_sock
*tsk
= tipc_sk(sk
);
567 int err
= sock_error(sk
);
570 if (sock
->state
== SS_DISCONNECTING
)
574 if (signal_pending(current
))
575 return sock_intr_errno(*timeo_p
);
577 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
578 done
= sk_wait_event(sk
, timeo_p
, !tsk
->port
.congested
);
579 finish_wait(sk_sleep(sk
), &wait
);
586 * tipc_sendmsg - send message in connectionless manner
587 * @iocb: if NULL, indicates that socket lock is already held
588 * @sock: socket structure
589 * @m: message to send
590 * @total_len: length of message
592 * Message must have an destination specified explicitly.
593 * Used for SOCK_RDM and SOCK_DGRAM messages,
594 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
595 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
597 * Returns the number of bytes sent on success, or errno otherwise
599 static int tipc_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
600 struct msghdr
*m
, size_t total_len
)
602 struct sock
*sk
= sock
->sk
;
603 struct tipc_sock
*tsk
= tipc_sk(sk
);
604 struct tipc_port
*port
= &tsk
->port
;
605 DECLARE_SOCKADDR(struct sockaddr_tipc
*, dest
, m
->msg_name
);
611 return -EDESTADDRREQ
;
612 if (unlikely((m
->msg_namelen
< sizeof(*dest
)) ||
613 (dest
->family
!= AF_TIPC
)))
615 if (total_len
> TIPC_MAX_USER_MSG_SIZE
)
621 needs_conn
= (sock
->state
!= SS_READY
);
622 if (unlikely(needs_conn
)) {
623 if (sock
->state
== SS_LISTENING
) {
627 if (sock
->state
!= SS_UNCONNECTED
) {
631 if (tsk
->port
.published
) {
635 if (dest
->addrtype
== TIPC_ADDR_NAME
) {
636 tsk
->port
.conn_type
= dest
->addr
.name
.name
.type
;
637 tsk
->port
.conn_instance
= dest
->addr
.name
.name
.instance
;
640 /* Abort any pending connection attempts (very unlikely) */
644 timeo
= sock_sndtimeo(sk
, m
->msg_flags
& MSG_DONTWAIT
);
646 if (dest
->addrtype
== TIPC_ADDR_NAME
) {
647 res
= dest_name_check(dest
, m
);
650 res
= tipc_send2name(port
,
651 &dest
->addr
.name
.name
,
652 dest
->addr
.name
.domain
,
655 } else if (dest
->addrtype
== TIPC_ADDR_ID
) {
656 res
= tipc_send2port(port
,
660 } else if (dest
->addrtype
== TIPC_ADDR_MCAST
) {
665 res
= dest_name_check(dest
, m
);
668 res
= tipc_port_mcast_xmit(port
,
673 if (likely(res
!= -ELINKCONG
)) {
674 if (needs_conn
&& (res
>= 0))
675 sock
->state
= SS_CONNECTING
;
678 res
= tipc_wait_for_sndmsg(sock
, &timeo
);
689 static int tipc_wait_for_sndpkt(struct socket
*sock
, long *timeo_p
)
691 struct sock
*sk
= sock
->sk
;
692 struct tipc_sock
*tsk
= tipc_sk(sk
);
693 struct tipc_port
*port
= &tsk
->port
;
698 int err
= sock_error(sk
);
701 if (sock
->state
== SS_DISCONNECTING
)
703 else if (sock
->state
!= SS_CONNECTED
)
707 if (signal_pending(current
))
708 return sock_intr_errno(*timeo_p
);
710 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
711 done
= sk_wait_event(sk
, timeo_p
,
712 (!port
->congested
|| !port
->connected
));
713 finish_wait(sk_sleep(sk
), &wait
);
719 * tipc_send_packet - send a connection-oriented message
720 * @iocb: if NULL, indicates that socket lock is already held
721 * @sock: socket structure
722 * @m: message to send
723 * @total_len: length of message
725 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
727 * Returns the number of bytes sent on success, or errno otherwise
729 static int tipc_send_packet(struct kiocb
*iocb
, struct socket
*sock
,
730 struct msghdr
*m
, size_t total_len
)
732 struct sock
*sk
= sock
->sk
;
733 struct tipc_sock
*tsk
= tipc_sk(sk
);
734 DECLARE_SOCKADDR(struct sockaddr_tipc
*, dest
, m
->msg_name
);
738 /* Handle implied connection establishment */
740 return tipc_sendmsg(iocb
, sock
, m
, total_len
);
742 if (total_len
> TIPC_MAX_USER_MSG_SIZE
)
748 if (unlikely(sock
->state
!= SS_CONNECTED
)) {
749 if (sock
->state
== SS_DISCONNECTING
)
756 timeo
= sock_sndtimeo(sk
, m
->msg_flags
& MSG_DONTWAIT
);
758 res
= tipc_send(&tsk
->port
, m
->msg_iov
, total_len
);
759 if (likely(res
!= -ELINKCONG
))
761 res
= tipc_wait_for_sndpkt(sock
, &timeo
);
772 * tipc_send_stream - send stream-oriented data
774 * @sock: socket structure
776 * @total_len: total length of data to be sent
778 * Used for SOCK_STREAM data.
780 * Returns the number of bytes sent on success (or partial success),
781 * or errno if no data sent
783 static int tipc_send_stream(struct kiocb
*iocb
, struct socket
*sock
,
784 struct msghdr
*m
, size_t total_len
)
786 struct sock
*sk
= sock
->sk
;
787 struct tipc_sock
*tsk
= tipc_sk(sk
);
788 struct msghdr my_msg
;
790 struct iovec
*curr_iov
;
792 char __user
*curr_start
;
801 /* Handle special cases where there is no connection */
802 if (unlikely(sock
->state
!= SS_CONNECTED
)) {
803 if (sock
->state
== SS_UNCONNECTED
)
804 res
= tipc_send_packet(NULL
, sock
, m
, total_len
);
806 res
= sock
->state
== SS_DISCONNECTING
? -EPIPE
: -ENOTCONN
;
810 if (unlikely(m
->msg_name
)) {
815 if (total_len
> (unsigned int)INT_MAX
) {
821 * Send each iovec entry using one or more messages
823 * Note: This algorithm is good for the most likely case
824 * (i.e. one large iovec entry), but could be improved to pass sets
825 * of small iovec entries into send_packet().
827 curr_iov
= m
->msg_iov
;
828 curr_iovlen
= m
->msg_iovlen
;
829 my_msg
.msg_iov
= &my_iov
;
830 my_msg
.msg_iovlen
= 1;
831 my_msg
.msg_flags
= m
->msg_flags
;
832 my_msg
.msg_name
= NULL
;
835 hdr_size
= msg_hdr_sz(&tsk
->port
.phdr
);
837 while (curr_iovlen
--) {
838 curr_start
= curr_iov
->iov_base
;
839 curr_left
= curr_iov
->iov_len
;
842 bytes_to_send
= tsk
->port
.max_pkt
- hdr_size
;
843 if (bytes_to_send
> TIPC_MAX_USER_MSG_SIZE
)
844 bytes_to_send
= TIPC_MAX_USER_MSG_SIZE
;
845 if (curr_left
< bytes_to_send
)
846 bytes_to_send
= curr_left
;
847 my_iov
.iov_base
= curr_start
;
848 my_iov
.iov_len
= bytes_to_send
;
849 res
= tipc_send_packet(NULL
, sock
, &my_msg
,
856 curr_left
-= bytes_to_send
;
857 curr_start
+= bytes_to_send
;
858 bytes_sent
+= bytes_to_send
;
870 * auto_connect - complete connection setup to a remote port
871 * @tsk: tipc socket structure
872 * @msg: peer's response message
874 * Returns 0 on success, errno otherwise
876 static int auto_connect(struct tipc_sock
*tsk
, struct tipc_msg
*msg
)
878 struct tipc_port
*port
= &tsk
->port
;
879 struct socket
*sock
= tsk
->sk
.sk_socket
;
880 struct tipc_portid peer
;
882 peer
.ref
= msg_origport(msg
);
883 peer
.node
= msg_orignode(msg
);
885 __tipc_port_connect(port
->ref
, port
, &peer
);
887 if (msg_importance(msg
) > TIPC_CRITICAL_IMPORTANCE
)
889 msg_set_importance(&port
->phdr
, (u32
)msg_importance(msg
));
890 sock
->state
= SS_CONNECTED
;
895 * set_orig_addr - capture sender's address for received message
896 * @m: descriptor for message info
897 * @msg: received message header
899 * Note: Address is not captured if not requested by receiver.
901 static void set_orig_addr(struct msghdr
*m
, struct tipc_msg
*msg
)
903 DECLARE_SOCKADDR(struct sockaddr_tipc
*, addr
, m
->msg_name
);
906 addr
->family
= AF_TIPC
;
907 addr
->addrtype
= TIPC_ADDR_ID
;
908 memset(&addr
->addr
, 0, sizeof(addr
->addr
));
909 addr
->addr
.id
.ref
= msg_origport(msg
);
910 addr
->addr
.id
.node
= msg_orignode(msg
);
911 addr
->addr
.name
.domain
= 0; /* could leave uninitialized */
912 addr
->scope
= 0; /* could leave uninitialized */
913 m
->msg_namelen
= sizeof(struct sockaddr_tipc
);
918 * anc_data_recv - optionally capture ancillary data for received message
919 * @m: descriptor for message info
920 * @msg: received message header
921 * @tport: TIPC port associated with message
923 * Note: Ancillary data is not captured if not requested by receiver.
925 * Returns 0 if successful, otherwise errno
927 static int anc_data_recv(struct msghdr
*m
, struct tipc_msg
*msg
,
928 struct tipc_port
*tport
)
936 if (likely(m
->msg_controllen
== 0))
939 /* Optionally capture errored message object(s) */
940 err
= msg
? msg_errcode(msg
) : 0;
943 anc_data
[1] = msg_data_sz(msg
);
944 res
= put_cmsg(m
, SOL_TIPC
, TIPC_ERRINFO
, 8, anc_data
);
948 res
= put_cmsg(m
, SOL_TIPC
, TIPC_RETDATA
, anc_data
[1],
955 /* Optionally capture message destination object */
956 dest_type
= msg
? msg_type(msg
) : TIPC_DIRECT_MSG
;
960 anc_data
[0] = msg_nametype(msg
);
961 anc_data
[1] = msg_namelower(msg
);
962 anc_data
[2] = msg_namelower(msg
);
966 anc_data
[0] = msg_nametype(msg
);
967 anc_data
[1] = msg_namelower(msg
);
968 anc_data
[2] = msg_nameupper(msg
);
971 has_name
= (tport
->conn_type
!= 0);
972 anc_data
[0] = tport
->conn_type
;
973 anc_data
[1] = tport
->conn_instance
;
974 anc_data
[2] = tport
->conn_instance
;
980 res
= put_cmsg(m
, SOL_TIPC
, TIPC_DESTNAME
, 12, anc_data
);
988 static int tipc_wait_for_rcvmsg(struct socket
*sock
, long *timeop
)
990 struct sock
*sk
= sock
->sk
;
992 long timeo
= *timeop
;
996 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
997 if (timeo
&& skb_queue_empty(&sk
->sk_receive_queue
)) {
998 if (sock
->state
== SS_DISCONNECTING
) {
1003 timeo
= schedule_timeout(timeo
);
1007 if (!skb_queue_empty(&sk
->sk_receive_queue
))
1009 err
= sock_intr_errno(timeo
);
1010 if (signal_pending(current
))
1016 finish_wait(sk_sleep(sk
), &wait
);
1022 * tipc_recvmsg - receive packet-oriented message
1024 * @m: descriptor for message info
1025 * @buf_len: total size of user buffer area
1026 * @flags: receive flags
1028 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1029 * If the complete message doesn't fit in user area, truncate it.
1031 * Returns size of returned message data, errno otherwise
1033 static int tipc_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1034 struct msghdr
*m
, size_t buf_len
, int flags
)
1036 struct sock
*sk
= sock
->sk
;
1037 struct tipc_sock
*tsk
= tipc_sk(sk
);
1038 struct tipc_port
*port
= &tsk
->port
;
1039 struct sk_buff
*buf
;
1040 struct tipc_msg
*msg
;
1046 /* Catch invalid receive requests */
1047 if (unlikely(!buf_len
))
1052 if (unlikely(sock
->state
== SS_UNCONNECTED
)) {
1057 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
1060 /* Look for a message in receive queue; wait if necessary */
1061 res
= tipc_wait_for_rcvmsg(sock
, &timeo
);
1065 /* Look at first message in receive queue */
1066 buf
= skb_peek(&sk
->sk_receive_queue
);
1068 sz
= msg_data_sz(msg
);
1069 err
= msg_errcode(msg
);
1071 /* Discard an empty non-errored message & try again */
1072 if ((!sz
) && (!err
)) {
1073 advance_rx_queue(sk
);
1077 /* Capture sender's address (optional) */
1078 set_orig_addr(m
, msg
);
1080 /* Capture ancillary data (optional) */
1081 res
= anc_data_recv(m
, msg
, port
);
1085 /* Capture message data (if valid) & compute return value (always) */
1087 if (unlikely(buf_len
< sz
)) {
1089 m
->msg_flags
|= MSG_TRUNC
;
1091 res
= skb_copy_datagram_iovec(buf
, msg_hdr_sz(msg
),
1097 if ((sock
->state
== SS_READY
) ||
1098 ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
))
1104 /* Consume received message (optional) */
1105 if (likely(!(flags
& MSG_PEEK
))) {
1106 if ((sock
->state
!= SS_READY
) &&
1107 (++port
->conn_unacked
>= TIPC_CONNACK_INTV
))
1108 tipc_acknowledge(port
->ref
, port
->conn_unacked
);
1109 advance_rx_queue(sk
);
1117 * tipc_recv_stream - receive stream-oriented data
1119 * @m: descriptor for message info
1120 * @buf_len: total size of user buffer area
1121 * @flags: receive flags
1123 * Used for SOCK_STREAM messages only. If not enough data is available
1124 * will optionally wait for more; never truncates data.
1126 * Returns size of returned message data, errno otherwise
1128 static int tipc_recv_stream(struct kiocb
*iocb
, struct socket
*sock
,
1129 struct msghdr
*m
, size_t buf_len
, int flags
)
1131 struct sock
*sk
= sock
->sk
;
1132 struct tipc_sock
*tsk
= tipc_sk(sk
);
1133 struct tipc_port
*port
= &tsk
->port
;
1134 struct sk_buff
*buf
;
1135 struct tipc_msg
*msg
;
1138 int sz_to_copy
, target
, needed
;
1143 /* Catch invalid receive attempts */
1144 if (unlikely(!buf_len
))
1149 if (unlikely(sock
->state
== SS_UNCONNECTED
)) {
1154 target
= sock_rcvlowat(sk
, flags
& MSG_WAITALL
, buf_len
);
1155 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
1158 /* Look for a message in receive queue; wait if necessary */
1159 res
= tipc_wait_for_rcvmsg(sock
, &timeo
);
1163 /* Look at first message in receive queue */
1164 buf
= skb_peek(&sk
->sk_receive_queue
);
1166 sz
= msg_data_sz(msg
);
1167 err
= msg_errcode(msg
);
1169 /* Discard an empty non-errored message & try again */
1170 if ((!sz
) && (!err
)) {
1171 advance_rx_queue(sk
);
1175 /* Optionally capture sender's address & ancillary data of first msg */
1176 if (sz_copied
== 0) {
1177 set_orig_addr(m
, msg
);
1178 res
= anc_data_recv(m
, msg
, port
);
1183 /* Capture message data (if valid) & compute return value (always) */
1185 u32 offset
= (u32
)(unsigned long)(TIPC_SKB_CB(buf
)->handle
);
1188 needed
= (buf_len
- sz_copied
);
1189 sz_to_copy
= (sz
<= needed
) ? sz
: needed
;
1191 res
= skb_copy_datagram_iovec(buf
, msg_hdr_sz(msg
) + offset
,
1192 m
->msg_iov
, sz_to_copy
);
1196 sz_copied
+= sz_to_copy
;
1198 if (sz_to_copy
< sz
) {
1199 if (!(flags
& MSG_PEEK
))
1200 TIPC_SKB_CB(buf
)->handle
=
1201 (void *)(unsigned long)(offset
+ sz_to_copy
);
1206 goto exit
; /* can't add error msg to valid data */
1208 if ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
)
1214 /* Consume received message (optional) */
1215 if (likely(!(flags
& MSG_PEEK
))) {
1216 if (unlikely(++port
->conn_unacked
>= TIPC_CONNACK_INTV
))
1217 tipc_acknowledge(port
->ref
, port
->conn_unacked
);
1218 advance_rx_queue(sk
);
1221 /* Loop around if more data is required */
1222 if ((sz_copied
< buf_len
) && /* didn't get all requested data */
1223 (!skb_queue_empty(&sk
->sk_receive_queue
) ||
1224 (sz_copied
< target
)) && /* and more is ready or required */
1225 (!(flags
& MSG_PEEK
)) && /* and aren't just peeking at data */
1226 (!err
)) /* and haven't reached a FIN */
1231 return sz_copied
? sz_copied
: res
;
1235 * tipc_write_space - wake up thread if port congestion is released
1238 static void tipc_write_space(struct sock
*sk
)
1240 struct socket_wq
*wq
;
1243 wq
= rcu_dereference(sk
->sk_wq
);
1244 if (wq_has_sleeper(wq
))
1245 wake_up_interruptible_sync_poll(&wq
->wait
, POLLOUT
|
1246 POLLWRNORM
| POLLWRBAND
);
1251 * tipc_data_ready - wake up threads to indicate messages have been received
1253 * @len: the length of messages
1255 static void tipc_data_ready(struct sock
*sk
)
1257 struct socket_wq
*wq
;
1260 wq
= rcu_dereference(sk
->sk_wq
);
1261 if (wq_has_sleeper(wq
))
1262 wake_up_interruptible_sync_poll(&wq
->wait
, POLLIN
|
1263 POLLRDNORM
| POLLRDBAND
);
1268 * filter_connect - Handle all incoming messages for a connection-based socket
1272 * Returns TIPC error status code and socket error status code
1273 * once it encounters some errors
1275 static u32
filter_connect(struct tipc_sock
*tsk
, struct sk_buff
**buf
)
1277 struct sock
*sk
= &tsk
->sk
;
1278 struct tipc_port
*port
= &tsk
->port
;
1279 struct socket
*sock
= sk
->sk_socket
;
1280 struct tipc_msg
*msg
= buf_msg(*buf
);
1282 u32 retval
= TIPC_ERR_NO_PORT
;
1288 switch ((int)sock
->state
) {
1290 /* Accept only connection-based messages sent by peer */
1291 if (msg_connected(msg
) && tipc_port_peer_msg(port
, msg
)) {
1292 if (unlikely(msg_errcode(msg
))) {
1293 sock
->state
= SS_DISCONNECTING
;
1294 __tipc_port_disconnect(port
);
1300 /* Accept only ACK or NACK message */
1301 if (unlikely(msg_errcode(msg
))) {
1302 sock
->state
= SS_DISCONNECTING
;
1303 sk
->sk_err
= ECONNREFUSED
;
1308 if (unlikely(!msg_connected(msg
)))
1311 res
= auto_connect(tsk
, msg
);
1313 sock
->state
= SS_DISCONNECTING
;
1319 /* If an incoming message is an 'ACK-', it should be
1320 * discarded here because it doesn't contain useful
1321 * data. In addition, we should try to wake up
1322 * connect() routine if sleeping.
1324 if (msg_data_sz(msg
) == 0) {
1327 if (waitqueue_active(sk_sleep(sk
)))
1328 wake_up_interruptible(sk_sleep(sk
));
1333 case SS_UNCONNECTED
:
1334 /* Accept only SYN message */
1335 if (!msg_connected(msg
) && !(msg_errcode(msg
)))
1338 case SS_DISCONNECTING
:
1341 pr_err("Unknown socket state %u\n", sock
->state
);
1347 * rcvbuf_limit - get proper overload limit of socket receive queue
1351 * For all connection oriented messages, irrespective of importance,
1352 * the default overload value (i.e. 67MB) is set as limit.
1354 * For all connectionless messages, by default new queue limits are
1357 * TIPC_LOW_IMPORTANCE (4 MB)
1358 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1359 * TIPC_HIGH_IMPORTANCE (16 MB)
1360 * TIPC_CRITICAL_IMPORTANCE (32 MB)
1362 * Returns overload limit according to corresponding message importance
1364 static unsigned int rcvbuf_limit(struct sock
*sk
, struct sk_buff
*buf
)
1366 struct tipc_msg
*msg
= buf_msg(buf
);
1368 if (msg_connected(msg
))
1369 return sysctl_tipc_rmem
[2];
1371 return sk
->sk_rcvbuf
>> TIPC_CRITICAL_IMPORTANCE
<<
1372 msg_importance(msg
);
1376 * filter_rcv - validate incoming message
1380 * Enqueues message on receive queue if acceptable; optionally handles
1381 * disconnect indication for a connected socket.
1383 * Called with socket lock already taken; port lock may also be taken.
1385 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1387 static u32
filter_rcv(struct sock
*sk
, struct sk_buff
*buf
)
1389 struct socket
*sock
= sk
->sk_socket
;
1390 struct tipc_sock
*tsk
= tipc_sk(sk
);
1391 struct tipc_msg
*msg
= buf_msg(buf
);
1392 unsigned int limit
= rcvbuf_limit(sk
, buf
);
1395 /* Reject message if it is wrong sort of message for socket */
1396 if (msg_type(msg
) > TIPC_DIRECT_MSG
)
1397 return TIPC_ERR_NO_PORT
;
1399 if (sock
->state
== SS_READY
) {
1400 if (msg_connected(msg
))
1401 return TIPC_ERR_NO_PORT
;
1403 res
= filter_connect(tsk
, &buf
);
1404 if (res
!= TIPC_OK
|| buf
== NULL
)
1408 /* Reject message if there isn't room to queue it */
1409 if (sk_rmem_alloc_get(sk
) + buf
->truesize
>= limit
)
1410 return TIPC_ERR_OVERLOAD
;
1412 /* Enqueue message */
1413 TIPC_SKB_CB(buf
)->handle
= NULL
;
1414 __skb_queue_tail(&sk
->sk_receive_queue
, buf
);
1415 skb_set_owner_r(buf
, sk
);
1417 sk
->sk_data_ready(sk
);
1422 * tipc_backlog_rcv - handle incoming message from backlog queue
1426 * Caller must hold socket lock, but not port lock.
1430 static int tipc_backlog_rcv(struct sock
*sk
, struct sk_buff
*buf
)
1433 struct tipc_sock
*tsk
= tipc_sk(sk
);
1434 uint truesize
= buf
->truesize
;
1436 res
= filter_rcv(sk
, buf
);
1438 tipc_reject_msg(buf
, res
);
1440 if (atomic_read(&tsk
->dupl_rcvcnt
) < TIPC_CONN_OVERLOAD_LIMIT
)
1441 atomic_add(truesize
, &tsk
->dupl_rcvcnt
);
1447 * tipc_sk_rcv - handle incoming message
1448 * @buf: buffer containing arriving message
1450 * Returns 0 if success, or errno: -EHOSTUNREACH
1452 int tipc_sk_rcv(struct sk_buff
*buf
)
1454 struct tipc_sock
*tsk
;
1455 struct tipc_port
*port
;
1457 u32 dport
= msg_destport(buf_msg(buf
));
1461 /* Forward unresolved named message */
1462 if (unlikely(!dport
)) {
1463 tipc_net_route_msg(buf
);
1467 /* Validate destination */
1468 port
= tipc_port_lock(dport
);
1469 if (unlikely(!port
)) {
1470 err
= TIPC_ERR_NO_PORT
;
1474 tsk
= tipc_port_to_sock(port
);
1480 if (!sock_owned_by_user(sk
)) {
1481 err
= filter_rcv(sk
, buf
);
1483 if (sk
->sk_backlog
.len
== 0)
1484 atomic_set(&tsk
->dupl_rcvcnt
, 0);
1485 limit
= rcvbuf_limit(sk
, buf
) + atomic_read(&tsk
->dupl_rcvcnt
);
1486 if (sk_add_backlog(sk
, buf
, limit
))
1487 err
= TIPC_ERR_OVERLOAD
;
1491 tipc_port_unlock(port
);
1496 tipc_reject_msg(buf
, err
);
1497 return -EHOSTUNREACH
;
1500 static int tipc_wait_for_connect(struct socket
*sock
, long *timeo_p
)
1502 struct sock
*sk
= sock
->sk
;
1507 int err
= sock_error(sk
);
1512 if (signal_pending(current
))
1513 return sock_intr_errno(*timeo_p
);
1515 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
1516 done
= sk_wait_event(sk
, timeo_p
, sock
->state
!= SS_CONNECTING
);
1517 finish_wait(sk_sleep(sk
), &wait
);
1523 * tipc_connect - establish a connection to another TIPC port
1524 * @sock: socket structure
1525 * @dest: socket address for destination port
1526 * @destlen: size of socket address data structure
1527 * @flags: file-related flags associated with socket
1529 * Returns 0 on success, errno otherwise
1531 static int tipc_connect(struct socket
*sock
, struct sockaddr
*dest
,
1532 int destlen
, int flags
)
1534 struct sock
*sk
= sock
->sk
;
1535 struct sockaddr_tipc
*dst
= (struct sockaddr_tipc
*)dest
;
1536 struct msghdr m
= {NULL
,};
1537 long timeout
= (flags
& O_NONBLOCK
) ? 0 : tipc_sk(sk
)->conn_timeout
;
1538 socket_state previous
;
1543 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
1544 if (sock
->state
== SS_READY
) {
1550 * Reject connection attempt using multicast address
1552 * Note: send_msg() validates the rest of the address fields,
1553 * so there's no need to do it here
1555 if (dst
->addrtype
== TIPC_ADDR_MCAST
) {
1560 previous
= sock
->state
;
1561 switch (sock
->state
) {
1562 case SS_UNCONNECTED
:
1563 /* Send a 'SYN-' to destination */
1565 m
.msg_namelen
= destlen
;
1567 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1568 * indicate send_msg() is never blocked.
1571 m
.msg_flags
= MSG_DONTWAIT
;
1573 res
= tipc_sendmsg(NULL
, sock
, &m
, 0);
1574 if ((res
< 0) && (res
!= -EWOULDBLOCK
))
1577 /* Just entered SS_CONNECTING state; the only
1578 * difference is that return value in non-blocking
1579 * case is EINPROGRESS, rather than EALREADY.
1583 if (previous
== SS_CONNECTING
)
1587 timeout
= msecs_to_jiffies(timeout
);
1588 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1589 res
= tipc_wait_for_connect(sock
, &timeout
);
1604 * tipc_listen - allow socket to listen for incoming connections
1605 * @sock: socket structure
1608 * Returns 0 on success, errno otherwise
1610 static int tipc_listen(struct socket
*sock
, int len
)
1612 struct sock
*sk
= sock
->sk
;
1617 if (sock
->state
!= SS_UNCONNECTED
)
1620 sock
->state
= SS_LISTENING
;
1628 static int tipc_wait_for_accept(struct socket
*sock
, long timeo
)
1630 struct sock
*sk
= sock
->sk
;
1634 /* True wake-one mechanism for incoming connections: only
1635 * one process gets woken up, not the 'whole herd'.
1636 * Since we do not 'race & poll' for established sockets
1637 * anymore, the common case will execute the loop only once.
1640 prepare_to_wait_exclusive(sk_sleep(sk
), &wait
,
1641 TASK_INTERRUPTIBLE
);
1642 if (timeo
&& skb_queue_empty(&sk
->sk_receive_queue
)) {
1644 timeo
= schedule_timeout(timeo
);
1648 if (!skb_queue_empty(&sk
->sk_receive_queue
))
1651 if (sock
->state
!= SS_LISTENING
)
1653 err
= sock_intr_errno(timeo
);
1654 if (signal_pending(current
))
1660 finish_wait(sk_sleep(sk
), &wait
);
1665 * tipc_accept - wait for connection request
1666 * @sock: listening socket
1667 * @newsock: new socket that is to be connected
1668 * @flags: file-related flags associated with socket
1670 * Returns 0 on success, errno otherwise
1672 static int tipc_accept(struct socket
*sock
, struct socket
*new_sock
, int flags
)
1674 struct sock
*new_sk
, *sk
= sock
->sk
;
1675 struct sk_buff
*buf
;
1676 struct tipc_port
*new_port
;
1677 struct tipc_msg
*msg
;
1678 struct tipc_portid peer
;
1685 if (sock
->state
!= SS_LISTENING
) {
1689 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
1690 res
= tipc_wait_for_accept(sock
, timeo
);
1694 buf
= skb_peek(&sk
->sk_receive_queue
);
1696 res
= tipc_sk_create(sock_net(sock
->sk
), new_sock
, 0, 1);
1699 security_sk_clone(sock
->sk
, new_sock
->sk
);
1701 new_sk
= new_sock
->sk
;
1702 new_port
= &tipc_sk(new_sk
)->port
;
1703 new_ref
= new_port
->ref
;
1706 /* we lock on new_sk; but lockdep sees the lock on sk */
1707 lock_sock_nested(new_sk
, SINGLE_DEPTH_NESTING
);
1710 * Reject any stray messages received by new socket
1711 * before the socket lock was taken (very, very unlikely)
1713 reject_rx_queue(new_sk
);
1715 /* Connect new socket to it's peer */
1716 peer
.ref
= msg_origport(msg
);
1717 peer
.node
= msg_orignode(msg
);
1718 tipc_port_connect(new_ref
, &peer
);
1719 new_sock
->state
= SS_CONNECTED
;
1721 tipc_port_set_importance(new_port
, msg_importance(msg
));
1722 if (msg_named(msg
)) {
1723 new_port
->conn_type
= msg_nametype(msg
);
1724 new_port
->conn_instance
= msg_nameinst(msg
);
1728 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1729 * Respond to 'SYN+' by queuing it on new socket.
1731 if (!msg_data_sz(msg
)) {
1732 struct msghdr m
= {NULL
,};
1734 advance_rx_queue(sk
);
1735 tipc_send_packet(NULL
, new_sock
, &m
, 0);
1737 __skb_dequeue(&sk
->sk_receive_queue
);
1738 __skb_queue_head(&new_sk
->sk_receive_queue
, buf
);
1739 skb_set_owner_r(buf
, new_sk
);
1741 release_sock(new_sk
);
1748 * tipc_shutdown - shutdown socket connection
1749 * @sock: socket structure
1750 * @how: direction to close (must be SHUT_RDWR)
1752 * Terminates connection (if necessary), then purges socket's receive queue.
1754 * Returns 0 on success, errno otherwise
1756 static int tipc_shutdown(struct socket
*sock
, int how
)
1758 struct sock
*sk
= sock
->sk
;
1759 struct tipc_sock
*tsk
= tipc_sk(sk
);
1760 struct tipc_port
*port
= &tsk
->port
;
1761 struct sk_buff
*buf
;
1764 if (how
!= SHUT_RDWR
)
1769 switch (sock
->state
) {
1774 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
1775 buf
= __skb_dequeue(&sk
->sk_receive_queue
);
1777 if (TIPC_SKB_CB(buf
)->handle
!= NULL
) {
1781 tipc_port_disconnect(port
->ref
);
1782 tipc_reject_msg(buf
, TIPC_CONN_SHUTDOWN
);
1784 tipc_port_shutdown(port
->ref
);
1787 sock
->state
= SS_DISCONNECTING
;
1791 case SS_DISCONNECTING
:
1793 /* Discard any unreceived messages */
1794 __skb_queue_purge(&sk
->sk_receive_queue
);
1796 /* Wake up anyone sleeping in poll */
1797 sk
->sk_state_change(sk
);
1810 * tipc_setsockopt - set socket option
1811 * @sock: socket structure
1812 * @lvl: option level
1813 * @opt: option identifier
1814 * @ov: pointer to new option value
1815 * @ol: length of option value
1817 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
1818 * (to ease compatibility).
1820 * Returns 0 on success, errno otherwise
1822 static int tipc_setsockopt(struct socket
*sock
, int lvl
, int opt
,
1823 char __user
*ov
, unsigned int ol
)
1825 struct sock
*sk
= sock
->sk
;
1826 struct tipc_sock
*tsk
= tipc_sk(sk
);
1827 struct tipc_port
*port
= &tsk
->port
;
1831 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
1833 if (lvl
!= SOL_TIPC
)
1834 return -ENOPROTOOPT
;
1835 if (ol
< sizeof(value
))
1837 res
= get_user(value
, (u32 __user
*)ov
);
1844 case TIPC_IMPORTANCE
:
1845 res
= tipc_port_set_importance(port
, value
);
1847 case TIPC_SRC_DROPPABLE
:
1848 if (sock
->type
!= SOCK_STREAM
)
1849 tipc_port_set_unreliable(port
, value
);
1853 case TIPC_DEST_DROPPABLE
:
1854 tipc_port_set_unreturnable(port
, value
);
1856 case TIPC_CONN_TIMEOUT
:
1857 tipc_sk(sk
)->conn_timeout
= value
;
1858 /* no need to set "res", since already 0 at this point */
1870 * tipc_getsockopt - get socket option
1871 * @sock: socket structure
1872 * @lvl: option level
1873 * @opt: option identifier
1874 * @ov: receptacle for option value
1875 * @ol: receptacle for length of option value
1877 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
1878 * (to ease compatibility).
1880 * Returns 0 on success, errno otherwise
1882 static int tipc_getsockopt(struct socket
*sock
, int lvl
, int opt
,
1883 char __user
*ov
, int __user
*ol
)
1885 struct sock
*sk
= sock
->sk
;
1886 struct tipc_sock
*tsk
= tipc_sk(sk
);
1887 struct tipc_port
*port
= &tsk
->port
;
1892 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
1893 return put_user(0, ol
);
1894 if (lvl
!= SOL_TIPC
)
1895 return -ENOPROTOOPT
;
1896 res
= get_user(len
, ol
);
1903 case TIPC_IMPORTANCE
:
1904 value
= tipc_port_importance(port
);
1906 case TIPC_SRC_DROPPABLE
:
1907 value
= tipc_port_unreliable(port
);
1909 case TIPC_DEST_DROPPABLE
:
1910 value
= tipc_port_unreturnable(port
);
1912 case TIPC_CONN_TIMEOUT
:
1913 value
= tipc_sk(sk
)->conn_timeout
;
1914 /* no need to set "res", since already 0 at this point */
1916 case TIPC_NODE_RECVQ_DEPTH
:
1917 value
= 0; /* was tipc_queue_size, now obsolete */
1919 case TIPC_SOCK_RECVQ_DEPTH
:
1920 value
= skb_queue_len(&sk
->sk_receive_queue
);
1929 return res
; /* "get" failed */
1931 if (len
< sizeof(value
))
1934 if (copy_to_user(ov
, &value
, sizeof(value
)))
1937 return put_user(sizeof(value
), ol
);
1940 int tipc_ioctl(struct socket
*sk
, unsigned int cmd
, unsigned long arg
)
1942 struct tipc_sioc_ln_req lnr
;
1943 void __user
*argp
= (void __user
*)arg
;
1946 case SIOCGETLINKNAME
:
1947 if (copy_from_user(&lnr
, argp
, sizeof(lnr
)))
1949 if (!tipc_node_get_linkname(lnr
.bearer_id
, lnr
.peer
,
1950 lnr
.linkname
, TIPC_MAX_LINK_NAME
)) {
1951 if (copy_to_user(argp
, &lnr
, sizeof(lnr
)))
1955 return -EADDRNOTAVAIL
;
1958 return -ENOIOCTLCMD
;
1962 /* Protocol switches for the various types of TIPC sockets */
1964 static const struct proto_ops msg_ops
= {
1965 .owner
= THIS_MODULE
,
1967 .release
= tipc_release
,
1969 .connect
= tipc_connect
,
1970 .socketpair
= sock_no_socketpair
,
1971 .accept
= sock_no_accept
,
1972 .getname
= tipc_getname
,
1974 .ioctl
= tipc_ioctl
,
1975 .listen
= sock_no_listen
,
1976 .shutdown
= tipc_shutdown
,
1977 .setsockopt
= tipc_setsockopt
,
1978 .getsockopt
= tipc_getsockopt
,
1979 .sendmsg
= tipc_sendmsg
,
1980 .recvmsg
= tipc_recvmsg
,
1981 .mmap
= sock_no_mmap
,
1982 .sendpage
= sock_no_sendpage
1985 static const struct proto_ops packet_ops
= {
1986 .owner
= THIS_MODULE
,
1988 .release
= tipc_release
,
1990 .connect
= tipc_connect
,
1991 .socketpair
= sock_no_socketpair
,
1992 .accept
= tipc_accept
,
1993 .getname
= tipc_getname
,
1995 .ioctl
= tipc_ioctl
,
1996 .listen
= tipc_listen
,
1997 .shutdown
= tipc_shutdown
,
1998 .setsockopt
= tipc_setsockopt
,
1999 .getsockopt
= tipc_getsockopt
,
2000 .sendmsg
= tipc_send_packet
,
2001 .recvmsg
= tipc_recvmsg
,
2002 .mmap
= sock_no_mmap
,
2003 .sendpage
= sock_no_sendpage
2006 static const struct proto_ops stream_ops
= {
2007 .owner
= THIS_MODULE
,
2009 .release
= tipc_release
,
2011 .connect
= tipc_connect
,
2012 .socketpair
= sock_no_socketpair
,
2013 .accept
= tipc_accept
,
2014 .getname
= tipc_getname
,
2016 .ioctl
= tipc_ioctl
,
2017 .listen
= tipc_listen
,
2018 .shutdown
= tipc_shutdown
,
2019 .setsockopt
= tipc_setsockopt
,
2020 .getsockopt
= tipc_getsockopt
,
2021 .sendmsg
= tipc_send_stream
,
2022 .recvmsg
= tipc_recv_stream
,
2023 .mmap
= sock_no_mmap
,
2024 .sendpage
= sock_no_sendpage
2027 static const struct net_proto_family tipc_family_ops
= {
2028 .owner
= THIS_MODULE
,
2030 .create
= tipc_sk_create
2033 static struct proto tipc_proto
= {
2035 .owner
= THIS_MODULE
,
2036 .obj_size
= sizeof(struct tipc_sock
),
2037 .sysctl_rmem
= sysctl_tipc_rmem
2040 static struct proto tipc_proto_kern
= {
2042 .obj_size
= sizeof(struct tipc_sock
),
2043 .sysctl_rmem
= sysctl_tipc_rmem
2047 * tipc_socket_init - initialize TIPC socket interface
2049 * Returns 0 on success, errno otherwise
2051 int tipc_socket_init(void)
2055 res
= proto_register(&tipc_proto
, 1);
2057 pr_err("Failed to register TIPC protocol type\n");
2061 res
= sock_register(&tipc_family_ops
);
2063 pr_err("Failed to register TIPC socket type\n");
2064 proto_unregister(&tipc_proto
);
2072 * tipc_socket_stop - stop TIPC socket interface
2074 void tipc_socket_stop(void)
2076 sock_unregister(tipc_family_ops
.family
);
2077 proto_unregister(&tipc_proto
);