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.
40 #include <linux/export.h>
42 #define SS_LISTENING -1 /* socket is listening */
43 #define SS_READY -2 /* socket is connectionless */
45 #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
47 static int backlog_rcv(struct sock
*sk
, struct sk_buff
*skb
);
48 static void tipc_data_ready(struct sock
*sk
, int len
);
49 static void tipc_write_space(struct sock
*sk
);
50 static int tipc_release(struct socket
*sock
);
51 static int tipc_accept(struct socket
*sock
, struct socket
*new_sock
, int flags
);
53 static const struct proto_ops packet_ops
;
54 static const struct proto_ops stream_ops
;
55 static const struct proto_ops msg_ops
;
57 static struct proto tipc_proto
;
58 static struct proto tipc_proto_kern
;
61 * Revised TIPC socket locking policy:
63 * Most socket operations take the standard socket lock when they start
64 * and hold it until they finish (or until they need to sleep). Acquiring
65 * this lock grants the owner exclusive access to the fields of the socket
66 * data structures, with the exception of the backlog queue. A few socket
67 * operations can be done without taking the socket lock because they only
68 * read socket information that never changes during the life of the socket.
70 * Socket operations may acquire the lock for the associated TIPC port if they
71 * need to perform an operation on the port. If any routine needs to acquire
72 * both the socket lock and the port lock it must take the socket lock first
73 * to avoid the risk of deadlock.
75 * The dispatcher handling incoming messages cannot grab the socket lock in
76 * the standard fashion, since invoked it runs at the BH level and cannot block.
77 * Instead, it checks to see if the socket lock is currently owned by someone,
78 * and either handles the message itself or adds it to the socket's backlog
79 * queue; in the latter case the queued message is processed once the process
80 * owning the socket lock releases it.
82 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
83 * the problem of a blocked socket operation preventing any other operations
84 * from occurring. However, applications must be careful if they have
85 * multiple threads trying to send (or receive) on the same socket, as these
86 * operations might interfere with each other. For example, doing a connect
87 * and a receive at the same time might allow the receive to consume the
88 * ACK message meant for the connect. While additional work could be done
89 * to try and overcome this, it doesn't seem to be worthwhile at the present.
91 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
92 * that another operation that must be performed in a non-blocking manner is
93 * not delayed for very long because the lock has already been taken.
95 * NOTE: This code assumes that certain fields of a port/socket pair are
96 * constant over its lifetime; such fields can be examined without taking
97 * the socket lock and/or port lock, and do not need to be re-read even
98 * after resuming processing after waiting. These fields include:
100 * - pointer to socket sk structure (aka tipc_sock structure)
101 * - pointer to port structure
108 * advance_rx_queue - discard first buffer in socket receive queue
110 * Caller must hold socket lock
112 static void advance_rx_queue(struct sock
*sk
)
114 kfree_skb(__skb_dequeue(&sk
->sk_receive_queue
));
118 * reject_rx_queue - reject all buffers in socket receive queue
120 * Caller must hold socket lock
122 static void reject_rx_queue(struct sock
*sk
)
126 while ((buf
= __skb_dequeue(&sk
->sk_receive_queue
)))
127 tipc_reject_msg(buf
, TIPC_ERR_NO_PORT
);
131 * tipc_sk_create - create a TIPC socket
132 * @net: network namespace (must be default network)
133 * @sock: pre-allocated socket structure
134 * @protocol: protocol indicator (must be 0)
135 * @kern: caused by kernel or by userspace?
137 * This routine creates additional data structures used by the TIPC socket,
138 * initializes them, and links them together.
140 * Returns 0 on success, errno otherwise
142 static int tipc_sk_create(struct net
*net
, struct socket
*sock
,
143 int protocol
, int kern
)
145 const struct proto_ops
*ops
;
148 struct tipc_sock
*tsk
;
149 struct tipc_port
*port
;
152 /* Validate arguments */
153 if (unlikely(protocol
!= 0))
154 return -EPROTONOSUPPORT
;
156 switch (sock
->type
) {
159 state
= SS_UNCONNECTED
;
163 state
= SS_UNCONNECTED
;
174 /* Allocate socket's protocol area */
176 sk
= sk_alloc(net
, AF_TIPC
, GFP_KERNEL
, &tipc_proto
);
178 sk
= sk_alloc(net
, AF_TIPC
, GFP_KERNEL
, &tipc_proto_kern
);
186 ref
= tipc_port_init(port
, TIPC_LOW_IMPORTANCE
);
188 pr_warn("Socket registration failed, ref. table exhausted\n");
193 /* Finish initializing socket data structures */
197 sock_init_data(sock
, sk
);
198 sk
->sk_backlog_rcv
= backlog_rcv
;
199 sk
->sk_rcvbuf
= sysctl_tipc_rmem
[1];
200 sk
->sk_data_ready
= tipc_data_ready
;
201 sk
->sk_write_space
= tipc_write_space
;
202 tipc_sk(sk
)->conn_timeout
= CONN_TIMEOUT_DEFAULT
;
203 tipc_port_unlock(port
);
205 if (sock
->state
== SS_READY
) {
206 tipc_port_set_unreturnable(port
, true);
207 if (sock
->type
== SOCK_DGRAM
)
208 tipc_port_set_unreliable(port
, true);
214 * tipc_sock_create_local - create TIPC socket from inside TIPC module
215 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
217 * We cannot use sock_creat_kern here because it bumps module user count.
218 * Since socket owner and creator is the same module we must make sure
219 * that module count remains zero for module local sockets, otherwise
220 * we cannot do rmmod.
222 * Returns 0 on success, errno otherwise
224 int tipc_sock_create_local(int type
, struct socket
**res
)
228 rc
= sock_create_lite(AF_TIPC
, type
, 0, res
);
230 pr_err("Failed to create kernel socket\n");
233 tipc_sk_create(&init_net
, *res
, 0, 1);
239 * tipc_sock_release_local - release socket created by tipc_sock_create_local
240 * @sock: the socket to be released.
242 * Module reference count is not incremented when such sockets are created,
243 * so we must keep it from being decremented when they are released.
245 void tipc_sock_release_local(struct socket
*sock
)
253 * tipc_sock_accept_local - accept a connection on a socket created
254 * with tipc_sock_create_local. Use this function to avoid that
255 * module reference count is inadvertently incremented.
257 * @sock: the accepting socket
258 * @newsock: reference to the new socket to be created
259 * @flags: socket flags
262 int tipc_sock_accept_local(struct socket
*sock
, struct socket
**newsock
,
265 struct sock
*sk
= sock
->sk
;
268 ret
= sock_create_lite(sk
->sk_family
, sk
->sk_type
,
269 sk
->sk_protocol
, newsock
);
273 ret
= tipc_accept(sock
, *newsock
, flags
);
275 sock_release(*newsock
);
278 (*newsock
)->ops
= sock
->ops
;
283 * tipc_release - destroy a TIPC socket
284 * @sock: socket to destroy
286 * This routine cleans up any messages that are still queued on the socket.
287 * For DGRAM and RDM socket types, all queued messages are rejected.
288 * For SEQPACKET and STREAM socket types, the first message is rejected
289 * and any others are discarded. (If the first message on a STREAM socket
290 * is partially-read, it is discarded and the next one is rejected instead.)
292 * NOTE: Rejected messages are not necessarily returned to the sender! They
293 * are returned or discarded according to the "destination droppable" setting
294 * specified for the message by the sender.
296 * Returns 0 on success, errno otherwise
298 static int tipc_release(struct socket
*sock
)
300 struct sock
*sk
= sock
->sk
;
301 struct tipc_sock
*tsk
;
302 struct tipc_port
*port
;
307 * Exit if socket isn't fully initialized (occurs when a failed accept()
308 * releases a pre-allocated child socket that was never used)
318 * Reject all unreceived messages, except on an active connection
319 * (which disconnects locally & sends a 'FIN+' to peer)
321 while (sock
->state
!= SS_DISCONNECTING
) {
322 buf
= __skb_dequeue(&sk
->sk_receive_queue
);
325 if (TIPC_SKB_CB(buf
)->handle
!= NULL
)
328 if ((sock
->state
== SS_CONNECTING
) ||
329 (sock
->state
== SS_CONNECTED
)) {
330 sock
->state
= SS_DISCONNECTING
;
331 tipc_port_disconnect(port
->ref
);
333 tipc_reject_msg(buf
, TIPC_ERR_NO_PORT
);
337 /* Destroy TIPC port; also disconnects an active connection and
338 * sends a 'FIN-' to peer.
340 tipc_port_destroy(port
);
342 /* Discard any remaining (connection-based) messages in receive queue */
343 __skb_queue_purge(&sk
->sk_receive_queue
);
345 /* Reject any messages that accumulated in backlog queue */
346 sock
->state
= SS_DISCONNECTING
;
356 * tipc_bind - associate or disassocate TIPC name(s) with a socket
357 * @sock: socket structure
358 * @uaddr: socket address describing name(s) and desired operation
359 * @uaddr_len: size of socket address data structure
361 * Name and name sequence binding is indicated using a positive scope value;
362 * a negative scope value unbinds the specified name. Specifying no name
363 * (i.e. a socket address length of 0) unbinds all names from the socket.
365 * Returns 0 on success, errno otherwise
367 * NOTE: This routine doesn't need to take the socket lock since it doesn't
368 * access any non-constant socket information.
370 static int tipc_bind(struct socket
*sock
, struct sockaddr
*uaddr
,
373 struct sock
*sk
= sock
->sk
;
374 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
375 struct tipc_sock
*tsk
= tipc_sk(sk
);
379 if (unlikely(!uaddr_len
)) {
380 res
= tipc_withdraw(&tsk
->port
, 0, NULL
);
384 if (uaddr_len
< sizeof(struct sockaddr_tipc
)) {
388 if (addr
->family
!= AF_TIPC
) {
393 if (addr
->addrtype
== TIPC_ADDR_NAME
)
394 addr
->addr
.nameseq
.upper
= addr
->addr
.nameseq
.lower
;
395 else if (addr
->addrtype
!= TIPC_ADDR_NAMESEQ
) {
400 if ((addr
->addr
.nameseq
.type
< TIPC_RESERVED_TYPES
) &&
401 (addr
->addr
.nameseq
.type
!= TIPC_TOP_SRV
) &&
402 (addr
->addr
.nameseq
.type
!= TIPC_CFG_SRV
)) {
407 res
= (addr
->scope
> 0) ?
408 tipc_publish(&tsk
->port
, addr
->scope
, &addr
->addr
.nameseq
) :
409 tipc_withdraw(&tsk
->port
, -addr
->scope
, &addr
->addr
.nameseq
);
416 * tipc_getname - get port ID of socket or peer socket
417 * @sock: socket structure
418 * @uaddr: area for returned socket address
419 * @uaddr_len: area for returned length of socket address
420 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
422 * Returns 0 on success, errno otherwise
424 * NOTE: This routine doesn't need to take the socket lock since it only
425 * accesses socket information that is unchanging (or which changes in
426 * a completely predictable manner).
428 static int tipc_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
429 int *uaddr_len
, int peer
)
431 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
432 struct tipc_sock
*tsk
= tipc_sk(sock
->sk
);
434 memset(addr
, 0, sizeof(*addr
));
436 if ((sock
->state
!= SS_CONNECTED
) &&
437 ((peer
!= 2) || (sock
->state
!= SS_DISCONNECTING
)))
439 addr
->addr
.id
.ref
= tipc_port_peerport(&tsk
->port
);
440 addr
->addr
.id
.node
= tipc_port_peernode(&tsk
->port
);
442 addr
->addr
.id
.ref
= tsk
->port
.ref
;
443 addr
->addr
.id
.node
= tipc_own_addr
;
446 *uaddr_len
= sizeof(*addr
);
447 addr
->addrtype
= TIPC_ADDR_ID
;
448 addr
->family
= AF_TIPC
;
450 addr
->addr
.name
.domain
= 0;
456 * tipc_poll - read and possibly block on pollmask
457 * @file: file structure associated with the socket
458 * @sock: socket for which to calculate the poll bits
461 * Returns pollmask value
464 * It appears that the usual socket locking mechanisms are not useful here
465 * since the pollmask info is potentially out-of-date the moment this routine
466 * exits. TCP and other protocols seem to rely on higher level poll routines
467 * to handle any preventable race conditions, so TIPC will do the same ...
469 * TIPC sets the returned events as follows:
471 * socket state flags set
472 * ------------ ---------
473 * unconnected no read flags
474 * POLLOUT if port is not congested
476 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
479 * connected POLLIN/POLLRDNORM if data in rx queue
480 * POLLOUT if port is not congested
482 * disconnecting POLLIN/POLLRDNORM/POLLHUP
485 * listening POLLIN if SYN in rx queue
488 * ready POLLIN/POLLRDNORM if data in rx queue
489 * [connectionless] POLLOUT (since port cannot be congested)
491 * IMPORTANT: The fact that a read or write operation is indicated does NOT
492 * imply that the operation will succeed, merely that it should be performed
493 * and will not block.
495 static unsigned int tipc_poll(struct file
*file
, struct socket
*sock
,
498 struct sock
*sk
= sock
->sk
;
499 struct tipc_sock
*tsk
= tipc_sk(sk
);
502 sock_poll_wait(file
, sk_sleep(sk
), wait
);
504 switch ((int)sock
->state
) {
506 if (!tsk
->port
.congested
)
511 if (!tsk
->port
.congested
)
516 if (!skb_queue_empty(&sk
->sk_receive_queue
))
517 mask
|= (POLLIN
| POLLRDNORM
);
519 case SS_DISCONNECTING
:
520 mask
= (POLLIN
| POLLRDNORM
| POLLHUP
);
528 * dest_name_check - verify user is permitted to send to specified port name
529 * @dest: destination address
530 * @m: descriptor for message to be sent
532 * Prevents restricted configuration commands from being issued by
533 * unauthorized users.
535 * Returns 0 if permission is granted, otherwise errno
537 static int dest_name_check(struct sockaddr_tipc
*dest
, struct msghdr
*m
)
539 struct tipc_cfg_msg_hdr hdr
;
541 if (likely(dest
->addr
.name
.name
.type
>= TIPC_RESERVED_TYPES
))
543 if (likely(dest
->addr
.name
.name
.type
== TIPC_TOP_SRV
))
545 if (likely(dest
->addr
.name
.name
.type
!= TIPC_CFG_SRV
))
548 if (!m
->msg_iovlen
|| (m
->msg_iov
[0].iov_len
< sizeof(hdr
)))
550 if (copy_from_user(&hdr
, m
->msg_iov
[0].iov_base
, sizeof(hdr
)))
552 if ((ntohs(hdr
.tcm_type
) & 0xC000) && (!capable(CAP_NET_ADMIN
)))
558 static int tipc_wait_for_sndmsg(struct socket
*sock
, long *timeo_p
)
560 struct sock
*sk
= sock
->sk
;
561 struct tipc_sock
*tsk
= tipc_sk(sk
);
566 int err
= sock_error(sk
);
569 if (sock
->state
== SS_DISCONNECTING
)
573 if (signal_pending(current
))
574 return sock_intr_errno(*timeo_p
);
576 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
577 done
= sk_wait_event(sk
, timeo_p
, !tsk
->port
.congested
);
578 finish_wait(sk_sleep(sk
), &wait
);
585 * tipc_sendmsg - send message in connectionless manner
586 * @iocb: if NULL, indicates that socket lock is already held
587 * @sock: socket structure
588 * @m: message to send
589 * @total_len: length of message
591 * Message must have an destination specified explicitly.
592 * Used for SOCK_RDM and SOCK_DGRAM messages,
593 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
594 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
596 * Returns the number of bytes sent on success, or errno otherwise
598 static int tipc_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
599 struct msghdr
*m
, size_t total_len
)
601 struct sock
*sk
= sock
->sk
;
602 struct tipc_sock
*tsk
= tipc_sk(sk
);
603 struct tipc_port
*port
= &tsk
->port
;
604 DECLARE_SOCKADDR(struct sockaddr_tipc
*, dest
, m
->msg_name
);
610 return -EDESTADDRREQ
;
611 if (unlikely((m
->msg_namelen
< sizeof(*dest
)) ||
612 (dest
->family
!= AF_TIPC
)))
614 if (total_len
> TIPC_MAX_USER_MSG_SIZE
)
620 needs_conn
= (sock
->state
!= SS_READY
);
621 if (unlikely(needs_conn
)) {
622 if (sock
->state
== SS_LISTENING
) {
626 if (sock
->state
!= SS_UNCONNECTED
) {
630 if (tsk
->port
.published
) {
634 if (dest
->addrtype
== TIPC_ADDR_NAME
) {
635 tsk
->port
.conn_type
= dest
->addr
.name
.name
.type
;
636 tsk
->port
.conn_instance
= dest
->addr
.name
.name
.instance
;
639 /* Abort any pending connection attempts (very unlikely) */
643 timeo
= sock_sndtimeo(sk
, m
->msg_flags
& MSG_DONTWAIT
);
645 if (dest
->addrtype
== TIPC_ADDR_NAME
) {
646 res
= dest_name_check(dest
, m
);
649 res
= tipc_send2name(port
,
650 &dest
->addr
.name
.name
,
651 dest
->addr
.name
.domain
,
654 } else if (dest
->addrtype
== TIPC_ADDR_ID
) {
655 res
= tipc_send2port(port
,
659 } else if (dest
->addrtype
== TIPC_ADDR_MCAST
) {
664 res
= dest_name_check(dest
, m
);
667 res
= tipc_port_mcast_xmit(port
,
672 if (likely(res
!= -ELINKCONG
)) {
673 if (needs_conn
&& (res
>= 0))
674 sock
->state
= SS_CONNECTING
;
677 res
= tipc_wait_for_sndmsg(sock
, &timeo
);
688 static int tipc_wait_for_sndpkt(struct socket
*sock
, long *timeo_p
)
690 struct sock
*sk
= sock
->sk
;
691 struct tipc_sock
*tsk
= tipc_sk(sk
);
692 struct tipc_port
*port
= &tsk
->port
;
697 int err
= sock_error(sk
);
700 if (sock
->state
== SS_DISCONNECTING
)
702 else if (sock
->state
!= SS_CONNECTED
)
706 if (signal_pending(current
))
707 return sock_intr_errno(*timeo_p
);
709 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
710 done
= sk_wait_event(sk
, timeo_p
,
711 (!port
->congested
|| !port
->connected
));
712 finish_wait(sk_sleep(sk
), &wait
);
718 * tipc_send_packet - send a connection-oriented message
719 * @iocb: if NULL, indicates that socket lock is already held
720 * @sock: socket structure
721 * @m: message to send
722 * @total_len: length of message
724 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
726 * Returns the number of bytes sent on success, or errno otherwise
728 static int tipc_send_packet(struct kiocb
*iocb
, struct socket
*sock
,
729 struct msghdr
*m
, size_t total_len
)
731 struct sock
*sk
= sock
->sk
;
732 struct tipc_sock
*tsk
= tipc_sk(sk
);
733 DECLARE_SOCKADDR(struct sockaddr_tipc
*, dest
, m
->msg_name
);
737 /* Handle implied connection establishment */
739 return tipc_sendmsg(iocb
, sock
, m
, total_len
);
741 if (total_len
> TIPC_MAX_USER_MSG_SIZE
)
747 if (unlikely(sock
->state
!= SS_CONNECTED
)) {
748 if (sock
->state
== SS_DISCONNECTING
)
755 timeo
= sock_sndtimeo(sk
, m
->msg_flags
& MSG_DONTWAIT
);
757 res
= tipc_send(&tsk
->port
, m
->msg_iov
, total_len
);
758 if (likely(res
!= -ELINKCONG
))
760 res
= tipc_wait_for_sndpkt(sock
, &timeo
);
771 * tipc_send_stream - send stream-oriented data
773 * @sock: socket structure
775 * @total_len: total length of data to be sent
777 * Used for SOCK_STREAM data.
779 * Returns the number of bytes sent on success (or partial success),
780 * or errno if no data sent
782 static int tipc_send_stream(struct kiocb
*iocb
, struct socket
*sock
,
783 struct msghdr
*m
, size_t total_len
)
785 struct sock
*sk
= sock
->sk
;
786 struct tipc_sock
*tsk
= tipc_sk(sk
);
787 struct msghdr my_msg
;
789 struct iovec
*curr_iov
;
791 char __user
*curr_start
;
800 /* Handle special cases where there is no connection */
801 if (unlikely(sock
->state
!= SS_CONNECTED
)) {
802 if (sock
->state
== SS_UNCONNECTED
)
803 res
= tipc_send_packet(NULL
, sock
, m
, total_len
);
805 res
= sock
->state
== SS_DISCONNECTING
? -EPIPE
: -ENOTCONN
;
809 if (unlikely(m
->msg_name
)) {
814 if (total_len
> (unsigned int)INT_MAX
) {
820 * Send each iovec entry using one or more messages
822 * Note: This algorithm is good for the most likely case
823 * (i.e. one large iovec entry), but could be improved to pass sets
824 * of small iovec entries into send_packet().
826 curr_iov
= m
->msg_iov
;
827 curr_iovlen
= m
->msg_iovlen
;
828 my_msg
.msg_iov
= &my_iov
;
829 my_msg
.msg_iovlen
= 1;
830 my_msg
.msg_flags
= m
->msg_flags
;
831 my_msg
.msg_name
= NULL
;
834 hdr_size
= msg_hdr_sz(&tsk
->port
.phdr
);
836 while (curr_iovlen
--) {
837 curr_start
= curr_iov
->iov_base
;
838 curr_left
= curr_iov
->iov_len
;
841 bytes_to_send
= tsk
->port
.max_pkt
- hdr_size
;
842 if (bytes_to_send
> TIPC_MAX_USER_MSG_SIZE
)
843 bytes_to_send
= TIPC_MAX_USER_MSG_SIZE
;
844 if (curr_left
< bytes_to_send
)
845 bytes_to_send
= curr_left
;
846 my_iov
.iov_base
= curr_start
;
847 my_iov
.iov_len
= bytes_to_send
;
848 res
= tipc_send_packet(NULL
, sock
, &my_msg
,
855 curr_left
-= bytes_to_send
;
856 curr_start
+= bytes_to_send
;
857 bytes_sent
+= bytes_to_send
;
869 * auto_connect - complete connection setup to a remote port
870 * @tsk: tipc socket structure
871 * @msg: peer's response message
873 * Returns 0 on success, errno otherwise
875 static int auto_connect(struct tipc_sock
*tsk
, struct tipc_msg
*msg
)
877 struct tipc_port
*port
= &tsk
->port
;
878 struct socket
*sock
= tsk
->sk
.sk_socket
;
879 struct tipc_portid peer
;
881 peer
.ref
= msg_origport(msg
);
882 peer
.node
= msg_orignode(msg
);
884 __tipc_port_connect(port
->ref
, port
, &peer
);
886 if (msg_importance(msg
) > TIPC_CRITICAL_IMPORTANCE
)
888 msg_set_importance(&port
->phdr
, (u32
)msg_importance(msg
));
889 sock
->state
= SS_CONNECTED
;
894 * set_orig_addr - capture sender's address for received message
895 * @m: descriptor for message info
896 * @msg: received message header
898 * Note: Address is not captured if not requested by receiver.
900 static void set_orig_addr(struct msghdr
*m
, struct tipc_msg
*msg
)
902 DECLARE_SOCKADDR(struct sockaddr_tipc
*, addr
, m
->msg_name
);
905 addr
->family
= AF_TIPC
;
906 addr
->addrtype
= TIPC_ADDR_ID
;
907 memset(&addr
->addr
, 0, sizeof(addr
->addr
));
908 addr
->addr
.id
.ref
= msg_origport(msg
);
909 addr
->addr
.id
.node
= msg_orignode(msg
);
910 addr
->addr
.name
.domain
= 0; /* could leave uninitialized */
911 addr
->scope
= 0; /* could leave uninitialized */
912 m
->msg_namelen
= sizeof(struct sockaddr_tipc
);
917 * anc_data_recv - optionally capture ancillary data for received message
918 * @m: descriptor for message info
919 * @msg: received message header
920 * @tport: TIPC port associated with message
922 * Note: Ancillary data is not captured if not requested by receiver.
924 * Returns 0 if successful, otherwise errno
926 static int anc_data_recv(struct msghdr
*m
, struct tipc_msg
*msg
,
927 struct tipc_port
*tport
)
935 if (likely(m
->msg_controllen
== 0))
938 /* Optionally capture errored message object(s) */
939 err
= msg
? msg_errcode(msg
) : 0;
942 anc_data
[1] = msg_data_sz(msg
);
943 res
= put_cmsg(m
, SOL_TIPC
, TIPC_ERRINFO
, 8, anc_data
);
947 res
= put_cmsg(m
, SOL_TIPC
, TIPC_RETDATA
, anc_data
[1],
954 /* Optionally capture message destination object */
955 dest_type
= msg
? msg_type(msg
) : TIPC_DIRECT_MSG
;
959 anc_data
[0] = msg_nametype(msg
);
960 anc_data
[1] = msg_namelower(msg
);
961 anc_data
[2] = msg_namelower(msg
);
965 anc_data
[0] = msg_nametype(msg
);
966 anc_data
[1] = msg_namelower(msg
);
967 anc_data
[2] = msg_nameupper(msg
);
970 has_name
= (tport
->conn_type
!= 0);
971 anc_data
[0] = tport
->conn_type
;
972 anc_data
[1] = tport
->conn_instance
;
973 anc_data
[2] = tport
->conn_instance
;
979 res
= put_cmsg(m
, SOL_TIPC
, TIPC_DESTNAME
, 12, anc_data
);
987 static int tipc_wait_for_rcvmsg(struct socket
*sock
, long timeo
)
989 struct sock
*sk
= sock
->sk
;
994 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
995 if (timeo
&& skb_queue_empty(&sk
->sk_receive_queue
)) {
996 if (sock
->state
== SS_DISCONNECTING
) {
1001 timeo
= schedule_timeout(timeo
);
1005 if (!skb_queue_empty(&sk
->sk_receive_queue
))
1007 err
= sock_intr_errno(timeo
);
1008 if (signal_pending(current
))
1014 finish_wait(sk_sleep(sk
), &wait
);
1019 * tipc_recvmsg - receive packet-oriented message
1021 * @m: descriptor for message info
1022 * @buf_len: total size of user buffer area
1023 * @flags: receive flags
1025 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1026 * If the complete message doesn't fit in user area, truncate it.
1028 * Returns size of returned message data, errno otherwise
1030 static int tipc_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1031 struct msghdr
*m
, size_t buf_len
, int flags
)
1033 struct sock
*sk
= sock
->sk
;
1034 struct tipc_sock
*tsk
= tipc_sk(sk
);
1035 struct tipc_port
*port
= &tsk
->port
;
1036 struct sk_buff
*buf
;
1037 struct tipc_msg
*msg
;
1043 /* Catch invalid receive requests */
1044 if (unlikely(!buf_len
))
1049 if (unlikely(sock
->state
== SS_UNCONNECTED
)) {
1054 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
1057 /* Look for a message in receive queue; wait if necessary */
1058 res
= tipc_wait_for_rcvmsg(sock
, timeo
);
1062 /* Look at first message in receive queue */
1063 buf
= skb_peek(&sk
->sk_receive_queue
);
1065 sz
= msg_data_sz(msg
);
1066 err
= msg_errcode(msg
);
1068 /* Discard an empty non-errored message & try again */
1069 if ((!sz
) && (!err
)) {
1070 advance_rx_queue(sk
);
1074 /* Capture sender's address (optional) */
1075 set_orig_addr(m
, msg
);
1077 /* Capture ancillary data (optional) */
1078 res
= anc_data_recv(m
, msg
, port
);
1082 /* Capture message data (if valid) & compute return value (always) */
1084 if (unlikely(buf_len
< sz
)) {
1086 m
->msg_flags
|= MSG_TRUNC
;
1088 res
= skb_copy_datagram_iovec(buf
, msg_hdr_sz(msg
),
1094 if ((sock
->state
== SS_READY
) ||
1095 ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
))
1101 /* Consume received message (optional) */
1102 if (likely(!(flags
& MSG_PEEK
))) {
1103 if ((sock
->state
!= SS_READY
) &&
1104 (++port
->conn_unacked
>= TIPC_FLOW_CONTROL_WIN
))
1105 tipc_acknowledge(port
->ref
, port
->conn_unacked
);
1106 advance_rx_queue(sk
);
1114 * tipc_recv_stream - receive stream-oriented data
1116 * @m: descriptor for message info
1117 * @buf_len: total size of user buffer area
1118 * @flags: receive flags
1120 * Used for SOCK_STREAM messages only. If not enough data is available
1121 * will optionally wait for more; never truncates data.
1123 * Returns size of returned message data, errno otherwise
1125 static int tipc_recv_stream(struct kiocb
*iocb
, struct socket
*sock
,
1126 struct msghdr
*m
, size_t buf_len
, int flags
)
1128 struct sock
*sk
= sock
->sk
;
1129 struct tipc_sock
*tsk
= tipc_sk(sk
);
1130 struct tipc_port
*port
= &tsk
->port
;
1131 struct sk_buff
*buf
;
1132 struct tipc_msg
*msg
;
1135 int sz_to_copy
, target
, needed
;
1140 /* Catch invalid receive attempts */
1141 if (unlikely(!buf_len
))
1146 if (unlikely(sock
->state
== SS_UNCONNECTED
)) {
1151 target
= sock_rcvlowat(sk
, flags
& MSG_WAITALL
, buf_len
);
1152 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
1155 /* Look for a message in receive queue; wait if necessary */
1156 res
= tipc_wait_for_rcvmsg(sock
, timeo
);
1160 /* Look at first message in receive queue */
1161 buf
= skb_peek(&sk
->sk_receive_queue
);
1163 sz
= msg_data_sz(msg
);
1164 err
= msg_errcode(msg
);
1166 /* Discard an empty non-errored message & try again */
1167 if ((!sz
) && (!err
)) {
1168 advance_rx_queue(sk
);
1172 /* Optionally capture sender's address & ancillary data of first msg */
1173 if (sz_copied
== 0) {
1174 set_orig_addr(m
, msg
);
1175 res
= anc_data_recv(m
, msg
, port
);
1180 /* Capture message data (if valid) & compute return value (always) */
1182 u32 offset
= (u32
)(unsigned long)(TIPC_SKB_CB(buf
)->handle
);
1185 needed
= (buf_len
- sz_copied
);
1186 sz_to_copy
= (sz
<= needed
) ? sz
: needed
;
1188 res
= skb_copy_datagram_iovec(buf
, msg_hdr_sz(msg
) + offset
,
1189 m
->msg_iov
, sz_to_copy
);
1193 sz_copied
+= sz_to_copy
;
1195 if (sz_to_copy
< sz
) {
1196 if (!(flags
& MSG_PEEK
))
1197 TIPC_SKB_CB(buf
)->handle
=
1198 (void *)(unsigned long)(offset
+ sz_to_copy
);
1203 goto exit
; /* can't add error msg to valid data */
1205 if ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
)
1211 /* Consume received message (optional) */
1212 if (likely(!(flags
& MSG_PEEK
))) {
1213 if (unlikely(++port
->conn_unacked
>= TIPC_FLOW_CONTROL_WIN
))
1214 tipc_acknowledge(port
->ref
, port
->conn_unacked
);
1215 advance_rx_queue(sk
);
1218 /* Loop around if more data is required */
1219 if ((sz_copied
< buf_len
) && /* didn't get all requested data */
1220 (!skb_queue_empty(&sk
->sk_receive_queue
) ||
1221 (sz_copied
< target
)) && /* and more is ready or required */
1222 (!(flags
& MSG_PEEK
)) && /* and aren't just peeking at data */
1223 (!err
)) /* and haven't reached a FIN */
1228 return sz_copied
? sz_copied
: res
;
1232 * tipc_write_space - wake up thread if port congestion is released
1235 static void tipc_write_space(struct sock
*sk
)
1237 struct socket_wq
*wq
;
1240 wq
= rcu_dereference(sk
->sk_wq
);
1241 if (wq_has_sleeper(wq
))
1242 wake_up_interruptible_sync_poll(&wq
->wait
, POLLOUT
|
1243 POLLWRNORM
| POLLWRBAND
);
1248 * tipc_data_ready - wake up threads to indicate messages have been received
1250 * @len: the length of messages
1252 static void tipc_data_ready(struct sock
*sk
, int len
)
1254 struct socket_wq
*wq
;
1257 wq
= rcu_dereference(sk
->sk_wq
);
1258 if (wq_has_sleeper(wq
))
1259 wake_up_interruptible_sync_poll(&wq
->wait
, POLLIN
|
1260 POLLRDNORM
| POLLRDBAND
);
1265 * filter_connect - Handle all incoming messages for a connection-based socket
1269 * Returns TIPC error status code and socket error status code
1270 * once it encounters some errors
1272 static u32
filter_connect(struct tipc_sock
*tsk
, struct sk_buff
**buf
)
1274 struct sock
*sk
= &tsk
->sk
;
1275 struct tipc_port
*port
= &tsk
->port
;
1276 struct socket
*sock
= sk
->sk_socket
;
1277 struct tipc_msg
*msg
= buf_msg(*buf
);
1279 u32 retval
= TIPC_ERR_NO_PORT
;
1285 switch ((int)sock
->state
) {
1287 /* Accept only connection-based messages sent by peer */
1288 if (msg_connected(msg
) && tipc_port_peer_msg(port
, msg
)) {
1289 if (unlikely(msg_errcode(msg
))) {
1290 sock
->state
= SS_DISCONNECTING
;
1291 __tipc_port_disconnect(port
);
1297 /* Accept only ACK or NACK message */
1298 if (unlikely(msg_errcode(msg
))) {
1299 sock
->state
= SS_DISCONNECTING
;
1300 sk
->sk_err
= ECONNREFUSED
;
1305 if (unlikely(!msg_connected(msg
)))
1308 res
= auto_connect(tsk
, msg
);
1310 sock
->state
= SS_DISCONNECTING
;
1316 /* If an incoming message is an 'ACK-', it should be
1317 * discarded here because it doesn't contain useful
1318 * data. In addition, we should try to wake up
1319 * connect() routine if sleeping.
1321 if (msg_data_sz(msg
) == 0) {
1324 if (waitqueue_active(sk_sleep(sk
)))
1325 wake_up_interruptible(sk_sleep(sk
));
1330 case SS_UNCONNECTED
:
1331 /* Accept only SYN message */
1332 if (!msg_connected(msg
) && !(msg_errcode(msg
)))
1335 case SS_DISCONNECTING
:
1338 pr_err("Unknown socket state %u\n", sock
->state
);
1344 * rcvbuf_limit - get proper overload limit of socket receive queue
1348 * For all connection oriented messages, irrespective of importance,
1349 * the default overload value (i.e. 67MB) is set as limit.
1351 * For all connectionless messages, by default new queue limits are
1354 * TIPC_LOW_IMPORTANCE (4 MB)
1355 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1356 * TIPC_HIGH_IMPORTANCE (16 MB)
1357 * TIPC_CRITICAL_IMPORTANCE (32 MB)
1359 * Returns overload limit according to corresponding message importance
1361 static unsigned int rcvbuf_limit(struct sock
*sk
, struct sk_buff
*buf
)
1363 struct tipc_msg
*msg
= buf_msg(buf
);
1365 if (msg_connected(msg
))
1366 return sysctl_tipc_rmem
[2];
1368 return sk
->sk_rcvbuf
>> TIPC_CRITICAL_IMPORTANCE
<<
1369 msg_importance(msg
);
1373 * filter_rcv - validate incoming message
1377 * Enqueues message on receive queue if acceptable; optionally handles
1378 * disconnect indication for a connected socket.
1380 * Called with socket lock already taken; port lock may also be taken.
1382 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1384 static u32
filter_rcv(struct sock
*sk
, struct sk_buff
*buf
)
1386 struct socket
*sock
= sk
->sk_socket
;
1387 struct tipc_sock
*tsk
= tipc_sk(sk
);
1388 struct tipc_msg
*msg
= buf_msg(buf
);
1389 unsigned int limit
= rcvbuf_limit(sk
, buf
);
1392 /* Reject message if it is wrong sort of message for socket */
1393 if (msg_type(msg
) > TIPC_DIRECT_MSG
)
1394 return TIPC_ERR_NO_PORT
;
1396 if (sock
->state
== SS_READY
) {
1397 if (msg_connected(msg
))
1398 return TIPC_ERR_NO_PORT
;
1400 res
= filter_connect(tsk
, &buf
);
1401 if (res
!= TIPC_OK
|| buf
== NULL
)
1405 /* Reject message if there isn't room to queue it */
1406 if (sk_rmem_alloc_get(sk
) + buf
->truesize
>= limit
)
1407 return TIPC_ERR_OVERLOAD
;
1409 /* Enqueue message */
1410 TIPC_SKB_CB(buf
)->handle
= NULL
;
1411 __skb_queue_tail(&sk
->sk_receive_queue
, buf
);
1412 skb_set_owner_r(buf
, sk
);
1414 sk
->sk_data_ready(sk
, 0);
1419 * backlog_rcv - handle incoming message from backlog queue
1423 * Caller must hold socket lock, but not port lock.
1427 static int backlog_rcv(struct sock
*sk
, struct sk_buff
*buf
)
1431 res
= filter_rcv(sk
, buf
);
1433 tipc_reject_msg(buf
, res
);
1438 * tipc_sk_rcv - handle incoming message
1439 * @sk: socket receiving message
1442 * Called with port lock already taken.
1444 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1446 u32
tipc_sk_rcv(struct sock
*sk
, struct sk_buff
*buf
)
1451 * Process message if socket is unlocked; otherwise add to backlog queue
1453 * This code is based on sk_receive_skb(), but must be distinct from it
1454 * since a TIPC-specific filter/reject mechanism is utilized
1457 if (!sock_owned_by_user(sk
)) {
1458 res
= filter_rcv(sk
, buf
);
1460 if (sk_add_backlog(sk
, buf
, rcvbuf_limit(sk
, buf
)))
1461 res
= TIPC_ERR_OVERLOAD
;
1470 static int tipc_wait_for_connect(struct socket
*sock
, long *timeo_p
)
1472 struct sock
*sk
= sock
->sk
;
1477 int err
= sock_error(sk
);
1482 if (signal_pending(current
))
1483 return sock_intr_errno(*timeo_p
);
1485 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
1486 done
= sk_wait_event(sk
, timeo_p
, sock
->state
!= SS_CONNECTING
);
1487 finish_wait(sk_sleep(sk
), &wait
);
1493 * tipc_connect - establish a connection to another TIPC port
1494 * @sock: socket structure
1495 * @dest: socket address for destination port
1496 * @destlen: size of socket address data structure
1497 * @flags: file-related flags associated with socket
1499 * Returns 0 on success, errno otherwise
1501 static int tipc_connect(struct socket
*sock
, struct sockaddr
*dest
,
1502 int destlen
, int flags
)
1504 struct sock
*sk
= sock
->sk
;
1505 struct sockaddr_tipc
*dst
= (struct sockaddr_tipc
*)dest
;
1506 struct msghdr m
= {NULL
,};
1507 long timeout
= (flags
& O_NONBLOCK
) ? 0 : tipc_sk(sk
)->conn_timeout
;
1508 socket_state previous
;
1513 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
1514 if (sock
->state
== SS_READY
) {
1520 * Reject connection attempt using multicast address
1522 * Note: send_msg() validates the rest of the address fields,
1523 * so there's no need to do it here
1525 if (dst
->addrtype
== TIPC_ADDR_MCAST
) {
1530 previous
= sock
->state
;
1531 switch (sock
->state
) {
1532 case SS_UNCONNECTED
:
1533 /* Send a 'SYN-' to destination */
1535 m
.msg_namelen
= destlen
;
1537 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1538 * indicate send_msg() is never blocked.
1541 m
.msg_flags
= MSG_DONTWAIT
;
1543 res
= tipc_sendmsg(NULL
, sock
, &m
, 0);
1544 if ((res
< 0) && (res
!= -EWOULDBLOCK
))
1547 /* Just entered SS_CONNECTING state; the only
1548 * difference is that return value in non-blocking
1549 * case is EINPROGRESS, rather than EALREADY.
1553 if (previous
== SS_CONNECTING
)
1557 timeout
= msecs_to_jiffies(timeout
);
1558 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1559 res
= tipc_wait_for_connect(sock
, &timeout
);
1574 * tipc_listen - allow socket to listen for incoming connections
1575 * @sock: socket structure
1578 * Returns 0 on success, errno otherwise
1580 static int tipc_listen(struct socket
*sock
, int len
)
1582 struct sock
*sk
= sock
->sk
;
1587 if (sock
->state
!= SS_UNCONNECTED
)
1590 sock
->state
= SS_LISTENING
;
1598 static int tipc_wait_for_accept(struct socket
*sock
, long timeo
)
1600 struct sock
*sk
= sock
->sk
;
1604 /* True wake-one mechanism for incoming connections: only
1605 * one process gets woken up, not the 'whole herd'.
1606 * Since we do not 'race & poll' for established sockets
1607 * anymore, the common case will execute the loop only once.
1610 prepare_to_wait_exclusive(sk_sleep(sk
), &wait
,
1611 TASK_INTERRUPTIBLE
);
1612 if (timeo
&& skb_queue_empty(&sk
->sk_receive_queue
)) {
1614 timeo
= schedule_timeout(timeo
);
1618 if (!skb_queue_empty(&sk
->sk_receive_queue
))
1621 if (sock
->state
!= SS_LISTENING
)
1623 err
= sock_intr_errno(timeo
);
1624 if (signal_pending(current
))
1630 finish_wait(sk_sleep(sk
), &wait
);
1635 * tipc_accept - wait for connection request
1636 * @sock: listening socket
1637 * @newsock: new socket that is to be connected
1638 * @flags: file-related flags associated with socket
1640 * Returns 0 on success, errno otherwise
1642 static int tipc_accept(struct socket
*sock
, struct socket
*new_sock
, int flags
)
1644 struct sock
*new_sk
, *sk
= sock
->sk
;
1645 struct sk_buff
*buf
;
1646 struct tipc_port
*new_port
;
1647 struct tipc_msg
*msg
;
1648 struct tipc_portid peer
;
1655 if (sock
->state
!= SS_LISTENING
) {
1659 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
1660 res
= tipc_wait_for_accept(sock
, timeo
);
1664 buf
= skb_peek(&sk
->sk_receive_queue
);
1666 res
= tipc_sk_create(sock_net(sock
->sk
), new_sock
, 0, 1);
1670 new_sk
= new_sock
->sk
;
1671 new_port
= &tipc_sk(new_sk
)->port
;
1672 new_ref
= new_port
->ref
;
1675 /* we lock on new_sk; but lockdep sees the lock on sk */
1676 lock_sock_nested(new_sk
, SINGLE_DEPTH_NESTING
);
1679 * Reject any stray messages received by new socket
1680 * before the socket lock was taken (very, very unlikely)
1682 reject_rx_queue(new_sk
);
1684 /* Connect new socket to it's peer */
1685 peer
.ref
= msg_origport(msg
);
1686 peer
.node
= msg_orignode(msg
);
1687 tipc_port_connect(new_ref
, &peer
);
1688 new_sock
->state
= SS_CONNECTED
;
1690 tipc_port_set_importance(new_port
, msg_importance(msg
));
1691 if (msg_named(msg
)) {
1692 new_port
->conn_type
= msg_nametype(msg
);
1693 new_port
->conn_instance
= msg_nameinst(msg
);
1697 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1698 * Respond to 'SYN+' by queuing it on new socket.
1700 if (!msg_data_sz(msg
)) {
1701 struct msghdr m
= {NULL
,};
1703 advance_rx_queue(sk
);
1704 tipc_send_packet(NULL
, new_sock
, &m
, 0);
1706 __skb_dequeue(&sk
->sk_receive_queue
);
1707 __skb_queue_head(&new_sk
->sk_receive_queue
, buf
);
1708 skb_set_owner_r(buf
, new_sk
);
1710 release_sock(new_sk
);
1717 * tipc_shutdown - shutdown socket connection
1718 * @sock: socket structure
1719 * @how: direction to close (must be SHUT_RDWR)
1721 * Terminates connection (if necessary), then purges socket's receive queue.
1723 * Returns 0 on success, errno otherwise
1725 static int tipc_shutdown(struct socket
*sock
, int how
)
1727 struct sock
*sk
= sock
->sk
;
1728 struct tipc_sock
*tsk
= tipc_sk(sk
);
1729 struct tipc_port
*port
= &tsk
->port
;
1730 struct sk_buff
*buf
;
1733 if (how
!= SHUT_RDWR
)
1738 switch (sock
->state
) {
1743 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
1744 buf
= __skb_dequeue(&sk
->sk_receive_queue
);
1746 if (TIPC_SKB_CB(buf
)->handle
!= NULL
) {
1750 tipc_port_disconnect(port
->ref
);
1751 tipc_reject_msg(buf
, TIPC_CONN_SHUTDOWN
);
1753 tipc_port_shutdown(port
->ref
);
1756 sock
->state
= SS_DISCONNECTING
;
1760 case SS_DISCONNECTING
:
1762 /* Discard any unreceived messages */
1763 __skb_queue_purge(&sk
->sk_receive_queue
);
1765 /* Wake up anyone sleeping in poll */
1766 sk
->sk_state_change(sk
);
1779 * tipc_setsockopt - set socket option
1780 * @sock: socket structure
1781 * @lvl: option level
1782 * @opt: option identifier
1783 * @ov: pointer to new option value
1784 * @ol: length of option value
1786 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
1787 * (to ease compatibility).
1789 * Returns 0 on success, errno otherwise
1791 static int tipc_setsockopt(struct socket
*sock
, int lvl
, int opt
,
1792 char __user
*ov
, unsigned int ol
)
1794 struct sock
*sk
= sock
->sk
;
1795 struct tipc_sock
*tsk
= tipc_sk(sk
);
1796 struct tipc_port
*port
= &tsk
->port
;
1800 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
1802 if (lvl
!= SOL_TIPC
)
1803 return -ENOPROTOOPT
;
1804 if (ol
< sizeof(value
))
1806 res
= get_user(value
, (u32 __user
*)ov
);
1813 case TIPC_IMPORTANCE
:
1814 tipc_port_set_importance(port
, value
);
1816 case TIPC_SRC_DROPPABLE
:
1817 if (sock
->type
!= SOCK_STREAM
)
1818 tipc_port_set_unreliable(port
, value
);
1822 case TIPC_DEST_DROPPABLE
:
1823 tipc_port_set_unreturnable(port
, value
);
1825 case TIPC_CONN_TIMEOUT
:
1826 tipc_sk(sk
)->conn_timeout
= value
;
1827 /* no need to set "res", since already 0 at this point */
1839 * tipc_getsockopt - get socket option
1840 * @sock: socket structure
1841 * @lvl: option level
1842 * @opt: option identifier
1843 * @ov: receptacle for option value
1844 * @ol: receptacle for length of option value
1846 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
1847 * (to ease compatibility).
1849 * Returns 0 on success, errno otherwise
1851 static int tipc_getsockopt(struct socket
*sock
, int lvl
, int opt
,
1852 char __user
*ov
, int __user
*ol
)
1854 struct sock
*sk
= sock
->sk
;
1855 struct tipc_sock
*tsk
= tipc_sk(sk
);
1856 struct tipc_port
*port
= &tsk
->port
;
1861 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
1862 return put_user(0, ol
);
1863 if (lvl
!= SOL_TIPC
)
1864 return -ENOPROTOOPT
;
1865 res
= get_user(len
, ol
);
1872 case TIPC_IMPORTANCE
:
1873 value
= tipc_port_importance(port
);
1875 case TIPC_SRC_DROPPABLE
:
1876 value
= tipc_port_unreliable(port
);
1878 case TIPC_DEST_DROPPABLE
:
1879 value
= tipc_port_unreturnable(port
);
1881 case TIPC_CONN_TIMEOUT
:
1882 value
= tipc_sk(sk
)->conn_timeout
;
1883 /* no need to set "res", since already 0 at this point */
1885 case TIPC_NODE_RECVQ_DEPTH
:
1886 value
= 0; /* was tipc_queue_size, now obsolete */
1888 case TIPC_SOCK_RECVQ_DEPTH
:
1889 value
= skb_queue_len(&sk
->sk_receive_queue
);
1898 return res
; /* "get" failed */
1900 if (len
< sizeof(value
))
1903 if (copy_to_user(ov
, &value
, sizeof(value
)))
1906 return put_user(sizeof(value
), ol
);
1909 /* Protocol switches for the various types of TIPC sockets */
1911 static const struct proto_ops msg_ops
= {
1912 .owner
= THIS_MODULE
,
1914 .release
= tipc_release
,
1916 .connect
= tipc_connect
,
1917 .socketpair
= sock_no_socketpair
,
1918 .accept
= sock_no_accept
,
1919 .getname
= tipc_getname
,
1921 .ioctl
= sock_no_ioctl
,
1922 .listen
= sock_no_listen
,
1923 .shutdown
= tipc_shutdown
,
1924 .setsockopt
= tipc_setsockopt
,
1925 .getsockopt
= tipc_getsockopt
,
1926 .sendmsg
= tipc_sendmsg
,
1927 .recvmsg
= tipc_recvmsg
,
1928 .mmap
= sock_no_mmap
,
1929 .sendpage
= sock_no_sendpage
1932 static const struct proto_ops packet_ops
= {
1933 .owner
= THIS_MODULE
,
1935 .release
= tipc_release
,
1937 .connect
= tipc_connect
,
1938 .socketpair
= sock_no_socketpair
,
1939 .accept
= tipc_accept
,
1940 .getname
= tipc_getname
,
1942 .ioctl
= sock_no_ioctl
,
1943 .listen
= tipc_listen
,
1944 .shutdown
= tipc_shutdown
,
1945 .setsockopt
= tipc_setsockopt
,
1946 .getsockopt
= tipc_getsockopt
,
1947 .sendmsg
= tipc_send_packet
,
1948 .recvmsg
= tipc_recvmsg
,
1949 .mmap
= sock_no_mmap
,
1950 .sendpage
= sock_no_sendpage
1953 static const struct proto_ops stream_ops
= {
1954 .owner
= THIS_MODULE
,
1956 .release
= tipc_release
,
1958 .connect
= tipc_connect
,
1959 .socketpair
= sock_no_socketpair
,
1960 .accept
= tipc_accept
,
1961 .getname
= tipc_getname
,
1963 .ioctl
= sock_no_ioctl
,
1964 .listen
= tipc_listen
,
1965 .shutdown
= tipc_shutdown
,
1966 .setsockopt
= tipc_setsockopt
,
1967 .getsockopt
= tipc_getsockopt
,
1968 .sendmsg
= tipc_send_stream
,
1969 .recvmsg
= tipc_recv_stream
,
1970 .mmap
= sock_no_mmap
,
1971 .sendpage
= sock_no_sendpage
1974 static const struct net_proto_family tipc_family_ops
= {
1975 .owner
= THIS_MODULE
,
1977 .create
= tipc_sk_create
1980 static struct proto tipc_proto
= {
1982 .owner
= THIS_MODULE
,
1983 .obj_size
= sizeof(struct tipc_sock
),
1984 .sysctl_rmem
= sysctl_tipc_rmem
1987 static struct proto tipc_proto_kern
= {
1989 .obj_size
= sizeof(struct tipc_sock
),
1990 .sysctl_rmem
= sysctl_tipc_rmem
1994 * tipc_socket_init - initialize TIPC socket interface
1996 * Returns 0 on success, errno otherwise
1998 int tipc_socket_init(void)
2002 res
= proto_register(&tipc_proto
, 1);
2004 pr_err("Failed to register TIPC protocol type\n");
2008 res
= sock_register(&tipc_family_ops
);
2010 pr_err("Failed to register TIPC socket type\n");
2011 proto_unregister(&tipc_proto
);
2019 * tipc_socket_stop - stop TIPC socket interface
2021 void tipc_socket_stop(void)
2023 sock_unregister(tipc_family_ops
.family
);
2024 proto_unregister(&tipc_proto
);