2 * net/tipc/socket.c: TIPC socket API
4 * Copyright (c) 2001-2007, Ericsson AB
5 * Copyright (c) 2004-2008, 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.
37 #include <linux/module.h>
38 #include <linux/types.h>
39 #include <linux/net.h>
40 #include <linux/socket.h>
41 #include <linux/errno.h>
43 #include <linux/slab.h>
44 #include <linux/poll.h>
45 #include <linux/fcntl.h>
46 #include <asm/string.h>
47 #include <asm/atomic.h>
50 #include <linux/tipc.h>
51 #include <linux/tipc_config.h>
52 #include <net/tipc/tipc_msg.h>
53 #include <net/tipc/tipc_port.h>
57 #define SS_LISTENING -1 /* socket is listening */
58 #define SS_READY -2 /* socket is connectionless */
60 #define OVERLOAD_LIMIT_BASE 5000
61 #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
68 #define tipc_sk(sk) ((struct tipc_sock *)(sk))
69 #define tipc_sk_port(sk) ((struct tipc_port *)(tipc_sk(sk)->p))
71 static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
72 static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
73 static void wakeupdispatch(struct tipc_port *tport);
75 static const struct proto_ops packet_ops;
76 static const struct proto_ops stream_ops;
77 static const struct proto_ops msg_ops;
79 static struct proto tipc_proto;
81 static int sockets_enabled = 0;
83 static atomic_t tipc_queue_size = ATOMIC_INIT(0);
86 * Revised TIPC socket locking policy:
88 * Most socket operations take the standard socket lock when they start
89 * and hold it until they finish (or until they need to sleep). Acquiring
90 * this lock grants the owner exclusive access to the fields of the socket
91 * data structures, with the exception of the backlog queue. A few socket
92 * operations can be done without taking the socket lock because they only
93 * read socket information that never changes during the life of the socket.
95 * Socket operations may acquire the lock for the associated TIPC port if they
96 * need to perform an operation on the port. If any routine needs to acquire
97 * both the socket lock and the port lock it must take the socket lock first
98 * to avoid the risk of deadlock.
100 * The dispatcher handling incoming messages cannot grab the socket lock in
101 * the standard fashion, since invoked it runs at the BH level and cannot block.
102 * Instead, it checks to see if the socket lock is currently owned by someone,
103 * and either handles the message itself or adds it to the socket's backlog
104 * queue; in the latter case the queued message is processed once the process
105 * owning the socket lock releases it.
107 * NOTE: Releasing the socket lock while an operation is sleeping overcomes
108 * the problem of a blocked socket operation preventing any other operations
109 * from occurring. However, applications must be careful if they have
110 * multiple threads trying to send (or receive) on the same socket, as these
111 * operations might interfere with each other. For example, doing a connect
112 * and a receive at the same time might allow the receive to consume the
113 * ACK message meant for the connect. While additional work could be done
114 * to try and overcome this, it doesn't seem to be worthwhile at the present.
116 * NOTE: Releasing the socket lock while an operation is sleeping also ensures
117 * that another operation that must be performed in a non-blocking manner is
118 * not delayed for very long because the lock has already been taken.
120 * NOTE: This code assumes that certain fields of a port/socket pair are
121 * constant over its lifetime; such fields can be examined without taking
122 * the socket lock and/or port lock, and do not need to be re-read even
123 * after resuming processing after waiting. These fields include:
125 * - pointer to socket sk structure (aka tipc_sock structure)
126 * - pointer to port structure
131 * advance_rx_queue - discard first buffer in socket receive queue
133 * Caller must hold socket lock
136 static void advance_rx_queue(struct sock *sk)
138 buf_discard(__skb_dequeue(&sk->sk_receive_queue));
139 atomic_dec(&tipc_queue_size);
143 * discard_rx_queue - discard all buffers in socket receive queue
145 * Caller must hold socket lock
148 static void discard_rx_queue(struct sock *sk)
152 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
153 atomic_dec(&tipc_queue_size);
159 * reject_rx_queue - reject all buffers in socket receive queue
161 * Caller must hold socket lock
164 static void reject_rx_queue(struct sock *sk)
168 while ((buf = __skb_dequeue(&sk->sk_receive_queue))) {
169 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
170 atomic_dec(&tipc_queue_size);
175 * tipc_create - create a TIPC socket
176 * @net: network namespace (must be default network)
177 * @sock: pre-allocated socket structure
178 * @protocol: protocol indicator (must be 0)
180 * This routine creates additional data structures used by the TIPC socket,
181 * initializes them, and links them together.
183 * Returns 0 on success, errno otherwise
186 static int tipc_create(struct net *net, struct socket *sock, int protocol)
188 const struct proto_ops *ops;
191 struct tipc_port *tp_ptr;
193 /* Validate arguments */
195 if (net != &init_net)
196 return -EAFNOSUPPORT;
198 if (unlikely(protocol != 0))
199 return -EPROTONOSUPPORT;
201 switch (sock->type) {
204 state = SS_UNCONNECTED;
208 state = SS_UNCONNECTED;
219 /* Allocate socket's protocol area */
221 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
225 /* Allocate TIPC port for socket to use */
227 tp_ptr = tipc_createport_raw(sk, &dispatch, &wakeupdispatch,
228 TIPC_LOW_IMPORTANCE);
229 if (unlikely(!tp_ptr)) {
234 /* Finish initializing socket data structures */
239 sock_init_data(sock, sk);
240 sk->sk_rcvtimeo = msecs_to_jiffies(CONN_TIMEOUT_DEFAULT);
241 sk->sk_backlog_rcv = backlog_rcv;
242 tipc_sk(sk)->p = tp_ptr;
244 spin_unlock_bh(tp_ptr->lock);
246 if (sock->state == SS_READY) {
247 tipc_set_portunreturnable(tp_ptr->ref, 1);
248 if (sock->type == SOCK_DGRAM)
249 tipc_set_portunreliable(tp_ptr->ref, 1);
252 atomic_inc(&tipc_user_count);
257 * release - destroy a TIPC socket
258 * @sock: socket to destroy
260 * This routine cleans up any messages that are still queued on the socket.
261 * For DGRAM and RDM socket types, all queued messages are rejected.
262 * For SEQPACKET and STREAM socket types, the first message is rejected
263 * and any others are discarded. (If the first message on a STREAM socket
264 * is partially-read, it is discarded and the next one is rejected instead.)
266 * NOTE: Rejected messages are not necessarily returned to the sender! They
267 * are returned or discarded according to the "destination droppable" setting
268 * specified for the message by the sender.
270 * Returns 0 on success, errno otherwise
273 static int release(struct socket *sock)
275 struct sock *sk = sock->sk;
276 struct tipc_port *tport;
281 * Exit if socket isn't fully initialized (occurs when a failed accept()
282 * releases a pre-allocated child socket that was never used)
288 tport = tipc_sk_port(sk);
292 * Reject all unreceived messages, except on an active connection
293 * (which disconnects locally & sends a 'FIN+' to peer)
296 while (sock->state != SS_DISCONNECTING) {
297 buf = __skb_dequeue(&sk->sk_receive_queue);
300 atomic_dec(&tipc_queue_size);
301 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf)))
304 if ((sock->state == SS_CONNECTING) ||
305 (sock->state == SS_CONNECTED)) {
306 sock->state = SS_DISCONNECTING;
307 tipc_disconnect(tport->ref);
309 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
314 * Delete TIPC port; this ensures no more messages are queued
315 * (also disconnects an active connection & sends a 'FIN-' to peer)
318 res = tipc_deleteport(tport->ref);
320 /* Discard any remaining (connection-based) messages in receive queue */
322 discard_rx_queue(sk);
324 /* Reject any messages that accumulated in backlog queue */
326 sock->state = SS_DISCONNECTING;
332 atomic_dec(&tipc_user_count);
337 * bind - associate or disassocate TIPC name(s) with a socket
338 * @sock: socket structure
339 * @uaddr: socket address describing name(s) and desired operation
340 * @uaddr_len: size of socket address data structure
342 * Name and name sequence binding is indicated using a positive scope value;
343 * a negative scope value unbinds the specified name. Specifying no name
344 * (i.e. a socket address length of 0) unbinds all names from the socket.
346 * Returns 0 on success, errno otherwise
348 * NOTE: This routine doesn't need to take the socket lock since it doesn't
349 * access any non-constant socket information.
352 static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
354 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
355 u32 portref = tipc_sk_port(sock->sk)->ref;
357 if (unlikely(!uaddr_len))
358 return tipc_withdraw(portref, 0, NULL);
360 if (uaddr_len < sizeof(struct sockaddr_tipc))
362 if (addr->family != AF_TIPC)
363 return -EAFNOSUPPORT;
365 if (addr->addrtype == TIPC_ADDR_NAME)
366 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
367 else if (addr->addrtype != TIPC_ADDR_NAMESEQ)
368 return -EAFNOSUPPORT;
370 return (addr->scope > 0) ?
371 tipc_publish(portref, addr->scope, &addr->addr.nameseq) :
372 tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq);
376 * get_name - get port ID of socket or peer socket
377 * @sock: socket structure
378 * @uaddr: area for returned socket address
379 * @uaddr_len: area for returned length of socket address
380 * @peer: 0 to obtain socket name, 1 to obtain peer socket name
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.
388 static int get_name(struct socket *sock, struct sockaddr *uaddr,
389 int *uaddr_len, int peer)
391 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
392 u32 portref = tipc_sk_port(sock->sk)->ref;
396 res = tipc_peer(portref, &addr->addr.id);
400 tipc_ownidentity(portref, &addr->addr.id);
403 *uaddr_len = sizeof(*addr);
404 addr->addrtype = TIPC_ADDR_ID;
405 addr->family = AF_TIPC;
407 addr->addr.name.domain = 0;
413 * poll - read and possibly block on pollmask
414 * @file: file structure associated with the socket
415 * @sock: socket for which to calculate the poll bits
418 * Returns pollmask value
421 * It appears that the usual socket locking mechanisms are not useful here
422 * since the pollmask info is potentially out-of-date the moment this routine
423 * exits. TCP and other protocols seem to rely on higher level poll routines
424 * to handle any preventable race conditions, so TIPC will do the same ...
426 * TIPC sets the returned events as follows:
427 * a) POLLRDNORM and POLLIN are set if the socket's receive queue is non-empty
428 * or if a connection-oriented socket is does not have an active connection
429 * (i.e. a read operation will not block).
430 * b) POLLOUT is set except when a socket's connection has been terminated
431 * (i.e. a write operation will not block).
432 * c) POLLHUP is set when a socket's connection has been terminated.
434 * IMPORTANT: The fact that a read or write operation will not block does NOT
435 * imply that the operation will succeed!
438 static unsigned int poll(struct file *file, struct socket *sock,
441 struct sock *sk = sock->sk;
444 poll_wait(file, sk->sk_sleep, wait);
446 if (!skb_queue_empty(&sk->sk_receive_queue) ||
447 (sock->state == SS_UNCONNECTED) ||
448 (sock->state == SS_DISCONNECTING))
449 mask = (POLLRDNORM | POLLIN);
453 if (sock->state == SS_DISCONNECTING)
462 * dest_name_check - verify user is permitted to send to specified port name
463 * @dest: destination address
464 * @m: descriptor for message to be sent
466 * Prevents restricted configuration commands from being issued by
467 * unauthorized users.
469 * Returns 0 if permission is granted, otherwise errno
472 static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m)
474 struct tipc_cfg_msg_hdr hdr;
476 if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES))
478 if (likely(dest->addr.name.name.type == TIPC_TOP_SRV))
480 if (likely(dest->addr.name.name.type != TIPC_CFG_SRV))
483 if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr)))
485 if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN)))
492 * send_msg - send message in connectionless manner
493 * @iocb: if NULL, indicates that socket lock is already held
494 * @sock: socket structure
495 * @m: message to send
496 * @total_len: length of message
498 * Message must have an destination specified explicitly.
499 * Used for SOCK_RDM and SOCK_DGRAM messages,
500 * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections.
501 * (Note: 'SYN+' is prohibited on SOCK_STREAM.)
503 * Returns the number of bytes sent on success, or errno otherwise
506 static int send_msg(struct kiocb *iocb, struct socket *sock,
507 struct msghdr *m, size_t total_len)
509 struct sock *sk = sock->sk;
510 struct tipc_port *tport = tipc_sk_port(sk);
511 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
516 return -EDESTADDRREQ;
517 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
518 (dest->family != AF_TIPC)))
524 needs_conn = (sock->state != SS_READY);
525 if (unlikely(needs_conn)) {
526 if (sock->state == SS_LISTENING) {
530 if (sock->state != SS_UNCONNECTED) {
534 if ((tport->published) ||
535 ((sock->type == SOCK_STREAM) && (total_len != 0))) {
539 if (dest->addrtype == TIPC_ADDR_NAME) {
540 tport->conn_type = dest->addr.name.name.type;
541 tport->conn_instance = dest->addr.name.name.instance;
544 /* Abort any pending connection attempts (very unlikely) */
550 if (dest->addrtype == TIPC_ADDR_NAME) {
551 if ((res = dest_name_check(dest, m)))
553 res = tipc_send2name(tport->ref,
554 &dest->addr.name.name,
555 dest->addr.name.domain,
559 else if (dest->addrtype == TIPC_ADDR_ID) {
560 res = tipc_send2port(tport->ref,
565 else if (dest->addrtype == TIPC_ADDR_MCAST) {
570 if ((res = dest_name_check(dest, m)))
572 res = tipc_multicast(tport->ref,
578 if (likely(res != -ELINKCONG)) {
579 if (needs_conn && (res >= 0)) {
580 sock->state = SS_CONNECTING;
584 if (m->msg_flags & MSG_DONTWAIT) {
589 res = wait_event_interruptible(*sk->sk_sleep,
603 * send_packet - send a connection-oriented message
604 * @iocb: if NULL, indicates that socket lock is already held
605 * @sock: socket structure
606 * @m: message to send
607 * @total_len: length of message
609 * Used for SOCK_SEQPACKET messages and SOCK_STREAM data.
611 * Returns the number of bytes sent on success, or errno otherwise
614 static int send_packet(struct kiocb *iocb, struct socket *sock,
615 struct msghdr *m, size_t total_len)
617 struct sock *sk = sock->sk;
618 struct tipc_port *tport = tipc_sk_port(sk);
619 struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name;
622 /* Handle implied connection establishment */
625 return send_msg(iocb, sock, m, total_len);
631 if (unlikely(sock->state != SS_CONNECTED)) {
632 if (sock->state == SS_DISCONNECTING)
639 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov);
640 if (likely(res != -ELINKCONG)) {
643 if (m->msg_flags & MSG_DONTWAIT) {
648 res = wait_event_interruptible(*sk->sk_sleep,
649 (!tport->congested || !tport->connected));
661 * send_stream - send stream-oriented data
663 * @sock: socket structure
665 * @total_len: total length of data to be sent
667 * Used for SOCK_STREAM data.
669 * Returns the number of bytes sent on success (or partial success),
670 * or errno if no data sent
673 static int send_stream(struct kiocb *iocb, struct socket *sock,
674 struct msghdr *m, size_t total_len)
676 struct sock *sk = sock->sk;
677 struct tipc_port *tport = tipc_sk_port(sk);
678 struct msghdr my_msg;
680 struct iovec *curr_iov;
682 char __user *curr_start;
691 /* Handle special cases where there is no connection */
693 if (unlikely(sock->state != SS_CONNECTED)) {
694 if (sock->state == SS_UNCONNECTED) {
695 res = send_packet(NULL, sock, m, total_len);
697 } else if (sock->state == SS_DISCONNECTING) {
706 if (unlikely(m->msg_name)) {
712 * Send each iovec entry using one or more messages
714 * Note: This algorithm is good for the most likely case
715 * (i.e. one large iovec entry), but could be improved to pass sets
716 * of small iovec entries into send_packet().
719 curr_iov = m->msg_iov;
720 curr_iovlen = m->msg_iovlen;
721 my_msg.msg_iov = &my_iov;
722 my_msg.msg_iovlen = 1;
723 my_msg.msg_flags = m->msg_flags;
724 my_msg.msg_name = NULL;
727 hdr_size = msg_hdr_sz(&tport->phdr);
729 while (curr_iovlen--) {
730 curr_start = curr_iov->iov_base;
731 curr_left = curr_iov->iov_len;
734 bytes_to_send = tport->max_pkt - hdr_size;
735 if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE)
736 bytes_to_send = TIPC_MAX_USER_MSG_SIZE;
737 if (curr_left < bytes_to_send)
738 bytes_to_send = curr_left;
739 my_iov.iov_base = curr_start;
740 my_iov.iov_len = bytes_to_send;
741 if ((res = send_packet(NULL, sock, &my_msg, 0)) < 0) {
746 curr_left -= bytes_to_send;
747 curr_start += bytes_to_send;
748 bytes_sent += bytes_to_send;
760 * auto_connect - complete connection setup to a remote port
761 * @sock: socket structure
762 * @msg: peer's response message
764 * Returns 0 on success, errno otherwise
767 static int auto_connect(struct socket *sock, struct tipc_msg *msg)
769 struct tipc_port *tport = tipc_sk_port(sock->sk);
770 struct tipc_portid peer;
772 if (msg_errcode(msg)) {
773 sock->state = SS_DISCONNECTING;
774 return -ECONNREFUSED;
777 peer.ref = msg_origport(msg);
778 peer.node = msg_orignode(msg);
779 tipc_connect2port(tport->ref, &peer);
780 tipc_set_portimportance(tport->ref, msg_importance(msg));
781 sock->state = SS_CONNECTED;
786 * set_orig_addr - capture sender's address for received message
787 * @m: descriptor for message info
788 * @msg: received message header
790 * Note: Address is not captured if not requested by receiver.
793 static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg)
795 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name;
798 addr->family = AF_TIPC;
799 addr->addrtype = TIPC_ADDR_ID;
800 addr->addr.id.ref = msg_origport(msg);
801 addr->addr.id.node = msg_orignode(msg);
802 addr->addr.name.domain = 0; /* could leave uninitialized */
803 addr->scope = 0; /* could leave uninitialized */
804 m->msg_namelen = sizeof(struct sockaddr_tipc);
809 * anc_data_recv - optionally capture ancillary data for received message
810 * @m: descriptor for message info
811 * @msg: received message header
812 * @tport: TIPC port associated with message
814 * Note: Ancillary data is not captured if not requested by receiver.
816 * Returns 0 if successful, otherwise errno
819 static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg,
820 struct tipc_port *tport)
828 if (likely(m->msg_controllen == 0))
831 /* Optionally capture errored message object(s) */
833 err = msg ? msg_errcode(msg) : 0;
836 anc_data[1] = msg_data_sz(msg);
837 if ((res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data)))
840 (res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1],
845 /* Optionally capture message destination object */
847 dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG;
851 anc_data[0] = msg_nametype(msg);
852 anc_data[1] = msg_namelower(msg);
853 anc_data[2] = msg_namelower(msg);
857 anc_data[0] = msg_nametype(msg);
858 anc_data[1] = msg_namelower(msg);
859 anc_data[2] = msg_nameupper(msg);
862 has_name = (tport->conn_type != 0);
863 anc_data[0] = tport->conn_type;
864 anc_data[1] = tport->conn_instance;
865 anc_data[2] = tport->conn_instance;
871 (res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data)))
878 * recv_msg - receive packet-oriented message
880 * @m: descriptor for message info
881 * @buf_len: total size of user buffer area
882 * @flags: receive flags
884 * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages.
885 * If the complete message doesn't fit in user area, truncate it.
887 * Returns size of returned message data, errno otherwise
890 static int recv_msg(struct kiocb *iocb, struct socket *sock,
891 struct msghdr *m, size_t buf_len, int flags)
893 struct sock *sk = sock->sk;
894 struct tipc_port *tport = tipc_sk_port(sk);
896 struct tipc_msg *msg;
901 /* Catch invalid receive requests */
903 if (m->msg_iovlen != 1)
904 return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
906 if (unlikely(!buf_len))
911 if (unlikely(sock->state == SS_UNCONNECTED)) {
918 /* Look for a message in receive queue; wait if necessary */
920 while (skb_queue_empty(&sk->sk_receive_queue)) {
921 if (sock->state == SS_DISCONNECTING) {
925 if (flags & MSG_DONTWAIT) {
930 res = wait_event_interruptible(*sk->sk_sleep,
931 (!skb_queue_empty(&sk->sk_receive_queue) ||
932 (sock->state == SS_DISCONNECTING)));
938 /* Look at first message in receive queue */
940 buf = skb_peek(&sk->sk_receive_queue);
942 sz = msg_data_sz(msg);
943 err = msg_errcode(msg);
945 /* Complete connection setup for an implied connect */
947 if (unlikely(sock->state == SS_CONNECTING)) {
948 res = auto_connect(sock, msg);
953 /* Discard an empty non-errored message & try again */
955 if ((!sz) && (!err)) {
956 advance_rx_queue(sk);
960 /* Capture sender's address (optional) */
962 set_orig_addr(m, msg);
964 /* Capture ancillary data (optional) */
966 res = anc_data_recv(m, msg, tport);
970 /* Capture message data (if valid) & compute return value (always) */
973 if (unlikely(buf_len < sz)) {
975 m->msg_flags |= MSG_TRUNC;
977 if (unlikely(copy_to_user(m->msg_iov->iov_base, msg_data(msg),
984 if ((sock->state == SS_READY) ||
985 ((err == TIPC_CONN_SHUTDOWN) || m->msg_control))
991 /* Consume received message (optional) */
993 if (likely(!(flags & MSG_PEEK))) {
994 if ((sock->state != SS_READY) &&
995 (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
996 tipc_acknowledge(tport->ref, tport->conn_unacked);
997 advance_rx_queue(sk);
1005 * recv_stream - receive stream-oriented data
1007 * @m: descriptor for message info
1008 * @buf_len: total size of user buffer area
1009 * @flags: receive flags
1011 * Used for SOCK_STREAM messages only. If not enough data is available
1012 * will optionally wait for more; never truncates data.
1014 * Returns size of returned message data, errno otherwise
1017 static int recv_stream(struct kiocb *iocb, struct socket *sock,
1018 struct msghdr *m, size_t buf_len, int flags)
1020 struct sock *sk = sock->sk;
1021 struct tipc_port *tport = tipc_sk_port(sk);
1022 struct sk_buff *buf;
1023 struct tipc_msg *msg;
1028 char __user *crs = m->msg_iov->iov_base;
1029 unsigned char *buf_crs;
1033 /* Catch invalid receive attempts */
1035 if (m->msg_iovlen != 1)
1036 return -EOPNOTSUPP; /* Don't do multiple iovec entries yet */
1038 if (unlikely(!buf_len))
1043 if (unlikely((sock->state == SS_UNCONNECTED) ||
1044 (sock->state == SS_CONNECTING))) {
1051 /* Look for a message in receive queue; wait if necessary */
1053 while (skb_queue_empty(&sk->sk_receive_queue)) {
1054 if (sock->state == SS_DISCONNECTING) {
1058 if (flags & MSG_DONTWAIT) {
1063 res = wait_event_interruptible(*sk->sk_sleep,
1064 (!skb_queue_empty(&sk->sk_receive_queue) ||
1065 (sock->state == SS_DISCONNECTING)));
1071 /* Look at first message in receive queue */
1073 buf = skb_peek(&sk->sk_receive_queue);
1075 sz = msg_data_sz(msg);
1076 err = msg_errcode(msg);
1078 /* Discard an empty non-errored message & try again */
1080 if ((!sz) && (!err)) {
1081 advance_rx_queue(sk);
1085 /* Optionally capture sender's address & ancillary data of first msg */
1087 if (sz_copied == 0) {
1088 set_orig_addr(m, msg);
1089 res = anc_data_recv(m, msg, tport);
1094 /* Capture message data (if valid) & compute return value (always) */
1097 buf_crs = (unsigned char *)(TIPC_SKB_CB(buf)->handle);
1098 sz = (unsigned char *)msg + msg_size(msg) - buf_crs;
1100 needed = (buf_len - sz_copied);
1101 sz_to_copy = (sz <= needed) ? sz : needed;
1102 if (unlikely(copy_to_user(crs, buf_crs, sz_to_copy))) {
1106 sz_copied += sz_to_copy;
1108 if (sz_to_copy < sz) {
1109 if (!(flags & MSG_PEEK))
1110 TIPC_SKB_CB(buf)->handle = buf_crs + sz_to_copy;
1117 goto exit; /* can't add error msg to valid data */
1119 if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)
1125 /* Consume received message (optional) */
1127 if (likely(!(flags & MSG_PEEK))) {
1128 if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN))
1129 tipc_acknowledge(tport->ref, tport->conn_unacked);
1130 advance_rx_queue(sk);
1133 /* Loop around if more data is required */
1135 if ((sz_copied < buf_len) /* didn't get all requested data */
1136 && (!skb_queue_empty(&sock->sk->sk_receive_queue) ||
1137 (flags & MSG_WAITALL))
1138 /* ... and more is ready or required */
1139 && (!(flags & MSG_PEEK)) /* ... and aren't just peeking at data */
1140 && (!err) /* ... and haven't reached a FIN */
1146 return sz_copied ? sz_copied : res;
1150 * rx_queue_full - determine if receive queue can accept another message
1151 * @msg: message to be added to queue
1152 * @queue_size: current size of queue
1153 * @base: nominal maximum size of queue
1155 * Returns 1 if queue is unable to accept message, 0 otherwise
1158 static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base)
1161 u32 imp = msg_importance(msg);
1163 if (imp == TIPC_LOW_IMPORTANCE)
1165 else if (imp == TIPC_MEDIUM_IMPORTANCE)
1166 threshold = base * 2;
1167 else if (imp == TIPC_HIGH_IMPORTANCE)
1168 threshold = base * 100;
1172 if (msg_connected(msg))
1175 return (queue_size >= threshold);
1179 * filter_rcv - validate incoming message
1183 * Enqueues message on receive queue if acceptable; optionally handles
1184 * disconnect indication for a connected socket.
1186 * Called with socket lock already taken; port lock may also be taken.
1188 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1191 static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
1193 struct socket *sock = sk->sk_socket;
1194 struct tipc_msg *msg = buf_msg(buf);
1197 /* Reject message if it is wrong sort of message for socket */
1200 * WOULD IT BE BETTER TO JUST DISCARD THESE MESSAGES INSTEAD?
1201 * "NO PORT" ISN'T REALLY THE RIGHT ERROR CODE, AND THERE MAY
1202 * BE SECURITY IMPLICATIONS INHERENT IN REJECTING INVALID TRAFFIC
1205 if (sock->state == SS_READY) {
1206 if (msg_connected(msg)) {
1207 msg_dbg(msg, "dispatch filter 1\n");
1208 return TIPC_ERR_NO_PORT;
1211 if (msg_mcast(msg)) {
1212 msg_dbg(msg, "dispatch filter 2\n");
1213 return TIPC_ERR_NO_PORT;
1215 if (sock->state == SS_CONNECTED) {
1216 if (!msg_connected(msg)) {
1217 msg_dbg(msg, "dispatch filter 3\n");
1218 return TIPC_ERR_NO_PORT;
1221 else if (sock->state == SS_CONNECTING) {
1222 if (!msg_connected(msg) && (msg_errcode(msg) == 0)) {
1223 msg_dbg(msg, "dispatch filter 4\n");
1224 return TIPC_ERR_NO_PORT;
1227 else if (sock->state == SS_LISTENING) {
1228 if (msg_connected(msg) || msg_errcode(msg)) {
1229 msg_dbg(msg, "dispatch filter 5\n");
1230 return TIPC_ERR_NO_PORT;
1233 else if (sock->state == SS_DISCONNECTING) {
1234 msg_dbg(msg, "dispatch filter 6\n");
1235 return TIPC_ERR_NO_PORT;
1237 else /* (sock->state == SS_UNCONNECTED) */ {
1238 if (msg_connected(msg) || msg_errcode(msg)) {
1239 msg_dbg(msg, "dispatch filter 7\n");
1240 return TIPC_ERR_NO_PORT;
1245 /* Reject message if there isn't room to queue it */
1247 recv_q_len = (u32)atomic_read(&tipc_queue_size);
1248 if (unlikely(recv_q_len >= OVERLOAD_LIMIT_BASE)) {
1249 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE))
1250 return TIPC_ERR_OVERLOAD;
1252 recv_q_len = skb_queue_len(&sk->sk_receive_queue);
1253 if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) {
1254 if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2))
1255 return TIPC_ERR_OVERLOAD;
1258 /* Enqueue message (finally!) */
1260 msg_dbg(msg, "<DISP<: ");
1261 TIPC_SKB_CB(buf)->handle = msg_data(msg);
1262 atomic_inc(&tipc_queue_size);
1263 __skb_queue_tail(&sk->sk_receive_queue, buf);
1265 /* Initiate connection termination for an incoming 'FIN' */
1267 if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) {
1268 sock->state = SS_DISCONNECTING;
1269 tipc_disconnect_port(tipc_sk_port(sk));
1272 if (waitqueue_active(sk->sk_sleep))
1273 wake_up_interruptible(sk->sk_sleep);
1278 * backlog_rcv - handle incoming message from backlog queue
1282 * Caller must hold socket lock, but not port lock.
1287 static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
1291 res = filter_rcv(sk, buf);
1293 tipc_reject_msg(buf, res);
1298 * dispatch - handle incoming message
1299 * @tport: TIPC port that received message
1302 * Called with port lock already taken.
1304 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1307 static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1309 struct sock *sk = (struct sock *)tport->usr_handle;
1313 * Process message if socket is unlocked; otherwise add to backlog queue
1315 * This code is based on sk_receive_skb(), but must be distinct from it
1316 * since a TIPC-specific filter/reject mechanism is utilized
1320 if (!sock_owned_by_user(sk)) {
1321 res = filter_rcv(sk, buf);
1323 sk_add_backlog(sk, buf);
1332 * wakeupdispatch - wake up port after congestion
1333 * @tport: port to wakeup
1335 * Called with port lock already taken.
1338 static void wakeupdispatch(struct tipc_port *tport)
1340 struct sock *sk = (struct sock *)tport->usr_handle;
1342 if (waitqueue_active(sk->sk_sleep))
1343 wake_up_interruptible(sk->sk_sleep);
1347 * connect - establish a connection to another TIPC port
1348 * @sock: socket structure
1349 * @dest: socket address for destination port
1350 * @destlen: size of socket address data structure
1351 * @flags: file-related flags associated with socket
1353 * Returns 0 on success, errno otherwise
1356 static int connect(struct socket *sock, struct sockaddr *dest, int destlen,
1359 struct sock *sk = sock->sk;
1360 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1361 struct msghdr m = {NULL,};
1362 struct sk_buff *buf;
1363 struct tipc_msg *msg;
1368 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */
1370 if (sock->state == SS_READY) {
1375 /* For now, TIPC does not support the non-blocking form of connect() */
1377 if (flags & O_NONBLOCK) {
1382 /* Issue Posix-compliant error code if socket is in the wrong state */
1384 if (sock->state == SS_LISTENING) {
1388 if (sock->state == SS_CONNECTING) {
1392 if (sock->state != SS_UNCONNECTED) {
1398 * Reject connection attempt using multicast address
1400 * Note: send_msg() validates the rest of the address fields,
1401 * so there's no need to do it here
1404 if (dst->addrtype == TIPC_ADDR_MCAST) {
1409 /* Reject any messages already in receive queue (very unlikely) */
1411 reject_rx_queue(sk);
1413 /* Send a 'SYN-' to destination */
1416 m.msg_namelen = destlen;
1417 res = send_msg(NULL, sock, &m, 0);
1422 /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */
1425 res = wait_event_interruptible_timeout(*sk->sk_sleep,
1426 (!skb_queue_empty(&sk->sk_receive_queue) ||
1427 (sock->state != SS_CONNECTING)),
1432 buf = skb_peek(&sk->sk_receive_queue);
1435 res = auto_connect(sock, msg);
1437 if (!msg_data_sz(msg))
1438 advance_rx_queue(sk);
1441 if (sock->state == SS_CONNECTED) {
1444 res = -ECONNREFUSED;
1451 ; /* leave "res" unchanged */
1452 sock->state = SS_DISCONNECTING;
1461 * listen - allow socket to listen for incoming connections
1462 * @sock: socket structure
1465 * Returns 0 on success, errno otherwise
1468 static int listen(struct socket *sock, int len)
1470 struct sock *sk = sock->sk;
1475 if (sock->state == SS_READY)
1477 else if (sock->state != SS_UNCONNECTED)
1480 sock->state = SS_LISTENING;
1489 * accept - wait for connection request
1490 * @sock: listening socket
1491 * @newsock: new socket that is to be connected
1492 * @flags: file-related flags associated with socket
1494 * Returns 0 on success, errno otherwise
1497 static int accept(struct socket *sock, struct socket *new_sock, int flags)
1499 struct sock *sk = sock->sk;
1500 struct sk_buff *buf;
1505 if (sock->state == SS_READY) {
1509 if (sock->state != SS_LISTENING) {
1514 while (skb_queue_empty(&sk->sk_receive_queue)) {
1515 if (flags & O_NONBLOCK) {
1520 res = wait_event_interruptible(*sk->sk_sleep,
1521 (!skb_queue_empty(&sk->sk_receive_queue)));
1527 buf = skb_peek(&sk->sk_receive_queue);
1529 res = tipc_create(sock_net(sock->sk), new_sock, 0);
1531 struct sock *new_sk = new_sock->sk;
1532 struct tipc_port *new_tport = tipc_sk_port(new_sk);
1533 u32 new_ref = new_tport->ref;
1534 struct tipc_portid id;
1535 struct tipc_msg *msg = buf_msg(buf);
1540 * Reject any stray messages received by new socket
1541 * before the socket lock was taken (very, very unlikely)
1544 reject_rx_queue(new_sk);
1546 /* Connect new socket to it's peer */
1548 id.ref = msg_origport(msg);
1549 id.node = msg_orignode(msg);
1550 tipc_connect2port(new_ref, &id);
1551 new_sock->state = SS_CONNECTED;
1553 tipc_set_portimportance(new_ref, msg_importance(msg));
1554 if (msg_named(msg)) {
1555 new_tport->conn_type = msg_nametype(msg);
1556 new_tport->conn_instance = msg_nameinst(msg);
1560 * Respond to 'SYN-' by discarding it & returning 'ACK'-.
1561 * Respond to 'SYN+' by queuing it on new socket.
1564 msg_dbg(msg,"<ACC<: ");
1565 if (!msg_data_sz(msg)) {
1566 struct msghdr m = {NULL,};
1568 advance_rx_queue(sk);
1569 send_packet(NULL, new_sock, &m, 0);
1571 __skb_dequeue(&sk->sk_receive_queue);
1572 __skb_queue_head(&new_sk->sk_receive_queue, buf);
1574 release_sock(new_sk);
1582 * shutdown - shutdown socket connection
1583 * @sock: socket structure
1584 * @how: direction to close (must be SHUT_RDWR)
1586 * Terminates connection (if necessary), then purges socket's receive queue.
1588 * Returns 0 on success, errno otherwise
1591 static int shutdown(struct socket *sock, int how)
1593 struct sock *sk = sock->sk;
1594 struct tipc_port *tport = tipc_sk_port(sk);
1595 struct sk_buff *buf;
1598 if (how != SHUT_RDWR)
1603 switch (sock->state) {
1607 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
1609 buf = __skb_dequeue(&sk->sk_receive_queue);
1611 atomic_dec(&tipc_queue_size);
1612 if (TIPC_SKB_CB(buf)->handle != msg_data(buf_msg(buf))) {
1616 tipc_disconnect(tport->ref);
1617 tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN);
1619 tipc_shutdown(tport->ref);
1622 sock->state = SS_DISCONNECTING;
1626 case SS_DISCONNECTING:
1628 /* Discard any unreceived messages; wake up sleeping tasks */
1630 discard_rx_queue(sk);
1631 if (waitqueue_active(sk->sk_sleep))
1632 wake_up_interruptible(sk->sk_sleep);
1645 * setsockopt - set socket option
1646 * @sock: socket structure
1647 * @lvl: option level
1648 * @opt: option identifier
1649 * @ov: pointer to new option value
1650 * @ol: length of option value
1652 * For stream sockets only, accepts and ignores all IPPROTO_TCP options
1653 * (to ease compatibility).
1655 * Returns 0 on success, errno otherwise
1658 static int setsockopt(struct socket *sock,
1659 int lvl, int opt, char __user *ov, int ol)
1661 struct sock *sk = sock->sk;
1662 struct tipc_port *tport = tipc_sk_port(sk);
1666 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1668 if (lvl != SOL_TIPC)
1669 return -ENOPROTOOPT;
1670 if (ol < sizeof(value))
1672 if ((res = get_user(value, (u32 __user *)ov)))
1678 case TIPC_IMPORTANCE:
1679 res = tipc_set_portimportance(tport->ref, value);
1681 case TIPC_SRC_DROPPABLE:
1682 if (sock->type != SOCK_STREAM)
1683 res = tipc_set_portunreliable(tport->ref, value);
1687 case TIPC_DEST_DROPPABLE:
1688 res = tipc_set_portunreturnable(tport->ref, value);
1690 case TIPC_CONN_TIMEOUT:
1691 sk->sk_rcvtimeo = msecs_to_jiffies(value);
1692 /* no need to set "res", since already 0 at this point */
1704 * getsockopt - get socket option
1705 * @sock: socket structure
1706 * @lvl: option level
1707 * @opt: option identifier
1708 * @ov: receptacle for option value
1709 * @ol: receptacle for length of option value
1711 * For stream sockets only, returns 0 length result for all IPPROTO_TCP options
1712 * (to ease compatibility).
1714 * Returns 0 on success, errno otherwise
1717 static int getsockopt(struct socket *sock,
1718 int lvl, int opt, char __user *ov, int __user *ol)
1720 struct sock *sk = sock->sk;
1721 struct tipc_port *tport = tipc_sk_port(sk);
1726 if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM))
1727 return put_user(0, ol);
1728 if (lvl != SOL_TIPC)
1729 return -ENOPROTOOPT;
1730 if ((res = get_user(len, ol)))
1736 case TIPC_IMPORTANCE:
1737 res = tipc_portimportance(tport->ref, &value);
1739 case TIPC_SRC_DROPPABLE:
1740 res = tipc_portunreliable(tport->ref, &value);
1742 case TIPC_DEST_DROPPABLE:
1743 res = tipc_portunreturnable(tport->ref, &value);
1745 case TIPC_CONN_TIMEOUT:
1746 value = jiffies_to_msecs(sk->sk_rcvtimeo);
1747 /* no need to set "res", since already 0 at this point */
1758 else if (len < sizeof(value)) {
1761 else if (copy_to_user(ov, &value, sizeof(value))) {
1765 res = put_user(sizeof(value), ol);
1772 * Protocol switches for the various types of TIPC sockets
1775 static const struct proto_ops msg_ops = {
1776 .owner = THIS_MODULE,
1781 .socketpair = sock_no_socketpair,
1783 .getname = get_name,
1785 .ioctl = sock_no_ioctl,
1787 .shutdown = shutdown,
1788 .setsockopt = setsockopt,
1789 .getsockopt = getsockopt,
1790 .sendmsg = send_msg,
1791 .recvmsg = recv_msg,
1792 .mmap = sock_no_mmap,
1793 .sendpage = sock_no_sendpage
1796 static const struct proto_ops packet_ops = {
1797 .owner = THIS_MODULE,
1802 .socketpair = sock_no_socketpair,
1804 .getname = get_name,
1806 .ioctl = sock_no_ioctl,
1808 .shutdown = shutdown,
1809 .setsockopt = setsockopt,
1810 .getsockopt = getsockopt,
1811 .sendmsg = send_packet,
1812 .recvmsg = recv_msg,
1813 .mmap = sock_no_mmap,
1814 .sendpage = sock_no_sendpage
1817 static const struct proto_ops stream_ops = {
1818 .owner = THIS_MODULE,
1823 .socketpair = sock_no_socketpair,
1825 .getname = get_name,
1827 .ioctl = sock_no_ioctl,
1829 .shutdown = shutdown,
1830 .setsockopt = setsockopt,
1831 .getsockopt = getsockopt,
1832 .sendmsg = send_stream,
1833 .recvmsg = recv_stream,
1834 .mmap = sock_no_mmap,
1835 .sendpage = sock_no_sendpage
1838 static const struct net_proto_family tipc_family_ops = {
1839 .owner = THIS_MODULE,
1841 .create = tipc_create
1844 static struct proto tipc_proto = {
1846 .owner = THIS_MODULE,
1847 .obj_size = sizeof(struct tipc_sock)
1851 * tipc_socket_init - initialize TIPC socket interface
1853 * Returns 0 on success, errno otherwise
1855 int tipc_socket_init(void)
1859 res = proto_register(&tipc_proto, 1);
1861 err("Failed to register TIPC protocol type\n");
1865 res = sock_register(&tipc_family_ops);
1867 err("Failed to register TIPC socket type\n");
1868 proto_unregister(&tipc_proto);
1872 sockets_enabled = 1;
1878 * tipc_socket_stop - stop TIPC socket interface
1881 void tipc_socket_stop(void)
1883 if (!sockets_enabled)
1886 sockets_enabled = 0;
1887 sock_unregister(tipc_family_ops.family);
1888 proto_unregister(&tipc_proto);