2 * net/tipc/socket.c: TIPC socket API
4 * Copyright (c) 2001-2007, 2012 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.
40 #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 */
51 struct tipc_portid peer_name
;
52 unsigned int conn_timeout
;
55 #define tipc_sk(sk) ((struct tipc_sock *)(sk))
56 #define tipc_sk_port(sk) (tipc_sk(sk)->p)
58 static int backlog_rcv(struct sock
*sk
, struct sk_buff
*skb
);
59 static u32
dispatch(struct tipc_port
*tport
, struct sk_buff
*buf
);
60 static void wakeupdispatch(struct tipc_port
*tport
);
61 static void tipc_data_ready(struct sock
*sk
, int len
);
62 static void tipc_write_space(struct sock
*sk
);
63 static int tipc_release(struct socket
*sock
);
64 static int tipc_accept(struct socket
*sock
, struct socket
*new_sock
, int flags
);
66 static const struct proto_ops packet_ops
;
67 static const struct proto_ops stream_ops
;
68 static const struct proto_ops msg_ops
;
70 static struct proto tipc_proto
;
71 static struct proto tipc_proto_kern
;
73 static int sockets_enabled
;
76 * Revised TIPC socket locking policy:
78 * Most socket operations take the standard socket lock when they start
79 * and hold it until they finish (or until they need to sleep). Acquiring
80 * this lock grants the owner exclusive access to the fields of the socket
81 * data structures, with the exception of the backlog queue. A few socket
82 * operations can be done without taking the socket lock because they only
83 * read socket information that never changes during the life of the socket.
85 * Socket operations may acquire the lock for the associated TIPC port if they
86 * need to perform an operation on the port. If any routine needs to acquire
87 * both the socket lock and the port lock it must take the socket lock first
88 * to avoid the risk of deadlock.
90 * The dispatcher handling incoming messages cannot grab the socket lock in
91 * the standard fashion, since invoked it runs at the BH level and cannot block.
92 * Instead, it checks to see if the socket lock is currently owned by someone,
93 * and either handles the message itself or adds it to the socket's backlog
94 * queue; in the latter case the queued message is processed once the process
95 * owning the socket lock releases it.
97 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
98 * the problem of a blocked socket operation preventing any other operations
99 * from occurring. However, applications must be careful if they have
100 * multiple threads trying to send (or receive) on the same socket, as these
101 * operations might interfere with each other. For example, doing a connect
102 * and a receive at the same time might allow the receive to consume the
103 * ACK message meant for the connect. While additional work could be done
104 * to try and overcome this, it doesn't seem to be worthwhile at the present.
106 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
107 * that another operation that must be performed in a non-blocking manner is
108 * not delayed for very long because the lock has already been taken.
110 * NOTE: This code assumes that certain fields of a port/socket pair are
111 * constant over its lifetime; such fields can be examined without taking
112 * the socket lock and/or port lock, and do not need to be re-read even
113 * after resuming processing after waiting. These fields include:
115 * - pointer to socket sk structure (aka tipc_sock structure)
116 * - pointer to port structure
121 * advance_rx_queue - discard first buffer in socket receive queue
123 * Caller must hold socket lock
125 static void advance_rx_queue(struct sock
*sk
)
127 kfree_skb(__skb_dequeue(&sk
->sk_receive_queue
));
131 * reject_rx_queue - reject all buffers in socket receive queue
133 * Caller must hold socket lock
135 static void reject_rx_queue(struct sock
*sk
)
139 while ((buf
= __skb_dequeue(&sk
->sk_receive_queue
)))
140 tipc_reject_msg(buf
, TIPC_ERR_NO_PORT
);
144 * tipc_sk_create - create a TIPC socket
145 * @net: network namespace (must be default network)
146 * @sock: pre-allocated socket structure
147 * @protocol: protocol indicator (must be 0)
148 * @kern: caused by kernel or by userspace?
150 * This routine creates additional data structures used by the TIPC socket,
151 * initializes them, and links them together.
153 * Returns 0 on success, errno otherwise
155 static int tipc_sk_create(struct net
*net
, struct socket
*sock
, int protocol
,
158 const struct proto_ops
*ops
;
161 struct tipc_port
*tp_ptr
;
163 /* Validate arguments */
164 if (unlikely(protocol
!= 0))
165 return -EPROTONOSUPPORT
;
167 switch (sock
->type
) {
170 state
= SS_UNCONNECTED
;
174 state
= SS_UNCONNECTED
;
185 /* Allocate socket's protocol area */
187 sk
= sk_alloc(net
, AF_TIPC
, GFP_KERNEL
, &tipc_proto
);
189 sk
= sk_alloc(net
, AF_TIPC
, GFP_KERNEL
, &tipc_proto_kern
);
194 /* Allocate TIPC port for socket to use */
195 tp_ptr
= tipc_createport(sk
, &dispatch
, &wakeupdispatch
,
196 TIPC_LOW_IMPORTANCE
);
197 if (unlikely(!tp_ptr
)) {
202 /* Finish initializing socket data structures */
206 sock_init_data(sock
, sk
);
207 sk
->sk_backlog_rcv
= backlog_rcv
;
208 sk
->sk_rcvbuf
= sysctl_tipc_rmem
[1];
209 sk
->sk_data_ready
= tipc_data_ready
;
210 sk
->sk_write_space
= tipc_write_space
;
211 tipc_sk(sk
)->p
= tp_ptr
;
212 tipc_sk(sk
)->conn_timeout
= CONN_TIMEOUT_DEFAULT
;
214 spin_unlock_bh(tp_ptr
->lock
);
216 if (sock
->state
== SS_READY
) {
217 tipc_set_portunreturnable(tp_ptr
->ref
, 1);
218 if (sock
->type
== SOCK_DGRAM
)
219 tipc_set_portunreliable(tp_ptr
->ref
, 1);
226 * tipc_sock_create_local - create TIPC socket from inside TIPC module
227 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
229 * We cannot use sock_creat_kern here because it bumps module user count.
230 * Since socket owner and creator is the same module we must make sure
231 * that module count remains zero for module local sockets, otherwise
232 * we cannot do rmmod.
234 * Returns 0 on success, errno otherwise
236 int tipc_sock_create_local(int type
, struct socket
**res
)
240 rc
= sock_create_lite(AF_TIPC
, type
, 0, res
);
242 pr_err("Failed to create kernel socket\n");
245 tipc_sk_create(&init_net
, *res
, 0, 1);
251 * tipc_sock_release_local - release socket created by tipc_sock_create_local
252 * @sock: the socket to be released.
254 * Module reference count is not incremented when such sockets are created,
255 * so we must keep it from being decremented when they are released.
257 void tipc_sock_release_local(struct socket
*sock
)
265 * tipc_sock_accept_local - accept a connection on a socket created
266 * with tipc_sock_create_local. Use this function to avoid that
267 * module reference count is inadvertently incremented.
269 * @sock: the accepting socket
270 * @newsock: reference to the new socket to be created
271 * @flags: socket flags
274 int tipc_sock_accept_local(struct socket
*sock
, struct socket
**newsock
,
277 struct sock
*sk
= sock
->sk
;
280 ret
= sock_create_lite(sk
->sk_family
, sk
->sk_type
,
281 sk
->sk_protocol
, newsock
);
285 ret
= tipc_accept(sock
, *newsock
, flags
);
287 sock_release(*newsock
);
290 (*newsock
)->ops
= sock
->ops
;
295 * tipc_release - destroy a TIPC socket
296 * @sock: socket to destroy
298 * This routine cleans up any messages that are still queued on the socket.
299 * For DGRAM and RDM socket types, all queued messages are rejected.
300 * For SEQPACKET and STREAM socket types, the first message is rejected
301 * and any others are discarded. (If the first message on a STREAM socket
302 * is partially-read, it is discarded and the next one is rejected instead.)
304 * NOTE: Rejected messages are not necessarily returned to the sender! They
305 * are returned or discarded according to the "destination droppable" setting
306 * specified for the message by the sender.
308 * Returns 0 on success, errno otherwise
310 static int tipc_release(struct socket
*sock
)
312 struct sock
*sk
= sock
->sk
;
313 struct tipc_port
*tport
;
318 * Exit if socket isn't fully initialized (occurs when a failed accept()
319 * releases a pre-allocated child socket that was never used)
324 tport
= tipc_sk_port(sk
);
328 * Reject all unreceived messages, except on an active connection
329 * (which disconnects locally & sends a 'FIN+' to peer)
331 while (sock
->state
!= SS_DISCONNECTING
) {
332 buf
= __skb_dequeue(&sk
->sk_receive_queue
);
335 if (TIPC_SKB_CB(buf
)->handle
!= NULL
)
338 if ((sock
->state
== SS_CONNECTING
) ||
339 (sock
->state
== SS_CONNECTED
)) {
340 sock
->state
= SS_DISCONNECTING
;
341 tipc_port_disconnect(tport
->ref
);
343 tipc_reject_msg(buf
, TIPC_ERR_NO_PORT
);
348 * Delete TIPC port; this ensures no more messages are queued
349 * (also disconnects an active connection & sends a 'FIN-' to peer)
351 res
= tipc_deleteport(tport
);
353 /* Discard any remaining (connection-based) messages in receive queue */
354 __skb_queue_purge(&sk
->sk_receive_queue
);
356 /* Reject any messages that accumulated in backlog queue */
357 sock
->state
= SS_DISCONNECTING
;
367 * tipc_bind - associate or disassocate TIPC name(s) with a socket
368 * @sock: socket structure
369 * @uaddr: socket address describing name(s) and desired operation
370 * @uaddr_len: size of socket address data structure
372 * Name and name sequence binding is indicated using a positive scope value;
373 * a negative scope value unbinds the specified name. Specifying no name
374 * (i.e. a socket address length of 0) unbinds all names from the socket.
376 * Returns 0 on success, errno otherwise
378 * NOTE: This routine doesn't need to take the socket lock since it doesn't
379 * access any non-constant socket information.
381 static int tipc_bind(struct socket
*sock
, struct sockaddr
*uaddr
,
384 struct sock
*sk
= sock
->sk
;
385 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
386 struct tipc_port
*tport
= tipc_sk_port(sock
->sk
);
390 if (unlikely(!uaddr_len
)) {
391 res
= tipc_withdraw(tport
, 0, NULL
);
395 if (uaddr_len
< sizeof(struct sockaddr_tipc
)) {
399 if (addr
->family
!= AF_TIPC
) {
404 if (addr
->addrtype
== TIPC_ADDR_NAME
)
405 addr
->addr
.nameseq
.upper
= addr
->addr
.nameseq
.lower
;
406 else if (addr
->addrtype
!= TIPC_ADDR_NAMESEQ
) {
411 if ((addr
->addr
.nameseq
.type
< TIPC_RESERVED_TYPES
) &&
412 (addr
->addr
.nameseq
.type
!= TIPC_TOP_SRV
) &&
413 (addr
->addr
.nameseq
.type
!= TIPC_CFG_SRV
)) {
418 res
= (addr
->scope
> 0) ?
419 tipc_publish(tport
, addr
->scope
, &addr
->addr
.nameseq
) :
420 tipc_withdraw(tport
, -addr
->scope
, &addr
->addr
.nameseq
);
427 * tipc_getname - get port ID of socket or peer socket
428 * @sock: socket structure
429 * @uaddr: area for returned socket address
430 * @uaddr_len: area for returned length of socket address
431 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
433 * Returns 0 on success, errno otherwise
435 * NOTE: This routine doesn't need to take the socket lock since it only
436 * accesses socket information that is unchanging (or which changes in
437 * a completely predictable manner).
439 static int tipc_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
440 int *uaddr_len
, int peer
)
442 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
443 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
445 memset(addr
, 0, sizeof(*addr
));
447 if ((sock
->state
!= SS_CONNECTED
) &&
448 ((peer
!= 2) || (sock
->state
!= SS_DISCONNECTING
)))
450 addr
->addr
.id
.ref
= tsock
->peer_name
.ref
;
451 addr
->addr
.id
.node
= tsock
->peer_name
.node
;
453 addr
->addr
.id
.ref
= tsock
->p
->ref
;
454 addr
->addr
.id
.node
= tipc_own_addr
;
457 *uaddr_len
= sizeof(*addr
);
458 addr
->addrtype
= TIPC_ADDR_ID
;
459 addr
->family
= AF_TIPC
;
461 addr
->addr
.name
.domain
= 0;
467 * tipc_poll - read and possibly block on pollmask
468 * @file: file structure associated with the socket
469 * @sock: socket for which to calculate the poll bits
472 * Returns pollmask value
475 * It appears that the usual socket locking mechanisms are not useful here
476 * since the pollmask info is potentially out-of-date the moment this routine
477 * exits. TCP and other protocols seem to rely on higher level poll routines
478 * to handle any preventable race conditions, so TIPC will do the same ...
480 * TIPC sets the returned events as follows:
482 * socket state flags set
483 * ------------ ---------
484 * unconnected no read flags
485 * POLLOUT if port is not congested
487 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
490 * connected POLLIN/POLLRDNORM if data in rx queue
491 * POLLOUT if port is not congested
493 * disconnecting POLLIN/POLLRDNORM/POLLHUP
496 * listening POLLIN if SYN in rx queue
499 * ready POLLIN/POLLRDNORM if data in rx queue
500 * [connectionless] POLLOUT (since port cannot be congested)
502 * IMPORTANT: The fact that a read or write operation is indicated does NOT
503 * imply that the operation will succeed, merely that it should be performed
504 * and will not block.
506 static unsigned int tipc_poll(struct file
*file
, struct socket
*sock
,
509 struct sock
*sk
= sock
->sk
;
512 sock_poll_wait(file
, sk_sleep(sk
), wait
);
514 switch ((int)sock
->state
) {
516 if (!tipc_sk_port(sk
)->congested
)
521 if (!tipc_sk_port(sk
)->congested
)
526 if (!skb_queue_empty(&sk
->sk_receive_queue
))
527 mask
|= (POLLIN
| POLLRDNORM
);
529 case SS_DISCONNECTING
:
530 mask
= (POLLIN
| POLLRDNORM
| POLLHUP
);
538 * dest_name_check - verify user is permitted to send to specified port name
539 * @dest: destination address
540 * @m: descriptor for message to be sent
542 * Prevents restricted configuration commands from being issued by
543 * unauthorized users.
545 * Returns 0 if permission is granted, otherwise errno
547 static int dest_name_check(struct sockaddr_tipc
*dest
, struct msghdr
*m
)
549 struct tipc_cfg_msg_hdr hdr
;
551 if (likely(dest
->addr
.name
.name
.type
>= TIPC_RESERVED_TYPES
))
553 if (likely(dest
->addr
.name
.name
.type
== TIPC_TOP_SRV
))
555 if (likely(dest
->addr
.name
.name
.type
!= TIPC_CFG_SRV
))
558 if (!m
->msg_iovlen
|| (m
->msg_iov
[0].iov_len
< sizeof(hdr
)))
560 if (copy_from_user(&hdr
, m
->msg_iov
[0].iov_base
, sizeof(hdr
)))
562 if ((ntohs(hdr
.tcm_type
) & 0xC000) && (!capable(CAP_NET_ADMIN
)))
568 static int tipc_wait_for_sndmsg(struct socket
*sock
, long *timeo_p
)
570 struct sock
*sk
= sock
->sk
;
571 struct tipc_port
*tport
= tipc_sk_port(sk
);
576 int err
= sock_error(sk
);
579 if (sock
->state
== SS_DISCONNECTING
)
583 if (signal_pending(current
))
584 return sock_intr_errno(*timeo_p
);
586 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
587 done
= sk_wait_event(sk
, timeo_p
, !tport
->congested
);
588 finish_wait(sk_sleep(sk
), &wait
);
594 * tipc_sendmsg - send message in connectionless manner
595 * @iocb: if NULL, indicates that socket lock is already held
596 * @sock: socket structure
597 * @m: message to send
598 * @total_len: length of message
600 * Message must have an destination specified explicitly.
601 * Used for SOCK_RDM and SOCK_DGRAM messages,
602 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
603 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
605 * Returns the number of bytes sent on success, or errno otherwise
607 static int tipc_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
608 struct msghdr
*m
, size_t total_len
)
610 struct sock
*sk
= sock
->sk
;
611 struct tipc_port
*tport
= tipc_sk_port(sk
);
612 DECLARE_SOCKADDR(struct sockaddr_tipc
*, dest
, m
->msg_name
);
618 return -EDESTADDRREQ
;
619 if (unlikely((m
->msg_namelen
< sizeof(*dest
)) ||
620 (dest
->family
!= AF_TIPC
)))
622 if (total_len
> TIPC_MAX_USER_MSG_SIZE
)
628 needs_conn
= (sock
->state
!= SS_READY
);
629 if (unlikely(needs_conn
)) {
630 if (sock
->state
== SS_LISTENING
) {
634 if (sock
->state
!= SS_UNCONNECTED
) {
638 if (tport
->published
) {
642 if (dest
->addrtype
== TIPC_ADDR_NAME
) {
643 tport
->conn_type
= dest
->addr
.name
.name
.type
;
644 tport
->conn_instance
= dest
->addr
.name
.name
.instance
;
647 /* Abort any pending connection attempts (very unlikely) */
651 timeo
= sock_sndtimeo(sk
, m
->msg_flags
& MSG_DONTWAIT
);
653 if (dest
->addrtype
== TIPC_ADDR_NAME
) {
654 res
= dest_name_check(dest
, m
);
657 res
= tipc_send2name(tport
->ref
,
658 &dest
->addr
.name
.name
,
659 dest
->addr
.name
.domain
,
662 } else if (dest
->addrtype
== TIPC_ADDR_ID
) {
663 res
= tipc_send2port(tport
->ref
,
667 } else if (dest
->addrtype
== TIPC_ADDR_MCAST
) {
672 res
= dest_name_check(dest
, m
);
675 res
= tipc_port_mcast_xmit(tport
->ref
,
680 if (likely(res
!= -ELINKCONG
)) {
681 if (needs_conn
&& (res
>= 0))
682 sock
->state
= SS_CONNECTING
;
685 res
= tipc_wait_for_sndmsg(sock
, &timeo
);
696 static int tipc_wait_for_sndpkt(struct socket
*sock
, long *timeo_p
)
698 struct sock
*sk
= sock
->sk
;
699 struct tipc_port
*tport
= tipc_sk_port(sk
);
704 int err
= sock_error(sk
);
707 if (sock
->state
== SS_DISCONNECTING
)
709 else if (sock
->state
!= SS_CONNECTED
)
713 if (signal_pending(current
))
714 return sock_intr_errno(*timeo_p
);
716 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
717 done
= sk_wait_event(sk
, timeo_p
,
718 (!tport
->congested
|| !tport
->connected
));
719 finish_wait(sk_sleep(sk
), &wait
);
725 * tipc_send_packet - send a connection-oriented message
726 * @iocb: if NULL, indicates that socket lock is already held
727 * @sock: socket structure
728 * @m: message to send
729 * @total_len: length of message
731 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
733 * Returns the number of bytes sent on success, or errno otherwise
735 static int tipc_send_packet(struct kiocb
*iocb
, struct socket
*sock
,
736 struct msghdr
*m
, size_t total_len
)
738 struct sock
*sk
= sock
->sk
;
739 struct tipc_port
*tport
= tipc_sk_port(sk
);
740 DECLARE_SOCKADDR(struct sockaddr_tipc
*, dest
, m
->msg_name
);
744 /* Handle implied connection establishment */
746 return tipc_sendmsg(iocb
, sock
, m
, total_len
);
748 if (total_len
> TIPC_MAX_USER_MSG_SIZE
)
754 if (unlikely(sock
->state
!= SS_CONNECTED
)) {
755 if (sock
->state
== SS_DISCONNECTING
)
762 timeo
= sock_sndtimeo(sk
, m
->msg_flags
& MSG_DONTWAIT
);
764 res
= tipc_send(tport
->ref
, m
->msg_iov
, total_len
);
765 if (likely(res
!= -ELINKCONG
))
767 res
= tipc_wait_for_sndpkt(sock
, &timeo
);
778 * tipc_send_stream - send stream-oriented data
780 * @sock: socket structure
782 * @total_len: total length of data to be sent
784 * Used for SOCK_STREAM data.
786 * Returns the number of bytes sent on success (or partial success),
787 * or errno if no data sent
789 static int tipc_send_stream(struct kiocb
*iocb
, struct socket
*sock
,
790 struct msghdr
*m
, size_t total_len
)
792 struct sock
*sk
= sock
->sk
;
793 struct tipc_port
*tport
= tipc_sk_port(sk
);
794 struct msghdr my_msg
;
796 struct iovec
*curr_iov
;
798 char __user
*curr_start
;
807 /* Handle special cases where there is no connection */
808 if (unlikely(sock
->state
!= SS_CONNECTED
)) {
809 if (sock
->state
== SS_UNCONNECTED
)
810 res
= tipc_send_packet(NULL
, sock
, m
, total_len
);
812 res
= sock
->state
== SS_DISCONNECTING
? -EPIPE
: -ENOTCONN
;
816 if (unlikely(m
->msg_name
)) {
821 if (total_len
> (unsigned int)INT_MAX
) {
827 * Send each iovec entry using one or more messages
829 * Note: This algorithm is good for the most likely case
830 * (i.e. one large iovec entry), but could be improved to pass sets
831 * of small iovec entries into send_packet().
833 curr_iov
= m
->msg_iov
;
834 curr_iovlen
= m
->msg_iovlen
;
835 my_msg
.msg_iov
= &my_iov
;
836 my_msg
.msg_iovlen
= 1;
837 my_msg
.msg_flags
= m
->msg_flags
;
838 my_msg
.msg_name
= NULL
;
841 hdr_size
= msg_hdr_sz(&tport
->phdr
);
843 while (curr_iovlen
--) {
844 curr_start
= curr_iov
->iov_base
;
845 curr_left
= curr_iov
->iov_len
;
848 bytes_to_send
= tport
->max_pkt
- hdr_size
;
849 if (bytes_to_send
> TIPC_MAX_USER_MSG_SIZE
)
850 bytes_to_send
= TIPC_MAX_USER_MSG_SIZE
;
851 if (curr_left
< bytes_to_send
)
852 bytes_to_send
= curr_left
;
853 my_iov
.iov_base
= curr_start
;
854 my_iov
.iov_len
= bytes_to_send
;
855 res
= tipc_send_packet(NULL
, sock
, &my_msg
,
862 curr_left
-= bytes_to_send
;
863 curr_start
+= bytes_to_send
;
864 bytes_sent
+= bytes_to_send
;
876 * auto_connect - complete connection setup to a remote port
877 * @sock: socket structure
878 * @msg: peer's response message
880 * Returns 0 on success, errno otherwise
882 static int auto_connect(struct socket
*sock
, struct tipc_msg
*msg
)
884 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
885 struct tipc_port
*p_ptr
;
887 tsock
->peer_name
.ref
= msg_origport(msg
);
888 tsock
->peer_name
.node
= msg_orignode(msg
);
889 p_ptr
= tipc_port_deref(tsock
->p
->ref
);
893 __tipc_port_connect(tsock
->p
->ref
, p_ptr
, &tsock
->peer_name
);
895 if (msg_importance(msg
) > TIPC_CRITICAL_IMPORTANCE
)
897 msg_set_importance(&p_ptr
->phdr
, (u32
)msg_importance(msg
));
898 sock
->state
= SS_CONNECTED
;
903 * set_orig_addr - capture sender's address for received message
904 * @m: descriptor for message info
905 * @msg: received message header
907 * Note: Address is not captured if not requested by receiver.
909 static void set_orig_addr(struct msghdr
*m
, struct tipc_msg
*msg
)
911 DECLARE_SOCKADDR(struct sockaddr_tipc
*, addr
, m
->msg_name
);
914 addr
->family
= AF_TIPC
;
915 addr
->addrtype
= TIPC_ADDR_ID
;
916 memset(&addr
->addr
, 0, sizeof(addr
->addr
));
917 addr
->addr
.id
.ref
= msg_origport(msg
);
918 addr
->addr
.id
.node
= msg_orignode(msg
);
919 addr
->addr
.name
.domain
= 0; /* could leave uninitialized */
920 addr
->scope
= 0; /* could leave uninitialized */
921 m
->msg_namelen
= sizeof(struct sockaddr_tipc
);
926 * anc_data_recv - optionally capture ancillary data for received message
927 * @m: descriptor for message info
928 * @msg: received message header
929 * @tport: TIPC port associated with message
931 * Note: Ancillary data is not captured if not requested by receiver.
933 * Returns 0 if successful, otherwise errno
935 static int anc_data_recv(struct msghdr
*m
, struct tipc_msg
*msg
,
936 struct tipc_port
*tport
)
944 if (likely(m
->msg_controllen
== 0))
947 /* Optionally capture errored message object(s) */
948 err
= msg
? msg_errcode(msg
) : 0;
951 anc_data
[1] = msg_data_sz(msg
);
952 res
= put_cmsg(m
, SOL_TIPC
, TIPC_ERRINFO
, 8, anc_data
);
956 res
= put_cmsg(m
, SOL_TIPC
, TIPC_RETDATA
, anc_data
[1],
963 /* Optionally capture message destination object */
964 dest_type
= msg
? msg_type(msg
) : TIPC_DIRECT_MSG
;
968 anc_data
[0] = msg_nametype(msg
);
969 anc_data
[1] = msg_namelower(msg
);
970 anc_data
[2] = msg_namelower(msg
);
974 anc_data
[0] = msg_nametype(msg
);
975 anc_data
[1] = msg_namelower(msg
);
976 anc_data
[2] = msg_nameupper(msg
);
979 has_name
= (tport
->conn_type
!= 0);
980 anc_data
[0] = tport
->conn_type
;
981 anc_data
[1] = tport
->conn_instance
;
982 anc_data
[2] = tport
->conn_instance
;
988 res
= put_cmsg(m
, SOL_TIPC
, TIPC_DESTNAME
, 12, anc_data
);
996 static int tipc_wait_for_rcvmsg(struct socket
*sock
, long timeo
)
998 struct sock
*sk
= sock
->sk
;
1003 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
1004 if (skb_queue_empty(&sk
->sk_receive_queue
)) {
1005 if (sock
->state
== SS_DISCONNECTING
) {
1010 timeo
= schedule_timeout(timeo
);
1014 if (!skb_queue_empty(&sk
->sk_receive_queue
))
1016 err
= sock_intr_errno(timeo
);
1017 if (signal_pending(current
))
1023 finish_wait(sk_sleep(sk
), &wait
);
1028 * tipc_recvmsg - receive packet-oriented message
1030 * @m: descriptor for message info
1031 * @buf_len: total size of user buffer area
1032 * @flags: receive flags
1034 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1035 * If the complete message doesn't fit in user area, truncate it.
1037 * Returns size of returned message data, errno otherwise
1039 static int tipc_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1040 struct msghdr
*m
, size_t buf_len
, int flags
)
1042 struct sock
*sk
= sock
->sk
;
1043 struct tipc_port
*tport
= tipc_sk_port(sk
);
1044 struct sk_buff
*buf
;
1045 struct tipc_msg
*msg
;
1051 /* Catch invalid receive requests */
1052 if (unlikely(!buf_len
))
1057 if (unlikely(sock
->state
== SS_UNCONNECTED
)) {
1062 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
1065 /* Look for a message in receive queue; wait if necessary */
1066 res
= tipc_wait_for_rcvmsg(sock
, timeo
);
1070 /* Look at first message in receive queue */
1071 buf
= skb_peek(&sk
->sk_receive_queue
);
1073 sz
= msg_data_sz(msg
);
1074 err
= msg_errcode(msg
);
1076 /* Discard an empty non-errored message & try again */
1077 if ((!sz
) && (!err
)) {
1078 advance_rx_queue(sk
);
1082 /* Capture sender's address (optional) */
1083 set_orig_addr(m
, msg
);
1085 /* Capture ancillary data (optional) */
1086 res
= anc_data_recv(m
, msg
, tport
);
1090 /* Capture message data (if valid) & compute return value (always) */
1092 if (unlikely(buf_len
< sz
)) {
1094 m
->msg_flags
|= MSG_TRUNC
;
1096 res
= skb_copy_datagram_iovec(buf
, msg_hdr_sz(msg
),
1102 if ((sock
->state
== SS_READY
) ||
1103 ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
))
1109 /* Consume received message (optional) */
1110 if (likely(!(flags
& MSG_PEEK
))) {
1111 if ((sock
->state
!= SS_READY
) &&
1112 (++tport
->conn_unacked
>= TIPC_FLOW_CONTROL_WIN
))
1113 tipc_acknowledge(tport
->ref
, tport
->conn_unacked
);
1114 advance_rx_queue(sk
);
1122 * tipc_recv_stream - receive stream-oriented data
1124 * @m: descriptor for message info
1125 * @buf_len: total size of user buffer area
1126 * @flags: receive flags
1128 * Used for SOCK_STREAM messages only. If not enough data is available
1129 * will optionally wait for more; never truncates data.
1131 * Returns size of returned message data, errno otherwise
1133 static int tipc_recv_stream(struct kiocb
*iocb
, struct socket
*sock
,
1134 struct msghdr
*m
, size_t buf_len
, int flags
)
1136 struct sock
*sk
= sock
->sk
;
1137 struct tipc_port
*tport
= tipc_sk_port(sk
);
1138 struct sk_buff
*buf
;
1139 struct tipc_msg
*msg
;
1142 int sz_to_copy
, target
, needed
;
1147 /* Catch invalid receive attempts */
1148 if (unlikely(!buf_len
))
1153 if (unlikely(sock
->state
== SS_UNCONNECTED
)) {
1158 target
= sock_rcvlowat(sk
, flags
& MSG_WAITALL
, buf_len
);
1159 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
1162 /* Look for a message in receive queue; wait if necessary */
1163 res
= tipc_wait_for_rcvmsg(sock
, timeo
);
1167 /* Look at first message in receive queue */
1168 buf
= skb_peek(&sk
->sk_receive_queue
);
1170 sz
= msg_data_sz(msg
);
1171 err
= msg_errcode(msg
);
1173 /* Discard an empty non-errored message & try again */
1174 if ((!sz
) && (!err
)) {
1175 advance_rx_queue(sk
);
1179 /* Optionally capture sender's address & ancillary data of first msg */
1180 if (sz_copied
== 0) {
1181 set_orig_addr(m
, msg
);
1182 res
= anc_data_recv(m
, msg
, tport
);
1187 /* Capture message data (if valid) & compute return value (always) */
1189 u32 offset
= (u32
)(unsigned long)(TIPC_SKB_CB(buf
)->handle
);
1192 needed
= (buf_len
- sz_copied
);
1193 sz_to_copy
= (sz
<= needed
) ? sz
: needed
;
1195 res
= skb_copy_datagram_iovec(buf
, msg_hdr_sz(msg
) + offset
,
1196 m
->msg_iov
, sz_to_copy
);
1200 sz_copied
+= sz_to_copy
;
1202 if (sz_to_copy
< sz
) {
1203 if (!(flags
& MSG_PEEK
))
1204 TIPC_SKB_CB(buf
)->handle
=
1205 (void *)(unsigned long)(offset
+ sz_to_copy
);
1210 goto exit
; /* can't add error msg to valid data */
1212 if ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
)
1218 /* Consume received message (optional) */
1219 if (likely(!(flags
& MSG_PEEK
))) {
1220 if (unlikely(++tport
->conn_unacked
>= TIPC_FLOW_CONTROL_WIN
))
1221 tipc_acknowledge(tport
->ref
, tport
->conn_unacked
);
1222 advance_rx_queue(sk
);
1225 /* Loop around if more data is required */
1226 if ((sz_copied
< buf_len
) && /* didn't get all requested data */
1227 (!skb_queue_empty(&sk
->sk_receive_queue
) ||
1228 (sz_copied
< target
)) && /* and more is ready or required */
1229 (!(flags
& MSG_PEEK
)) && /* and aren't just peeking at data */
1230 (!err
)) /* and haven't reached a FIN */
1235 return sz_copied
? sz_copied
: res
;
1239 * tipc_write_space - wake up thread if port congestion is released
1242 static void tipc_write_space(struct sock
*sk
)
1244 struct socket_wq
*wq
;
1247 wq
= rcu_dereference(sk
->sk_wq
);
1248 if (wq_has_sleeper(wq
))
1249 wake_up_interruptible_sync_poll(&wq
->wait
, POLLOUT
|
1250 POLLWRNORM
| POLLWRBAND
);
1255 * tipc_data_ready - wake up threads to indicate messages have been received
1257 * @len: the length of messages
1259 static void tipc_data_ready(struct sock
*sk
, int len
)
1261 struct socket_wq
*wq
;
1264 wq
= rcu_dereference(sk
->sk_wq
);
1265 if (wq_has_sleeper(wq
))
1266 wake_up_interruptible_sync_poll(&wq
->wait
, POLLIN
|
1267 POLLRDNORM
| POLLRDBAND
);
1272 * filter_connect - Handle all incoming messages for a connection-based socket
1273 * @tsock: TIPC socket
1276 * Returns TIPC error status code and socket error status code
1277 * once it encounters some errors
1279 static u32
filter_connect(struct tipc_sock
*tsock
, struct sk_buff
**buf
)
1281 struct socket
*sock
= tsock
->sk
.sk_socket
;
1282 struct tipc_msg
*msg
= buf_msg(*buf
);
1283 struct sock
*sk
= &tsock
->sk
;
1284 u32 retval
= TIPC_ERR_NO_PORT
;
1290 switch ((int)sock
->state
) {
1292 /* Accept only connection-based messages sent by peer */
1293 if (msg_connected(msg
) && tipc_port_peer_msg(tsock
->p
, msg
)) {
1294 if (unlikely(msg_errcode(msg
))) {
1295 sock
->state
= SS_DISCONNECTING
;
1296 __tipc_port_disconnect(tsock
->p
);
1302 /* Accept only ACK or NACK message */
1303 if (unlikely(msg_errcode(msg
))) {
1304 sock
->state
= SS_DISCONNECTING
;
1305 sk
->sk_err
= ECONNREFUSED
;
1310 if (unlikely(!msg_connected(msg
)))
1313 res
= auto_connect(sock
, msg
);
1315 sock
->state
= SS_DISCONNECTING
;
1321 /* If an incoming message is an 'ACK-', it should be
1322 * discarded here because it doesn't contain useful
1323 * data. In addition, we should try to wake up
1324 * connect() routine if sleeping.
1326 if (msg_data_sz(msg
) == 0) {
1329 if (waitqueue_active(sk_sleep(sk
)))
1330 wake_up_interruptible(sk_sleep(sk
));
1335 case SS_UNCONNECTED
:
1336 /* Accept only SYN message */
1337 if (!msg_connected(msg
) && !(msg_errcode(msg
)))
1340 case SS_DISCONNECTING
:
1343 pr_err("Unknown socket state %u\n", sock
->state
);
1349 * rcvbuf_limit - get proper overload limit of socket receive queue
1353 * For all connection oriented messages, irrespective of importance,
1354 * the default overload value (i.e. 67MB) is set as limit.
1356 * For all connectionless messages, by default new queue limits are
1359 * TIPC_LOW_IMPORTANCE (4 MB)
1360 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1361 * TIPC_HIGH_IMPORTANCE (16 MB)
1362 * TIPC_CRITICAL_IMPORTANCE (32 MB)
1364 * Returns overload limit according to corresponding message importance
1366 static unsigned int rcvbuf_limit(struct sock
*sk
, struct sk_buff
*buf
)
1368 struct tipc_msg
*msg
= buf_msg(buf
);
1370 if (msg_connected(msg
))
1371 return sysctl_tipc_rmem
[2];
1373 return sk
->sk_rcvbuf
>> TIPC_CRITICAL_IMPORTANCE
<<
1374 msg_importance(msg
);
1378 * filter_rcv - validate incoming message
1382 * Enqueues message on receive queue if acceptable; optionally handles
1383 * disconnect indication for a connected socket.
1385 * Called with socket lock already taken; port lock may also be taken.
1387 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1389 static u32
filter_rcv(struct sock
*sk
, struct sk_buff
*buf
)
1391 struct socket
*sock
= sk
->sk_socket
;
1392 struct tipc_msg
*msg
= buf_msg(buf
);
1393 unsigned int limit
= rcvbuf_limit(sk
, buf
);
1396 /* Reject message if it is wrong sort of message for socket */
1397 if (msg_type(msg
) > TIPC_DIRECT_MSG
)
1398 return TIPC_ERR_NO_PORT
;
1400 if (sock
->state
== SS_READY
) {
1401 if (msg_connected(msg
))
1402 return TIPC_ERR_NO_PORT
;
1404 res
= filter_connect(tipc_sk(sk
), &buf
);
1405 if (res
!= TIPC_OK
|| buf
== NULL
)
1409 /* Reject message if there isn't room to queue it */
1410 if (sk_rmem_alloc_get(sk
) + buf
->truesize
>= limit
)
1411 return TIPC_ERR_OVERLOAD
;
1413 /* Enqueue message */
1414 TIPC_SKB_CB(buf
)->handle
= NULL
;
1415 __skb_queue_tail(&sk
->sk_receive_queue
, buf
);
1416 skb_set_owner_r(buf
, sk
);
1418 sk
->sk_data_ready(sk
, 0);
1423 * backlog_rcv - handle incoming message from backlog queue
1427 * Caller must hold socket lock, but not port lock.
1431 static int backlog_rcv(struct sock
*sk
, struct sk_buff
*buf
)
1435 res
= filter_rcv(sk
, buf
);
1437 tipc_reject_msg(buf
, res
);
1442 * dispatch - handle incoming message
1443 * @tport: TIPC port that received message
1446 * Called with port lock already taken.
1448 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1450 static u32
dispatch(struct tipc_port
*tport
, struct sk_buff
*buf
)
1452 struct sock
*sk
= tport
->sk
;
1456 * Process message if socket is unlocked; otherwise add to backlog queue
1458 * This code is based on sk_receive_skb(), but must be distinct from it
1459 * since a TIPC-specific filter/reject mechanism is utilized
1462 if (!sock_owned_by_user(sk
)) {
1463 res
= filter_rcv(sk
, buf
);
1465 if (sk_add_backlog(sk
, buf
, rcvbuf_limit(sk
, buf
)))
1466 res
= TIPC_ERR_OVERLOAD
;
1476 * wakeupdispatch - wake up port after congestion
1477 * @tport: port to wakeup
1479 * Called with port lock already taken.
1481 static void wakeupdispatch(struct tipc_port
*tport
)
1483 struct sock
*sk
= tport
->sk
;
1485 sk
->sk_write_space(sk
);
1488 static int tipc_wait_for_connect(struct socket
*sock
, long *timeo_p
)
1490 struct sock
*sk
= sock
->sk
;
1495 int err
= sock_error(sk
);
1500 if (signal_pending(current
))
1501 return sock_intr_errno(*timeo_p
);
1503 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
1504 done
= sk_wait_event(sk
, timeo_p
, sock
->state
!= SS_CONNECTING
);
1505 finish_wait(sk_sleep(sk
), &wait
);
1511 * tipc_connect - establish a connection to another TIPC port
1512 * @sock: socket structure
1513 * @dest: socket address for destination port
1514 * @destlen: size of socket address data structure
1515 * @flags: file-related flags associated with socket
1517 * Returns 0 on success, errno otherwise
1519 static int tipc_connect(struct socket
*sock
, struct sockaddr
*dest
,
1520 int destlen
, int flags
)
1522 struct sock
*sk
= sock
->sk
;
1523 struct sockaddr_tipc
*dst
= (struct sockaddr_tipc
*)dest
;
1524 struct msghdr m
= {NULL
,};
1525 long timeout
= (flags
& O_NONBLOCK
) ? 0 : tipc_sk(sk
)->conn_timeout
;
1526 socket_state previous
;
1531 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
1532 if (sock
->state
== SS_READY
) {
1538 * Reject connection attempt using multicast address
1540 * Note: send_msg() validates the rest of the address fields,
1541 * so there's no need to do it here
1543 if (dst
->addrtype
== TIPC_ADDR_MCAST
) {
1548 previous
= sock
->state
;
1549 switch (sock
->state
) {
1550 case SS_UNCONNECTED
:
1551 /* Send a 'SYN-' to destination */
1553 m
.msg_namelen
= destlen
;
1555 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1556 * indicate send_msg() is never blocked.
1559 m
.msg_flags
= MSG_DONTWAIT
;
1561 res
= tipc_sendmsg(NULL
, sock
, &m
, 0);
1562 if ((res
< 0) && (res
!= -EWOULDBLOCK
))
1565 /* Just entered SS_CONNECTING state; the only
1566 * difference is that return value in non-blocking
1567 * case is EINPROGRESS, rather than EALREADY.
1571 if (previous
== SS_CONNECTING
)
1575 timeout
= msecs_to_jiffies(timeout
);
1576 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1577 res
= tipc_wait_for_connect(sock
, &timeout
);
1592 * tipc_listen - allow socket to listen for incoming connections
1593 * @sock: socket structure
1596 * Returns 0 on success, errno otherwise
1598 static int tipc_listen(struct socket
*sock
, int len
)
1600 struct sock
*sk
= sock
->sk
;
1605 if (sock
->state
!= SS_UNCONNECTED
)
1608 sock
->state
= SS_LISTENING
;
1616 static int tipc_wait_for_accept(struct socket
*sock
, long timeo
)
1618 struct sock
*sk
= sock
->sk
;
1622 /* True wake-one mechanism for incoming connections: only
1623 * one process gets woken up, not the 'whole herd'.
1624 * Since we do not 'race & poll' for established sockets
1625 * anymore, the common case will execute the loop only once.
1628 prepare_to_wait_exclusive(sk_sleep(sk
), &wait
,
1629 TASK_INTERRUPTIBLE
);
1630 if (skb_queue_empty(&sk
->sk_receive_queue
)) {
1632 timeo
= schedule_timeout(timeo
);
1636 if (!skb_queue_empty(&sk
->sk_receive_queue
))
1639 if (sock
->state
!= SS_LISTENING
)
1641 err
= sock_intr_errno(timeo
);
1642 if (signal_pending(current
))
1648 finish_wait(sk_sleep(sk
), &wait
);
1653 * tipc_accept - wait for connection request
1654 * @sock: listening socket
1655 * @newsock: new socket that is to be connected
1656 * @flags: file-related flags associated with socket
1658 * Returns 0 on success, errno otherwise
1660 static int tipc_accept(struct socket
*sock
, struct socket
*new_sock
, int flags
)
1662 struct sock
*new_sk
, *sk
= sock
->sk
;
1663 struct sk_buff
*buf
;
1664 struct tipc_sock
*new_tsock
;
1665 struct tipc_port
*new_tport
;
1666 struct tipc_msg
*msg
;
1673 if (sock
->state
!= SS_LISTENING
) {
1678 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
1679 res
= tipc_wait_for_accept(sock
, timeo
);
1683 buf
= skb_peek(&sk
->sk_receive_queue
);
1685 res
= tipc_sk_create(sock_net(sock
->sk
), new_sock
, 0, 1);
1689 new_sk
= new_sock
->sk
;
1690 new_tsock
= tipc_sk(new_sk
);
1691 new_tport
= new_tsock
->p
;
1692 new_ref
= new_tport
->ref
;
1695 /* we lock on new_sk; but lockdep sees the lock on sk */
1696 lock_sock_nested(new_sk
, SINGLE_DEPTH_NESTING
);
1699 * Reject any stray messages received by new socket
1700 * before the socket lock was taken (very, very unlikely)
1702 reject_rx_queue(new_sk
);
1704 /* Connect new socket to it's peer */
1705 new_tsock
->peer_name
.ref
= msg_origport(msg
);
1706 new_tsock
->peer_name
.node
= msg_orignode(msg
);
1707 tipc_port_connect(new_ref
, &new_tsock
->peer_name
);
1708 new_sock
->state
= SS_CONNECTED
;
1710 tipc_set_portimportance(new_ref
, msg_importance(msg
));
1711 if (msg_named(msg
)) {
1712 new_tport
->conn_type
= msg_nametype(msg
);
1713 new_tport
->conn_instance
= msg_nameinst(msg
);
1717 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1718 * Respond to 'SYN+' by queuing it on new socket.
1720 if (!msg_data_sz(msg
)) {
1721 struct msghdr m
= {NULL
,};
1723 advance_rx_queue(sk
);
1724 tipc_send_packet(NULL
, new_sock
, &m
, 0);
1726 __skb_dequeue(&sk
->sk_receive_queue
);
1727 __skb_queue_head(&new_sk
->sk_receive_queue
, buf
);
1728 skb_set_owner_r(buf
, new_sk
);
1730 release_sock(new_sk
);
1738 * tipc_shutdown - shutdown socket connection
1739 * @sock: socket structure
1740 * @how: direction to close (must be SHUT_RDWR)
1742 * Terminates connection (if necessary), then purges socket's receive queue.
1744 * Returns 0 on success, errno otherwise
1746 static int tipc_shutdown(struct socket
*sock
, int how
)
1748 struct sock
*sk
= sock
->sk
;
1749 struct tipc_port
*tport
= tipc_sk_port(sk
);
1750 struct sk_buff
*buf
;
1753 if (how
!= SHUT_RDWR
)
1758 switch (sock
->state
) {
1763 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
1764 buf
= __skb_dequeue(&sk
->sk_receive_queue
);
1766 if (TIPC_SKB_CB(buf
)->handle
!= NULL
) {
1770 tipc_port_disconnect(tport
->ref
);
1771 tipc_reject_msg(buf
, TIPC_CONN_SHUTDOWN
);
1773 tipc_port_shutdown(tport
->ref
);
1776 sock
->state
= SS_DISCONNECTING
;
1780 case SS_DISCONNECTING
:
1782 /* Discard any unreceived messages */
1783 __skb_queue_purge(&sk
->sk_receive_queue
);
1785 /* Wake up anyone sleeping in poll */
1786 sk
->sk_state_change(sk
);
1799 * tipc_setsockopt - set socket option
1800 * @sock: socket structure
1801 * @lvl: option level
1802 * @opt: option identifier
1803 * @ov: pointer to new option value
1804 * @ol: length of option value
1806 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
1807 * (to ease compatibility).
1809 * Returns 0 on success, errno otherwise
1811 static int tipc_setsockopt(struct socket
*sock
, int lvl
, int opt
,
1812 char __user
*ov
, unsigned int ol
)
1814 struct sock
*sk
= sock
->sk
;
1815 struct tipc_port
*tport
= tipc_sk_port(sk
);
1819 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
1821 if (lvl
!= SOL_TIPC
)
1822 return -ENOPROTOOPT
;
1823 if (ol
< sizeof(value
))
1825 res
= get_user(value
, (u32 __user
*)ov
);
1832 case TIPC_IMPORTANCE
:
1833 res
= tipc_set_portimportance(tport
->ref
, value
);
1835 case TIPC_SRC_DROPPABLE
:
1836 if (sock
->type
!= SOCK_STREAM
)
1837 res
= tipc_set_portunreliable(tport
->ref
, value
);
1841 case TIPC_DEST_DROPPABLE
:
1842 res
= tipc_set_portunreturnable(tport
->ref
, value
);
1844 case TIPC_CONN_TIMEOUT
:
1845 tipc_sk(sk
)->conn_timeout
= value
;
1846 /* no need to set "res", since already 0 at this point */
1858 * tipc_getsockopt - get socket option
1859 * @sock: socket structure
1860 * @lvl: option level
1861 * @opt: option identifier
1862 * @ov: receptacle for option value
1863 * @ol: receptacle for length of option value
1865 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
1866 * (to ease compatibility).
1868 * Returns 0 on success, errno otherwise
1870 static int tipc_getsockopt(struct socket
*sock
, int lvl
, int opt
,
1871 char __user
*ov
, int __user
*ol
)
1873 struct sock
*sk
= sock
->sk
;
1874 struct tipc_port
*tport
= tipc_sk_port(sk
);
1879 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
1880 return put_user(0, ol
);
1881 if (lvl
!= SOL_TIPC
)
1882 return -ENOPROTOOPT
;
1883 res
= get_user(len
, ol
);
1890 case TIPC_IMPORTANCE
:
1891 res
= tipc_portimportance(tport
->ref
, &value
);
1893 case TIPC_SRC_DROPPABLE
:
1894 res
= tipc_portunreliable(tport
->ref
, &value
);
1896 case TIPC_DEST_DROPPABLE
:
1897 res
= tipc_portunreturnable(tport
->ref
, &value
);
1899 case TIPC_CONN_TIMEOUT
:
1900 value
= tipc_sk(sk
)->conn_timeout
;
1901 /* no need to set "res", since already 0 at this point */
1903 case TIPC_NODE_RECVQ_DEPTH
:
1904 value
= 0; /* was tipc_queue_size, now obsolete */
1906 case TIPC_SOCK_RECVQ_DEPTH
:
1907 value
= skb_queue_len(&sk
->sk_receive_queue
);
1916 return res
; /* "get" failed */
1918 if (len
< sizeof(value
))
1921 if (copy_to_user(ov
, &value
, sizeof(value
)))
1924 return put_user(sizeof(value
), ol
);
1927 /* Protocol switches for the various types of TIPC sockets */
1929 static const struct proto_ops msg_ops
= {
1930 .owner
= THIS_MODULE
,
1932 .release
= tipc_release
,
1934 .connect
= tipc_connect
,
1935 .socketpair
= sock_no_socketpair
,
1936 .accept
= sock_no_accept
,
1937 .getname
= tipc_getname
,
1939 .ioctl
= sock_no_ioctl
,
1940 .listen
= sock_no_listen
,
1941 .shutdown
= tipc_shutdown
,
1942 .setsockopt
= tipc_setsockopt
,
1943 .getsockopt
= tipc_getsockopt
,
1944 .sendmsg
= tipc_sendmsg
,
1945 .recvmsg
= tipc_recvmsg
,
1946 .mmap
= sock_no_mmap
,
1947 .sendpage
= sock_no_sendpage
1950 static const struct proto_ops packet_ops
= {
1951 .owner
= THIS_MODULE
,
1953 .release
= tipc_release
,
1955 .connect
= tipc_connect
,
1956 .socketpair
= sock_no_socketpair
,
1957 .accept
= tipc_accept
,
1958 .getname
= tipc_getname
,
1960 .ioctl
= sock_no_ioctl
,
1961 .listen
= tipc_listen
,
1962 .shutdown
= tipc_shutdown
,
1963 .setsockopt
= tipc_setsockopt
,
1964 .getsockopt
= tipc_getsockopt
,
1965 .sendmsg
= tipc_send_packet
,
1966 .recvmsg
= tipc_recvmsg
,
1967 .mmap
= sock_no_mmap
,
1968 .sendpage
= sock_no_sendpage
1971 static const struct proto_ops stream_ops
= {
1972 .owner
= THIS_MODULE
,
1974 .release
= tipc_release
,
1976 .connect
= tipc_connect
,
1977 .socketpair
= sock_no_socketpair
,
1978 .accept
= tipc_accept
,
1979 .getname
= tipc_getname
,
1981 .ioctl
= sock_no_ioctl
,
1982 .listen
= tipc_listen
,
1983 .shutdown
= tipc_shutdown
,
1984 .setsockopt
= tipc_setsockopt
,
1985 .getsockopt
= tipc_getsockopt
,
1986 .sendmsg
= tipc_send_stream
,
1987 .recvmsg
= tipc_recv_stream
,
1988 .mmap
= sock_no_mmap
,
1989 .sendpage
= sock_no_sendpage
1992 static const struct net_proto_family tipc_family_ops
= {
1993 .owner
= THIS_MODULE
,
1995 .create
= tipc_sk_create
1998 static struct proto tipc_proto
= {
2000 .owner
= THIS_MODULE
,
2001 .obj_size
= sizeof(struct tipc_sock
),
2002 .sysctl_rmem
= sysctl_tipc_rmem
2005 static struct proto tipc_proto_kern
= {
2007 .obj_size
= sizeof(struct tipc_sock
),
2008 .sysctl_rmem
= sysctl_tipc_rmem
2012 * tipc_socket_init - initialize TIPC socket interface
2014 * Returns 0 on success, errno otherwise
2016 int tipc_socket_init(void)
2020 res
= proto_register(&tipc_proto
, 1);
2022 pr_err("Failed to register TIPC protocol type\n");
2026 res
= sock_register(&tipc_family_ops
);
2028 pr_err("Failed to register TIPC socket type\n");
2029 proto_unregister(&tipc_proto
);
2033 sockets_enabled
= 1;
2039 * tipc_socket_stop - stop TIPC socket interface
2041 void tipc_socket_stop(void)
2043 if (!sockets_enabled
)
2046 sockets_enabled
= 0;
2047 sock_unregister(tipc_family_ops
.family
);
2048 proto_unregister(&tipc_proto
);