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 #define tipc_rx_ready(sock) (!skb_queue_empty(&sock->sk->sk_receive_queue) || \
59 (sock->state == SS_DISCONNECTING))
61 static int backlog_rcv(struct sock
*sk
, struct sk_buff
*skb
);
62 static u32
dispatch(struct tipc_port
*tport
, struct sk_buff
*buf
);
63 static void wakeupdispatch(struct tipc_port
*tport
);
64 static void tipc_data_ready(struct sock
*sk
, int len
);
65 static void tipc_write_space(struct sock
*sk
);
66 static int release(struct socket
*sock
);
67 static int accept(struct socket
*sock
, struct socket
*new_sock
, int flags
);
69 static const struct proto_ops packet_ops
;
70 static const struct proto_ops stream_ops
;
71 static const struct proto_ops msg_ops
;
73 static struct proto tipc_proto
;
74 static struct proto tipc_proto_kern
;
76 static int sockets_enabled
;
79 * Revised TIPC socket locking policy:
81 * Most socket operations take the standard socket lock when they start
82 * and hold it until they finish (or until they need to sleep). Acquiring
83 * this lock grants the owner exclusive access to the fields of the socket
84 * data structures, with the exception of the backlog queue. A few socket
85 * operations can be done without taking the socket lock because they only
86 * read socket information that never changes during the life of the socket.
88 * Socket operations may acquire the lock for the associated TIPC port if they
89 * need to perform an operation on the port. If any routine needs to acquire
90 * both the socket lock and the port lock it must take the socket lock first
91 * to avoid the risk of deadlock.
93 * The dispatcher handling incoming messages cannot grab the socket lock in
94 * the standard fashion, since invoked it runs at the BH level and cannot block.
95 * Instead, it checks to see if the socket lock is currently owned by someone,
96 * and either handles the message itself or adds it to the socket's backlog
97 * queue; in the latter case the queued message is processed once the process
98 * owning the socket lock releases it.
100 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
101 * the problem of a blocked socket operation preventing any other operations
102 * from occurring. However, applications must be careful if they have
103 * multiple threads trying to send (or receive) on the same socket, as these
104 * operations might interfere with each other. For example, doing a connect
105 * and a receive at the same time might allow the receive to consume the
106 * ACK message meant for the connect. While additional work could be done
107 * to try and overcome this, it doesn't seem to be worthwhile at the present.
109 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
110 * that another operation that must be performed in a non-blocking manner is
111 * not delayed for very long because the lock has already been taken.
113 * NOTE: This code assumes that certain fields of a port/socket pair are
114 * constant over its lifetime; such fields can be examined without taking
115 * the socket lock and/or port lock, and do not need to be re-read even
116 * after resuming processing after waiting. These fields include:
118 * - pointer to socket sk structure (aka tipc_sock structure)
119 * - pointer to port structure
124 * advance_rx_queue - discard first buffer in socket receive queue
126 * Caller must hold socket lock
128 static void advance_rx_queue(struct sock
*sk
)
130 kfree_skb(__skb_dequeue(&sk
->sk_receive_queue
));
134 * reject_rx_queue - reject all buffers in socket receive queue
136 * Caller must hold socket lock
138 static void reject_rx_queue(struct sock
*sk
)
142 while ((buf
= __skb_dequeue(&sk
->sk_receive_queue
)))
143 tipc_reject_msg(buf
, TIPC_ERR_NO_PORT
);
147 * tipc_sk_create - create a TIPC socket
148 * @net: network namespace (must be default network)
149 * @sock: pre-allocated socket structure
150 * @protocol: protocol indicator (must be 0)
151 * @kern: caused by kernel or by userspace?
153 * This routine creates additional data structures used by the TIPC socket,
154 * initializes them, and links them together.
156 * Returns 0 on success, errno otherwise
158 static int tipc_sk_create(struct net
*net
, struct socket
*sock
, int protocol
,
161 const struct proto_ops
*ops
;
164 struct tipc_port
*tp_ptr
;
166 /* Validate arguments */
167 if (unlikely(protocol
!= 0))
168 return -EPROTONOSUPPORT
;
170 switch (sock
->type
) {
173 state
= SS_UNCONNECTED
;
177 state
= SS_UNCONNECTED
;
188 /* Allocate socket's protocol area */
190 sk
= sk_alloc(net
, AF_TIPC
, GFP_KERNEL
, &tipc_proto
);
192 sk
= sk_alloc(net
, AF_TIPC
, GFP_KERNEL
, &tipc_proto_kern
);
197 /* Allocate TIPC port for socket to use */
198 tp_ptr
= tipc_createport_raw(sk
, &dispatch
, &wakeupdispatch
,
199 TIPC_LOW_IMPORTANCE
);
200 if (unlikely(!tp_ptr
)) {
205 /* Finish initializing socket data structures */
209 sock_init_data(sock
, sk
);
210 sk
->sk_backlog_rcv
= backlog_rcv
;
211 sk
->sk_rcvbuf
= sysctl_tipc_rmem
[1];
212 sk
->sk_data_ready
= tipc_data_ready
;
213 sk
->sk_write_space
= tipc_write_space
;
214 tipc_sk(sk
)->p
= tp_ptr
;
215 tipc_sk(sk
)->conn_timeout
= CONN_TIMEOUT_DEFAULT
;
217 spin_unlock_bh(tp_ptr
->lock
);
219 if (sock
->state
== SS_READY
) {
220 tipc_set_portunreturnable(tp_ptr
->ref
, 1);
221 if (sock
->type
== SOCK_DGRAM
)
222 tipc_set_portunreliable(tp_ptr
->ref
, 1);
229 * tipc_sock_create_local - create TIPC socket from inside TIPC module
230 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
232 * We cannot use sock_creat_kern here because it bumps module user count.
233 * Since socket owner and creator is the same module we must make sure
234 * that module count remains zero for module local sockets, otherwise
235 * we cannot do rmmod.
237 * Returns 0 on success, errno otherwise
239 int tipc_sock_create_local(int type
, struct socket
**res
)
244 rc
= sock_create_lite(AF_TIPC
, type
, 0, res
);
246 pr_err("Failed to create kernel socket\n");
249 tipc_sk_create(&init_net
, *res
, 0, 1);
257 * tipc_sock_release_local - release socket created by tipc_sock_create_local
258 * @sock: the socket to be released.
260 * Module reference count is not incremented when such sockets are created,
261 * so we must keep it from being decremented when they are released.
263 void tipc_sock_release_local(struct socket
*sock
)
271 * tipc_sock_accept_local - accept a connection on a socket created
272 * with tipc_sock_create_local. Use this function to avoid that
273 * module reference count is inadvertently incremented.
275 * @sock: the accepting socket
276 * @newsock: reference to the new socket to be created
277 * @flags: socket flags
280 int tipc_sock_accept_local(struct socket
*sock
, struct socket
**newsock
,
283 struct sock
*sk
= sock
->sk
;
286 ret
= sock_create_lite(sk
->sk_family
, sk
->sk_type
,
287 sk
->sk_protocol
, newsock
);
291 ret
= accept(sock
, *newsock
, flags
);
293 sock_release(*newsock
);
296 (*newsock
)->ops
= sock
->ops
;
301 * release - destroy a TIPC socket
302 * @sock: socket to destroy
304 * This routine cleans up any messages that are still queued on the socket.
305 * For DGRAM and RDM socket types, all queued messages are rejected.
306 * For SEQPACKET and STREAM socket types, the first message is rejected
307 * and any others are discarded. (If the first message on a STREAM socket
308 * is partially-read, it is discarded and the next one is rejected instead.)
310 * NOTE: Rejected messages are not necessarily returned to the sender! They
311 * are returned or discarded according to the "destination droppable" setting
312 * specified for the message by the sender.
314 * Returns 0 on success, errno otherwise
316 static int release(struct socket
*sock
)
318 struct sock
*sk
= sock
->sk
;
319 struct tipc_port
*tport
;
324 * Exit if socket isn't fully initialized (occurs when a failed accept()
325 * releases a pre-allocated child socket that was never used)
330 tport
= tipc_sk_port(sk
);
334 * Reject all unreceived messages, except on an active connection
335 * (which disconnects locally & sends a 'FIN+' to peer)
337 while (sock
->state
!= SS_DISCONNECTING
) {
338 buf
= __skb_dequeue(&sk
->sk_receive_queue
);
341 if (TIPC_SKB_CB(buf
)->handle
!= 0)
344 if ((sock
->state
== SS_CONNECTING
) ||
345 (sock
->state
== SS_CONNECTED
)) {
346 sock
->state
= SS_DISCONNECTING
;
347 tipc_disconnect(tport
->ref
);
349 tipc_reject_msg(buf
, TIPC_ERR_NO_PORT
);
354 * Delete TIPC port; this ensures no more messages are queued
355 * (also disconnects an active connection & sends a 'FIN-' to peer)
357 res
= tipc_deleteport(tport
->ref
);
359 /* Discard any remaining (connection-based) messages in receive queue */
360 __skb_queue_purge(&sk
->sk_receive_queue
);
362 /* Reject any messages that accumulated in backlog queue */
363 sock
->state
= SS_DISCONNECTING
;
373 * bind - associate or disassocate TIPC name(s) with a socket
374 * @sock: socket structure
375 * @uaddr: socket address describing name(s) and desired operation
376 * @uaddr_len: size of socket address data structure
378 * Name and name sequence binding is indicated using a positive scope value;
379 * a negative scope value unbinds the specified name. Specifying no name
380 * (i.e. a socket address length of 0) unbinds all names from the socket.
382 * Returns 0 on success, errno otherwise
384 * NOTE: This routine doesn't need to take the socket lock since it doesn't
385 * access any non-constant socket information.
387 static int bind(struct socket
*sock
, struct sockaddr
*uaddr
, int uaddr_len
)
389 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
390 u32 portref
= tipc_sk_port(sock
->sk
)->ref
;
392 if (unlikely(!uaddr_len
))
393 return tipc_withdraw(portref
, 0, NULL
);
395 if (uaddr_len
< sizeof(struct sockaddr_tipc
))
397 if (addr
->family
!= AF_TIPC
)
398 return -EAFNOSUPPORT
;
400 if (addr
->addrtype
== TIPC_ADDR_NAME
)
401 addr
->addr
.nameseq
.upper
= addr
->addr
.nameseq
.lower
;
402 else if (addr
->addrtype
!= TIPC_ADDR_NAMESEQ
)
403 return -EAFNOSUPPORT
;
405 if (addr
->addr
.nameseq
.type
< TIPC_RESERVED_TYPES
)
408 return (addr
->scope
> 0) ?
409 tipc_publish(portref
, addr
->scope
, &addr
->addr
.nameseq
) :
410 tipc_withdraw(portref
, -addr
->scope
, &addr
->addr
.nameseq
);
414 * get_name - get port ID of socket or peer socket
415 * @sock: socket structure
416 * @uaddr: area for returned socket address
417 * @uaddr_len: area for returned length of socket address
418 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
420 * Returns 0 on success, errno otherwise
422 * NOTE: This routine doesn't need to take the socket lock since it only
423 * accesses socket information that is unchanging (or which changes in
424 * a completely predictable manner).
426 static int get_name(struct socket
*sock
, struct sockaddr
*uaddr
,
427 int *uaddr_len
, int peer
)
429 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)uaddr
;
430 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
432 memset(addr
, 0, sizeof(*addr
));
434 if ((sock
->state
!= SS_CONNECTED
) &&
435 ((peer
!= 2) || (sock
->state
!= SS_DISCONNECTING
)))
437 addr
->addr
.id
.ref
= tsock
->peer_name
.ref
;
438 addr
->addr
.id
.node
= tsock
->peer_name
.node
;
440 addr
->addr
.id
.ref
= tsock
->p
->ref
;
441 addr
->addr
.id
.node
= tipc_own_addr
;
444 *uaddr_len
= sizeof(*addr
);
445 addr
->addrtype
= TIPC_ADDR_ID
;
446 addr
->family
= AF_TIPC
;
448 addr
->addr
.name
.domain
= 0;
454 * poll - read and possibly block on pollmask
455 * @file: file structure associated with the socket
456 * @sock: socket for which to calculate the poll bits
459 * Returns pollmask value
462 * It appears that the usual socket locking mechanisms are not useful here
463 * since the pollmask info is potentially out-of-date the moment this routine
464 * exits. TCP and other protocols seem to rely on higher level poll routines
465 * to handle any preventable race conditions, so TIPC will do the same ...
467 * TIPC sets the returned events as follows:
469 * socket state flags set
470 * ------------ ---------
471 * unconnected no read flags
472 * POLLOUT if port is not congested
474 * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue
477 * connected POLLIN/POLLRDNORM if data in rx queue
478 * POLLOUT if port is not congested
480 * disconnecting POLLIN/POLLRDNORM/POLLHUP
483 * listening POLLIN if SYN in rx queue
486 * ready POLLIN/POLLRDNORM if data in rx queue
487 * [connectionless] POLLOUT (since port cannot be congested)
489 * IMPORTANT: The fact that a read or write operation is indicated does NOT
490 * imply that the operation will succeed, merely that it should be performed
491 * and will not block.
493 static unsigned int poll(struct file
*file
, struct socket
*sock
,
496 struct sock
*sk
= sock
->sk
;
499 sock_poll_wait(file
, sk_sleep(sk
), wait
);
501 switch ((int)sock
->state
) {
503 if (!tipc_sk_port(sk
)->congested
)
508 if (!tipc_sk_port(sk
)->congested
)
513 if (!skb_queue_empty(&sk
->sk_receive_queue
))
514 mask
|= (POLLIN
| POLLRDNORM
);
516 case SS_DISCONNECTING
:
517 mask
= (POLLIN
| POLLRDNORM
| POLLHUP
);
525 * dest_name_check - verify user is permitted to send to specified port name
526 * @dest: destination address
527 * @m: descriptor for message to be sent
529 * Prevents restricted configuration commands from being issued by
530 * unauthorized users.
532 * Returns 0 if permission is granted, otherwise errno
534 static int dest_name_check(struct sockaddr_tipc
*dest
, struct msghdr
*m
)
536 struct tipc_cfg_msg_hdr hdr
;
538 if (likely(dest
->addr
.name
.name
.type
>= TIPC_RESERVED_TYPES
))
540 if (likely(dest
->addr
.name
.name
.type
== TIPC_TOP_SRV
))
542 if (likely(dest
->addr
.name
.name
.type
!= TIPC_CFG_SRV
))
545 if (!m
->msg_iovlen
|| (m
->msg_iov
[0].iov_len
< sizeof(hdr
)))
547 if (copy_from_user(&hdr
, m
->msg_iov
[0].iov_base
, sizeof(hdr
)))
549 if ((ntohs(hdr
.tcm_type
) & 0xC000) && (!capable(CAP_NET_ADMIN
)))
556 * send_msg - send message in connectionless manner
557 * @iocb: if NULL, indicates that socket lock is already held
558 * @sock: socket structure
559 * @m: message to send
560 * @total_len: length of message
562 * Message must have an destination specified explicitly.
563 * Used for SOCK_RDM and SOCK_DGRAM messages,
564 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
565 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
567 * Returns the number of bytes sent on success, or errno otherwise
569 static int send_msg(struct kiocb
*iocb
, struct socket
*sock
,
570 struct msghdr
*m
, size_t total_len
)
572 struct sock
*sk
= sock
->sk
;
573 struct tipc_port
*tport
= tipc_sk_port(sk
);
574 struct sockaddr_tipc
*dest
= (struct sockaddr_tipc
*)m
->msg_name
;
580 return -EDESTADDRREQ
;
581 if (unlikely((m
->msg_namelen
< sizeof(*dest
)) ||
582 (dest
->family
!= AF_TIPC
)))
584 if (total_len
> TIPC_MAX_USER_MSG_SIZE
)
590 needs_conn
= (sock
->state
!= SS_READY
);
591 if (unlikely(needs_conn
)) {
592 if (sock
->state
== SS_LISTENING
) {
596 if (sock
->state
!= SS_UNCONNECTED
) {
600 if (tport
->published
) {
604 if (dest
->addrtype
== TIPC_ADDR_NAME
) {
605 tport
->conn_type
= dest
->addr
.name
.name
.type
;
606 tport
->conn_instance
= dest
->addr
.name
.name
.instance
;
609 /* Abort any pending connection attempts (very unlikely) */
613 timeout_val
= sock_sndtimeo(sk
, m
->msg_flags
& MSG_DONTWAIT
);
616 if (dest
->addrtype
== TIPC_ADDR_NAME
) {
617 res
= dest_name_check(dest
, m
);
620 res
= tipc_send2name(tport
->ref
,
621 &dest
->addr
.name
.name
,
622 dest
->addr
.name
.domain
,
626 } else if (dest
->addrtype
== TIPC_ADDR_ID
) {
627 res
= tipc_send2port(tport
->ref
,
632 } else if (dest
->addrtype
== TIPC_ADDR_MCAST
) {
637 res
= dest_name_check(dest
, m
);
640 res
= tipc_multicast(tport
->ref
,
646 if (likely(res
!= -ELINKCONG
)) {
647 if (needs_conn
&& (res
>= 0))
648 sock
->state
= SS_CONNECTING
;
651 if (timeout_val
<= 0L) {
652 res
= timeout_val
? timeout_val
: -EWOULDBLOCK
;
656 timeout_val
= wait_event_interruptible_timeout(*sk_sleep(sk
),
657 !tport
->congested
, timeout_val
);
668 * send_packet - send a connection-oriented message
669 * @iocb: if NULL, indicates that socket lock is already held
670 * @sock: socket structure
671 * @m: message to send
672 * @total_len: length of message
674 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
676 * Returns the number of bytes sent on success, or errno otherwise
678 static int send_packet(struct kiocb
*iocb
, struct socket
*sock
,
679 struct msghdr
*m
, size_t total_len
)
681 struct sock
*sk
= sock
->sk
;
682 struct tipc_port
*tport
= tipc_sk_port(sk
);
683 struct sockaddr_tipc
*dest
= (struct sockaddr_tipc
*)m
->msg_name
;
687 /* Handle implied connection establishment */
689 return send_msg(iocb
, sock
, m
, total_len
);
691 if (total_len
> TIPC_MAX_USER_MSG_SIZE
)
697 timeout_val
= sock_sndtimeo(sk
, m
->msg_flags
& MSG_DONTWAIT
);
700 if (unlikely(sock
->state
!= SS_CONNECTED
)) {
701 if (sock
->state
== SS_DISCONNECTING
)
708 res
= tipc_send(tport
->ref
, m
->msg_iovlen
, m
->msg_iov
,
710 if (likely(res
!= -ELINKCONG
))
712 if (timeout_val
<= 0L) {
713 res
= timeout_val
? timeout_val
: -EWOULDBLOCK
;
717 timeout_val
= wait_event_interruptible_timeout(*sk_sleep(sk
),
718 (!tport
->congested
|| !tport
->connected
), timeout_val
);
728 * send_stream - send stream-oriented data
730 * @sock: socket structure
732 * @total_len: total length of data to be sent
734 * Used for SOCK_STREAM data.
736 * Returns the number of bytes sent on success (or partial success),
737 * or errno if no data sent
739 static int send_stream(struct kiocb
*iocb
, struct socket
*sock
,
740 struct msghdr
*m
, size_t total_len
)
742 struct sock
*sk
= sock
->sk
;
743 struct tipc_port
*tport
= tipc_sk_port(sk
);
744 struct msghdr my_msg
;
746 struct iovec
*curr_iov
;
748 char __user
*curr_start
;
757 /* Handle special cases where there is no connection */
758 if (unlikely(sock
->state
!= SS_CONNECTED
)) {
759 if (sock
->state
== SS_UNCONNECTED
) {
760 res
= send_packet(NULL
, sock
, m
, total_len
);
762 } else if (sock
->state
== SS_DISCONNECTING
) {
771 if (unlikely(m
->msg_name
)) {
776 if (total_len
> (unsigned int)INT_MAX
) {
782 * Send each iovec entry using one or more messages
784 * Note: This algorithm is good for the most likely case
785 * (i.e. one large iovec entry), but could be improved to pass sets
786 * of small iovec entries into send_packet().
788 curr_iov
= m
->msg_iov
;
789 curr_iovlen
= m
->msg_iovlen
;
790 my_msg
.msg_iov
= &my_iov
;
791 my_msg
.msg_iovlen
= 1;
792 my_msg
.msg_flags
= m
->msg_flags
;
793 my_msg
.msg_name
= NULL
;
796 hdr_size
= msg_hdr_sz(&tport
->phdr
);
798 while (curr_iovlen
--) {
799 curr_start
= curr_iov
->iov_base
;
800 curr_left
= curr_iov
->iov_len
;
803 bytes_to_send
= tport
->max_pkt
- hdr_size
;
804 if (bytes_to_send
> TIPC_MAX_USER_MSG_SIZE
)
805 bytes_to_send
= TIPC_MAX_USER_MSG_SIZE
;
806 if (curr_left
< bytes_to_send
)
807 bytes_to_send
= curr_left
;
808 my_iov
.iov_base
= curr_start
;
809 my_iov
.iov_len
= bytes_to_send
;
810 res
= send_packet(NULL
, sock
, &my_msg
, bytes_to_send
);
816 curr_left
-= bytes_to_send
;
817 curr_start
+= bytes_to_send
;
818 bytes_sent
+= bytes_to_send
;
830 * auto_connect - complete connection setup to a remote port
831 * @sock: socket structure
832 * @msg: peer's response message
834 * Returns 0 on success, errno otherwise
836 static int auto_connect(struct socket
*sock
, struct tipc_msg
*msg
)
838 struct tipc_sock
*tsock
= tipc_sk(sock
->sk
);
839 struct tipc_port
*p_ptr
;
841 tsock
->peer_name
.ref
= msg_origport(msg
);
842 tsock
->peer_name
.node
= msg_orignode(msg
);
843 p_ptr
= tipc_port_deref(tsock
->p
->ref
);
847 __tipc_connect(tsock
->p
->ref
, p_ptr
, &tsock
->peer_name
);
849 if (msg_importance(msg
) > TIPC_CRITICAL_IMPORTANCE
)
851 msg_set_importance(&p_ptr
->phdr
, (u32
)msg_importance(msg
));
852 sock
->state
= SS_CONNECTED
;
857 * set_orig_addr - capture sender's address for received message
858 * @m: descriptor for message info
859 * @msg: received message header
861 * Note: Address is not captured if not requested by receiver.
863 static void set_orig_addr(struct msghdr
*m
, struct tipc_msg
*msg
)
865 struct sockaddr_tipc
*addr
= (struct sockaddr_tipc
*)m
->msg_name
;
868 addr
->family
= AF_TIPC
;
869 addr
->addrtype
= TIPC_ADDR_ID
;
870 memset(&addr
->addr
, 0, sizeof(addr
->addr
));
871 addr
->addr
.id
.ref
= msg_origport(msg
);
872 addr
->addr
.id
.node
= msg_orignode(msg
);
873 addr
->addr
.name
.domain
= 0; /* could leave uninitialized */
874 addr
->scope
= 0; /* could leave uninitialized */
875 m
->msg_namelen
= sizeof(struct sockaddr_tipc
);
880 * anc_data_recv - optionally capture ancillary data for received message
881 * @m: descriptor for message info
882 * @msg: received message header
883 * @tport: TIPC port associated with message
885 * Note: Ancillary data is not captured if not requested by receiver.
887 * Returns 0 if successful, otherwise errno
889 static int anc_data_recv(struct msghdr
*m
, struct tipc_msg
*msg
,
890 struct tipc_port
*tport
)
898 if (likely(m
->msg_controllen
== 0))
901 /* Optionally capture errored message object(s) */
902 err
= msg
? msg_errcode(msg
) : 0;
905 anc_data
[1] = msg_data_sz(msg
);
906 res
= put_cmsg(m
, SOL_TIPC
, TIPC_ERRINFO
, 8, anc_data
);
910 res
= put_cmsg(m
, SOL_TIPC
, TIPC_RETDATA
, anc_data
[1],
917 /* Optionally capture message destination object */
918 dest_type
= msg
? msg_type(msg
) : TIPC_DIRECT_MSG
;
922 anc_data
[0] = msg_nametype(msg
);
923 anc_data
[1] = msg_namelower(msg
);
924 anc_data
[2] = msg_namelower(msg
);
928 anc_data
[0] = msg_nametype(msg
);
929 anc_data
[1] = msg_namelower(msg
);
930 anc_data
[2] = msg_nameupper(msg
);
933 has_name
= (tport
->conn_type
!= 0);
934 anc_data
[0] = tport
->conn_type
;
935 anc_data
[1] = tport
->conn_instance
;
936 anc_data
[2] = tport
->conn_instance
;
942 res
= put_cmsg(m
, SOL_TIPC
, TIPC_DESTNAME
, 12, anc_data
);
951 * recv_msg - receive packet-oriented message
953 * @m: descriptor for message info
954 * @buf_len: total size of user buffer area
955 * @flags: receive flags
957 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
958 * If the complete message doesn't fit in user area, truncate it.
960 * Returns size of returned message data, errno otherwise
962 static int recv_msg(struct kiocb
*iocb
, struct socket
*sock
,
963 struct msghdr
*m
, size_t buf_len
, int flags
)
965 struct sock
*sk
= sock
->sk
;
966 struct tipc_port
*tport
= tipc_sk_port(sk
);
968 struct tipc_msg
*msg
;
974 /* Catch invalid receive requests */
975 if (unlikely(!buf_len
))
980 if (unlikely(sock
->state
== SS_UNCONNECTED
)) {
985 /* will be updated in set_orig_addr() if needed */
988 timeout
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
991 /* Look for a message in receive queue; wait if necessary */
992 while (skb_queue_empty(&sk
->sk_receive_queue
)) {
993 if (sock
->state
== SS_DISCONNECTING
) {
998 res
= timeout
? timeout
: -EWOULDBLOCK
;
1002 timeout
= wait_event_interruptible_timeout(*sk_sleep(sk
),
1003 tipc_rx_ready(sock
),
1008 /* Look at first message in receive queue */
1009 buf
= skb_peek(&sk
->sk_receive_queue
);
1011 sz
= msg_data_sz(msg
);
1012 err
= msg_errcode(msg
);
1014 /* Discard an empty non-errored message & try again */
1015 if ((!sz
) && (!err
)) {
1016 advance_rx_queue(sk
);
1020 /* Capture sender's address (optional) */
1021 set_orig_addr(m
, msg
);
1023 /* Capture ancillary data (optional) */
1024 res
= anc_data_recv(m
, msg
, tport
);
1028 /* Capture message data (if valid) & compute return value (always) */
1030 if (unlikely(buf_len
< sz
)) {
1032 m
->msg_flags
|= MSG_TRUNC
;
1034 res
= skb_copy_datagram_iovec(buf
, msg_hdr_sz(msg
),
1040 if ((sock
->state
== SS_READY
) ||
1041 ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
))
1047 /* Consume received message (optional) */
1048 if (likely(!(flags
& MSG_PEEK
))) {
1049 if ((sock
->state
!= SS_READY
) &&
1050 (++tport
->conn_unacked
>= TIPC_FLOW_CONTROL_WIN
))
1051 tipc_acknowledge(tport
->ref
, tport
->conn_unacked
);
1052 advance_rx_queue(sk
);
1060 * recv_stream - receive stream-oriented data
1062 * @m: descriptor for message info
1063 * @buf_len: total size of user buffer area
1064 * @flags: receive flags
1066 * Used for SOCK_STREAM messages only. If not enough data is available
1067 * will optionally wait for more; never truncates data.
1069 * Returns size of returned message data, errno otherwise
1071 static int recv_stream(struct kiocb
*iocb
, struct socket
*sock
,
1072 struct msghdr
*m
, size_t buf_len
, int flags
)
1074 struct sock
*sk
= sock
->sk
;
1075 struct tipc_port
*tport
= tipc_sk_port(sk
);
1076 struct sk_buff
*buf
;
1077 struct tipc_msg
*msg
;
1080 int sz_to_copy
, target
, needed
;
1085 /* Catch invalid receive attempts */
1086 if (unlikely(!buf_len
))
1091 if (unlikely((sock
->state
== SS_UNCONNECTED
))) {
1096 /* will be updated in set_orig_addr() if needed */
1099 target
= sock_rcvlowat(sk
, flags
& MSG_WAITALL
, buf_len
);
1100 timeout
= sock_rcvtimeo(sk
, flags
& MSG_DONTWAIT
);
1103 /* Look for a message in receive queue; wait if necessary */
1104 while (skb_queue_empty(&sk
->sk_receive_queue
)) {
1105 if (sock
->state
== SS_DISCONNECTING
) {
1109 if (timeout
<= 0L) {
1110 res
= timeout
? timeout
: -EWOULDBLOCK
;
1114 timeout
= wait_event_interruptible_timeout(*sk_sleep(sk
),
1115 tipc_rx_ready(sock
),
1120 /* Look at first message in receive queue */
1121 buf
= skb_peek(&sk
->sk_receive_queue
);
1123 sz
= msg_data_sz(msg
);
1124 err
= msg_errcode(msg
);
1126 /* Discard an empty non-errored message & try again */
1127 if ((!sz
) && (!err
)) {
1128 advance_rx_queue(sk
);
1132 /* Optionally capture sender's address & ancillary data of first msg */
1133 if (sz_copied
== 0) {
1134 set_orig_addr(m
, msg
);
1135 res
= anc_data_recv(m
, msg
, tport
);
1140 /* Capture message data (if valid) & compute return value (always) */
1142 u32 offset
= (u32
)(unsigned long)(TIPC_SKB_CB(buf
)->handle
);
1145 needed
= (buf_len
- sz_copied
);
1146 sz_to_copy
= (sz
<= needed
) ? sz
: needed
;
1148 res
= skb_copy_datagram_iovec(buf
, msg_hdr_sz(msg
) + offset
,
1149 m
->msg_iov
, sz_to_copy
);
1153 sz_copied
+= sz_to_copy
;
1155 if (sz_to_copy
< sz
) {
1156 if (!(flags
& MSG_PEEK
))
1157 TIPC_SKB_CB(buf
)->handle
=
1158 (void *)(unsigned long)(offset
+ sz_to_copy
);
1163 goto exit
; /* can't add error msg to valid data */
1165 if ((err
== TIPC_CONN_SHUTDOWN
) || m
->msg_control
)
1171 /* Consume received message (optional) */
1172 if (likely(!(flags
& MSG_PEEK
))) {
1173 if (unlikely(++tport
->conn_unacked
>= TIPC_FLOW_CONTROL_WIN
))
1174 tipc_acknowledge(tport
->ref
, tport
->conn_unacked
);
1175 advance_rx_queue(sk
);
1178 /* Loop around if more data is required */
1179 if ((sz_copied
< buf_len
) && /* didn't get all requested data */
1180 (!skb_queue_empty(&sk
->sk_receive_queue
) ||
1181 (sz_copied
< target
)) && /* and more is ready or required */
1182 (!(flags
& MSG_PEEK
)) && /* and aren't just peeking at data */
1183 (!err
)) /* and haven't reached a FIN */
1188 return sz_copied
? sz_copied
: res
;
1192 * tipc_write_space - wake up thread if port congestion is released
1195 static void tipc_write_space(struct sock
*sk
)
1197 struct socket_wq
*wq
;
1200 wq
= rcu_dereference(sk
->sk_wq
);
1201 if (wq_has_sleeper(wq
))
1202 wake_up_interruptible_sync_poll(&wq
->wait
, POLLOUT
|
1203 POLLWRNORM
| POLLWRBAND
);
1208 * tipc_data_ready - wake up threads to indicate messages have been received
1210 * @len: the length of messages
1212 static void tipc_data_ready(struct sock
*sk
, int len
)
1214 struct socket_wq
*wq
;
1217 wq
= rcu_dereference(sk
->sk_wq
);
1218 if (wq_has_sleeper(wq
))
1219 wake_up_interruptible_sync_poll(&wq
->wait
, POLLIN
|
1220 POLLRDNORM
| POLLRDBAND
);
1225 * filter_connect - Handle all incoming messages for a connection-based socket
1226 * @tsock: TIPC socket
1229 * Returns TIPC error status code and socket error status code
1230 * once it encounters some errors
1232 static u32
filter_connect(struct tipc_sock
*tsock
, struct sk_buff
**buf
)
1234 struct socket
*sock
= tsock
->sk
.sk_socket
;
1235 struct tipc_msg
*msg
= buf_msg(*buf
);
1236 struct sock
*sk
= &tsock
->sk
;
1237 u32 retval
= TIPC_ERR_NO_PORT
;
1243 switch ((int)sock
->state
) {
1245 /* Accept only connection-based messages sent by peer */
1246 if (msg_connected(msg
) && tipc_port_peer_msg(tsock
->p
, msg
)) {
1247 if (unlikely(msg_errcode(msg
))) {
1248 sock
->state
= SS_DISCONNECTING
;
1249 __tipc_disconnect(tsock
->p
);
1255 /* Accept only ACK or NACK message */
1256 if (unlikely(msg_errcode(msg
))) {
1257 sock
->state
= SS_DISCONNECTING
;
1258 sk
->sk_err
= -ECONNREFUSED
;
1263 if (unlikely(!msg_connected(msg
)))
1266 res
= auto_connect(sock
, msg
);
1268 sock
->state
= SS_DISCONNECTING
;
1274 /* If an incoming message is an 'ACK-', it should be
1275 * discarded here because it doesn't contain useful
1276 * data. In addition, we should try to wake up
1277 * connect() routine if sleeping.
1279 if (msg_data_sz(msg
) == 0) {
1282 if (waitqueue_active(sk_sleep(sk
)))
1283 wake_up_interruptible(sk_sleep(sk
));
1288 case SS_UNCONNECTED
:
1289 /* Accept only SYN message */
1290 if (!msg_connected(msg
) && !(msg_errcode(msg
)))
1293 case SS_DISCONNECTING
:
1296 pr_err("Unknown socket state %u\n", sock
->state
);
1302 * rcvbuf_limit - get proper overload limit of socket receive queue
1306 * For all connection oriented messages, irrespective of importance,
1307 * the default overload value (i.e. 67MB) is set as limit.
1309 * For all connectionless messages, by default new queue limits are
1312 * TIPC_LOW_IMPORTANCE (4 MB)
1313 * TIPC_MEDIUM_IMPORTANCE (8 MB)
1314 * TIPC_HIGH_IMPORTANCE (16 MB)
1315 * TIPC_CRITICAL_IMPORTANCE (32 MB)
1317 * Returns overload limit according to corresponding message importance
1319 static unsigned int rcvbuf_limit(struct sock
*sk
, struct sk_buff
*buf
)
1321 struct tipc_msg
*msg
= buf_msg(buf
);
1324 if (msg_connected(msg
))
1325 limit
= sysctl_tipc_rmem
[2];
1327 limit
= sk
->sk_rcvbuf
>> TIPC_CRITICAL_IMPORTANCE
<<
1328 msg_importance(msg
);
1333 * filter_rcv - validate incoming message
1337 * Enqueues message on receive queue if acceptable; optionally handles
1338 * disconnect indication for a connected socket.
1340 * Called with socket lock already taken; port lock may also be taken.
1342 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1344 static u32
filter_rcv(struct sock
*sk
, struct sk_buff
*buf
)
1346 struct socket
*sock
= sk
->sk_socket
;
1347 struct tipc_msg
*msg
= buf_msg(buf
);
1348 unsigned int limit
= rcvbuf_limit(sk
, buf
);
1351 /* Reject message if it is wrong sort of message for socket */
1352 if (msg_type(msg
) > TIPC_DIRECT_MSG
)
1353 return TIPC_ERR_NO_PORT
;
1355 if (sock
->state
== SS_READY
) {
1356 if (msg_connected(msg
))
1357 return TIPC_ERR_NO_PORT
;
1359 res
= filter_connect(tipc_sk(sk
), &buf
);
1360 if (res
!= TIPC_OK
|| buf
== NULL
)
1364 /* Reject message if there isn't room to queue it */
1365 if (sk_rmem_alloc_get(sk
) + buf
->truesize
>= limit
)
1366 return TIPC_ERR_OVERLOAD
;
1368 /* Enqueue message */
1369 TIPC_SKB_CB(buf
)->handle
= 0;
1370 __skb_queue_tail(&sk
->sk_receive_queue
, buf
);
1371 skb_set_owner_r(buf
, sk
);
1373 sk
->sk_data_ready(sk
, 0);
1378 * backlog_rcv - handle incoming message from backlog queue
1382 * Caller must hold socket lock, but not port lock.
1386 static int backlog_rcv(struct sock
*sk
, struct sk_buff
*buf
)
1390 res
= filter_rcv(sk
, buf
);
1392 tipc_reject_msg(buf
, res
);
1397 * dispatch - handle incoming message
1398 * @tport: TIPC port that received message
1401 * Called with port lock already taken.
1403 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1405 static u32
dispatch(struct tipc_port
*tport
, struct sk_buff
*buf
)
1407 struct sock
*sk
= (struct sock
*)tport
->usr_handle
;
1411 * Process message if socket is unlocked; otherwise add to backlog queue
1413 * This code is based on sk_receive_skb(), but must be distinct from it
1414 * since a TIPC-specific filter/reject mechanism is utilized
1417 if (!sock_owned_by_user(sk
)) {
1418 res
= filter_rcv(sk
, buf
);
1420 if (sk_add_backlog(sk
, buf
, rcvbuf_limit(sk
, buf
)))
1421 res
= TIPC_ERR_OVERLOAD
;
1431 * wakeupdispatch - wake up port after congestion
1432 * @tport: port to wakeup
1434 * Called with port lock already taken.
1436 static void wakeupdispatch(struct tipc_port
*tport
)
1438 struct sock
*sk
= (struct sock
*)tport
->usr_handle
;
1440 sk
->sk_write_space(sk
);
1444 * connect - establish a connection to another TIPC port
1445 * @sock: socket structure
1446 * @dest: socket address for destination port
1447 * @destlen: size of socket address data structure
1448 * @flags: file-related flags associated with socket
1450 * Returns 0 on success, errno otherwise
1452 static int connect(struct socket
*sock
, struct sockaddr
*dest
, int destlen
,
1455 struct sock
*sk
= sock
->sk
;
1456 struct sockaddr_tipc
*dst
= (struct sockaddr_tipc
*)dest
;
1457 struct msghdr m
= {NULL
,};
1458 unsigned int timeout
;
1463 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
1464 if (sock
->state
== SS_READY
) {
1470 * Reject connection attempt using multicast address
1472 * Note: send_msg() validates the rest of the address fields,
1473 * so there's no need to do it here
1475 if (dst
->addrtype
== TIPC_ADDR_MCAST
) {
1480 timeout
= (flags
& O_NONBLOCK
) ? 0 : tipc_sk(sk
)->conn_timeout
;
1482 switch (sock
->state
) {
1483 case SS_UNCONNECTED
:
1484 /* Send a 'SYN-' to destination */
1486 m
.msg_namelen
= destlen
;
1488 /* If connect is in non-blocking case, set MSG_DONTWAIT to
1489 * indicate send_msg() is never blocked.
1492 m
.msg_flags
= MSG_DONTWAIT
;
1494 res
= send_msg(NULL
, sock
, &m
, 0);
1495 if ((res
< 0) && (res
!= -EWOULDBLOCK
))
1498 /* Just entered SS_CONNECTING state; the only
1499 * difference is that return value in non-blocking
1500 * case is EINPROGRESS, rather than EALREADY.
1515 if (sock
->state
== SS_CONNECTING
) {
1519 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1521 res
= wait_event_interruptible_timeout(*sk_sleep(sk
),
1522 sock
->state
!= SS_CONNECTING
,
1523 timeout
? (long)msecs_to_jiffies(timeout
)
1524 : MAX_SCHEDULE_TIMEOUT
);
1530 ; /* leave "res" unchanged */
1535 if (unlikely(sock
->state
== SS_DISCONNECTING
))
1536 res
= sock_error(sk
);
1546 * listen - allow socket to listen for incoming connections
1547 * @sock: socket structure
1550 * Returns 0 on success, errno otherwise
1552 static int listen(struct socket
*sock
, int len
)
1554 struct sock
*sk
= sock
->sk
;
1559 if (sock
->state
!= SS_UNCONNECTED
)
1562 sock
->state
= SS_LISTENING
;
1571 * accept - wait for connection request
1572 * @sock: listening socket
1573 * @newsock: new socket that is to be connected
1574 * @flags: file-related flags associated with socket
1576 * Returns 0 on success, errno otherwise
1578 static int accept(struct socket
*sock
, struct socket
*new_sock
, int flags
)
1580 struct sock
*new_sk
, *sk
= sock
->sk
;
1581 struct sk_buff
*buf
;
1582 struct tipc_sock
*new_tsock
;
1583 struct tipc_port
*new_tport
;
1584 struct tipc_msg
*msg
;
1591 if (sock
->state
!= SS_LISTENING
) {
1596 while (skb_queue_empty(&sk
->sk_receive_queue
)) {
1597 if (flags
& O_NONBLOCK
) {
1602 res
= wait_event_interruptible(*sk_sleep(sk
),
1603 (!skb_queue_empty(&sk
->sk_receive_queue
)));
1609 buf
= skb_peek(&sk
->sk_receive_queue
);
1611 res
= tipc_sk_create(sock_net(sock
->sk
), new_sock
, 0, 1);
1615 new_sk
= new_sock
->sk
;
1616 new_tsock
= tipc_sk(new_sk
);
1617 new_tport
= new_tsock
->p
;
1618 new_ref
= new_tport
->ref
;
1621 /* we lock on new_sk; but lockdep sees the lock on sk */
1622 lock_sock_nested(new_sk
, SINGLE_DEPTH_NESTING
);
1625 * Reject any stray messages received by new socket
1626 * before the socket lock was taken (very, very unlikely)
1628 reject_rx_queue(new_sk
);
1630 /* Connect new socket to it's peer */
1631 new_tsock
->peer_name
.ref
= msg_origport(msg
);
1632 new_tsock
->peer_name
.node
= msg_orignode(msg
);
1633 tipc_connect(new_ref
, &new_tsock
->peer_name
);
1634 new_sock
->state
= SS_CONNECTED
;
1636 tipc_set_portimportance(new_ref
, msg_importance(msg
));
1637 if (msg_named(msg
)) {
1638 new_tport
->conn_type
= msg_nametype(msg
);
1639 new_tport
->conn_instance
= msg_nameinst(msg
);
1643 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1644 * Respond to 'SYN+' by queuing it on new socket.
1646 if (!msg_data_sz(msg
)) {
1647 struct msghdr m
= {NULL
,};
1649 advance_rx_queue(sk
);
1650 send_packet(NULL
, new_sock
, &m
, 0);
1652 __skb_dequeue(&sk
->sk_receive_queue
);
1653 __skb_queue_head(&new_sk
->sk_receive_queue
, buf
);
1654 skb_set_owner_r(buf
, new_sk
);
1656 release_sock(new_sk
);
1664 * shutdown - shutdown socket connection
1665 * @sock: socket structure
1666 * @how: direction to close (must be SHUT_RDWR)
1668 * Terminates connection (if necessary), then purges socket's receive queue.
1670 * Returns 0 on success, errno otherwise
1672 static int shutdown(struct socket
*sock
, int how
)
1674 struct sock
*sk
= sock
->sk
;
1675 struct tipc_port
*tport
= tipc_sk_port(sk
);
1676 struct sk_buff
*buf
;
1679 if (how
!= SHUT_RDWR
)
1684 switch (sock
->state
) {
1689 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
1690 buf
= __skb_dequeue(&sk
->sk_receive_queue
);
1692 if (TIPC_SKB_CB(buf
)->handle
!= 0) {
1696 tipc_disconnect(tport
->ref
);
1697 tipc_reject_msg(buf
, TIPC_CONN_SHUTDOWN
);
1699 tipc_shutdown(tport
->ref
);
1702 sock
->state
= SS_DISCONNECTING
;
1706 case SS_DISCONNECTING
:
1708 /* Discard any unreceived messages */
1709 __skb_queue_purge(&sk
->sk_receive_queue
);
1711 /* Wake up anyone sleeping in poll */
1712 sk
->sk_state_change(sk
);
1725 * setsockopt - set socket option
1726 * @sock: socket structure
1727 * @lvl: option level
1728 * @opt: option identifier
1729 * @ov: pointer to new option value
1730 * @ol: length of option value
1732 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
1733 * (to ease compatibility).
1735 * Returns 0 on success, errno otherwise
1737 static int setsockopt(struct socket
*sock
,
1738 int lvl
, int opt
, char __user
*ov
, unsigned int ol
)
1740 struct sock
*sk
= sock
->sk
;
1741 struct tipc_port
*tport
= tipc_sk_port(sk
);
1745 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
1747 if (lvl
!= SOL_TIPC
)
1748 return -ENOPROTOOPT
;
1749 if (ol
< sizeof(value
))
1751 res
= get_user(value
, (u32 __user
*)ov
);
1758 case TIPC_IMPORTANCE
:
1759 res
= tipc_set_portimportance(tport
->ref
, value
);
1761 case TIPC_SRC_DROPPABLE
:
1762 if (sock
->type
!= SOCK_STREAM
)
1763 res
= tipc_set_portunreliable(tport
->ref
, value
);
1767 case TIPC_DEST_DROPPABLE
:
1768 res
= tipc_set_portunreturnable(tport
->ref
, value
);
1770 case TIPC_CONN_TIMEOUT
:
1771 tipc_sk(sk
)->conn_timeout
= value
;
1772 /* no need to set "res", since already 0 at this point */
1784 * getsockopt - get socket option
1785 * @sock: socket structure
1786 * @lvl: option level
1787 * @opt: option identifier
1788 * @ov: receptacle for option value
1789 * @ol: receptacle for length of option value
1791 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
1792 * (to ease compatibility).
1794 * Returns 0 on success, errno otherwise
1796 static int getsockopt(struct socket
*sock
,
1797 int lvl
, int opt
, char __user
*ov
, int __user
*ol
)
1799 struct sock
*sk
= sock
->sk
;
1800 struct tipc_port
*tport
= tipc_sk_port(sk
);
1805 if ((lvl
== IPPROTO_TCP
) && (sock
->type
== SOCK_STREAM
))
1806 return put_user(0, ol
);
1807 if (lvl
!= SOL_TIPC
)
1808 return -ENOPROTOOPT
;
1809 res
= get_user(len
, ol
);
1816 case TIPC_IMPORTANCE
:
1817 res
= tipc_portimportance(tport
->ref
, &value
);
1819 case TIPC_SRC_DROPPABLE
:
1820 res
= tipc_portunreliable(tport
->ref
, &value
);
1822 case TIPC_DEST_DROPPABLE
:
1823 res
= tipc_portunreturnable(tport
->ref
, &value
);
1825 case TIPC_CONN_TIMEOUT
:
1826 value
= tipc_sk(sk
)->conn_timeout
;
1827 /* no need to set "res", since already 0 at this point */
1829 case TIPC_NODE_RECVQ_DEPTH
:
1830 value
= 0; /* was tipc_queue_size, now obsolete */
1832 case TIPC_SOCK_RECVQ_DEPTH
:
1833 value
= skb_queue_len(&sk
->sk_receive_queue
);
1842 return res
; /* "get" failed */
1844 if (len
< sizeof(value
))
1847 if (copy_to_user(ov
, &value
, sizeof(value
)))
1850 return put_user(sizeof(value
), ol
);
1853 /* Protocol switches for the various types of TIPC sockets */
1855 static const struct proto_ops msg_ops
= {
1856 .owner
= THIS_MODULE
,
1861 .socketpair
= sock_no_socketpair
,
1862 .accept
= sock_no_accept
,
1863 .getname
= get_name
,
1865 .ioctl
= sock_no_ioctl
,
1866 .listen
= sock_no_listen
,
1867 .shutdown
= shutdown
,
1868 .setsockopt
= setsockopt
,
1869 .getsockopt
= getsockopt
,
1870 .sendmsg
= send_msg
,
1871 .recvmsg
= recv_msg
,
1872 .mmap
= sock_no_mmap
,
1873 .sendpage
= sock_no_sendpage
1876 static const struct proto_ops packet_ops
= {
1877 .owner
= THIS_MODULE
,
1882 .socketpair
= sock_no_socketpair
,
1884 .getname
= get_name
,
1886 .ioctl
= sock_no_ioctl
,
1888 .shutdown
= shutdown
,
1889 .setsockopt
= setsockopt
,
1890 .getsockopt
= getsockopt
,
1891 .sendmsg
= send_packet
,
1892 .recvmsg
= recv_msg
,
1893 .mmap
= sock_no_mmap
,
1894 .sendpage
= sock_no_sendpage
1897 static const struct proto_ops stream_ops
= {
1898 .owner
= THIS_MODULE
,
1903 .socketpair
= sock_no_socketpair
,
1905 .getname
= get_name
,
1907 .ioctl
= sock_no_ioctl
,
1909 .shutdown
= shutdown
,
1910 .setsockopt
= setsockopt
,
1911 .getsockopt
= getsockopt
,
1912 .sendmsg
= send_stream
,
1913 .recvmsg
= recv_stream
,
1914 .mmap
= sock_no_mmap
,
1915 .sendpage
= sock_no_sendpage
1918 static const struct net_proto_family tipc_family_ops
= {
1919 .owner
= THIS_MODULE
,
1921 .create
= tipc_sk_create
1924 static struct proto tipc_proto
= {
1926 .owner
= THIS_MODULE
,
1927 .obj_size
= sizeof(struct tipc_sock
),
1928 .sysctl_rmem
= sysctl_tipc_rmem
1931 static struct proto tipc_proto_kern
= {
1933 .obj_size
= sizeof(struct tipc_sock
),
1934 .sysctl_rmem
= sysctl_tipc_rmem
1938 * tipc_socket_init - initialize TIPC socket interface
1940 * Returns 0 on success, errno otherwise
1942 int tipc_socket_init(void)
1946 res
= proto_register(&tipc_proto
, 1);
1948 pr_err("Failed to register TIPC protocol type\n");
1952 res
= sock_register(&tipc_family_ops
);
1954 pr_err("Failed to register TIPC socket type\n");
1955 proto_unregister(&tipc_proto
);
1959 sockets_enabled
= 1;
1965 * tipc_socket_stop - stop TIPC socket interface
1967 void tipc_socket_stop(void)
1969 if (!sockets_enabled
)
1972 sockets_enabled
= 0;
1973 sock_unregister(tipc_family_ops
.family
);
1974 proto_unregister(&tipc_proto
);