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.
39 #include "name_table.h"
42 #include <linux/export.h>
44 #define SS_LISTENING -1 /* socket is listening */
45 #define SS_READY -2 /* socket is connectionless */
47 #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
48 #define TIPC_FWD_MSG 1
50 static int tipc_backlog_rcv(struct sock
*sk
, struct sk_buff
*skb
);
51 static void tipc_data_ready(struct sock
*sk
);
52 static void tipc_write_space(struct sock
*sk
);
53 static int tipc_release(struct socket
*sock
);
54 static int tipc_accept(struct socket
*sock
, struct socket
*new_sock
, int flags
);
55 static int tipc_wait_for_sndmsg(struct socket
*sock
, long *timeo_p
);
57 static const struct proto_ops packet_ops
;
58 static const struct proto_ops stream_ops
;
59 static const struct proto_ops msg_ops
;
61 static struct proto tipc_proto
;
62 static struct proto tipc_proto_kern
;
65 * Revised TIPC socket locking policy:
67 * Most socket operations take the standard socket lock when they start
68 * and hold it until they finish (or until they need to sleep). Acquiring
69 * this lock grants the owner exclusive access to the fields of the socket
70 * data structures, with the exception of the backlog queue. A few socket
71 * operations can be done without taking the socket lock because they only
72 * read socket information that never changes during the life of the socket.
74 * Socket operations may acquire the lock for the associated TIPC port if they
75 * need to perform an operation on the port. If any routine needs to acquire
76 * both the socket lock and the port lock it must take the socket lock first
77 * to avoid the risk of deadlock.
79 * The dispatcher handling incoming messages cannot grab the socket lock in
80 * the standard fashion, since invoked it runs at the BH level and cannot block.
81 * Instead, it checks to see if the socket lock is currently owned by someone,
82 * and either handles the message itself or adds it to the socket's backlog
83 * queue; in the latter case the queued message is processed once the process
84 * owning the socket lock releases it.
86 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
87 * the problem of a blocked socket operation preventing any other operations
88 * from occurring. However, applications must be careful if they have
89 * multiple threads trying to send (or receive) on the same socket, as these
90 * operations might interfere with each other. For example, doing a connect
91 * and a receive at the same time might allow the receive to consume the
92 * ACK message meant for the connect. While additional work could be done
93 * to try and overcome this, it doesn't seem to be worthwhile at the present.
95 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
96 * that another operation that must be performed in a non-blocking manner is
97 * not delayed for very long because the lock has already been taken.
99 * NOTE: This code assumes that certain fields of a port/socket pair are
100 * constant over its lifetime; such fields can be examined without taking
101 * the socket lock and/or port lock, and do not need to be re-read even
102 * after resuming processing after waiting. These fields include:
104 * - pointer to socket sk structure (aka tipc_sock structure)
105 * - pointer to port structure
112 * advance_rx_queue - discard first buffer in socket receive queue
114 * Caller must hold socket lock
116 static void advance_rx_queue(struct sock
*sk
)
118 kfree_skb(__skb_dequeue(&sk
->sk_receive_queue
));
122 * reject_rx_queue - reject all buffers in socket receive queue
124 * Caller must hold socket lock
126 static void reject_rx_queue(struct sock
*sk
)
131 while ((buf
= __skb_dequeue(&sk
->sk_receive_queue
))) {
132 if (tipc_msg_reverse(buf
, &dnode
, TIPC_ERR_NO_PORT
))
133 tipc_link_xmit(buf
, dnode
, 0);
138 * tipc_sk_create - create a TIPC socket
139 * @net: network namespace (must be default network)
140 * @sock: pre-allocated socket structure
141 * @protocol: protocol indicator (must be 0)
142 * @kern: caused by kernel or by userspace?
144 * This routine creates additional data structures used by the TIPC socket,
145 * initializes them, and links them together.
147 * Returns 0 on success, errno otherwise
149 static int tipc_sk_create(struct net
*net
, struct socket
*sock
,
150 int protocol
, int kern
)
152 const struct proto_ops
*ops
;
155 struct tipc_sock
*tsk
;
156 struct tipc_port
*port
;
159 /* Validate arguments */
160 if (unlikely(protocol
!= 0))
161 return -EPROTONOSUPPORT
;
163 switch (sock
->type
) {
166 state
= SS_UNCONNECTED
;
170 state
= SS_UNCONNECTED
;
181 /* Allocate socket's protocol area */
183 sk
= sk_alloc(net
, AF_TIPC
, GFP_KERNEL
, &tipc_proto
);
185 sk
= sk_alloc(net
, AF_TIPC
, GFP_KERNEL
, &tipc_proto_kern
);
193 ref
= tipc_port_init(port
, TIPC_LOW_IMPORTANCE
);
195 pr_warn("Socket registration failed, ref. table exhausted\n");
200 /* Finish initializing socket data structures */
204 sock_init_data(sock
, sk
);
205 sk
->sk_backlog_rcv
= tipc_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 tsk
->conn_timeout
= CONN_TIMEOUT_DEFAULT
;
210 tsk
->sent_unacked
= 0;
211 atomic_set(&tsk
->dupl_rcvcnt
, 0);
212 tipc_port_unlock(port
);
214 if (sock
->state
== SS_READY
) {
215 tipc_port_set_unreturnable(port
, true);
216 if (sock
->type
== SOCK_DGRAM
)
217 tipc_port_set_unreliable(port
, true);
223 * tipc_sock_create_local - create TIPC socket from inside TIPC module
224 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
226 * We cannot use sock_creat_kern here because it bumps module user count.
227 * Since socket owner and creator is the same module we must make sure
228 * that module count remains zero for module local sockets, otherwise
229 * we cannot do rmmod.
231 * Returns 0 on success, errno otherwise
233 int tipc_sock_create_local(int type
, struct socket
**res
)
237 rc
= sock_create_lite(AF_TIPC
, type
, 0, res
);
239 pr_err("Failed to create kernel socket\n");
242 tipc_sk_create(&init_net
, *res
, 0, 1);
248 * tipc_sock_release_local - release socket created by tipc_sock_create_local
249 * @sock: the socket to be released.
251 * Module reference count is not incremented when such sockets are created,
252 * so we must keep it from being decremented when they are released.
254 void tipc_sock_release_local(struct socket
*sock
)
262 * tipc_sock_accept_local - accept a connection on a socket created
263 * with tipc_sock_create_local. Use this function to avoid that
264 * module reference count is inadvertently incremented.
266 * @sock: the accepting socket
267 * @newsock: reference to the new socket to be created
268 * @flags: socket flags
271 int tipc_sock_accept_local(struct socket
*sock
, struct socket
**newsock
,
274 struct sock
*sk
= sock
->sk
;
277 ret
= sock_create_lite(sk
->sk_family
, sk
->sk_type
,
278 sk
->sk_protocol
, newsock
);
282 ret
= tipc_accept(sock
, *newsock
, flags
);
284 sock_release(*newsock
);
287 (*newsock
)->ops
= sock
->ops
;
292 * tipc_release - destroy a TIPC socket
293 * @sock: socket to destroy
295 * This routine cleans up any messages that are still queued on the socket.
296 * For DGRAM and RDM socket types, all queued messages are rejected.
297 * For SEQPACKET and STREAM socket types, the first message is rejected
298 * and any others are discarded. (If the first message on a STREAM socket
299 * is partially-read, it is discarded and the next one is rejected instead.)
301 * NOTE: Rejected messages are not necessarily returned to the sender! They
302 * are returned or discarded according to the "destination droppable" setting
303 * specified for the message by the sender.
305 * Returns 0 on success, errno otherwise
307 static int tipc_release(struct socket
*sock
)
309 struct sock
*sk
= sock
->sk
;
310 struct tipc_sock
*tsk
;
311 struct tipc_port
*port
;
316 * Exit if socket isn't fully initialized (occurs when a failed accept()
317 * releases a pre-allocated child socket that was never used)
327 * Reject all unreceived messages, except on an active connection
328 * (which disconnects locally & sends a 'FIN+' to peer)
330 while (sock
->state
!= SS_DISCONNECTING
) {
331 buf
= __skb_dequeue(&sk
->sk_receive_queue
);
334 if (TIPC_SKB_CB(buf
)->handle
!= NULL
)
337 if ((sock
->state
== SS_CONNECTING
) ||
338 (sock
->state
== SS_CONNECTED
)) {
339 sock
->state
= SS_DISCONNECTING
;
340 tipc_port_disconnect(port
->ref
);
342 if (tipc_msg_reverse(buf
, &dnode
, TIPC_ERR_NO_PORT
))
343 tipc_link_xmit(buf
, dnode
, 0);
347 /* Destroy TIPC port; also disconnects an active connection and
348 * sends a 'FIN-' to peer.
350 tipc_port_destroy(port
);
352 /* Discard any remaining (connection-based) messages in receive queue */
353 __skb_queue_purge(&sk
->sk_receive_queue
);
355 /* Reject any messages that accumulated in backlog queue */
356 sock
->state
= SS_DISCONNECTING
;
366 * tipc_bind - associate or disassocate TIPC name(s) with a socket
367 * @sock: socket structure
368 * @uaddr: socket address describing name(s) and desired operation
369 * @uaddr_len: size of socket address data structure
371 * Name and name sequence binding is indicated using a positive scope value;
372 * a negative scope value unbinds the specified name. Specifying no name
373 * (i.e. a socket address length of 0) unbinds all names from the socket.
375 * Returns 0 on success, errno otherwise
377 * NOTE: This routine doesn't need to take the socket lock since it doesn't
378 * access any non-constant socket information.
380 static int tipc_bind(struct socket
*sock
, struct sockaddr
*uaddr
,
383 struct sock
*sk
= sock
->sk
;
384 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
385 struct tipc_sock
*tsk
= tipc_sk(sk
);
389 if (unlikely(!uaddr_len
)) {
390 res
= tipc_withdraw(&tsk
->port
, 0, NULL
);
394 if (uaddr_len
< sizeof(struct sockaddr_tipc
)) {
398 if (addr
->family
!= AF_TIPC
) {
403 if (addr
->addrtype
== TIPC_ADDR_NAME
)
404 addr
->addr
.nameseq
.upper
= addr
->addr
.nameseq
.lower
;
405 else if (addr
->addrtype
!= TIPC_ADDR_NAMESEQ
) {
410 if ((addr
->addr
.nameseq
.type
< TIPC_RESERVED_TYPES
) &&
411 (addr
->addr
.nameseq
.type
!= TIPC_TOP_SRV
) &&
412 (addr
->addr
.nameseq
.type
!= TIPC_CFG_SRV
)) {
417 res
= (addr
->scope
> 0) ?
418 tipc_publish(&tsk
->port
, addr
->scope
, &addr
->addr
.nameseq
) :
419 tipc_withdraw(&tsk
->port
, -addr
->scope
, &addr
->addr
.nameseq
);
426 * tipc_getname - get port ID of socket or peer socket
427 * @sock: socket structure
428 * @uaddr: area for returned socket address
429 * @uaddr_len: area for returned length of socket address
430 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
432 * Returns 0 on success, errno otherwise
434 * NOTE: This routine doesn't need to take the socket lock since it only
435 * accesses socket information that is unchanging (or which changes in
436 * a completely predictable manner).
438 static int tipc_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
439 int *uaddr_len
, int peer
)
441 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
442 struct tipc_sock
*tsk
= tipc_sk(sock
->sk
);
444 memset(addr
, 0, sizeof(*addr
));
446 if ((sock
->state
!= SS_CONNECTED
) &&
447 ((peer
!= 2) || (sock
->state
!= SS_DISCONNECTING
)))
449 addr
->addr
.id
.ref
= tipc_port_peerport(&tsk
->port
);
450 addr
->addr
.id
.node
= tipc_port_peernode(&tsk
->port
);
452 addr
->addr
.id
.ref
= tsk
->port
.ref
;
453 addr
->addr
.id
.node
= tipc_own_addr
;
456 *uaddr_len
= sizeof(*addr
);
457 addr
->addrtype
= TIPC_ADDR_ID
;
458 addr
->family
= AF_TIPC
;
460 addr
->addr
.name
.domain
= 0;
466 * tipc_poll - read and possibly block on pollmask
467 * @file: file structure associated with the socket
468 * @sock: socket for which to calculate the poll bits
471 * Returns pollmask value
474 * It appears that the usual socket locking mechanisms are not useful here
475 * since the pollmask info is potentially out-of-date the moment this routine
476 * exits. TCP and other protocols seem to rely on higher level poll routines
477 * to handle any preventable race conditions, so TIPC will do the same ...
479 * TIPC sets the returned events as follows:
481 * socket state flags set
482 * ------------ ---------
483 * unconnected no read flags
484 * POLLOUT if port is not congested
486 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
489 * connected POLLIN/POLLRDNORM if data in rx queue
490 * POLLOUT if port is not congested
492 * disconnecting POLLIN/POLLRDNORM/POLLHUP
495 * listening POLLIN if SYN in rx queue
498 * ready POLLIN/POLLRDNORM if data in rx queue
499 * [connectionless] POLLOUT (since port cannot be congested)
501 * IMPORTANT: The fact that a read or write operation is indicated does NOT
502 * imply that the operation will succeed, merely that it should be performed
503 * and will not block.
505 static unsigned int tipc_poll(struct file
*file
, struct socket
*sock
,
508 struct sock
*sk
= sock
->sk
;
509 struct tipc_sock
*tsk
= tipc_sk(sk
);
512 sock_poll_wait(file
, sk_sleep(sk
), wait
);
514 switch ((int)sock
->state
) {
521 if (!tsk
->link_cong
&& !tipc_sk_conn_cong(tsk
))
526 if (!skb_queue_empty(&sk
->sk_receive_queue
))
527 mask
|= (POLLIN
| POLLRDNORM
);
529 case SS_DISCONNECTING
:
530 mask
= (POLLIN
| POLLRDNORM
| POLLHUP
);
538 * tipc_sendmcast - send multicast message
539 * @sock: socket structure
540 * @seq: destination address
541 * @iov: message data to send
542 * @dsz: total length of message data
543 * @timeo: timeout to wait for wakeup
545 * Called from function tipc_sendmsg(), which has done all sanity checks
546 * Returns the number of bytes sent on success, or errno
548 static int tipc_sendmcast(struct socket
*sock
, struct tipc_name_seq
*seq
,
549 struct iovec
*iov
, size_t dsz
, long timeo
)
551 struct sock
*sk
= sock
->sk
;
552 struct tipc_msg
*mhdr
= &tipc_sk(sk
)->port
.phdr
;
557 msg_set_type(mhdr
, TIPC_MCAST_MSG
);
558 msg_set_lookup_scope(mhdr
, TIPC_CLUSTER_SCOPE
);
559 msg_set_destport(mhdr
, 0);
560 msg_set_destnode(mhdr
, 0);
561 msg_set_nametype(mhdr
, seq
->type
);
562 msg_set_namelower(mhdr
, seq
->lower
);
563 msg_set_nameupper(mhdr
, seq
->upper
);
564 msg_set_hdr_sz(mhdr
, MCAST_H_SIZE
);
567 mtu
= tipc_bclink_get_mtu();
568 rc
= tipc_msg_build(mhdr
, iov
, 0, dsz
, mtu
, &buf
);
569 if (unlikely(rc
< 0))
573 rc
= tipc_bclink_xmit(buf
);
574 if (likely(rc
>= 0)) {
580 if (rc
!= -ELINKCONG
)
582 rc
= tipc_wait_for_sndmsg(sock
, &timeo
);
589 /* tipc_sk_mcast_rcv - Deliver multicast message to all destination sockets
591 void tipc_sk_mcast_rcv(struct sk_buff
*buf
)
593 struct tipc_msg
*msg
= buf_msg(buf
);
594 struct tipc_port_list dports
= {0, NULL
, };
595 struct tipc_port_list
*item
;
597 uint i
, last
, dst
= 0;
598 u32 scope
= TIPC_CLUSTER_SCOPE
;
600 if (in_own_node(msg_orignode(msg
)))
601 scope
= TIPC_NODE_SCOPE
;
603 /* Create destination port list: */
604 tipc_nametbl_mc_translate(msg_nametype(msg
),
615 for (item
= &dports
; item
; item
= item
->next
) {
616 for (i
= 0; i
< PLSIZE
&& ++dst
<= last
; i
++) {
617 b
= (dst
!= last
) ? skb_clone(buf
, GFP_ATOMIC
) : buf
;
619 pr_warn("Failed do clone mcast rcv buffer\n");
622 msg_set_destport(msg
, item
->ports
[i
]);
626 tipc_port_list_free(&dports
);
630 * tipc_sk_proto_rcv - receive a connection mng protocol message
631 * @tsk: receiving socket
632 * @dnode: node to send response message to, if any
633 * @buf: buffer containing protocol message
634 * Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
635 * (CONN_PROBE_REPLY) message should be forwarded.
637 static int tipc_sk_proto_rcv(struct tipc_sock
*tsk
, u32
*dnode
,
640 struct tipc_msg
*msg
= buf_msg(buf
);
641 struct tipc_port
*port
= &tsk
->port
;
644 /* Ignore if connection cannot be validated: */
645 if (!port
->connected
|| !tipc_port_peer_msg(port
, msg
))
648 port
->probing_state
= TIPC_CONN_OK
;
650 if (msg_type(msg
) == CONN_ACK
) {
651 conn_cong
= tipc_sk_conn_cong(tsk
);
652 tsk
->sent_unacked
-= msg_msgcnt(msg
);
654 tipc_sock_wakeup(tsk
);
655 } else if (msg_type(msg
) == CONN_PROBE
) {
656 if (!tipc_msg_reverse(buf
, dnode
, TIPC_OK
))
658 msg_set_type(msg
, CONN_PROBE_REPLY
);
661 /* Do nothing if msg_type() == CONN_PROBE_REPLY */
668 * dest_name_check - verify user is permitted to send to specified port name
669 * @dest: destination address
670 * @m: descriptor for message to be sent
672 * Prevents restricted configuration commands from being issued by
673 * unauthorized users.
675 * Returns 0 if permission is granted, otherwise errno
677 static int dest_name_check(struct sockaddr_tipc
*dest
, struct msghdr
*m
)
679 struct tipc_cfg_msg_hdr hdr
;
681 if (unlikely(dest
->addrtype
== TIPC_ADDR_ID
))
683 if (likely(dest
->addr
.name
.name
.type
>= TIPC_RESERVED_TYPES
))
685 if (likely(dest
->addr
.name
.name
.type
== TIPC_TOP_SRV
))
687 if (likely(dest
->addr
.name
.name
.type
!= TIPC_CFG_SRV
))
690 if (!m
->msg_iovlen
|| (m
->msg_iov
[0].iov_len
< sizeof(hdr
)))
692 if (copy_from_user(&hdr
, m
->msg_iov
[0].iov_base
, sizeof(hdr
)))
694 if ((ntohs(hdr
.tcm_type
) & 0xC000) && (!capable(CAP_NET_ADMIN
)))
700 static int tipc_wait_for_sndmsg(struct socket
*sock
, long *timeo_p
)
702 struct sock
*sk
= sock
->sk
;
703 struct tipc_sock
*tsk
= tipc_sk(sk
);
708 int err
= sock_error(sk
);
711 if (sock
->state
== SS_DISCONNECTING
)
715 if (signal_pending(current
))
716 return sock_intr_errno(*timeo_p
);
718 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
719 done
= sk_wait_event(sk
, timeo_p
, !tsk
->link_cong
);
720 finish_wait(sk_sleep(sk
), &wait
);
726 * tipc_sendmsg - send message in connectionless manner
727 * @iocb: if NULL, indicates that socket lock is already held
728 * @sock: socket structure
729 * @m: message to send
730 * @dsz: amount of user data to be sent
732 * Message must have an destination specified explicitly.
733 * Used for SOCK_RDM and SOCK_DGRAM messages,
734 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
735 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
737 * Returns the number of bytes sent on success, or errno otherwise
739 static int tipc_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
740 struct msghdr
*m
, size_t dsz
)
742 DECLARE_SOCKADDR(struct sockaddr_tipc
*, dest
, m
->msg_name
);
743 struct sock
*sk
= sock
->sk
;
744 struct tipc_sock
*tsk
= tipc_sk(sk
);
745 struct tipc_port
*port
= &tsk
->port
;
746 struct tipc_msg
*mhdr
= &port
->phdr
;
747 struct iovec
*iov
= m
->msg_iov
;
750 struct tipc_name_seq
*seq
= &dest
->addr
.nameseq
;
756 return -EDESTADDRREQ
;
758 if (unlikely((m
->msg_namelen
< sizeof(*dest
)) ||
759 (dest
->family
!= AF_TIPC
)))
762 if (dsz
> TIPC_MAX_USER_MSG_SIZE
)
768 if (unlikely(sock
->state
!= SS_READY
)) {
769 if (sock
->state
== SS_LISTENING
) {
773 if (sock
->state
!= SS_UNCONNECTED
) {
777 if (tsk
->port
.published
) {
781 if (dest
->addrtype
== TIPC_ADDR_NAME
) {
782 tsk
->port
.conn_type
= dest
->addr
.name
.name
.type
;
783 tsk
->port
.conn_instance
= dest
->addr
.name
.name
.instance
;
786 rc
= dest_name_check(dest
, m
);
790 timeo
= sock_sndtimeo(sk
, m
->msg_flags
& MSG_DONTWAIT
);
792 if (dest
->addrtype
== TIPC_ADDR_MCAST
) {
793 rc
= tipc_sendmcast(sock
, seq
, iov
, dsz
, timeo
);
795 } else if (dest
->addrtype
== TIPC_ADDR_NAME
) {
796 u32 type
= dest
->addr
.name
.name
.type
;
797 u32 inst
= dest
->addr
.name
.name
.instance
;
798 u32 domain
= dest
->addr
.name
.domain
;
801 msg_set_type(mhdr
, TIPC_NAMED_MSG
);
802 msg_set_hdr_sz(mhdr
, NAMED_H_SIZE
);
803 msg_set_nametype(mhdr
, type
);
804 msg_set_nameinst(mhdr
, inst
);
805 msg_set_lookup_scope(mhdr
, tipc_addr_scope(domain
));
806 dport
= tipc_nametbl_translate(type
, inst
, &dnode
);
807 msg_set_destnode(mhdr
, dnode
);
808 msg_set_destport(mhdr
, dport
);
809 if (unlikely(!dport
&& !dnode
)) {
813 } else if (dest
->addrtype
== TIPC_ADDR_ID
) {
814 dnode
= dest
->addr
.id
.node
;
815 msg_set_type(mhdr
, TIPC_DIRECT_MSG
);
816 msg_set_lookup_scope(mhdr
, 0);
817 msg_set_destnode(mhdr
, dnode
);
818 msg_set_destport(mhdr
, dest
->addr
.id
.ref
);
819 msg_set_hdr_sz(mhdr
, BASIC_H_SIZE
);
823 mtu
= tipc_node_get_mtu(dnode
, tsk
->port
.ref
);
824 rc
= tipc_msg_build(mhdr
, iov
, 0, dsz
, mtu
, &buf
);
829 rc
= tipc_link_xmit(buf
, dnode
, tsk
->port
.ref
);
830 if (likely(rc
>= 0)) {
831 if (sock
->state
!= SS_READY
)
832 sock
->state
= SS_CONNECTING
;
839 if (rc
!= -ELINKCONG
)
842 rc
= tipc_wait_for_sndmsg(sock
, &timeo
);
853 static int tipc_wait_for_sndpkt(struct socket
*sock
, long *timeo_p
)
855 struct sock
*sk
= sock
->sk
;
856 struct tipc_sock
*tsk
= tipc_sk(sk
);
861 int err
= sock_error(sk
);
864 if (sock
->state
== SS_DISCONNECTING
)
866 else if (sock
->state
!= SS_CONNECTED
)
870 if (signal_pending(current
))
871 return sock_intr_errno(*timeo_p
);
873 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
874 done
= sk_wait_event(sk
, timeo_p
,
876 !tipc_sk_conn_cong(tsk
)) ||
877 !tsk
->port
.connected
);
878 finish_wait(sk_sleep(sk
), &wait
);
884 * tipc_send_stream - send stream-oriented data
886 * @sock: socket structure
888 * @dsz: total length of data to be transmitted
890 * Used for SOCK_STREAM data.
892 * Returns the number of bytes sent on success (or partial success),
893 * or errno if no data sent
895 static int tipc_send_stream(struct kiocb
*iocb
, struct socket
*sock
,
896 struct msghdr
*m
, size_t dsz
)
898 struct sock
*sk
= sock
->sk
;
899 struct tipc_sock
*tsk
= tipc_sk(sk
);
900 struct tipc_port
*port
= &tsk
->port
;
901 struct tipc_msg
*mhdr
= &port
->phdr
;
903 DECLARE_SOCKADDR(struct sockaddr_tipc
*, dest
, m
->msg_name
);
908 uint mtu
, send
, sent
= 0;
910 /* Handle implied connection establishment */
911 if (unlikely(dest
)) {
912 rc
= tipc_sendmsg(iocb
, sock
, m
, dsz
);
913 if (dsz
&& (dsz
== rc
))
914 tsk
->sent_unacked
= 1;
917 if (dsz
> (uint
)INT_MAX
)
923 if (unlikely(sock
->state
!= SS_CONNECTED
)) {
924 if (sock
->state
== SS_DISCONNECTING
)
931 timeo
= sock_sndtimeo(sk
, m
->msg_flags
& MSG_DONTWAIT
);
932 dnode
= tipc_port_peernode(port
);
936 send
= min_t(uint
, dsz
- sent
, TIPC_MAX_USER_MSG_SIZE
);
937 rc
= tipc_msg_build(mhdr
, m
->msg_iov
, sent
, send
, mtu
, &buf
);
938 if (unlikely(rc
< 0))
941 if (likely(!tipc_sk_conn_cong(tsk
))) {
942 rc
= tipc_link_xmit(buf
, dnode
, ref
);
950 if (rc
== -EMSGSIZE
) {
951 port
->max_pkt
= tipc_node_get_mtu(dnode
, ref
);
954 if (rc
!= -ELINKCONG
)
957 rc
= tipc_wait_for_sndpkt(sock
, &timeo
);
964 return sent
? sent
: rc
;
968 * tipc_send_packet - send a connection-oriented message
969 * @iocb: if NULL, indicates that socket lock is already held
970 * @sock: socket structure
971 * @m: message to send
972 * @dsz: length of data to be transmitted
974 * Used for SOCK_SEQPACKET messages.
976 * Returns the number of bytes sent on success, or errno otherwise
978 static int tipc_send_packet(struct kiocb
*iocb
, struct socket
*sock
,
979 struct msghdr
*m
, size_t dsz
)
981 if (dsz
> TIPC_MAX_USER_MSG_SIZE
)
984 return tipc_send_stream(iocb
, sock
, m
, dsz
);
988 * auto_connect - complete connection setup to a remote port
989 * @tsk: tipc socket structure
990 * @msg: peer's response message
992 * Returns 0 on success, errno otherwise
994 static int auto_connect(struct tipc_sock
*tsk
, struct tipc_msg
*msg
)
996 struct tipc_port
*port
= &tsk
->port
;
997 struct socket
*sock
= tsk
->sk
.sk_socket
;
998 struct tipc_portid peer
;
1000 peer
.ref
= msg_origport(msg
);
1001 peer
.node
= msg_orignode(msg
);
1003 __tipc_port_connect(port
->ref
, port
, &peer
);
1005 if (msg_importance(msg
) > TIPC_CRITICAL_IMPORTANCE
)
1007 msg_set_importance(&port
->phdr
, (u32
)msg_importance(msg
));
1008 sock
->state
= SS_CONNECTED
;
1013 * set_orig_addr - capture sender's address for received message
1014 * @m: descriptor for message info
1015 * @msg: received message header
1017 * Note: Address is not captured if not requested by receiver.
1019 static void set_orig_addr(struct msghdr
*m
, struct tipc_msg
*msg
)
1021 DECLARE_SOCKADDR(struct sockaddr_tipc
*, addr
, m
->msg_name
);
1024 addr
->family
= AF_TIPC
;
1025 addr
->addrtype
= TIPC_ADDR_ID
;
1026 memset(&addr
->addr
, 0, sizeof(addr
->addr
));
1027 addr
->addr
.id
.ref
= msg_origport(msg
);
1028 addr
->addr
.id
.node
= msg_orignode(msg
);
1029 addr
->addr
.name
.domain
= 0; /* could leave uninitialized */
1030 addr
->scope
= 0; /* could leave uninitialized */
1031 m
->msg_namelen
= sizeof(struct sockaddr_tipc
);
1036 * anc_data_recv - optionally capture ancillary data for received message
1037 * @m: descriptor for message info
1038 * @msg: received message header
1039 * @tport: TIPC port associated with message
1041 * Note: Ancillary data is not captured if not requested by receiver.
1043 * Returns 0 if successful, otherwise errno
1045 static int anc_data_recv(struct msghdr
*m
, struct tipc_msg
*msg
,
1046 struct tipc_port
*tport
)
1054 if (likely(m
->msg_controllen
== 0))
1057 /* Optionally capture errored message object(s) */
1058 err
= msg
? msg_errcode(msg
) : 0;
1059 if (unlikely(err
)) {
1061 anc_data
[1] = msg_data_sz(msg
);
1062 res
= put_cmsg(m
, SOL_TIPC
, TIPC_ERRINFO
, 8, anc_data
);
1066 res
= put_cmsg(m
, SOL_TIPC
, TIPC_RETDATA
, anc_data
[1],
1073 /* Optionally capture message destination object */
1074 dest_type
= msg
? msg_type(msg
) : TIPC_DIRECT_MSG
;
1075 switch (dest_type
) {
1076 case TIPC_NAMED_MSG
:
1078 anc_data
[0] = msg_nametype(msg
);
1079 anc_data
[1] = msg_namelower(msg
);
1080 anc_data
[2] = msg_namelower(msg
);
1082 case TIPC_MCAST_MSG
:
1084 anc_data
[0] = msg_nametype(msg
);
1085 anc_data
[1] = msg_namelower(msg
);
1086 anc_data
[2] = msg_nameupper(msg
);
1089 has_name
= (tport
->conn_type
!= 0);
1090 anc_data
[0] = tport
->conn_type
;
1091 anc_data
[1] = tport
->conn_instance
;
1092 anc_data
[2] = tport
->conn_instance
;
1098 res
= put_cmsg(m
, SOL_TIPC
, TIPC_DESTNAME
, 12, anc_data
);
1106 static int tipc_wait_for_rcvmsg(struct socket
*sock
, long *timeop
)
1108 struct sock
*sk
= sock
->sk
;
1110 long timeo
= *timeop
;
1114 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
1115 if (timeo
&& skb_queue_empty(&sk
->sk_receive_queue
)) {
1116 if (sock
->state
== SS_DISCONNECTING
) {
1121 timeo
= schedule_timeout(timeo
);
1125 if (!skb_queue_empty(&sk
->sk_receive_queue
))
1127 err
= sock_intr_errno(timeo
);
1128 if (signal_pending(current
))
1134 finish_wait(sk_sleep(sk
), &wait
);
1140 * tipc_recvmsg - receive packet-oriented message
1142 * @m: descriptor for message info
1143 * @buf_len: total size of user buffer area
1144 * @flags: receive flags
1146 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
1147 * If the complete message doesn't fit in user area, truncate it.
1149 * Returns size of returned message data, errno otherwise
1151 static int tipc_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1152 struct msghdr
*m
, size_t buf_len
, int flags
)
1154 struct sock
*sk
= sock
->sk
;
1155 struct tipc_sock
*tsk
= tipc_sk(sk
);
1156 struct tipc_port
*port
= &tsk
->port
;
1157 struct sk_buff
*buf
;
1158 struct tipc_msg
*msg
;
1164 /* Catch invalid receive requests */
1165 if (unlikely(!buf_len
))
1170 if (unlikely(sock
->state
== SS_UNCONNECTED
)) {
1175 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
1178 /* Look for a message in receive queue; wait if necessary */
1179 res
= tipc_wait_for_rcvmsg(sock
, &timeo
);
1183 /* Look at first message in receive queue */
1184 buf
= skb_peek(&sk
->sk_receive_queue
);
1186 sz
= msg_data_sz(msg
);
1187 err
= msg_errcode(msg
);
1189 /* Discard an empty non-errored message & try again */
1190 if ((!sz
) && (!err
)) {
1191 advance_rx_queue(sk
);
1195 /* Capture sender's address (optional) */
1196 set_orig_addr(m
, msg
);
1198 /* Capture ancillary data (optional) */
1199 res
= anc_data_recv(m
, msg
, port
);
1203 /* Capture message data (if valid) & compute return value (always) */
1205 if (unlikely(buf_len
< sz
)) {
1207 m
->msg_flags
|= MSG_TRUNC
;
1209 res
= skb_copy_datagram_iovec(buf
, msg_hdr_sz(msg
),
1215 if ((sock
->state
== SS_READY
) ||
1216 ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
))
1222 /* Consume received message (optional) */
1223 if (likely(!(flags
& MSG_PEEK
))) {
1224 if ((sock
->state
!= SS_READY
) &&
1225 (++tsk
->rcv_unacked
>= TIPC_CONNACK_INTV
)) {
1226 tipc_acknowledge(port
->ref
, tsk
->rcv_unacked
);
1227 tsk
->rcv_unacked
= 0;
1229 advance_rx_queue(sk
);
1237 * tipc_recv_stream - receive stream-oriented data
1239 * @m: descriptor for message info
1240 * @buf_len: total size of user buffer area
1241 * @flags: receive flags
1243 * Used for SOCK_STREAM messages only. If not enough data is available
1244 * will optionally wait for more; never truncates data.
1246 * Returns size of returned message data, errno otherwise
1248 static int tipc_recv_stream(struct kiocb
*iocb
, struct socket
*sock
,
1249 struct msghdr
*m
, size_t buf_len
, int flags
)
1251 struct sock
*sk
= sock
->sk
;
1252 struct tipc_sock
*tsk
= tipc_sk(sk
);
1253 struct tipc_port
*port
= &tsk
->port
;
1254 struct sk_buff
*buf
;
1255 struct tipc_msg
*msg
;
1258 int sz_to_copy
, target
, needed
;
1263 /* Catch invalid receive attempts */
1264 if (unlikely(!buf_len
))
1269 if (unlikely(sock
->state
== SS_UNCONNECTED
)) {
1274 target
= sock_rcvlowat(sk
, flags
& MSG_WAITALL
, buf_len
);
1275 timeo
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
1278 /* Look for a message in receive queue; wait if necessary */
1279 res
= tipc_wait_for_rcvmsg(sock
, &timeo
);
1283 /* Look at first message in receive queue */
1284 buf
= skb_peek(&sk
->sk_receive_queue
);
1286 sz
= msg_data_sz(msg
);
1287 err
= msg_errcode(msg
);
1289 /* Discard an empty non-errored message & try again */
1290 if ((!sz
) && (!err
)) {
1291 advance_rx_queue(sk
);
1295 /* Optionally capture sender's address & ancillary data of first msg */
1296 if (sz_copied
== 0) {
1297 set_orig_addr(m
, msg
);
1298 res
= anc_data_recv(m
, msg
, port
);
1303 /* Capture message data (if valid) & compute return value (always) */
1305 u32 offset
= (u32
)(unsigned long)(TIPC_SKB_CB(buf
)->handle
);
1308 needed
= (buf_len
- sz_copied
);
1309 sz_to_copy
= (sz
<= needed
) ? sz
: needed
;
1311 res
= skb_copy_datagram_iovec(buf
, msg_hdr_sz(msg
) + offset
,
1312 m
->msg_iov
, sz_to_copy
);
1316 sz_copied
+= sz_to_copy
;
1318 if (sz_to_copy
< sz
) {
1319 if (!(flags
& MSG_PEEK
))
1320 TIPC_SKB_CB(buf
)->handle
=
1321 (void *)(unsigned long)(offset
+ sz_to_copy
);
1326 goto exit
; /* can't add error msg to valid data */
1328 if ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
)
1334 /* Consume received message (optional) */
1335 if (likely(!(flags
& MSG_PEEK
))) {
1336 if (unlikely(++tsk
->rcv_unacked
>= TIPC_CONNACK_INTV
)) {
1337 tipc_acknowledge(port
->ref
, tsk
->rcv_unacked
);
1338 tsk
->rcv_unacked
= 0;
1340 advance_rx_queue(sk
);
1343 /* Loop around if more data is required */
1344 if ((sz_copied
< buf_len
) && /* didn't get all requested data */
1345 (!skb_queue_empty(&sk
->sk_receive_queue
) ||
1346 (sz_copied
< target
)) && /* and more is ready or required */
1347 (!(flags
& MSG_PEEK
)) && /* and aren't just peeking at data */
1348 (!err
)) /* and haven't reached a FIN */
1353 return sz_copied
? sz_copied
: res
;
1357 * tipc_write_space - wake up thread if port congestion is released
1360 static void tipc_write_space(struct sock
*sk
)
1362 struct socket_wq
*wq
;
1365 wq
= rcu_dereference(sk
->sk_wq
);
1366 if (wq_has_sleeper(wq
))
1367 wake_up_interruptible_sync_poll(&wq
->wait
, POLLOUT
|
1368 POLLWRNORM
| POLLWRBAND
);
1373 * tipc_data_ready - wake up threads to indicate messages have been received
1375 * @len: the length of messages
1377 static void tipc_data_ready(struct sock
*sk
)
1379 struct socket_wq
*wq
;
1382 wq
= rcu_dereference(sk
->sk_wq
);
1383 if (wq_has_sleeper(wq
))
1384 wake_up_interruptible_sync_poll(&wq
->wait
, POLLIN
|
1385 POLLRDNORM
| POLLRDBAND
);
1390 * filter_connect - Handle all incoming messages for a connection-based socket
1394 * Returns 0 (TIPC_OK) if everyting ok, -TIPC_ERR_NO_PORT otherwise
1396 static int filter_connect(struct tipc_sock
*tsk
, struct sk_buff
**buf
)
1398 struct sock
*sk
= &tsk
->sk
;
1399 struct tipc_port
*port
= &tsk
->port
;
1400 struct socket
*sock
= sk
->sk_socket
;
1401 struct tipc_msg
*msg
= buf_msg(*buf
);
1403 int retval
= -TIPC_ERR_NO_PORT
;
1409 switch ((int)sock
->state
) {
1411 /* Accept only connection-based messages sent by peer */
1412 if (msg_connected(msg
) && tipc_port_peer_msg(port
, msg
)) {
1413 if (unlikely(msg_errcode(msg
))) {
1414 sock
->state
= SS_DISCONNECTING
;
1415 __tipc_port_disconnect(port
);
1421 /* Accept only ACK or NACK message */
1422 if (unlikely(msg_errcode(msg
))) {
1423 sock
->state
= SS_DISCONNECTING
;
1424 sk
->sk_err
= ECONNREFUSED
;
1429 if (unlikely(!msg_connected(msg
)))
1432 res
= auto_connect(tsk
, msg
);
1434 sock
->state
= SS_DISCONNECTING
;
1440 /* If an incoming message is an 'ACK-', it should be
1441 * discarded here because it doesn't contain useful
1442 * data. In addition, we should try to wake up
1443 * connect() routine if sleeping.
1445 if (msg_data_sz(msg
) == 0) {
1448 if (waitqueue_active(sk_sleep(sk
)))
1449 wake_up_interruptible(sk_sleep(sk
));
1454 case SS_UNCONNECTED
:
1455 /* Accept only SYN message */
1456 if (!msg_connected(msg
) && !(msg_errcode(msg
)))
1459 case SS_DISCONNECTING
:
1462 pr_err("Unknown socket state %u\n", sock
->state
);
1468 * rcvbuf_limit - get proper overload limit of socket receive queue
1472 * For all connection oriented messages, irrespective of importance,
1473 * the default overload value (i.e. 67MB) is set as limit.
1475 * For all connectionless messages, by default new queue limits are
1478 * TIPC_LOW_IMPORTANCE (4 MB)
1479 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1480 * TIPC_HIGH_IMPORTANCE (16 MB)
1481 * TIPC_CRITICAL_IMPORTANCE (32 MB)
1483 * Returns overload limit according to corresponding message importance
1485 static unsigned int rcvbuf_limit(struct sock
*sk
, struct sk_buff
*buf
)
1487 struct tipc_msg
*msg
= buf_msg(buf
);
1489 if (msg_connected(msg
))
1490 return sysctl_tipc_rmem
[2];
1492 return sk
->sk_rcvbuf
>> TIPC_CRITICAL_IMPORTANCE
<<
1493 msg_importance(msg
);
1497 * filter_rcv - validate incoming message
1501 * Enqueues message on receive queue if acceptable; optionally handles
1502 * disconnect indication for a connected socket.
1504 * Called with socket lock already taken; port lock may also be taken.
1506 * Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
1507 * to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
1509 static int filter_rcv(struct sock
*sk
, struct sk_buff
*buf
)
1511 struct socket
*sock
= sk
->sk_socket
;
1512 struct tipc_sock
*tsk
= tipc_sk(sk
);
1513 struct tipc_msg
*msg
= buf_msg(buf
);
1514 unsigned int limit
= rcvbuf_limit(sk
, buf
);
1518 if (unlikely(msg_user(msg
) == CONN_MANAGER
))
1519 return tipc_sk_proto_rcv(tsk
, &onode
, buf
);
1521 /* Reject message if it is wrong sort of message for socket */
1522 if (msg_type(msg
) > TIPC_DIRECT_MSG
)
1523 return -TIPC_ERR_NO_PORT
;
1525 if (sock
->state
== SS_READY
) {
1526 if (msg_connected(msg
))
1527 return -TIPC_ERR_NO_PORT
;
1529 rc
= filter_connect(tsk
, &buf
);
1530 if (rc
!= TIPC_OK
|| buf
== NULL
)
1534 /* Reject message if there isn't room to queue it */
1535 if (sk_rmem_alloc_get(sk
) + buf
->truesize
>= limit
)
1536 return -TIPC_ERR_OVERLOAD
;
1538 /* Enqueue message */
1539 TIPC_SKB_CB(buf
)->handle
= NULL
;
1540 __skb_queue_tail(&sk
->sk_receive_queue
, buf
);
1541 skb_set_owner_r(buf
, sk
);
1543 sk
->sk_data_ready(sk
);
1548 * tipc_backlog_rcv - handle incoming message from backlog queue
1552 * Caller must hold socket lock, but not port lock.
1556 static int tipc_backlog_rcv(struct sock
*sk
, struct sk_buff
*buf
)
1560 struct tipc_sock
*tsk
= tipc_sk(sk
);
1561 uint truesize
= buf
->truesize
;
1563 rc
= filter_rcv(sk
, buf
);
1566 if (atomic_read(&tsk
->dupl_rcvcnt
) < TIPC_CONN_OVERLOAD_LIMIT
)
1567 atomic_add(truesize
, &tsk
->dupl_rcvcnt
);
1571 if ((rc
< 0) && !tipc_msg_reverse(buf
, &onode
, -rc
))
1574 tipc_link_xmit(buf
, onode
, 0);
1580 * tipc_sk_rcv - handle incoming message
1581 * @buf: buffer containing arriving message
1583 * Returns 0 if success, or errno: -EHOSTUNREACH
1585 int tipc_sk_rcv(struct sk_buff
*buf
)
1587 struct tipc_sock
*tsk
;
1588 struct tipc_port
*port
;
1590 u32 dport
= msg_destport(buf_msg(buf
));
1595 /* Validate destination and message */
1596 port
= tipc_port_lock(dport
);
1597 if (unlikely(!port
)) {
1598 rc
= tipc_msg_eval(buf
, &dnode
);
1602 tsk
= tipc_port_to_sock(port
);
1608 if (!sock_owned_by_user(sk
)) {
1609 rc
= filter_rcv(sk
, buf
);
1611 if (sk
->sk_backlog
.len
== 0)
1612 atomic_set(&tsk
->dupl_rcvcnt
, 0);
1613 limit
= rcvbuf_limit(sk
, buf
) + atomic_read(&tsk
->dupl_rcvcnt
);
1614 if (sk_add_backlog(sk
, buf
, limit
))
1615 rc
= -TIPC_ERR_OVERLOAD
;
1618 tipc_port_unlock(port
);
1623 if ((rc
< 0) && !tipc_msg_reverse(buf
, &dnode
, -rc
))
1624 return -EHOSTUNREACH
;
1626 tipc_link_xmit(buf
, dnode
, 0);
1627 return (rc
< 0) ? -EHOSTUNREACH
: 0;
1630 static int tipc_wait_for_connect(struct socket
*sock
, long *timeo_p
)
1632 struct sock
*sk
= sock
->sk
;
1637 int err
= sock_error(sk
);
1642 if (signal_pending(current
))
1643 return sock_intr_errno(*timeo_p
);
1645 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
1646 done
= sk_wait_event(sk
, timeo_p
, sock
->state
!= SS_CONNECTING
);
1647 finish_wait(sk_sleep(sk
), &wait
);
1653 * tipc_connect - establish a connection to another TIPC port
1654 * @sock: socket structure
1655 * @dest: socket address for destination port
1656 * @destlen: size of socket address data structure
1657 * @flags: file-related flags associated with socket
1659 * Returns 0 on success, errno otherwise
1661 static int tipc_connect(struct socket
*sock
, struct sockaddr
*dest
,
1662 int destlen
, int flags
)
1664 struct sock
*sk
= sock
->sk
;
1665 struct sockaddr_tipc
*dst
= (struct sockaddr_tipc
*)dest
;
1666 struct msghdr m
= {NULL
,};
1667 long timeout
= (flags
& O_NONBLOCK
) ? 0 : tipc_sk(sk
)->conn_timeout
;
1668 socket_state previous
;
1673 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
1674 if (sock
->state
== SS_READY
) {
1680 * Reject connection attempt using multicast address
1682 * Note: send_msg() validates the rest of the address fields,
1683 * so there's no need to do it here
1685 if (dst
->addrtype
== TIPC_ADDR_MCAST
) {
1690 previous
= sock
->state
;
1691 switch (sock
->state
) {
1692 case SS_UNCONNECTED
:
1693 /* Send a 'SYN-' to destination */
1695 m
.msg_namelen
= destlen
;
1697 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1698 * indicate send_msg() is never blocked.
1701 m
.msg_flags
= MSG_DONTWAIT
;
1703 res
= tipc_sendmsg(NULL
, sock
, &m
, 0);
1704 if ((res
< 0) && (res
!= -EWOULDBLOCK
))
1707 /* Just entered SS_CONNECTING state; the only
1708 * difference is that return value in non-blocking
1709 * case is EINPROGRESS, rather than EALREADY.
1713 if (previous
== SS_CONNECTING
)
1717 timeout
= msecs_to_jiffies(timeout
);
1718 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1719 res
= tipc_wait_for_connect(sock
, &timeout
);
1734 * tipc_listen - allow socket to listen for incoming connections
1735 * @sock: socket structure
1738 * Returns 0 on success, errno otherwise
1740 static int tipc_listen(struct socket
*sock
, int len
)
1742 struct sock
*sk
= sock
->sk
;
1747 if (sock
->state
!= SS_UNCONNECTED
)
1750 sock
->state
= SS_LISTENING
;
1758 static int tipc_wait_for_accept(struct socket
*sock
, long timeo
)
1760 struct sock
*sk
= sock
->sk
;
1764 /* True wake-one mechanism for incoming connections: only
1765 * one process gets woken up, not the 'whole herd'.
1766 * Since we do not 'race & poll' for established sockets
1767 * anymore, the common case will execute the loop only once.
1770 prepare_to_wait_exclusive(sk_sleep(sk
), &wait
,
1771 TASK_INTERRUPTIBLE
);
1772 if (timeo
&& skb_queue_empty(&sk
->sk_receive_queue
)) {
1774 timeo
= schedule_timeout(timeo
);
1778 if (!skb_queue_empty(&sk
->sk_receive_queue
))
1781 if (sock
->state
!= SS_LISTENING
)
1783 err
= sock_intr_errno(timeo
);
1784 if (signal_pending(current
))
1790 finish_wait(sk_sleep(sk
), &wait
);
1795 * tipc_accept - wait for connection request
1796 * @sock: listening socket
1797 * @newsock: new socket that is to be connected
1798 * @flags: file-related flags associated with socket
1800 * Returns 0 on success, errno otherwise
1802 static int tipc_accept(struct socket
*sock
, struct socket
*new_sock
, int flags
)
1804 struct sock
*new_sk
, *sk
= sock
->sk
;
1805 struct sk_buff
*buf
;
1806 struct tipc_port
*new_port
;
1807 struct tipc_msg
*msg
;
1808 struct tipc_portid peer
;
1815 if (sock
->state
!= SS_LISTENING
) {
1819 timeo
= sock_rcvtimeo(sk
, flags
& O_NONBLOCK
);
1820 res
= tipc_wait_for_accept(sock
, timeo
);
1824 buf
= skb_peek(&sk
->sk_receive_queue
);
1826 res
= tipc_sk_create(sock_net(sock
->sk
), new_sock
, 0, 1);
1830 new_sk
= new_sock
->sk
;
1831 new_port
= &tipc_sk(new_sk
)->port
;
1832 new_ref
= new_port
->ref
;
1835 /* we lock on new_sk; but lockdep sees the lock on sk */
1836 lock_sock_nested(new_sk
, SINGLE_DEPTH_NESTING
);
1839 * Reject any stray messages received by new socket
1840 * before the socket lock was taken (very, very unlikely)
1842 reject_rx_queue(new_sk
);
1844 /* Connect new socket to it's peer */
1845 peer
.ref
= msg_origport(msg
);
1846 peer
.node
= msg_orignode(msg
);
1847 tipc_port_connect(new_ref
, &peer
);
1848 new_sock
->state
= SS_CONNECTED
;
1850 tipc_port_set_importance(new_port
, msg_importance(msg
));
1851 if (msg_named(msg
)) {
1852 new_port
->conn_type
= msg_nametype(msg
);
1853 new_port
->conn_instance
= msg_nameinst(msg
);
1857 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1858 * Respond to 'SYN+' by queuing it on new socket.
1860 if (!msg_data_sz(msg
)) {
1861 struct msghdr m
= {NULL
,};
1863 advance_rx_queue(sk
);
1864 tipc_send_packet(NULL
, new_sock
, &m
, 0);
1866 __skb_dequeue(&sk
->sk_receive_queue
);
1867 __skb_queue_head(&new_sk
->sk_receive_queue
, buf
);
1868 skb_set_owner_r(buf
, new_sk
);
1870 release_sock(new_sk
);
1877 * tipc_shutdown - shutdown socket connection
1878 * @sock: socket structure
1879 * @how: direction to close (must be SHUT_RDWR)
1881 * Terminates connection (if necessary), then purges socket's receive queue.
1883 * Returns 0 on success, errno otherwise
1885 static int tipc_shutdown(struct socket
*sock
, int how
)
1887 struct sock
*sk
= sock
->sk
;
1888 struct tipc_sock
*tsk
= tipc_sk(sk
);
1889 struct tipc_port
*port
= &tsk
->port
;
1890 struct sk_buff
*buf
;
1894 if (how
!= SHUT_RDWR
)
1899 switch (sock
->state
) {
1904 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
1905 buf
= __skb_dequeue(&sk
->sk_receive_queue
);
1907 if (TIPC_SKB_CB(buf
)->handle
!= NULL
) {
1911 tipc_port_disconnect(port
->ref
);
1912 if (tipc_msg_reverse(buf
, &peer
, TIPC_CONN_SHUTDOWN
))
1913 tipc_link_xmit(buf
, peer
, 0);
1915 tipc_port_shutdown(port
->ref
);
1918 sock
->state
= SS_DISCONNECTING
;
1922 case SS_DISCONNECTING
:
1924 /* Discard any unreceived messages */
1925 __skb_queue_purge(&sk
->sk_receive_queue
);
1927 /* Wake up anyone sleeping in poll */
1928 sk
->sk_state_change(sk
);
1941 * tipc_setsockopt - set socket option
1942 * @sock: socket structure
1943 * @lvl: option level
1944 * @opt: option identifier
1945 * @ov: pointer to new option value
1946 * @ol: length of option value
1948 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
1949 * (to ease compatibility).
1951 * Returns 0 on success, errno otherwise
1953 static int tipc_setsockopt(struct socket
*sock
, int lvl
, int opt
,
1954 char __user
*ov
, unsigned int ol
)
1956 struct sock
*sk
= sock
->sk
;
1957 struct tipc_sock
*tsk
= tipc_sk(sk
);
1958 struct tipc_port
*port
= &tsk
->port
;
1962 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
1964 if (lvl
!= SOL_TIPC
)
1965 return -ENOPROTOOPT
;
1966 if (ol
< sizeof(value
))
1968 res
= get_user(value
, (u32 __user
*)ov
);
1975 case TIPC_IMPORTANCE
:
1976 tipc_port_set_importance(port
, value
);
1978 case TIPC_SRC_DROPPABLE
:
1979 if (sock
->type
!= SOCK_STREAM
)
1980 tipc_port_set_unreliable(port
, value
);
1984 case TIPC_DEST_DROPPABLE
:
1985 tipc_port_set_unreturnable(port
, value
);
1987 case TIPC_CONN_TIMEOUT
:
1988 tipc_sk(sk
)->conn_timeout
= value
;
1989 /* no need to set "res", since already 0 at this point */
2001 * tipc_getsockopt - get socket option
2002 * @sock: socket structure
2003 * @lvl: option level
2004 * @opt: option identifier
2005 * @ov: receptacle for option value
2006 * @ol: receptacle for length of option value
2008 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
2009 * (to ease compatibility).
2011 * Returns 0 on success, errno otherwise
2013 static int tipc_getsockopt(struct socket
*sock
, int lvl
, int opt
,
2014 char __user
*ov
, int __user
*ol
)
2016 struct sock
*sk
= sock
->sk
;
2017 struct tipc_sock
*tsk
= tipc_sk(sk
);
2018 struct tipc_port
*port
= &tsk
->port
;
2023 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
2024 return put_user(0, ol
);
2025 if (lvl
!= SOL_TIPC
)
2026 return -ENOPROTOOPT
;
2027 res
= get_user(len
, ol
);
2034 case TIPC_IMPORTANCE
:
2035 value
= tipc_port_importance(port
);
2037 case TIPC_SRC_DROPPABLE
:
2038 value
= tipc_port_unreliable(port
);
2040 case TIPC_DEST_DROPPABLE
:
2041 value
= tipc_port_unreturnable(port
);
2043 case TIPC_CONN_TIMEOUT
:
2044 value
= tipc_sk(sk
)->conn_timeout
;
2045 /* no need to set "res", since already 0 at this point */
2047 case TIPC_NODE_RECVQ_DEPTH
:
2048 value
= 0; /* was tipc_queue_size, now obsolete */
2050 case TIPC_SOCK_RECVQ_DEPTH
:
2051 value
= skb_queue_len(&sk
->sk_receive_queue
);
2060 return res
; /* "get" failed */
2062 if (len
< sizeof(value
))
2065 if (copy_to_user(ov
, &value
, sizeof(value
)))
2068 return put_user(sizeof(value
), ol
);
2071 static int tipc_ioctl(struct socket
*sk
, unsigned int cmd
, unsigned long arg
)
2073 struct tipc_sioc_ln_req lnr
;
2074 void __user
*argp
= (void __user
*)arg
;
2077 case SIOCGETLINKNAME
:
2078 if (copy_from_user(&lnr
, argp
, sizeof(lnr
)))
2080 if (!tipc_node_get_linkname(lnr
.bearer_id
, lnr
.peer
,
2081 lnr
.linkname
, TIPC_MAX_LINK_NAME
)) {
2082 if (copy_to_user(argp
, &lnr
, sizeof(lnr
)))
2086 return -EADDRNOTAVAIL
;
2088 return -ENOIOCTLCMD
;
2092 /* Protocol switches for the various types of TIPC sockets */
2094 static const struct proto_ops msg_ops
= {
2095 .owner
= THIS_MODULE
,
2097 .release
= tipc_release
,
2099 .connect
= tipc_connect
,
2100 .socketpair
= sock_no_socketpair
,
2101 .accept
= sock_no_accept
,
2102 .getname
= tipc_getname
,
2104 .ioctl
= tipc_ioctl
,
2105 .listen
= sock_no_listen
,
2106 .shutdown
= tipc_shutdown
,
2107 .setsockopt
= tipc_setsockopt
,
2108 .getsockopt
= tipc_getsockopt
,
2109 .sendmsg
= tipc_sendmsg
,
2110 .recvmsg
= tipc_recvmsg
,
2111 .mmap
= sock_no_mmap
,
2112 .sendpage
= sock_no_sendpage
2115 static const struct proto_ops packet_ops
= {
2116 .owner
= THIS_MODULE
,
2118 .release
= tipc_release
,
2120 .connect
= tipc_connect
,
2121 .socketpair
= sock_no_socketpair
,
2122 .accept
= tipc_accept
,
2123 .getname
= tipc_getname
,
2125 .ioctl
= tipc_ioctl
,
2126 .listen
= tipc_listen
,
2127 .shutdown
= tipc_shutdown
,
2128 .setsockopt
= tipc_setsockopt
,
2129 .getsockopt
= tipc_getsockopt
,
2130 .sendmsg
= tipc_send_packet
,
2131 .recvmsg
= tipc_recvmsg
,
2132 .mmap
= sock_no_mmap
,
2133 .sendpage
= sock_no_sendpage
2136 static const struct proto_ops stream_ops
= {
2137 .owner
= THIS_MODULE
,
2139 .release
= tipc_release
,
2141 .connect
= tipc_connect
,
2142 .socketpair
= sock_no_socketpair
,
2143 .accept
= tipc_accept
,
2144 .getname
= tipc_getname
,
2146 .ioctl
= tipc_ioctl
,
2147 .listen
= tipc_listen
,
2148 .shutdown
= tipc_shutdown
,
2149 .setsockopt
= tipc_setsockopt
,
2150 .getsockopt
= tipc_getsockopt
,
2151 .sendmsg
= tipc_send_stream
,
2152 .recvmsg
= tipc_recv_stream
,
2153 .mmap
= sock_no_mmap
,
2154 .sendpage
= sock_no_sendpage
2157 static const struct net_proto_family tipc_family_ops
= {
2158 .owner
= THIS_MODULE
,
2160 .create
= tipc_sk_create
2163 static struct proto tipc_proto
= {
2165 .owner
= THIS_MODULE
,
2166 .obj_size
= sizeof(struct tipc_sock
),
2167 .sysctl_rmem
= sysctl_tipc_rmem
2170 static struct proto tipc_proto_kern
= {
2172 .obj_size
= sizeof(struct tipc_sock
),
2173 .sysctl_rmem
= sysctl_tipc_rmem
2177 * tipc_socket_init - initialize TIPC socket interface
2179 * Returns 0 on success, errno otherwise
2181 int tipc_socket_init(void)
2185 res
= proto_register(&tipc_proto
, 1);
2187 pr_err("Failed to register TIPC protocol type\n");
2191 res
= sock_register(&tipc_family_ops
);
2193 pr_err("Failed to register TIPC socket type\n");
2194 proto_unregister(&tipc_proto
);
2202 * tipc_socket_stop - stop TIPC socket interface
2204 void tipc_socket_stop(void)
2206 sock_unregister(tipc_family_ops
.family
);
2207 proto_unregister(&tipc_proto
);