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 release(struct socket
*sock
);
64 static int 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
;
74 * Revised TIPC socket locking policy:
76 * Most socket operations take the standard socket lock when they start
77 * and hold it until they finish (or until they need to sleep). Acquiring
78 * this lock grants the owner exclusive access to the fields of the socket
79 * data structures, with the exception of the backlog queue. A few socket
80 * operations can be done without taking the socket lock because they only
81 * read socket information that never changes during the life of the socket.
83 * Socket operations may acquire the lock for the associated TIPC port if they
84 * need to perform an operation on the port. If any routine needs to acquire
85 * both the socket lock and the port lock it must take the socket lock first
86 * to avoid the risk of deadlock.
88 * The dispatcher handling incoming messages cannot grab the socket lock in
89 * the standard fashion, since invoked it runs at the BH level and cannot block.
90 * Instead, it checks to see if the socket lock is currently owned by someone,
91 * and either handles the message itself or adds it to the socket's backlog
92 * queue; in the latter case the queued message is processed once the process
93 * owning the socket lock releases it.
95 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
96 * the problem of a blocked socket operation preventing any other operations
97 * from occurring. However, applications must be careful if they have
98 * multiple threads trying to send (or receive) on the same socket, as these
99 * operations might interfere with each other. For example, doing a connect
100 * and a receive at the same time might allow the receive to consume the
101 * ACK message meant for the connect. While additional work could be done
102 * to try and overcome this, it doesn't seem to be worthwhile at the present.
104 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
105 * that another operation that must be performed in a non-blocking manner is
106 * not delayed for very long because the lock has already been taken.
108 * NOTE: This code assumes that certain fields of a port/socket pair are
109 * constant over its lifetime; such fields can be examined without taking
110 * the socket lock and/or port lock, and do not need to be re-read even
111 * after resuming processing after waiting. These fields include:
113 * - pointer to socket sk structure (aka tipc_sock structure)
114 * - pointer to port structure
119 * advance_rx_queue - discard first buffer in socket receive queue
121 * Caller must hold socket lock
123 static void advance_rx_queue(struct sock
*sk
)
125 kfree_skb(__skb_dequeue(&sk
->sk_receive_queue
));
129 * reject_rx_queue - reject all buffers in socket receive queue
131 * Caller must hold socket lock
133 static void reject_rx_queue(struct sock
*sk
)
137 while ((buf
= __skb_dequeue(&sk
->sk_receive_queue
)))
138 tipc_reject_msg(buf
, TIPC_ERR_NO_PORT
);
142 * tipc_sk_create - create a TIPC socket
143 * @net: network namespace (must be default network)
144 * @sock: pre-allocated socket structure
145 * @protocol: protocol indicator (must be 0)
146 * @kern: caused by kernel or by userspace?
148 * This routine creates additional data structures used by the TIPC socket,
149 * initializes them, and links them together.
151 * Returns 0 on success, errno otherwise
153 static int tipc_sk_create(struct net
*net
, struct socket
*sock
, int protocol
,
156 const struct proto_ops
*ops
;
159 struct tipc_port
*tp_ptr
;
161 /* Validate arguments */
162 if (unlikely(protocol
!= 0))
163 return -EPROTONOSUPPORT
;
165 switch (sock
->type
) {
168 state
= SS_UNCONNECTED
;
172 state
= SS_UNCONNECTED
;
183 /* Allocate socket's protocol area */
185 sk
= sk_alloc(net
, AF_TIPC
, GFP_KERNEL
, &tipc_proto
);
187 sk
= sk_alloc(net
, AF_TIPC
, GFP_KERNEL
, &tipc_proto_kern
);
192 /* Allocate TIPC port for socket to use */
193 tp_ptr
= tipc_createport(sk
, &dispatch
, &wakeupdispatch
,
194 TIPC_LOW_IMPORTANCE
);
195 if (unlikely(!tp_ptr
)) {
200 /* Finish initializing socket data structures */
204 sock_init_data(sock
, sk
);
205 sk
->sk_backlog_rcv
= backlog_rcv
;
206 sk
->sk_rcvbuf
= sysctl_tipc_rmem
[1];
207 sk
->sk_data_ready
= tipc_data_ready
;
208 sk
->sk_write_space
= tipc_write_space
;
209 tipc_sk(sk
)->p
= tp_ptr
;
210 tipc_sk(sk
)->conn_timeout
= CONN_TIMEOUT_DEFAULT
;
212 spin_unlock_bh(tp_ptr
->lock
);
214 if (sock
->state
== SS_READY
) {
215 tipc_set_portunreturnable(tp_ptr
->ref
, 1);
216 if (sock
->type
== SOCK_DGRAM
)
217 tipc_set_portunreliable(tp_ptr
->ref
, 1);
224 * tipc_sock_create_local - create TIPC socket from inside TIPC module
225 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
227 * We cannot use sock_creat_kern here because it bumps module user count.
228 * Since socket owner and creator is the same module we must make sure
229 * that module count remains zero for module local sockets, otherwise
230 * we cannot do rmmod.
232 * Returns 0 on success, errno otherwise
234 int tipc_sock_create_local(int type
, struct socket
**res
)
238 rc
= sock_create_lite(AF_TIPC
, type
, 0, res
);
240 pr_err("Failed to create kernel socket\n");
243 tipc_sk_create(&init_net
, *res
, 0, 1);
249 * tipc_sock_release_local - release socket created by tipc_sock_create_local
250 * @sock: the socket to be released.
252 * Module reference count is not incremented when such sockets are created,
253 * so we must keep it from being decremented when they are released.
255 void tipc_sock_release_local(struct socket
*sock
)
263 * tipc_sock_accept_local - accept a connection on a socket created
264 * with tipc_sock_create_local. Use this function to avoid that
265 * module reference count is inadvertently incremented.
267 * @sock: the accepting socket
268 * @newsock: reference to the new socket to be created
269 * @flags: socket flags
272 int tipc_sock_accept_local(struct socket
*sock
, struct socket
**newsock
,
275 struct sock
*sk
= sock
->sk
;
278 ret
= sock_create_lite(sk
->sk_family
, sk
->sk_type
,
279 sk
->sk_protocol
, newsock
);
283 ret
= accept(sock
, *newsock
, flags
);
285 sock_release(*newsock
);
288 (*newsock
)->ops
= sock
->ops
;
293 * release - destroy a TIPC socket
294 * @sock: socket to destroy
296 * This routine cleans up any messages that are still queued on the socket.
297 * For DGRAM and RDM socket types, all queued messages are rejected.
298 * For SEQPACKET and STREAM socket types, the first message is rejected
299 * and any others are discarded. (If the first message on a STREAM socket
300 * is partially-read, it is discarded and the next one is rejected instead.)
302 * NOTE: Rejected messages are not necessarily returned to the sender! They
303 * are returned or discarded according to the "destination droppable" setting
304 * specified for the message by the sender.
306 * Returns 0 on success, errno otherwise
308 static int release(struct socket
*sock
)
310 struct sock
*sk
= sock
->sk
;
311 struct tipc_port
*tport
;
316 * Exit if socket isn't fully initialized (occurs when a failed accept()
317 * releases a pre-allocated child socket that was never used)
322 tport
= tipc_sk_port(sk
);
326 * Reject all unreceived messages, except on an active connection
327 * (which disconnects locally & sends a 'FIN+' to peer)
329 while (sock
->state
!= SS_DISCONNECTING
) {
330 buf
= __skb_dequeue(&sk
->sk_receive_queue
);
333 if (TIPC_SKB_CB(buf
)->handle
!= NULL
)
336 if ((sock
->state
== SS_CONNECTING
) ||
337 (sock
->state
== SS_CONNECTED
)) {
338 sock
->state
= SS_DISCONNECTING
;
339 tipc_disconnect(tport
->ref
);
341 tipc_reject_msg(buf
, TIPC_ERR_NO_PORT
);
346 * Delete TIPC port; this ensures no more messages are queued
347 * (also disconnects an active connection & sends a 'FIN-' to peer)
349 res
= tipc_deleteport(tport
);
351 /* Discard any remaining (connection-based) messages in receive queue */
352 __skb_queue_purge(&sk
->sk_receive_queue
);
354 /* Reject any messages that accumulated in backlog queue */
355 sock
->state
= SS_DISCONNECTING
;
365 * bind - associate or disassocate TIPC name(s) with a socket
366 * @sock: socket structure
367 * @uaddr: socket address describing name(s) and desired operation
368 * @uaddr_len: size of socket address data structure
370 * Name and name sequence binding is indicated using a positive scope value;
371 * a negative scope value unbinds the specified name. Specifying no name
372 * (i.e. a socket address length of 0) unbinds all names from the socket.
374 * Returns 0 on success, errno otherwise
376 * NOTE: This routine doesn't need to take the socket lock since it doesn't
377 * access any non-constant socket information.
379 static int bind(struct socket
*sock
, struct sockaddr
*uaddr
, int uaddr_len
)
381 struct sock
*sk
= sock
->sk
;
382 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
383 struct tipc_port
*tport
= tipc_sk_port(sock
->sk
);
387 if (unlikely(!uaddr_len
)) {
388 res
= tipc_withdraw(tport
, 0, NULL
);
392 if (uaddr_len
< sizeof(struct sockaddr_tipc
)) {
396 if (addr
->family
!= AF_TIPC
) {
401 if (addr
->addrtype
== TIPC_ADDR_NAME
)
402 addr
->addr
.nameseq
.upper
= addr
->addr
.nameseq
.lower
;
403 else if (addr
->addrtype
!= TIPC_ADDR_NAMESEQ
) {
408 if ((addr
->addr
.nameseq
.type
< TIPC_RESERVED_TYPES
) &&
409 (addr
->addr
.nameseq
.type
!= TIPC_TOP_SRV
) &&
410 (addr
->addr
.nameseq
.type
!= TIPC_CFG_SRV
)) {
415 res
= (addr
->scope
> 0) ?
416 tipc_publish(tport
, addr
->scope
, &addr
->addr
.nameseq
) :
417 tipc_withdraw(tport
, -addr
->scope
, &addr
->addr
.nameseq
);
424 * get_name - get port ID of socket or peer socket
425 * @sock: socket structure
426 * @uaddr: area for returned socket address
427 * @uaddr_len: area for returned length of socket address
428 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
430 * Returns 0 on success, errno otherwise
432 * NOTE: This routine doesn't need to take the socket lock since it only
433 * accesses socket information that is unchanging (or which changes in
434 * a completely predictable manner).
436 static int get_name(struct socket
*sock
, struct sockaddr
*uaddr
,
437 int *uaddr_len
, int peer
)
439 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
440 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
442 memset(addr
, 0, sizeof(*addr
));
444 if ((sock
->state
!= SS_CONNECTED
) &&
445 ((peer
!= 2) || (sock
->state
!= SS_DISCONNECTING
)))
447 addr
->addr
.id
.ref
= tsock
->peer_name
.ref
;
448 addr
->addr
.id
.node
= tsock
->peer_name
.node
;
450 addr
->addr
.id
.ref
= tsock
->p
->ref
;
451 addr
->addr
.id
.node
= tipc_own_addr
;
454 *uaddr_len
= sizeof(*addr
);
455 addr
->addrtype
= TIPC_ADDR_ID
;
456 addr
->family
= AF_TIPC
;
458 addr
->addr
.name
.domain
= 0;
464 * poll - read and possibly block on pollmask
465 * @file: file structure associated with the socket
466 * @sock: socket for which to calculate the poll bits
469 * Returns pollmask value
472 * It appears that the usual socket locking mechanisms are not useful here
473 * since the pollmask info is potentially out-of-date the moment this routine
474 * exits. TCP and other protocols seem to rely on higher level poll routines
475 * to handle any preventable race conditions, so TIPC will do the same ...
477 * TIPC sets the returned events as follows:
479 * socket state flags set
480 * ------------ ---------
481 * unconnected no read flags
482 * POLLOUT if port is not congested
484 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
487 * connected POLLIN/POLLRDNORM if data in rx queue
488 * POLLOUT if port is not congested
490 * disconnecting POLLIN/POLLRDNORM/POLLHUP
493 * listening POLLIN if SYN in rx queue
496 * ready POLLIN/POLLRDNORM if data in rx queue
497 * [connectionless] POLLOUT (since port cannot be congested)
499 * IMPORTANT: The fact that a read or write operation is indicated does NOT
500 * imply that the operation will succeed, merely that it should be performed
501 * and will not block.
503 static unsigned int poll(struct file
*file
, struct socket
*sock
,
506 struct sock
*sk
= sock
->sk
;
509 sock_poll_wait(file
, sk_sleep(sk
), wait
);
511 switch ((int)sock
->state
) {
513 if (!tipc_sk_port(sk
)->congested
)
518 if (!tipc_sk_port(sk
)->congested
)
523 if (!skb_queue_empty(&sk
->sk_receive_queue
))
524 mask
|= (POLLIN
| POLLRDNORM
);
526 case SS_DISCONNECTING
:
527 mask
= (POLLIN
| POLLRDNORM
| POLLHUP
);
535 * dest_name_check - verify user is permitted to send to specified port name
536 * @dest: destination address
537 * @m: descriptor for message to be sent
539 * Prevents restricted configuration commands from being issued by
540 * unauthorized users.
542 * Returns 0 if permission is granted, otherwise errno
544 static int dest_name_check(struct sockaddr_tipc
*dest
, struct msghdr
*m
)
546 struct tipc_cfg_msg_hdr hdr
;
548 if (likely(dest
->addr
.name
.name
.type
>= TIPC_RESERVED_TYPES
))
550 if (likely(dest
->addr
.name
.name
.type
== TIPC_TOP_SRV
))
552 if (likely(dest
->addr
.name
.name
.type
!= TIPC_CFG_SRV
))
555 if (!m
->msg_iovlen
|| (m
->msg_iov
[0].iov_len
< sizeof(hdr
)))
557 if (copy_from_user(&hdr
, m
->msg_iov
[0].iov_base
, sizeof(hdr
)))
559 if ((ntohs(hdr
.tcm_type
) & 0xC000) && (!capable(CAP_NET_ADMIN
)))
565 static int tipc_wait_for_sndmsg(struct socket
*sock
, long *timeo_p
)
567 struct sock
*sk
= sock
->sk
;
568 struct tipc_port
*tport
= tipc_sk_port(sk
);
573 int err
= sock_error(sk
);
576 if (sock
->state
== SS_DISCONNECTING
)
580 if (signal_pending(current
))
581 return sock_intr_errno(*timeo_p
);
583 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
584 done
= sk_wait_event(sk
, timeo_p
, !tport
->congested
);
585 finish_wait(sk_sleep(sk
), &wait
);
591 * send_msg - send message in connectionless manner
592 * @iocb: if NULL, indicates that socket lock is already held
593 * @sock: socket structure
594 * @m: message to send
595 * @total_len: length of message
597 * Message must have an destination specified explicitly.
598 * Used for SOCK_RDM and SOCK_DGRAM messages,
599 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
600 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
602 * Returns the number of bytes sent on success, or errno otherwise
604 static int send_msg(struct kiocb
*iocb
, struct socket
*sock
,
605 struct msghdr
*m
, size_t total_len
)
607 struct sock
*sk
= sock
->sk
;
608 struct tipc_port
*tport
= tipc_sk_port(sk
);
609 DECLARE_SOCKADDR(struct sockaddr_tipc
*, dest
, m
->msg_name
);
615 return -EDESTADDRREQ
;
616 if (unlikely((m
->msg_namelen
< sizeof(*dest
)) ||
617 (dest
->family
!= AF_TIPC
)))
619 if (total_len
> TIPC_MAX_USER_MSG_SIZE
)
625 needs_conn
= (sock
->state
!= SS_READY
);
626 if (unlikely(needs_conn
)) {
627 if (sock
->state
== SS_LISTENING
) {
631 if (sock
->state
!= SS_UNCONNECTED
) {
635 if (tport
->published
) {
639 if (dest
->addrtype
== TIPC_ADDR_NAME
) {
640 tport
->conn_type
= dest
->addr
.name
.name
.type
;
641 tport
->conn_instance
= dest
->addr
.name
.name
.instance
;
644 /* Abort any pending connection attempts (very unlikely) */
648 timeo
= sock_sndtimeo(sk
, m
->msg_flags
& MSG_DONTWAIT
);
650 if (dest
->addrtype
== TIPC_ADDR_NAME
) {
651 res
= dest_name_check(dest
, m
);
654 res
= tipc_send2name(tport
->ref
,
655 &dest
->addr
.name
.name
,
656 dest
->addr
.name
.domain
,
659 } else if (dest
->addrtype
== TIPC_ADDR_ID
) {
660 res
= tipc_send2port(tport
->ref
,
664 } else if (dest
->addrtype
== TIPC_ADDR_MCAST
) {
669 res
= dest_name_check(dest
, m
);
672 res
= tipc_multicast(tport
->ref
,
677 if (likely(res
!= -ELINKCONG
)) {
678 if (needs_conn
&& (res
>= 0))
679 sock
->state
= SS_CONNECTING
;
682 res
= tipc_wait_for_sndmsg(sock
, &timeo
);
693 static int tipc_wait_for_sndpkt(struct socket
*sock
, long *timeo_p
)
695 struct sock
*sk
= sock
->sk
;
696 struct tipc_port
*tport
= tipc_sk_port(sk
);
701 int err
= sock_error(sk
);
704 if (sock
->state
== SS_DISCONNECTING
)
706 else if (sock
->state
!= SS_CONNECTED
)
710 if (signal_pending(current
))
711 return sock_intr_errno(*timeo_p
);
713 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
714 done
= sk_wait_event(sk
, timeo_p
,
715 (!tport
->congested
|| !tport
->connected
));
716 finish_wait(sk_sleep(sk
), &wait
);
722 * send_packet - send a connection-oriented message
723 * @iocb: if NULL, indicates that socket lock is already held
724 * @sock: socket structure
725 * @m: message to send
726 * @total_len: length of message
728 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
730 * Returns the number of bytes sent on success, or errno otherwise
732 static int send_packet(struct kiocb
*iocb
, struct socket
*sock
,
733 struct msghdr
*m
, size_t total_len
)
735 struct sock
*sk
= sock
->sk
;
736 struct tipc_port
*tport
= tipc_sk_port(sk
);
737 DECLARE_SOCKADDR(struct sockaddr_tipc
*, dest
, m
->msg_name
);
741 /* Handle implied connection establishment */
743 return send_msg(iocb
, sock
, m
, total_len
);
745 if (total_len
> TIPC_MAX_USER_MSG_SIZE
)
751 if (unlikely(sock
->state
!= SS_CONNECTED
)) {
752 if (sock
->state
== SS_DISCONNECTING
)
759 timeo
= sock_sndtimeo(sk
, m
->msg_flags
& MSG_DONTWAIT
);
761 res
= tipc_send(tport
->ref
, m
->msg_iov
, total_len
);
762 if (likely(res
!= -ELINKCONG
))
764 res
= tipc_wait_for_sndpkt(sock
, &timeo
);
775 * send_stream - send stream-oriented data
777 * @sock: socket structure
779 * @total_len: total length of data to be sent
781 * Used for SOCK_STREAM data.
783 * Returns the number of bytes sent on success (or partial success),
784 * or errno if no data sent
786 static int send_stream(struct kiocb
*iocb
, struct socket
*sock
,
787 struct msghdr
*m
, size_t total_len
)
789 struct sock
*sk
= sock
->sk
;
790 struct tipc_port
*tport
= tipc_sk_port(sk
);
791 struct msghdr my_msg
;
793 struct iovec
*curr_iov
;
795 char __user
*curr_start
;
804 /* Handle special cases where there is no connection */
805 if (unlikely(sock
->state
!= SS_CONNECTED
)) {
806 if (sock
->state
== SS_UNCONNECTED
)
807 res
= send_packet(NULL
, sock
, m
, total_len
);
809 res
= sock
->state
== SS_DISCONNECTING
? -EPIPE
: -ENOTCONN
;
813 if (unlikely(m
->msg_name
)) {
818 if (total_len
> (unsigned int)INT_MAX
) {
824 * Send each iovec entry using one or more messages
826 * Note: This algorithm is good for the most likely case
827 * (i.e. one large iovec entry), but could be improved to pass sets
828 * of small iovec entries into send_packet().
830 curr_iov
= m
->msg_iov
;
831 curr_iovlen
= m
->msg_iovlen
;
832 my_msg
.msg_iov
= &my_iov
;
833 my_msg
.msg_iovlen
= 1;
834 my_msg
.msg_flags
= m
->msg_flags
;
835 my_msg
.msg_name
= NULL
;
838 hdr_size
= msg_hdr_sz(&tport
->phdr
);
840 while (curr_iovlen
--) {
841 curr_start
= curr_iov
->iov_base
;
842 curr_left
= curr_iov
->iov_len
;
845 bytes_to_send
= tport
->max_pkt
- hdr_size
;
846 if (bytes_to_send
> TIPC_MAX_USER_MSG_SIZE
)
847 bytes_to_send
= TIPC_MAX_USER_MSG_SIZE
;
848 if (curr_left
< bytes_to_send
)
849 bytes_to_send
= curr_left
;
850 my_iov
.iov_base
= curr_start
;
851 my_iov
.iov_len
= bytes_to_send
;
852 res
= send_packet(NULL
, sock
, &my_msg
, bytes_to_send
);
858 curr_left
-= bytes_to_send
;
859 curr_start
+= bytes_to_send
;
860 bytes_sent
+= bytes_to_send
;
872 * auto_connect - complete connection setup to a remote port
873 * @sock: socket structure
874 * @msg: peer's response message
876 * Returns 0 on success, errno otherwise
878 static int auto_connect(struct socket
*sock
, struct tipc_msg
*msg
)
880 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
881 struct tipc_port
*p_ptr
;
883 tsock
->peer_name
.ref
= msg_origport(msg
);
884 tsock
->peer_name
.node
= msg_orignode(msg
);
885 p_ptr
= tipc_port_deref(tsock
->p
->ref
);
889 __tipc_connect(tsock
->p
->ref
, p_ptr
, &tsock
->peer_name
);
891 if (msg_importance(msg
) > TIPC_CRITICAL_IMPORTANCE
)
893 msg_set_importance(&p_ptr
->phdr
, (u32
)msg_importance(msg
));
894 sock
->state
= SS_CONNECTED
;
899 * set_orig_addr - capture sender's address for received message
900 * @m: descriptor for message info
901 * @msg: received message header
903 * Note: Address is not captured if not requested by receiver.
905 static void set_orig_addr(struct msghdr
*m
, struct tipc_msg
*msg
)
907 DECLARE_SOCKADDR(struct sockaddr_tipc
*, addr
, m
->msg_name
);
910 addr
->family
= AF_TIPC
;
911 addr
->addrtype
= TIPC_ADDR_ID
;
912 memset(&addr
->addr
, 0, sizeof(addr
->addr
));
913 addr
->addr
.id
.ref
= msg_origport(msg
);
914 addr
->addr
.id
.node
= msg_orignode(msg
);
915 addr
->addr
.name
.domain
= 0; /* could leave uninitialized */
916 addr
->scope
= 0; /* could leave uninitialized */
917 m
->msg_namelen
= sizeof(struct sockaddr_tipc
);
922 * anc_data_recv - optionally capture ancillary data for received message
923 * @m: descriptor for message info
924 * @msg: received message header
925 * @tport: TIPC port associated with message
927 * Note: Ancillary data is not captured if not requested by receiver.
929 * Returns 0 if successful, otherwise errno
931 static int anc_data_recv(struct msghdr
*m
, struct tipc_msg
*msg
,
932 struct tipc_port
*tport
)
940 if (likely(m
->msg_controllen
== 0))
943 /* Optionally capture errored message object(s) */
944 err
= msg
? msg_errcode(msg
) : 0;
947 anc_data
[1] = msg_data_sz(msg
);
948 res
= put_cmsg(m
, SOL_TIPC
, TIPC_ERRINFO
, 8, anc_data
);
952 res
= put_cmsg(m
, SOL_TIPC
, TIPC_RETDATA
, anc_data
[1],
959 /* Optionally capture message destination object */
960 dest_type
= msg
? msg_type(msg
) : TIPC_DIRECT_MSG
;
964 anc_data
[0] = msg_nametype(msg
);
965 anc_data
[1] = msg_namelower(msg
);
966 anc_data
[2] = msg_namelower(msg
);
970 anc_data
[0] = msg_nametype(msg
);
971 anc_data
[1] = msg_namelower(msg
);
972 anc_data
[2] = msg_nameupper(msg
);
975 has_name
= (tport
->conn_type
!= 0);
976 anc_data
[0] = tport
->conn_type
;
977 anc_data
[1] = tport
->conn_instance
;
978 anc_data
[2] = tport
->conn_instance
;
984 res
= put_cmsg(m
, SOL_TIPC
, TIPC_DESTNAME
, 12, anc_data
);
992 static int tipc_wait_for_rcvmsg(struct socket
*sock
, long timeo
)
994 struct sock
*sk
= sock
->sk
;
999 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
1000 if (timeo
&& skb_queue_empty(&sk
->sk_receive_queue
)) {
1001 if (sock
->state
== SS_DISCONNECTING
) {
1006 timeo
= schedule_timeout(timeo
);
1010 if (!skb_queue_empty(&sk
->sk_receive_queue
))
1012 err
= sock_intr_errno(timeo
);
1013 if (signal_pending(current
))
1019 finish_wait(sk_sleep(sk
), &wait
);
1024 * recv_msg - receive packet-oriented message
1026 * @m: descriptor for message info
1027 * @buf_len: total size of user buffer area
1028 * @flags: receive flags
1030 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1031 * If the complete message doesn't fit in user area, truncate it.
1033 * Returns size of returned message data, errno otherwise
1035 static int recv_msg(struct kiocb
*iocb
, struct socket
*sock
,
1036 struct msghdr
*m
, size_t buf_len
, int flags
)
1038 struct sock
*sk
= sock
->sk
;
1039 struct tipc_port
*tport
= tipc_sk_port(sk
);
1040 struct sk_buff
*buf
;
1041 struct tipc_msg
*msg
;
1047 /* Catch invalid receive requests */
1048 if (unlikely(!buf_len
))
1053 if (unlikely(sock
->state
== SS_UNCONNECTED
)) {
1058 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
1061 /* Look for a message in receive queue; wait if necessary */
1062 res
= tipc_wait_for_rcvmsg(sock
, timeo
);
1066 /* Look at first message in receive queue */
1067 buf
= skb_peek(&sk
->sk_receive_queue
);
1069 sz
= msg_data_sz(msg
);
1070 err
= msg_errcode(msg
);
1072 /* Discard an empty non-errored message & try again */
1073 if ((!sz
) && (!err
)) {
1074 advance_rx_queue(sk
);
1078 /* Capture sender's address (optional) */
1079 set_orig_addr(m
, msg
);
1081 /* Capture ancillary data (optional) */
1082 res
= anc_data_recv(m
, msg
, tport
);
1086 /* Capture message data (if valid) & compute return value (always) */
1088 if (unlikely(buf_len
< sz
)) {
1090 m
->msg_flags
|= MSG_TRUNC
;
1092 res
= skb_copy_datagram_iovec(buf
, msg_hdr_sz(msg
),
1098 if ((sock
->state
== SS_READY
) ||
1099 ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
))
1105 /* Consume received message (optional) */
1106 if (likely(!(flags
& MSG_PEEK
))) {
1107 if ((sock
->state
!= SS_READY
) &&
1108 (++tport
->conn_unacked
>= TIPC_FLOW_CONTROL_WIN
))
1109 tipc_acknowledge(tport
->ref
, tport
->conn_unacked
);
1110 advance_rx_queue(sk
);
1118 * recv_stream - receive stream-oriented data
1120 * @m: descriptor for message info
1121 * @buf_len: total size of user buffer area
1122 * @flags: receive flags
1124 * Used for SOCK_STREAM messages only. If not enough data is available
1125 * will optionally wait for more; never truncates data.
1127 * Returns size of returned message data, errno otherwise
1129 static int recv_stream(struct kiocb
*iocb
, struct socket
*sock
,
1130 struct msghdr
*m
, size_t buf_len
, int flags
)
1132 struct sock
*sk
= sock
->sk
;
1133 struct tipc_port
*tport
= tipc_sk_port(sk
);
1134 struct sk_buff
*buf
;
1135 struct tipc_msg
*msg
;
1138 int sz_to_copy
, target
, needed
;
1143 /* Catch invalid receive attempts */
1144 if (unlikely(!buf_len
))
1149 if (unlikely(sock
->state
== SS_UNCONNECTED
)) {
1154 target
= sock_rcvlowat(sk
, flags
& MSG_WAITALL
, buf_len
);
1155 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
1158 /* Look for a message in receive queue; wait if necessary */
1159 res
= tipc_wait_for_rcvmsg(sock
, timeo
);
1163 /* Look at first message in receive queue */
1164 buf
= skb_peek(&sk
->sk_receive_queue
);
1166 sz
= msg_data_sz(msg
);
1167 err
= msg_errcode(msg
);
1169 /* Discard an empty non-errored message & try again */
1170 if ((!sz
) && (!err
)) {
1171 advance_rx_queue(sk
);
1175 /* Optionally capture sender's address & ancillary data of first msg */
1176 if (sz_copied
== 0) {
1177 set_orig_addr(m
, msg
);
1178 res
= anc_data_recv(m
, msg
, tport
);
1183 /* Capture message data (if valid) & compute return value (always) */
1185 u32 offset
= (u32
)(unsigned long)(TIPC_SKB_CB(buf
)->handle
);
1188 needed
= (buf_len
- sz_copied
);
1189 sz_to_copy
= (sz
<= needed
) ? sz
: needed
;
1191 res
= skb_copy_datagram_iovec(buf
, msg_hdr_sz(msg
) + offset
,
1192 m
->msg_iov
, sz_to_copy
);
1196 sz_copied
+= sz_to_copy
;
1198 if (sz_to_copy
< sz
) {
1199 if (!(flags
& MSG_PEEK
))
1200 TIPC_SKB_CB(buf
)->handle
=
1201 (void *)(unsigned long)(offset
+ sz_to_copy
);
1206 goto exit
; /* can't add error msg to valid data */
1208 if ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
)
1214 /* Consume received message (optional) */
1215 if (likely(!(flags
& MSG_PEEK
))) {
1216 if (unlikely(++tport
->conn_unacked
>= TIPC_FLOW_CONTROL_WIN
))
1217 tipc_acknowledge(tport
->ref
, tport
->conn_unacked
);
1218 advance_rx_queue(sk
);
1221 /* Loop around if more data is required */
1222 if ((sz_copied
< buf_len
) && /* didn't get all requested data */
1223 (!skb_queue_empty(&sk
->sk_receive_queue
) ||
1224 (sz_copied
< target
)) && /* and more is ready or required */
1225 (!(flags
& MSG_PEEK
)) && /* and aren't just peeking at data */
1226 (!err
)) /* and haven't reached a FIN */
1231 return sz_copied
? sz_copied
: res
;
1235 * tipc_write_space - wake up thread if port congestion is released
1238 static void tipc_write_space(struct sock
*sk
)
1240 struct socket_wq
*wq
;
1243 wq
= rcu_dereference(sk
->sk_wq
);
1244 if (wq_has_sleeper(wq
))
1245 wake_up_interruptible_sync_poll(&wq
->wait
, POLLOUT
|
1246 POLLWRNORM
| POLLWRBAND
);
1251 * tipc_data_ready - wake up threads to indicate messages have been received
1253 * @len: the length of messages
1255 static void tipc_data_ready(struct sock
*sk
, int len
)
1257 struct socket_wq
*wq
;
1260 wq
= rcu_dereference(sk
->sk_wq
);
1261 if (wq_has_sleeper(wq
))
1262 wake_up_interruptible_sync_poll(&wq
->wait
, POLLIN
|
1263 POLLRDNORM
| POLLRDBAND
);
1268 * filter_connect - Handle all incoming messages for a connection-based socket
1269 * @tsock: TIPC socket
1272 * Returns TIPC error status code and socket error status code
1273 * once it encounters some errors
1275 static u32
filter_connect(struct tipc_sock
*tsock
, struct sk_buff
**buf
)
1277 struct socket
*sock
= tsock
->sk
.sk_socket
;
1278 struct tipc_msg
*msg
= buf_msg(*buf
);
1279 struct sock
*sk
= &tsock
->sk
;
1280 u32 retval
= TIPC_ERR_NO_PORT
;
1286 switch ((int)sock
->state
) {
1288 /* Accept only connection-based messages sent by peer */
1289 if (msg_connected(msg
) && tipc_port_peer_msg(tsock
->p
, msg
)) {
1290 if (unlikely(msg_errcode(msg
))) {
1291 sock
->state
= SS_DISCONNECTING
;
1292 __tipc_disconnect(tsock
->p
);
1298 /* Accept only ACK or NACK message */
1299 if (unlikely(msg_errcode(msg
))) {
1300 sock
->state
= SS_DISCONNECTING
;
1301 sk
->sk_err
= ECONNREFUSED
;
1306 if (unlikely(!msg_connected(msg
)))
1309 res
= auto_connect(sock
, msg
);
1311 sock
->state
= SS_DISCONNECTING
;
1317 /* If an incoming message is an 'ACK-', it should be
1318 * discarded here because it doesn't contain useful
1319 * data. In addition, we should try to wake up
1320 * connect() routine if sleeping.
1322 if (msg_data_sz(msg
) == 0) {
1325 if (waitqueue_active(sk_sleep(sk
)))
1326 wake_up_interruptible(sk_sleep(sk
));
1331 case SS_UNCONNECTED
:
1332 /* Accept only SYN message */
1333 if (!msg_connected(msg
) && !(msg_errcode(msg
)))
1336 case SS_DISCONNECTING
:
1339 pr_err("Unknown socket state %u\n", sock
->state
);
1345 * rcvbuf_limit - get proper overload limit of socket receive queue
1349 * For all connection oriented messages, irrespective of importance,
1350 * the default overload value (i.e. 67MB) is set as limit.
1352 * For all connectionless messages, by default new queue limits are
1355 * TIPC_LOW_IMPORTANCE (4 MB)
1356 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1357 * TIPC_HIGH_IMPORTANCE (16 MB)
1358 * TIPC_CRITICAL_IMPORTANCE (32 MB)
1360 * Returns overload limit according to corresponding message importance
1362 static unsigned int rcvbuf_limit(struct sock
*sk
, struct sk_buff
*buf
)
1364 struct tipc_msg
*msg
= buf_msg(buf
);
1366 if (msg_connected(msg
))
1367 return sysctl_tipc_rmem
[2];
1369 return sk
->sk_rcvbuf
>> TIPC_CRITICAL_IMPORTANCE
<<
1370 msg_importance(msg
);
1374 * filter_rcv - validate incoming message
1378 * Enqueues message on receive queue if acceptable; optionally handles
1379 * disconnect indication for a connected socket.
1381 * Called with socket lock already taken; port lock may also be taken.
1383 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1385 static u32
filter_rcv(struct sock
*sk
, struct sk_buff
*buf
)
1387 struct socket
*sock
= sk
->sk_socket
;
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(tipc_sk(sk
), &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 * dispatch - handle incoming message
1439 * @tport: TIPC port that received message
1442 * Called with port lock already taken.
1444 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1446 static u32
dispatch(struct tipc_port
*tport
, struct sk_buff
*buf
)
1448 struct sock
*sk
= tport
->sk
;
1452 * Process message if socket is unlocked; otherwise add to backlog queue
1454 * This code is based on sk_receive_skb(), but must be distinct from it
1455 * since a TIPC-specific filter/reject mechanism is utilized
1458 if (!sock_owned_by_user(sk
)) {
1459 res
= filter_rcv(sk
, buf
);
1461 if (sk_add_backlog(sk
, buf
, rcvbuf_limit(sk
, buf
)))
1462 res
= TIPC_ERR_OVERLOAD
;
1472 * wakeupdispatch - wake up port after congestion
1473 * @tport: port to wakeup
1475 * Called with port lock already taken.
1477 static void wakeupdispatch(struct tipc_port
*tport
)
1479 struct sock
*sk
= tport
->sk
;
1481 sk
->sk_write_space(sk
);
1484 static int tipc_wait_for_connect(struct socket
*sock
, long *timeo_p
)
1486 struct sock
*sk
= sock
->sk
;
1491 int err
= sock_error(sk
);
1496 if (signal_pending(current
))
1497 return sock_intr_errno(*timeo_p
);
1499 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
1500 done
= sk_wait_event(sk
, timeo_p
, sock
->state
!= SS_CONNECTING
);
1501 finish_wait(sk_sleep(sk
), &wait
);
1507 * connect - establish a connection to another TIPC port
1508 * @sock: socket structure
1509 * @dest: socket address for destination port
1510 * @destlen: size of socket address data structure
1511 * @flags: file-related flags associated with socket
1513 * Returns 0 on success, errno otherwise
1515 static int connect(struct socket
*sock
, struct sockaddr
*dest
, int destlen
,
1518 struct sock
*sk
= sock
->sk
;
1519 struct sockaddr_tipc
*dst
= (struct sockaddr_tipc
*)dest
;
1520 struct msghdr m
= {NULL
,};
1521 long timeout
= (flags
& O_NONBLOCK
) ? 0 : tipc_sk(sk
)->conn_timeout
;
1522 socket_state previous
;
1527 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
1528 if (sock
->state
== SS_READY
) {
1534 * Reject connection attempt using multicast address
1536 * Note: send_msg() validates the rest of the address fields,
1537 * so there's no need to do it here
1539 if (dst
->addrtype
== TIPC_ADDR_MCAST
) {
1544 previous
= sock
->state
;
1545 switch (sock
->state
) {
1546 case SS_UNCONNECTED
:
1547 /* Send a 'SYN-' to destination */
1549 m
.msg_namelen
= destlen
;
1551 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1552 * indicate send_msg() is never blocked.
1555 m
.msg_flags
= MSG_DONTWAIT
;
1557 res
= send_msg(NULL
, sock
, &m
, 0);
1558 if ((res
< 0) && (res
!= -EWOULDBLOCK
))
1561 /* Just entered SS_CONNECTING state; the only
1562 * difference is that return value in non-blocking
1563 * case is EINPROGRESS, rather than EALREADY.
1567 if (previous
== SS_CONNECTING
)
1571 timeout
= msecs_to_jiffies(timeout
);
1572 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1573 res
= tipc_wait_for_connect(sock
, &timeout
);
1588 * listen - allow socket to listen for incoming connections
1589 * @sock: socket structure
1592 * Returns 0 on success, errno otherwise
1594 static int listen(struct socket
*sock
, int len
)
1596 struct sock
*sk
= sock
->sk
;
1601 if (sock
->state
!= SS_UNCONNECTED
)
1604 sock
->state
= SS_LISTENING
;
1612 static int tipc_wait_for_accept(struct socket
*sock
, long timeo
)
1614 struct sock
*sk
= sock
->sk
;
1618 /* True wake-one mechanism for incoming connections: only
1619 * one process gets woken up, not the 'whole herd'.
1620 * Since we do not 'race & poll' for established sockets
1621 * anymore, the common case will execute the loop only once.
1624 prepare_to_wait_exclusive(sk_sleep(sk
), &wait
,
1625 TASK_INTERRUPTIBLE
);
1626 if (timeo
&& skb_queue_empty(&sk
->sk_receive_queue
)) {
1628 timeo
= schedule_timeout(timeo
);
1632 if (!skb_queue_empty(&sk
->sk_receive_queue
))
1635 if (sock
->state
!= SS_LISTENING
)
1637 err
= sock_intr_errno(timeo
);
1638 if (signal_pending(current
))
1644 finish_wait(sk_sleep(sk
), &wait
);
1649 * accept - wait for connection request
1650 * @sock: listening socket
1651 * @newsock: new socket that is to be connected
1652 * @flags: file-related flags associated with socket
1654 * Returns 0 on success, errno otherwise
1656 static int accept(struct socket
*sock
, struct socket
*new_sock
, int flags
)
1658 struct sock
*new_sk
, *sk
= sock
->sk
;
1659 struct sk_buff
*buf
;
1660 struct tipc_sock
*new_tsock
;
1661 struct tipc_port
*new_tport
;
1662 struct tipc_msg
*msg
;
1669 if (sock
->state
!= SS_LISTENING
) {
1674 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
1675 res
= tipc_wait_for_accept(sock
, timeo
);
1679 buf
= skb_peek(&sk
->sk_receive_queue
);
1681 res
= tipc_sk_create(sock_net(sock
->sk
), new_sock
, 0, 1);
1685 new_sk
= new_sock
->sk
;
1686 new_tsock
= tipc_sk(new_sk
);
1687 new_tport
= new_tsock
->p
;
1688 new_ref
= new_tport
->ref
;
1691 /* we lock on new_sk; but lockdep sees the lock on sk */
1692 lock_sock_nested(new_sk
, SINGLE_DEPTH_NESTING
);
1695 * Reject any stray messages received by new socket
1696 * before the socket lock was taken (very, very unlikely)
1698 reject_rx_queue(new_sk
);
1700 /* Connect new socket to it's peer */
1701 new_tsock
->peer_name
.ref
= msg_origport(msg
);
1702 new_tsock
->peer_name
.node
= msg_orignode(msg
);
1703 tipc_connect(new_ref
, &new_tsock
->peer_name
);
1704 new_sock
->state
= SS_CONNECTED
;
1706 tipc_set_portimportance(new_ref
, msg_importance(msg
));
1707 if (msg_named(msg
)) {
1708 new_tport
->conn_type
= msg_nametype(msg
);
1709 new_tport
->conn_instance
= msg_nameinst(msg
);
1713 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1714 * Respond to 'SYN+' by queuing it on new socket.
1716 if (!msg_data_sz(msg
)) {
1717 struct msghdr m
= {NULL
,};
1719 advance_rx_queue(sk
);
1720 send_packet(NULL
, new_sock
, &m
, 0);
1722 __skb_dequeue(&sk
->sk_receive_queue
);
1723 __skb_queue_head(&new_sk
->sk_receive_queue
, buf
);
1724 skb_set_owner_r(buf
, new_sk
);
1726 release_sock(new_sk
);
1734 * shutdown - shutdown socket connection
1735 * @sock: socket structure
1736 * @how: direction to close (must be SHUT_RDWR)
1738 * Terminates connection (if necessary), then purges socket's receive queue.
1740 * Returns 0 on success, errno otherwise
1742 static int shutdown(struct socket
*sock
, int how
)
1744 struct sock
*sk
= sock
->sk
;
1745 struct tipc_port
*tport
= tipc_sk_port(sk
);
1746 struct sk_buff
*buf
;
1749 if (how
!= SHUT_RDWR
)
1754 switch (sock
->state
) {
1759 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
1760 buf
= __skb_dequeue(&sk
->sk_receive_queue
);
1762 if (TIPC_SKB_CB(buf
)->handle
!= NULL
) {
1766 tipc_disconnect(tport
->ref
);
1767 tipc_reject_msg(buf
, TIPC_CONN_SHUTDOWN
);
1769 tipc_shutdown(tport
->ref
);
1772 sock
->state
= SS_DISCONNECTING
;
1776 case SS_DISCONNECTING
:
1778 /* Discard any unreceived messages */
1779 __skb_queue_purge(&sk
->sk_receive_queue
);
1781 /* Wake up anyone sleeping in poll */
1782 sk
->sk_state_change(sk
);
1795 * setsockopt - set socket option
1796 * @sock: socket structure
1797 * @lvl: option level
1798 * @opt: option identifier
1799 * @ov: pointer to new option value
1800 * @ol: length of option value
1802 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
1803 * (to ease compatibility).
1805 * Returns 0 on success, errno otherwise
1807 static int setsockopt(struct socket
*sock
, int lvl
, int opt
, char __user
*ov
,
1810 struct sock
*sk
= sock
->sk
;
1811 struct tipc_port
*tport
= tipc_sk_port(sk
);
1815 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
1817 if (lvl
!= SOL_TIPC
)
1818 return -ENOPROTOOPT
;
1819 if (ol
< sizeof(value
))
1821 res
= get_user(value
, (u32 __user
*)ov
);
1828 case TIPC_IMPORTANCE
:
1829 res
= tipc_set_portimportance(tport
->ref
, value
);
1831 case TIPC_SRC_DROPPABLE
:
1832 if (sock
->type
!= SOCK_STREAM
)
1833 res
= tipc_set_portunreliable(tport
->ref
, value
);
1837 case TIPC_DEST_DROPPABLE
:
1838 res
= tipc_set_portunreturnable(tport
->ref
, value
);
1840 case TIPC_CONN_TIMEOUT
:
1841 tipc_sk(sk
)->conn_timeout
= value
;
1842 /* no need to set "res", since already 0 at this point */
1854 * getsockopt - get socket option
1855 * @sock: socket structure
1856 * @lvl: option level
1857 * @opt: option identifier
1858 * @ov: receptacle for option value
1859 * @ol: receptacle for length of option value
1861 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
1862 * (to ease compatibility).
1864 * Returns 0 on success, errno otherwise
1866 static int getsockopt(struct socket
*sock
, int lvl
, int opt
, char __user
*ov
,
1869 struct sock
*sk
= sock
->sk
;
1870 struct tipc_port
*tport
= tipc_sk_port(sk
);
1875 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
1876 return put_user(0, ol
);
1877 if (lvl
!= SOL_TIPC
)
1878 return -ENOPROTOOPT
;
1879 res
= get_user(len
, ol
);
1886 case TIPC_IMPORTANCE
:
1887 res
= tipc_portimportance(tport
->ref
, &value
);
1889 case TIPC_SRC_DROPPABLE
:
1890 res
= tipc_portunreliable(tport
->ref
, &value
);
1892 case TIPC_DEST_DROPPABLE
:
1893 res
= tipc_portunreturnable(tport
->ref
, &value
);
1895 case TIPC_CONN_TIMEOUT
:
1896 value
= tipc_sk(sk
)->conn_timeout
;
1897 /* no need to set "res", since already 0 at this point */
1899 case TIPC_NODE_RECVQ_DEPTH
:
1900 value
= 0; /* was tipc_queue_size, now obsolete */
1902 case TIPC_SOCK_RECVQ_DEPTH
:
1903 value
= skb_queue_len(&sk
->sk_receive_queue
);
1912 return res
; /* "get" failed */
1914 if (len
< sizeof(value
))
1917 if (copy_to_user(ov
, &value
, sizeof(value
)))
1920 return put_user(sizeof(value
), ol
);
1923 /* Protocol switches for the various types of TIPC sockets */
1925 static const struct proto_ops msg_ops
= {
1926 .owner
= THIS_MODULE
,
1931 .socketpair
= sock_no_socketpair
,
1932 .accept
= sock_no_accept
,
1933 .getname
= get_name
,
1935 .ioctl
= sock_no_ioctl
,
1936 .listen
= sock_no_listen
,
1937 .shutdown
= shutdown
,
1938 .setsockopt
= setsockopt
,
1939 .getsockopt
= getsockopt
,
1940 .sendmsg
= send_msg
,
1941 .recvmsg
= recv_msg
,
1942 .mmap
= sock_no_mmap
,
1943 .sendpage
= sock_no_sendpage
1946 static const struct proto_ops packet_ops
= {
1947 .owner
= THIS_MODULE
,
1952 .socketpair
= sock_no_socketpair
,
1954 .getname
= get_name
,
1956 .ioctl
= sock_no_ioctl
,
1958 .shutdown
= shutdown
,
1959 .setsockopt
= setsockopt
,
1960 .getsockopt
= getsockopt
,
1961 .sendmsg
= send_packet
,
1962 .recvmsg
= recv_msg
,
1963 .mmap
= sock_no_mmap
,
1964 .sendpage
= sock_no_sendpage
1967 static const struct proto_ops stream_ops
= {
1968 .owner
= THIS_MODULE
,
1973 .socketpair
= sock_no_socketpair
,
1975 .getname
= get_name
,
1977 .ioctl
= sock_no_ioctl
,
1979 .shutdown
= shutdown
,
1980 .setsockopt
= setsockopt
,
1981 .getsockopt
= getsockopt
,
1982 .sendmsg
= send_stream
,
1983 .recvmsg
= recv_stream
,
1984 .mmap
= sock_no_mmap
,
1985 .sendpage
= sock_no_sendpage
1988 static const struct net_proto_family tipc_family_ops
= {
1989 .owner
= THIS_MODULE
,
1991 .create
= tipc_sk_create
1994 static struct proto tipc_proto
= {
1996 .owner
= THIS_MODULE
,
1997 .obj_size
= sizeof(struct tipc_sock
),
1998 .sysctl_rmem
= sysctl_tipc_rmem
2001 static struct proto tipc_proto_kern
= {
2003 .obj_size
= sizeof(struct tipc_sock
),
2004 .sysctl_rmem
= sysctl_tipc_rmem
2008 * tipc_socket_init - initialize TIPC socket interface
2010 * Returns 0 on success, errno otherwise
2012 int tipc_socket_init(void)
2016 res
= proto_register(&tipc_proto
, 1);
2018 pr_err("Failed to register TIPC protocol type\n");
2022 res
= sock_register(&tipc_family_ops
);
2024 pr_err("Failed to register TIPC socket type\n");
2025 proto_unregister(&tipc_proto
);
2033 * tipc_socket_stop - stop TIPC socket interface
2035 void tipc_socket_stop(void)
2037 sock_unregister(tipc_family_ops
.family
);
2038 proto_unregister(&tipc_proto
);