Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * net/tipc/socket.c: TIPC socket API |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 3 | * |
Allan Stephens | 5eee6a6 | 2007-06-10 17:24:55 -0700 | [diff] [blame] | 4 | * Copyright (c) 2001-2007, Ericsson AB |
Allan Stephens | 4132fac | 2011-01-07 13:12:12 -0500 | [diff] [blame] | 5 | * Copyright (c) 2004-2008, 2010-2011, Wind River Systems |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 6 | * All rights reserved. |
| 7 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 8 | * Redistribution and use in source and binary forms, with or without |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 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. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 19 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 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. |
| 23 | * |
| 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 |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 34 | * POSSIBILITY OF SUCH DAMAGE. |
| 35 | */ |
| 36 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 37 | #include "core.h" |
Allan Stephens | d265fef | 2010-11-30 12:00:53 +0000 | [diff] [blame] | 38 | #include "port.h" |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 39 | |
Erik Hugne | 2cf8aa1 | 2012-06-29 00:16:37 -0400 | [diff] [blame] | 40 | #include <linux/export.h> |
| 41 | #include <net/sock.h> |
| 42 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 43 | #define SS_LISTENING -1 /* socket is listening */ |
| 44 | #define SS_READY -2 /* socket is connectionless */ |
| 45 | |
Allan Stephens | 3654ea0 | 2008-04-13 21:35:11 -0700 | [diff] [blame] | 46 | #define OVERLOAD_LIMIT_BASE 5000 |
| 47 | #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 48 | |
| 49 | struct tipc_sock { |
| 50 | struct sock sk; |
| 51 | struct tipc_port *p; |
Allan Stephens | 2da5991 | 2008-07-14 22:43:32 -0700 | [diff] [blame] | 52 | struct tipc_portid peer_name; |
Allan Stephens | a0f40f0 | 2011-05-26 13:44:34 -0400 | [diff] [blame] | 53 | unsigned int conn_timeout; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 54 | }; |
| 55 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 56 | #define tipc_sk(sk) ((struct tipc_sock *)(sk)) |
Joe Perches | e319269 | 2012-06-03 17:41:40 +0000 | [diff] [blame] | 57 | #define tipc_sk_port(sk) (tipc_sk(sk)->p) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 58 | |
Allan Stephens | 71092ea | 2011-02-23 14:52:14 -0500 | [diff] [blame] | 59 | #define tipc_rx_ready(sock) (!skb_queue_empty(&sock->sk->sk_receive_queue) || \ |
| 60 | (sock->state == SS_DISCONNECTING)) |
| 61 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 62 | static int backlog_rcv(struct sock *sk, struct sk_buff *skb); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 63 | static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf); |
| 64 | static void wakeupdispatch(struct tipc_port *tport); |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 65 | static void tipc_data_ready(struct sock *sk, int len); |
| 66 | static void tipc_write_space(struct sock *sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 67 | |
Florian Westphal | bca65ea | 2008-02-07 18:18:01 -0800 | [diff] [blame] | 68 | static const struct proto_ops packet_ops; |
| 69 | static const struct proto_ops stream_ops; |
| 70 | static const struct proto_ops msg_ops; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 71 | |
| 72 | static struct proto tipc_proto; |
| 73 | |
Allan Stephens | e3ec9c7 | 2010-12-31 18:59:34 +0000 | [diff] [blame] | 74 | static int sockets_enabled; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 75 | |
| 76 | static atomic_t tipc_queue_size = ATOMIC_INIT(0); |
| 77 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 78 | /* |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 79 | * Revised TIPC socket locking policy: |
| 80 | * |
| 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. |
| 87 | * |
| 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. |
| 92 | * |
| 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. |
| 99 | * |
| 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. |
| 108 | * |
| 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. |
| 112 | * |
| 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: |
| 117 | * - socket type |
| 118 | * - pointer to socket sk structure (aka tipc_sock structure) |
| 119 | * - pointer to port structure |
| 120 | * - port reference |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 121 | */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 122 | |
| 123 | /** |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 124 | * advance_rx_queue - discard first buffer in socket receive queue |
| 125 | * |
| 126 | * Caller must hold socket lock |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 127 | */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 128 | static void advance_rx_queue(struct sock *sk) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 129 | { |
Allan Stephens | 5f6d912 | 2011-11-04 13:24:29 -0400 | [diff] [blame] | 130 | kfree_skb(__skb_dequeue(&sk->sk_receive_queue)); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 131 | atomic_dec(&tipc_queue_size); |
| 132 | } |
| 133 | |
| 134 | /** |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 135 | * discard_rx_queue - discard all buffers in socket receive queue |
| 136 | * |
| 137 | * Caller must hold socket lock |
| 138 | */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 139 | static void discard_rx_queue(struct sock *sk) |
| 140 | { |
| 141 | struct sk_buff *buf; |
| 142 | |
| 143 | while ((buf = __skb_dequeue(&sk->sk_receive_queue))) { |
| 144 | atomic_dec(&tipc_queue_size); |
Allan Stephens | 5f6d912 | 2011-11-04 13:24:29 -0400 | [diff] [blame] | 145 | kfree_skb(buf); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * reject_rx_queue - reject all buffers in socket receive queue |
| 151 | * |
| 152 | * Caller must hold socket lock |
| 153 | */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 154 | static void reject_rx_queue(struct sock *sk) |
| 155 | { |
| 156 | struct sk_buff *buf; |
| 157 | |
| 158 | while ((buf = __skb_dequeue(&sk->sk_receive_queue))) { |
| 159 | tipc_reject_msg(buf, TIPC_ERR_NO_PORT); |
| 160 | atomic_dec(&tipc_queue_size); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 165 | * tipc_create - create a TIPC socket |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 166 | * @net: network namespace (must be default network) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 167 | * @sock: pre-allocated socket structure |
| 168 | * @protocol: protocol indicator (must be 0) |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 169 | * @kern: caused by kernel or by userspace? |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 170 | * |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 171 | * This routine creates additional data structures used by the TIPC socket, |
| 172 | * initializes them, and links them together. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 173 | * |
| 174 | * Returns 0 on success, errno otherwise |
| 175 | */ |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 176 | static int tipc_create(struct net *net, struct socket *sock, int protocol, |
| 177 | int kern) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 178 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 179 | const struct proto_ops *ops; |
| 180 | socket_state state; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 181 | struct sock *sk; |
Allan Stephens | 7ef43eb | 2008-05-12 15:42:28 -0700 | [diff] [blame] | 182 | struct tipc_port *tp_ptr; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 183 | |
| 184 | /* Validate arguments */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 185 | if (unlikely(protocol != 0)) |
| 186 | return -EPROTONOSUPPORT; |
| 187 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 188 | switch (sock->type) { |
| 189 | case SOCK_STREAM: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 190 | ops = &stream_ops; |
| 191 | state = SS_UNCONNECTED; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 192 | break; |
| 193 | case SOCK_SEQPACKET: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 194 | ops = &packet_ops; |
| 195 | state = SS_UNCONNECTED; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 196 | break; |
| 197 | case SOCK_DGRAM: |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 198 | case SOCK_RDM: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 199 | ops = &msg_ops; |
| 200 | state = SS_READY; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 201 | break; |
Allan Stephens | 4997865 | 2006-06-25 23:47:18 -0700 | [diff] [blame] | 202 | default: |
Allan Stephens | 4997865 | 2006-06-25 23:47:18 -0700 | [diff] [blame] | 203 | return -EPROTOTYPE; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 206 | /* Allocate socket's protocol area */ |
Pavel Emelyanov | 6257ff2 | 2007-11-01 00:39:31 -0700 | [diff] [blame] | 207 | sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 208 | if (sk == NULL) |
| 209 | return -ENOMEM; |
| 210 | |
| 211 | /* Allocate TIPC port for socket to use */ |
Allan Stephens | 0ea5224 | 2008-07-14 22:42:19 -0700 | [diff] [blame] | 212 | tp_ptr = tipc_createport_raw(sk, &dispatch, &wakeupdispatch, |
| 213 | TIPC_LOW_IMPORTANCE); |
| 214 | if (unlikely(!tp_ptr)) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 215 | sk_free(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 216 | return -ENOMEM; |
| 217 | } |
| 218 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 219 | /* Finish initializing socket data structures */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 220 | sock->ops = ops; |
| 221 | sock->state = state; |
| 222 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 223 | sock_init_data(sock, sk); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 224 | sk->sk_backlog_rcv = backlog_rcv; |
Erik Hugne | e57edf6 | 2012-10-04 05:00:43 +0000 | [diff] [blame] | 225 | sk->sk_rcvbuf = TIPC_FLOW_CONTROL_WIN * 2 * TIPC_MAX_USER_MSG_SIZE * 2; |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 226 | sk->sk_data_ready = tipc_data_ready; |
| 227 | sk->sk_write_space = tipc_write_space; |
Allan Stephens | 0ea5224 | 2008-07-14 22:42:19 -0700 | [diff] [blame] | 228 | tipc_sk(sk)->p = tp_ptr; |
Allan Stephens | a0f40f0 | 2011-05-26 13:44:34 -0400 | [diff] [blame] | 229 | tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 230 | |
Allan Stephens | 7ef43eb | 2008-05-12 15:42:28 -0700 | [diff] [blame] | 231 | spin_unlock_bh(tp_ptr->lock); |
| 232 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 233 | if (sock->state == SS_READY) { |
Allan Stephens | 0ea5224 | 2008-07-14 22:42:19 -0700 | [diff] [blame] | 234 | tipc_set_portunreturnable(tp_ptr->ref, 1); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 235 | if (sock->type == SOCK_DGRAM) |
Allan Stephens | 0ea5224 | 2008-07-14 22:42:19 -0700 | [diff] [blame] | 236 | tipc_set_portunreliable(tp_ptr->ref, 1); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 237 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 238 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * release - destroy a TIPC socket |
| 244 | * @sock: socket to destroy |
| 245 | * |
| 246 | * This routine cleans up any messages that are still queued on the socket. |
| 247 | * For DGRAM and RDM socket types, all queued messages are rejected. |
| 248 | * For SEQPACKET and STREAM socket types, the first message is rejected |
| 249 | * and any others are discarded. (If the first message on a STREAM socket |
| 250 | * is partially-read, it is discarded and the next one is rejected instead.) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 251 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 252 | * NOTE: Rejected messages are not necessarily returned to the sender! They |
| 253 | * are returned or discarded according to the "destination droppable" setting |
| 254 | * specified for the message by the sender. |
| 255 | * |
| 256 | * Returns 0 on success, errno otherwise |
| 257 | */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 258 | static int release(struct socket *sock) |
| 259 | { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 260 | struct sock *sk = sock->sk; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 261 | struct tipc_port *tport; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 262 | struct sk_buff *buf; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 263 | int res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 264 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 265 | /* |
| 266 | * Exit if socket isn't fully initialized (occurs when a failed accept() |
| 267 | * releases a pre-allocated child socket that was never used) |
| 268 | */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 269 | if (sk == NULL) |
| 270 | return 0; |
| 271 | |
| 272 | tport = tipc_sk_port(sk); |
| 273 | lock_sock(sk); |
| 274 | |
| 275 | /* |
| 276 | * Reject all unreceived messages, except on an active connection |
| 277 | * (which disconnects locally & sends a 'FIN+' to peer) |
| 278 | */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 279 | while (sock->state != SS_DISCONNECTING) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 280 | buf = __skb_dequeue(&sk->sk_receive_queue); |
| 281 | if (buf == NULL) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 282 | break; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 283 | atomic_dec(&tipc_queue_size); |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 284 | if (TIPC_SKB_CB(buf)->handle != 0) |
Allan Stephens | 5f6d912 | 2011-11-04 13:24:29 -0400 | [diff] [blame] | 285 | kfree_skb(buf); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 286 | else { |
| 287 | if ((sock->state == SS_CONNECTING) || |
| 288 | (sock->state == SS_CONNECTED)) { |
| 289 | sock->state = SS_DISCONNECTING; |
| 290 | tipc_disconnect(tport->ref); |
| 291 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 292 | tipc_reject_msg(buf, TIPC_ERR_NO_PORT); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 293 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 294 | } |
| 295 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 296 | /* |
| 297 | * Delete TIPC port; this ensures no more messages are queued |
| 298 | * (also disconnects an active connection & sends a 'FIN-' to peer) |
| 299 | */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 300 | res = tipc_deleteport(tport->ref); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 301 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 302 | /* Discard any remaining (connection-based) messages in receive queue */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 303 | discard_rx_queue(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 304 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 305 | /* Reject any messages that accumulated in backlog queue */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 306 | sock->state = SS_DISCONNECTING; |
| 307 | release_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 308 | |
| 309 | sock_put(sk); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 310 | sock->sk = NULL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 311 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 312 | return res; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * bind - associate or disassocate TIPC name(s) with a socket |
| 317 | * @sock: socket structure |
| 318 | * @uaddr: socket address describing name(s) and desired operation |
| 319 | * @uaddr_len: size of socket address data structure |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 320 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 321 | * Name and name sequence binding is indicated using a positive scope value; |
| 322 | * a negative scope value unbinds the specified name. Specifying no name |
| 323 | * (i.e. a socket address length of 0) unbinds all names from the socket. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 324 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 325 | * Returns 0 on success, errno otherwise |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 326 | * |
| 327 | * NOTE: This routine doesn't need to take the socket lock since it doesn't |
| 328 | * access any non-constant socket information. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 329 | */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 330 | static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len) |
| 331 | { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 332 | struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 333 | u32 portref = tipc_sk_port(sock->sk)->ref; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 334 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 335 | if (unlikely(!uaddr_len)) |
| 336 | return tipc_withdraw(portref, 0, NULL); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 337 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 338 | if (uaddr_len < sizeof(struct sockaddr_tipc)) |
| 339 | return -EINVAL; |
| 340 | if (addr->family != AF_TIPC) |
| 341 | return -EAFNOSUPPORT; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 342 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 343 | if (addr->addrtype == TIPC_ADDR_NAME) |
| 344 | addr->addr.nameseq.upper = addr->addr.nameseq.lower; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 345 | else if (addr->addrtype != TIPC_ADDR_NAMESEQ) |
| 346 | return -EAFNOSUPPORT; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 347 | |
Allan Stephens | c422f1b | 2011-11-02 15:49:40 -0400 | [diff] [blame] | 348 | if (addr->addr.nameseq.type < TIPC_RESERVED_TYPES) |
| 349 | return -EACCES; |
| 350 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 351 | return (addr->scope > 0) ? |
| 352 | tipc_publish(portref, addr->scope, &addr->addr.nameseq) : |
| 353 | tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 354 | } |
| 355 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 356 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 357 | * get_name - get port ID of socket or peer socket |
| 358 | * @sock: socket structure |
| 359 | * @uaddr: area for returned socket address |
| 360 | * @uaddr_len: area for returned length of socket address |
Allan Stephens | 2da5991 | 2008-07-14 22:43:32 -0700 | [diff] [blame] | 361 | * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 362 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 363 | * Returns 0 on success, errno otherwise |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 364 | * |
Allan Stephens | 2da5991 | 2008-07-14 22:43:32 -0700 | [diff] [blame] | 365 | * NOTE: This routine doesn't need to take the socket lock since it only |
| 366 | * accesses socket information that is unchanging (or which changes in |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 367 | * a completely predictable manner). |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 368 | */ |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 369 | static int get_name(struct socket *sock, struct sockaddr *uaddr, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 370 | int *uaddr_len, int peer) |
| 371 | { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 372 | struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; |
Allan Stephens | 2da5991 | 2008-07-14 22:43:32 -0700 | [diff] [blame] | 373 | struct tipc_sock *tsock = tipc_sk(sock->sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 374 | |
Kulikov Vasiliy | 88f8a5e | 2010-10-31 07:10:32 +0000 | [diff] [blame] | 375 | memset(addr, 0, sizeof(*addr)); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 376 | if (peer) { |
Allan Stephens | 2da5991 | 2008-07-14 22:43:32 -0700 | [diff] [blame] | 377 | if ((sock->state != SS_CONNECTED) && |
| 378 | ((peer != 2) || (sock->state != SS_DISCONNECTING))) |
| 379 | return -ENOTCONN; |
| 380 | addr->addr.id.ref = tsock->peer_name.ref; |
| 381 | addr->addr.id.node = tsock->peer_name.node; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 382 | } else { |
Allan Stephens | b924dcf | 2010-11-30 12:01:03 +0000 | [diff] [blame] | 383 | addr->addr.id.ref = tsock->p->ref; |
| 384 | addr->addr.id.node = tipc_own_addr; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 385 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 386 | |
| 387 | *uaddr_len = sizeof(*addr); |
| 388 | addr->addrtype = TIPC_ADDR_ID; |
| 389 | addr->family = AF_TIPC; |
| 390 | addr->scope = 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 391 | addr->addr.name.domain = 0; |
| 392 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 393 | return 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /** |
| 397 | * poll - read and possibly block on pollmask |
| 398 | * @file: file structure associated with the socket |
| 399 | * @sock: socket for which to calculate the poll bits |
| 400 | * @wait: ??? |
| 401 | * |
Allan Stephens | 9b674e8 | 2008-03-26 16:48:21 -0700 | [diff] [blame] | 402 | * Returns pollmask value |
| 403 | * |
| 404 | * COMMENTARY: |
| 405 | * It appears that the usual socket locking mechanisms are not useful here |
| 406 | * since the pollmask info is potentially out-of-date the moment this routine |
| 407 | * exits. TCP and other protocols seem to rely on higher level poll routines |
| 408 | * to handle any preventable race conditions, so TIPC will do the same ... |
| 409 | * |
| 410 | * TIPC sets the returned events as follows: |
Allan Stephens | 9b674e8 | 2008-03-26 16:48:21 -0700 | [diff] [blame] | 411 | * |
Allan Stephens | f662c07 | 2010-08-17 11:00:06 +0000 | [diff] [blame] | 412 | * socket state flags set |
| 413 | * ------------ --------- |
| 414 | * unconnected no read flags |
Erik Hugne | c4fc298 | 2012-10-16 16:47:06 +0200 | [diff] [blame] | 415 | * POLLOUT if port is not congested |
Allan Stephens | f662c07 | 2010-08-17 11:00:06 +0000 | [diff] [blame] | 416 | * |
| 417 | * connecting POLLIN/POLLRDNORM if ACK/NACK in rx queue |
| 418 | * no write flags |
| 419 | * |
| 420 | * connected POLLIN/POLLRDNORM if data in rx queue |
| 421 | * POLLOUT if port is not congested |
| 422 | * |
| 423 | * disconnecting POLLIN/POLLRDNORM/POLLHUP |
| 424 | * no write flags |
| 425 | * |
| 426 | * listening POLLIN if SYN in rx queue |
| 427 | * no write flags |
| 428 | * |
| 429 | * ready POLLIN/POLLRDNORM if data in rx queue |
| 430 | * [connectionless] POLLOUT (since port cannot be congested) |
| 431 | * |
| 432 | * IMPORTANT: The fact that a read or write operation is indicated does NOT |
| 433 | * imply that the operation will succeed, merely that it should be performed |
| 434 | * and will not block. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 435 | */ |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 436 | static unsigned int poll(struct file *file, struct socket *sock, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 437 | poll_table *wait) |
| 438 | { |
Allan Stephens | 9b674e8 | 2008-03-26 16:48:21 -0700 | [diff] [blame] | 439 | struct sock *sk = sock->sk; |
Allan Stephens | f662c07 | 2010-08-17 11:00:06 +0000 | [diff] [blame] | 440 | u32 mask = 0; |
Allan Stephens | 9b674e8 | 2008-03-26 16:48:21 -0700 | [diff] [blame] | 441 | |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 442 | sock_poll_wait(file, sk_sleep(sk), wait); |
Allan Stephens | 9b674e8 | 2008-03-26 16:48:21 -0700 | [diff] [blame] | 443 | |
Allan Stephens | f662c07 | 2010-08-17 11:00:06 +0000 | [diff] [blame] | 444 | switch ((int)sock->state) { |
Erik Hugne | c4fc298 | 2012-10-16 16:47:06 +0200 | [diff] [blame] | 445 | case SS_UNCONNECTED: |
| 446 | if (!tipc_sk_port(sk)->congested) |
| 447 | mask |= POLLOUT; |
| 448 | break; |
Allan Stephens | f662c07 | 2010-08-17 11:00:06 +0000 | [diff] [blame] | 449 | case SS_READY: |
| 450 | case SS_CONNECTED: |
| 451 | if (!tipc_sk_port(sk)->congested) |
| 452 | mask |= POLLOUT; |
| 453 | /* fall thru' */ |
| 454 | case SS_CONNECTING: |
| 455 | case SS_LISTENING: |
| 456 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
| 457 | mask |= (POLLIN | POLLRDNORM); |
| 458 | break; |
| 459 | case SS_DISCONNECTING: |
| 460 | mask = (POLLIN | POLLRDNORM | POLLHUP); |
| 461 | break; |
| 462 | } |
Allan Stephens | 9b674e8 | 2008-03-26 16:48:21 -0700 | [diff] [blame] | 463 | |
| 464 | return mask; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 465 | } |
| 466 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 467 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 468 | * dest_name_check - verify user is permitted to send to specified port name |
| 469 | * @dest: destination address |
| 470 | * @m: descriptor for message to be sent |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 471 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 472 | * Prevents restricted configuration commands from being issued by |
| 473 | * unauthorized users. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 474 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 475 | * Returns 0 if permission is granted, otherwise errno |
| 476 | */ |
Sam Ravnborg | 05790c6 | 2006-03-20 22:37:04 -0800 | [diff] [blame] | 477 | static int dest_name_check(struct sockaddr_tipc *dest, struct msghdr *m) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 478 | { |
| 479 | struct tipc_cfg_msg_hdr hdr; |
| 480 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 481 | if (likely(dest->addr.name.name.type >= TIPC_RESERVED_TYPES)) |
| 482 | return 0; |
| 483 | if (likely(dest->addr.name.name.type == TIPC_TOP_SRV)) |
| 484 | return 0; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 485 | if (likely(dest->addr.name.name.type != TIPC_CFG_SRV)) |
| 486 | return -EACCES; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 487 | |
Allan Stephens | 3f8dd94 | 2011-01-18 13:09:29 -0500 | [diff] [blame] | 488 | if (!m->msg_iovlen || (m->msg_iov[0].iov_len < sizeof(hdr))) |
| 489 | return -EMSGSIZE; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 490 | if (copy_from_user(&hdr, m->msg_iov[0].iov_base, sizeof(hdr))) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 491 | return -EFAULT; |
Allan Stephens | 70cb234 | 2006-06-25 23:41:47 -0700 | [diff] [blame] | 492 | if ((ntohs(hdr.tcm_type) & 0xC000) && (!capable(CAP_NET_ADMIN))) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 493 | return -EACCES; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 494 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * send_msg - send message in connectionless manner |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 500 | * @iocb: if NULL, indicates that socket lock is already held |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 501 | * @sock: socket structure |
| 502 | * @m: message to send |
Allan Stephens | e9024f0f | 2006-06-25 23:43:57 -0700 | [diff] [blame] | 503 | * @total_len: length of message |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 504 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 505 | * Message must have an destination specified explicitly. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 506 | * Used for SOCK_RDM and SOCK_DGRAM messages, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 507 | * and for 'SYN' messages on SOCK_SEQPACKET and SOCK_STREAM connections. |
| 508 | * (Note: 'SYN+' is prohibited on SOCK_STREAM.) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 509 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 510 | * Returns the number of bytes sent on success, or errno otherwise |
| 511 | */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 512 | static int send_msg(struct kiocb *iocb, struct socket *sock, |
| 513 | struct msghdr *m, size_t total_len) |
| 514 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 515 | struct sock *sk = sock->sk; |
| 516 | struct tipc_port *tport = tipc_sk_port(sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 517 | struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 518 | int needs_conn; |
Ying Xue | 1d83587 | 2011-07-06 05:53:15 -0400 | [diff] [blame] | 519 | long timeout_val; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 520 | int res = -EINVAL; |
| 521 | |
| 522 | if (unlikely(!dest)) |
| 523 | return -EDESTADDRREQ; |
Allan Stephens | 51f9cc1 | 2006-06-25 23:49:06 -0700 | [diff] [blame] | 524 | if (unlikely((m->msg_namelen < sizeof(*dest)) || |
| 525 | (dest->family != AF_TIPC))) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 526 | return -EINVAL; |
Allan Stephens | c29c3f7 | 2010-04-20 17:58:24 -0400 | [diff] [blame] | 527 | if ((total_len > TIPC_MAX_USER_MSG_SIZE) || |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 528 | (m->msg_iovlen > (unsigned int)INT_MAX)) |
Allan Stephens | c29c3f7 | 2010-04-20 17:58:24 -0400 | [diff] [blame] | 529 | return -EMSGSIZE; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 530 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 531 | if (iocb) |
| 532 | lock_sock(sk); |
| 533 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 534 | needs_conn = (sock->state != SS_READY); |
| 535 | if (unlikely(needs_conn)) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 536 | if (sock->state == SS_LISTENING) { |
| 537 | res = -EPIPE; |
| 538 | goto exit; |
Allan Stephens | 3388007 | 2006-06-25 23:44:57 -0700 | [diff] [blame] | 539 | } |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 540 | if (sock->state != SS_UNCONNECTED) { |
| 541 | res = -EISCONN; |
| 542 | goto exit; |
| 543 | } |
| 544 | if ((tport->published) || |
| 545 | ((sock->type == SOCK_STREAM) && (total_len != 0))) { |
| 546 | res = -EOPNOTSUPP; |
| 547 | goto exit; |
| 548 | } |
| 549 | if (dest->addrtype == TIPC_ADDR_NAME) { |
| 550 | tport->conn_type = dest->addr.name.name.type; |
| 551 | tport->conn_instance = dest->addr.name.name.instance; |
| 552 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 553 | |
| 554 | /* Abort any pending connection attempts (very unlikely) */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 555 | reject_rx_queue(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 556 | } |
| 557 | |
Ying Xue | 1d83587 | 2011-07-06 05:53:15 -0400 | [diff] [blame] | 558 | timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); |
| 559 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 560 | do { |
| 561 | if (dest->addrtype == TIPC_ADDR_NAME) { |
Allan Stephens | 2db9983 | 2010-12-31 18:59:33 +0000 | [diff] [blame] | 562 | res = dest_name_check(dest, m); |
| 563 | if (res) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 564 | break; |
| 565 | res = tipc_send2name(tport->ref, |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 566 | &dest->addr.name.name, |
| 567 | dest->addr.name.domain, |
| 568 | m->msg_iovlen, |
Allan Stephens | 2689690 | 2011-04-21 10:42:07 -0500 | [diff] [blame] | 569 | m->msg_iov, |
| 570 | total_len); |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 571 | } else if (dest->addrtype == TIPC_ADDR_ID) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 572 | res = tipc_send2port(tport->ref, |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 573 | &dest->addr.id, |
| 574 | m->msg_iovlen, |
Allan Stephens | 2689690 | 2011-04-21 10:42:07 -0500 | [diff] [blame] | 575 | m->msg_iov, |
| 576 | total_len); |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 577 | } else if (dest->addrtype == TIPC_ADDR_MCAST) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 578 | if (needs_conn) { |
| 579 | res = -EOPNOTSUPP; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 580 | break; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 581 | } |
Allan Stephens | 2db9983 | 2010-12-31 18:59:33 +0000 | [diff] [blame] | 582 | res = dest_name_check(dest, m); |
| 583 | if (res) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 584 | break; |
| 585 | res = tipc_multicast(tport->ref, |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 586 | &dest->addr.nameseq, |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 587 | m->msg_iovlen, |
Allan Stephens | 2689690 | 2011-04-21 10:42:07 -0500 | [diff] [blame] | 588 | m->msg_iov, |
| 589 | total_len); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 590 | } |
| 591 | if (likely(res != -ELINKCONG)) { |
Allan Stephens | a016892 | 2010-12-31 18:59:35 +0000 | [diff] [blame] | 592 | if (needs_conn && (res >= 0)) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 593 | sock->state = SS_CONNECTING; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 594 | break; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 595 | } |
Ying Xue | 1d83587 | 2011-07-06 05:53:15 -0400 | [diff] [blame] | 596 | if (timeout_val <= 0L) { |
| 597 | res = timeout_val ? timeout_val : -EWOULDBLOCK; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 598 | break; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 599 | } |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 600 | release_sock(sk); |
Ying Xue | 1d83587 | 2011-07-06 05:53:15 -0400 | [diff] [blame] | 601 | timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk), |
| 602 | !tport->congested, timeout_val); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 603 | lock_sock(sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 604 | } while (1); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 605 | |
| 606 | exit: |
| 607 | if (iocb) |
| 608 | release_sock(sk); |
| 609 | return res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 610 | } |
| 611 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 612 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 613 | * send_packet - send a connection-oriented message |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 614 | * @iocb: if NULL, indicates that socket lock is already held |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 615 | * @sock: socket structure |
| 616 | * @m: message to send |
Allan Stephens | e9024f0f | 2006-06-25 23:43:57 -0700 | [diff] [blame] | 617 | * @total_len: length of message |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 618 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 619 | * Used for SOCK_SEQPACKET messages and SOCK_STREAM data. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 620 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 621 | * Returns the number of bytes sent on success, or errno otherwise |
| 622 | */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 623 | static int send_packet(struct kiocb *iocb, struct socket *sock, |
| 624 | struct msghdr *m, size_t total_len) |
| 625 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 626 | struct sock *sk = sock->sk; |
| 627 | struct tipc_port *tport = tipc_sk_port(sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 628 | struct sockaddr_tipc *dest = (struct sockaddr_tipc *)m->msg_name; |
Ying Xue | 1d83587 | 2011-07-06 05:53:15 -0400 | [diff] [blame] | 629 | long timeout_val; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 630 | int res; |
| 631 | |
| 632 | /* Handle implied connection establishment */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 633 | if (unlikely(dest)) |
| 634 | return send_msg(iocb, sock, m, total_len); |
| 635 | |
Allan Stephens | c29c3f7 | 2010-04-20 17:58:24 -0400 | [diff] [blame] | 636 | if ((total_len > TIPC_MAX_USER_MSG_SIZE) || |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 637 | (m->msg_iovlen > (unsigned int)INT_MAX)) |
Allan Stephens | c29c3f7 | 2010-04-20 17:58:24 -0400 | [diff] [blame] | 638 | return -EMSGSIZE; |
| 639 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 640 | if (iocb) |
| 641 | lock_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 642 | |
Ying Xue | 1d83587 | 2011-07-06 05:53:15 -0400 | [diff] [blame] | 643 | timeout_val = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); |
| 644 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 645 | do { |
Allan Stephens | bdd9478 | 2006-06-25 23:45:53 -0700 | [diff] [blame] | 646 | if (unlikely(sock->state != SS_CONNECTED)) { |
| 647 | if (sock->state == SS_DISCONNECTING) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 648 | res = -EPIPE; |
Allan Stephens | bdd9478 | 2006-06-25 23:45:53 -0700 | [diff] [blame] | 649 | else |
| 650 | res = -ENOTCONN; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 651 | break; |
Allan Stephens | bdd9478 | 2006-06-25 23:45:53 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Allan Stephens | 2689690 | 2011-04-21 10:42:07 -0500 | [diff] [blame] | 654 | res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov, |
| 655 | total_len); |
Allan Stephens | a016892 | 2010-12-31 18:59:35 +0000 | [diff] [blame] | 656 | if (likely(res != -ELINKCONG)) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 657 | break; |
Ying Xue | 1d83587 | 2011-07-06 05:53:15 -0400 | [diff] [blame] | 658 | if (timeout_val <= 0L) { |
| 659 | res = timeout_val ? timeout_val : -EWOULDBLOCK; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 660 | break; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 661 | } |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 662 | release_sock(sk); |
Ying Xue | 1d83587 | 2011-07-06 05:53:15 -0400 | [diff] [blame] | 663 | timeout_val = wait_event_interruptible_timeout(*sk_sleep(sk), |
| 664 | (!tport->congested || !tport->connected), timeout_val); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 665 | lock_sock(sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 666 | } while (1); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 667 | |
| 668 | if (iocb) |
| 669 | release_sock(sk); |
| 670 | return res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 671 | } |
| 672 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 673 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 674 | * send_stream - send stream-oriented data |
| 675 | * @iocb: (unused) |
| 676 | * @sock: socket structure |
| 677 | * @m: data to send |
| 678 | * @total_len: total length of data to be sent |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 679 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 680 | * Used for SOCK_STREAM data. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 681 | * |
| 682 | * Returns the number of bytes sent on success (or partial success), |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 683 | * or errno if no data sent |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 684 | */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 685 | static int send_stream(struct kiocb *iocb, struct socket *sock, |
| 686 | struct msghdr *m, size_t total_len) |
| 687 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 688 | struct sock *sk = sock->sk; |
| 689 | struct tipc_port *tport = tipc_sk_port(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 690 | struct msghdr my_msg; |
| 691 | struct iovec my_iov; |
| 692 | struct iovec *curr_iov; |
| 693 | int curr_iovlen; |
| 694 | char __user *curr_start; |
Allan Stephens | 05646c9 | 2007-06-10 17:25:24 -0700 | [diff] [blame] | 695 | u32 hdr_size; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 696 | int curr_left; |
| 697 | int bytes_to_send; |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 698 | int bytes_sent; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 699 | int res; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 700 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 701 | lock_sock(sk); |
| 702 | |
Allan Stephens | 05646c9 | 2007-06-10 17:25:24 -0700 | [diff] [blame] | 703 | /* Handle special cases where there is no connection */ |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 704 | if (unlikely(sock->state != SS_CONNECTED)) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 705 | if (sock->state == SS_UNCONNECTED) { |
| 706 | res = send_packet(NULL, sock, m, total_len); |
| 707 | goto exit; |
| 708 | } else if (sock->state == SS_DISCONNECTING) { |
| 709 | res = -EPIPE; |
| 710 | goto exit; |
| 711 | } else { |
| 712 | res = -ENOTCONN; |
| 713 | goto exit; |
| 714 | } |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 715 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 716 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 717 | if (unlikely(m->msg_name)) { |
| 718 | res = -EISCONN; |
| 719 | goto exit; |
| 720 | } |
Allan Stephens | eb5959c | 2006-10-16 21:43:54 -0700 | [diff] [blame] | 721 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 722 | if ((total_len > (unsigned int)INT_MAX) || |
| 723 | (m->msg_iovlen > (unsigned int)INT_MAX)) { |
Allan Stephens | c29c3f7 | 2010-04-20 17:58:24 -0400 | [diff] [blame] | 724 | res = -EMSGSIZE; |
| 725 | goto exit; |
| 726 | } |
| 727 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 728 | /* |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 729 | * Send each iovec entry using one or more messages |
| 730 | * |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 731 | * Note: This algorithm is good for the most likely case |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 732 | * (i.e. one large iovec entry), but could be improved to pass sets |
| 733 | * of small iovec entries into send_packet(). |
| 734 | */ |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 735 | curr_iov = m->msg_iov; |
| 736 | curr_iovlen = m->msg_iovlen; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 737 | my_msg.msg_iov = &my_iov; |
| 738 | my_msg.msg_iovlen = 1; |
Allan Stephens | eb5959c | 2006-10-16 21:43:54 -0700 | [diff] [blame] | 739 | my_msg.msg_flags = m->msg_flags; |
| 740 | my_msg.msg_name = NULL; |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 741 | bytes_sent = 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 742 | |
Allan Stephens | 05646c9 | 2007-06-10 17:25:24 -0700 | [diff] [blame] | 743 | hdr_size = msg_hdr_sz(&tport->phdr); |
| 744 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 745 | while (curr_iovlen--) { |
| 746 | curr_start = curr_iov->iov_base; |
| 747 | curr_left = curr_iov->iov_len; |
| 748 | |
| 749 | while (curr_left) { |
Allan Stephens | 05646c9 | 2007-06-10 17:25:24 -0700 | [diff] [blame] | 750 | bytes_to_send = tport->max_pkt - hdr_size; |
| 751 | if (bytes_to_send > TIPC_MAX_USER_MSG_SIZE) |
| 752 | bytes_to_send = TIPC_MAX_USER_MSG_SIZE; |
| 753 | if (curr_left < bytes_to_send) |
| 754 | bytes_to_send = curr_left; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 755 | my_iov.iov_base = curr_start; |
| 756 | my_iov.iov_len = bytes_to_send; |
Allan Stephens | 2689690 | 2011-04-21 10:42:07 -0500 | [diff] [blame] | 757 | res = send_packet(NULL, sock, &my_msg, bytes_to_send); |
Allan Stephens | 2db9983 | 2010-12-31 18:59:33 +0000 | [diff] [blame] | 758 | if (res < 0) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 759 | if (bytes_sent) |
Allan Stephens | 05646c9 | 2007-06-10 17:25:24 -0700 | [diff] [blame] | 760 | res = bytes_sent; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 761 | goto exit; |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 762 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 763 | curr_left -= bytes_to_send; |
| 764 | curr_start += bytes_to_send; |
Allan Stephens | 1303e8f | 2006-06-25 23:46:50 -0700 | [diff] [blame] | 765 | bytes_sent += bytes_to_send; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | curr_iov++; |
| 769 | } |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 770 | res = bytes_sent; |
| 771 | exit: |
| 772 | release_sock(sk); |
| 773 | return res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | /** |
| 777 | * auto_connect - complete connection setup to a remote port |
| 778 | * @sock: socket structure |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 779 | * @msg: peer's response message |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 780 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 781 | * Returns 0 on success, errno otherwise |
| 782 | */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 783 | static int auto_connect(struct socket *sock, struct tipc_msg *msg) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 784 | { |
Allan Stephens | 2da5991 | 2008-07-14 22:43:32 -0700 | [diff] [blame] | 785 | struct tipc_sock *tsock = tipc_sk(sock->sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 786 | |
| 787 | if (msg_errcode(msg)) { |
| 788 | sock->state = SS_DISCONNECTING; |
| 789 | return -ECONNREFUSED; |
| 790 | } |
| 791 | |
Allan Stephens | 2da5991 | 2008-07-14 22:43:32 -0700 | [diff] [blame] | 792 | tsock->peer_name.ref = msg_origport(msg); |
| 793 | tsock->peer_name.node = msg_orignode(msg); |
| 794 | tipc_connect2port(tsock->p->ref, &tsock->peer_name); |
| 795 | tipc_set_portimportance(tsock->p->ref, msg_importance(msg)); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 796 | sock->state = SS_CONNECTED; |
| 797 | return 0; |
| 798 | } |
| 799 | |
| 800 | /** |
| 801 | * set_orig_addr - capture sender's address for received message |
| 802 | * @m: descriptor for message info |
| 803 | * @msg: received message header |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 804 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 805 | * Note: Address is not captured if not requested by receiver. |
| 806 | */ |
Sam Ravnborg | 05790c6 | 2006-03-20 22:37:04 -0800 | [diff] [blame] | 807 | static void set_orig_addr(struct msghdr *m, struct tipc_msg *msg) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 808 | { |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 809 | struct sockaddr_tipc *addr = (struct sockaddr_tipc *)m->msg_name; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 810 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 811 | if (addr) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 812 | addr->family = AF_TIPC; |
| 813 | addr->addrtype = TIPC_ADDR_ID; |
| 814 | addr->addr.id.ref = msg_origport(msg); |
| 815 | addr->addr.id.node = msg_orignode(msg); |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 816 | addr->addr.name.domain = 0; /* could leave uninitialized */ |
| 817 | addr->scope = 0; /* could leave uninitialized */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 818 | m->msg_namelen = sizeof(struct sockaddr_tipc); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | /** |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 823 | * anc_data_recv - optionally capture ancillary data for received message |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 824 | * @m: descriptor for message info |
| 825 | * @msg: received message header |
| 826 | * @tport: TIPC port associated with message |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 827 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 828 | * Note: Ancillary data is not captured if not requested by receiver. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 829 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 830 | * Returns 0 if successful, otherwise errno |
| 831 | */ |
Sam Ravnborg | 05790c6 | 2006-03-20 22:37:04 -0800 | [diff] [blame] | 832 | static int anc_data_recv(struct msghdr *m, struct tipc_msg *msg, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 833 | struct tipc_port *tport) |
| 834 | { |
| 835 | u32 anc_data[3]; |
| 836 | u32 err; |
| 837 | u32 dest_type; |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 838 | int has_name; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 839 | int res; |
| 840 | |
| 841 | if (likely(m->msg_controllen == 0)) |
| 842 | return 0; |
| 843 | |
| 844 | /* Optionally capture errored message object(s) */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 845 | err = msg ? msg_errcode(msg) : 0; |
| 846 | if (unlikely(err)) { |
| 847 | anc_data[0] = err; |
| 848 | anc_data[1] = msg_data_sz(msg); |
Allan Stephens | 2db9983 | 2010-12-31 18:59:33 +0000 | [diff] [blame] | 849 | res = put_cmsg(m, SOL_TIPC, TIPC_ERRINFO, 8, anc_data); |
| 850 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 851 | return res; |
Allan Stephens | 2db9983 | 2010-12-31 18:59:33 +0000 | [diff] [blame] | 852 | if (anc_data[1]) { |
| 853 | res = put_cmsg(m, SOL_TIPC, TIPC_RETDATA, anc_data[1], |
| 854 | msg_data(msg)); |
| 855 | if (res) |
| 856 | return res; |
| 857 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | /* Optionally capture message destination object */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 861 | dest_type = msg ? msg_type(msg) : TIPC_DIRECT_MSG; |
| 862 | switch (dest_type) { |
| 863 | case TIPC_NAMED_MSG: |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 864 | has_name = 1; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 865 | anc_data[0] = msg_nametype(msg); |
| 866 | anc_data[1] = msg_namelower(msg); |
| 867 | anc_data[2] = msg_namelower(msg); |
| 868 | break; |
| 869 | case TIPC_MCAST_MSG: |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 870 | has_name = 1; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 871 | anc_data[0] = msg_nametype(msg); |
| 872 | anc_data[1] = msg_namelower(msg); |
| 873 | anc_data[2] = msg_nameupper(msg); |
| 874 | break; |
| 875 | case TIPC_CONN_MSG: |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 876 | has_name = (tport->conn_type != 0); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 877 | anc_data[0] = tport->conn_type; |
| 878 | anc_data[1] = tport->conn_instance; |
| 879 | anc_data[2] = tport->conn_instance; |
| 880 | break; |
| 881 | default: |
Allan Stephens | 3546c75 | 2006-06-25 23:45:24 -0700 | [diff] [blame] | 882 | has_name = 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 883 | } |
Allan Stephens | 2db9983 | 2010-12-31 18:59:33 +0000 | [diff] [blame] | 884 | if (has_name) { |
| 885 | res = put_cmsg(m, SOL_TIPC, TIPC_DESTNAME, 12, anc_data); |
| 886 | if (res) |
| 887 | return res; |
| 888 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 889 | |
| 890 | return 0; |
| 891 | } |
| 892 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 893 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 894 | * recv_msg - receive packet-oriented message |
| 895 | * @iocb: (unused) |
| 896 | * @m: descriptor for message info |
| 897 | * @buf_len: total size of user buffer area |
| 898 | * @flags: receive flags |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 899 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 900 | * Used for SOCK_DGRAM, SOCK_RDM, and SOCK_SEQPACKET messages. |
| 901 | * If the complete message doesn't fit in user area, truncate it. |
| 902 | * |
| 903 | * Returns size of returned message data, errno otherwise |
| 904 | */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 905 | static int recv_msg(struct kiocb *iocb, struct socket *sock, |
| 906 | struct msghdr *m, size_t buf_len, int flags) |
| 907 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 908 | struct sock *sk = sock->sk; |
| 909 | struct tipc_port *tport = tipc_sk_port(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 910 | struct sk_buff *buf; |
| 911 | struct tipc_msg *msg; |
Allan Stephens | 71092ea | 2011-02-23 14:52:14 -0500 | [diff] [blame] | 912 | long timeout; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 913 | unsigned int sz; |
| 914 | u32 err; |
| 915 | int res; |
| 916 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 917 | /* Catch invalid receive requests */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 918 | if (unlikely(!buf_len)) |
| 919 | return -EINVAL; |
| 920 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 921 | lock_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 922 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 923 | if (unlikely(sock->state == SS_UNCONNECTED)) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 924 | res = -ENOTCONN; |
| 925 | goto exit; |
| 926 | } |
| 927 | |
Allan Stephens | 71092ea | 2011-02-23 14:52:14 -0500 | [diff] [blame] | 928 | timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 929 | restart: |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 930 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 931 | /* Look for a message in receive queue; wait if necessary */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 932 | while (skb_queue_empty(&sk->sk_receive_queue)) { |
| 933 | if (sock->state == SS_DISCONNECTING) { |
| 934 | res = -ENOTCONN; |
| 935 | goto exit; |
| 936 | } |
Allan Stephens | 71092ea | 2011-02-23 14:52:14 -0500 | [diff] [blame] | 937 | if (timeout <= 0L) { |
| 938 | res = timeout ? timeout : -EWOULDBLOCK; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 939 | goto exit; |
| 940 | } |
| 941 | release_sock(sk); |
Allan Stephens | 71092ea | 2011-02-23 14:52:14 -0500 | [diff] [blame] | 942 | timeout = wait_event_interruptible_timeout(*sk_sleep(sk), |
| 943 | tipc_rx_ready(sock), |
| 944 | timeout); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 945 | lock_sock(sk); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | /* Look at first message in receive queue */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 949 | buf = skb_peek(&sk->sk_receive_queue); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 950 | msg = buf_msg(buf); |
| 951 | sz = msg_data_sz(msg); |
| 952 | err = msg_errcode(msg); |
| 953 | |
| 954 | /* Complete connection setup for an implied connect */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 955 | if (unlikely(sock->state == SS_CONNECTING)) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 956 | res = auto_connect(sock, msg); |
| 957 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 958 | goto exit; |
| 959 | } |
| 960 | |
| 961 | /* Discard an empty non-errored message & try again */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 962 | if ((!sz) && (!err)) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 963 | advance_rx_queue(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 964 | goto restart; |
| 965 | } |
| 966 | |
| 967 | /* Capture sender's address (optional) */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 968 | set_orig_addr(m, msg); |
| 969 | |
| 970 | /* Capture ancillary data (optional) */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 971 | res = anc_data_recv(m, msg, tport); |
| 972 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 973 | goto exit; |
| 974 | |
| 975 | /* Capture message data (if valid) & compute return value (always) */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 976 | if (!err) { |
| 977 | if (unlikely(buf_len < sz)) { |
| 978 | sz = buf_len; |
| 979 | m->msg_flags |= MSG_TRUNC; |
| 980 | } |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 981 | res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg), |
| 982 | m->msg_iov, sz); |
| 983 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 984 | goto exit; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 985 | res = sz; |
| 986 | } else { |
| 987 | if ((sock->state == SS_READY) || |
| 988 | ((err == TIPC_CONN_SHUTDOWN) || m->msg_control)) |
| 989 | res = 0; |
| 990 | else |
| 991 | res = -ECONNRESET; |
| 992 | } |
| 993 | |
| 994 | /* Consume received message (optional) */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 995 | if (likely(!(flags & MSG_PEEK))) { |
Allan Stephens | 9900980 | 2008-04-15 00:06:12 -0700 | [diff] [blame] | 996 | if ((sock->state != SS_READY) && |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 997 | (++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN)) |
| 998 | tipc_acknowledge(tport->ref, tport->conn_unacked); |
| 999 | advance_rx_queue(sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1000 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1001 | exit: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1002 | release_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1003 | return res; |
| 1004 | } |
| 1005 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1006 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1007 | * recv_stream - receive stream-oriented data |
| 1008 | * @iocb: (unused) |
| 1009 | * @m: descriptor for message info |
| 1010 | * @buf_len: total size of user buffer area |
| 1011 | * @flags: receive flags |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1012 | * |
| 1013 | * Used for SOCK_STREAM messages only. If not enough data is available |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1014 | * will optionally wait for more; never truncates data. |
| 1015 | * |
| 1016 | * Returns size of returned message data, errno otherwise |
| 1017 | */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1018 | static int recv_stream(struct kiocb *iocb, struct socket *sock, |
| 1019 | struct msghdr *m, size_t buf_len, int flags) |
| 1020 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1021 | struct sock *sk = sock->sk; |
| 1022 | struct tipc_port *tport = tipc_sk_port(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1023 | struct sk_buff *buf; |
| 1024 | struct tipc_msg *msg; |
Allan Stephens | 71092ea | 2011-02-23 14:52:14 -0500 | [diff] [blame] | 1025 | long timeout; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1026 | unsigned int sz; |
Florian Westphal | 3720d40 | 2010-08-17 11:00:04 +0000 | [diff] [blame] | 1027 | int sz_to_copy, target, needed; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1028 | int sz_copied = 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1029 | u32 err; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1030 | int res = 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1031 | |
| 1032 | /* Catch invalid receive attempts */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1033 | if (unlikely(!buf_len)) |
| 1034 | return -EINVAL; |
| 1035 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1036 | lock_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1037 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1038 | if (unlikely((sock->state == SS_UNCONNECTED) || |
| 1039 | (sock->state == SS_CONNECTING))) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1040 | res = -ENOTCONN; |
| 1041 | goto exit; |
| 1042 | } |
| 1043 | |
Florian Westphal | 3720d40 | 2010-08-17 11:00:04 +0000 | [diff] [blame] | 1044 | target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len); |
Allan Stephens | 71092ea | 2011-02-23 14:52:14 -0500 | [diff] [blame] | 1045 | timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
Paul Gortmaker | 617d3c7 | 2012-04-30 15:29:02 -0400 | [diff] [blame] | 1046 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1047 | restart: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1048 | /* Look for a message in receive queue; wait if necessary */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1049 | while (skb_queue_empty(&sk->sk_receive_queue)) { |
| 1050 | if (sock->state == SS_DISCONNECTING) { |
| 1051 | res = -ENOTCONN; |
| 1052 | goto exit; |
| 1053 | } |
Allan Stephens | 71092ea | 2011-02-23 14:52:14 -0500 | [diff] [blame] | 1054 | if (timeout <= 0L) { |
| 1055 | res = timeout ? timeout : -EWOULDBLOCK; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1056 | goto exit; |
| 1057 | } |
| 1058 | release_sock(sk); |
Allan Stephens | 71092ea | 2011-02-23 14:52:14 -0500 | [diff] [blame] | 1059 | timeout = wait_event_interruptible_timeout(*sk_sleep(sk), |
| 1060 | tipc_rx_ready(sock), |
| 1061 | timeout); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1062 | lock_sock(sk); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | /* Look at first message in receive queue */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1066 | buf = skb_peek(&sk->sk_receive_queue); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1067 | msg = buf_msg(buf); |
| 1068 | sz = msg_data_sz(msg); |
| 1069 | err = msg_errcode(msg); |
| 1070 | |
| 1071 | /* Discard an empty non-errored message & try again */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1072 | if ((!sz) && (!err)) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1073 | advance_rx_queue(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1074 | goto restart; |
| 1075 | } |
| 1076 | |
| 1077 | /* Optionally capture sender's address & ancillary data of first msg */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1078 | if (sz_copied == 0) { |
| 1079 | set_orig_addr(m, msg); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1080 | res = anc_data_recv(m, msg, tport); |
| 1081 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1082 | goto exit; |
| 1083 | } |
| 1084 | |
| 1085 | /* Capture message data (if valid) & compute return value (always) */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1086 | if (!err) { |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 1087 | u32 offset = (u32)(unsigned long)(TIPC_SKB_CB(buf)->handle); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1088 | |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 1089 | sz -= offset; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1090 | needed = (buf_len - sz_copied); |
| 1091 | sz_to_copy = (sz <= needed) ? sz : needed; |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 1092 | |
| 1093 | res = skb_copy_datagram_iovec(buf, msg_hdr_sz(msg) + offset, |
| 1094 | m->msg_iov, sz_to_copy); |
| 1095 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1096 | goto exit; |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 1097 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1098 | sz_copied += sz_to_copy; |
| 1099 | |
| 1100 | if (sz_to_copy < sz) { |
| 1101 | if (!(flags & MSG_PEEK)) |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 1102 | TIPC_SKB_CB(buf)->handle = |
| 1103 | (void *)(unsigned long)(offset + sz_to_copy); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1104 | goto exit; |
| 1105 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1106 | } else { |
| 1107 | if (sz_copied != 0) |
| 1108 | goto exit; /* can't add error msg to valid data */ |
| 1109 | |
| 1110 | if ((err == TIPC_CONN_SHUTDOWN) || m->msg_control) |
| 1111 | res = 0; |
| 1112 | else |
| 1113 | res = -ECONNRESET; |
| 1114 | } |
| 1115 | |
| 1116 | /* Consume received message (optional) */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1117 | if (likely(!(flags & MSG_PEEK))) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1118 | if (unlikely(++tport->conn_unacked >= TIPC_FLOW_CONTROL_WIN)) |
| 1119 | tipc_acknowledge(tport->ref, tport->conn_unacked); |
| 1120 | advance_rx_queue(sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1121 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1122 | |
| 1123 | /* Loop around if more data is required */ |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 1124 | if ((sz_copied < buf_len) && /* didn't get all requested data */ |
| 1125 | (!skb_queue_empty(&sk->sk_receive_queue) || |
Florian Westphal | 3720d40 | 2010-08-17 11:00:04 +0000 | [diff] [blame] | 1126 | (sz_copied < target)) && /* and more is ready or required */ |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 1127 | (!(flags & MSG_PEEK)) && /* and aren't just peeking at data */ |
| 1128 | (!err)) /* and haven't reached a FIN */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1129 | goto restart; |
| 1130 | |
| 1131 | exit: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1132 | release_sock(sk); |
Allan Stephens | a3b0a5a | 2006-06-25 23:48:22 -0700 | [diff] [blame] | 1133 | return sz_copied ? sz_copied : res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | /** |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 1137 | * tipc_write_space - wake up thread if port congestion is released |
| 1138 | * @sk: socket |
| 1139 | */ |
| 1140 | static void tipc_write_space(struct sock *sk) |
| 1141 | { |
| 1142 | struct socket_wq *wq; |
| 1143 | |
| 1144 | rcu_read_lock(); |
| 1145 | wq = rcu_dereference(sk->sk_wq); |
| 1146 | if (wq_has_sleeper(wq)) |
| 1147 | wake_up_interruptible_sync_poll(&wq->wait, POLLOUT | |
| 1148 | POLLWRNORM | POLLWRBAND); |
| 1149 | rcu_read_unlock(); |
| 1150 | } |
| 1151 | |
| 1152 | /** |
| 1153 | * tipc_data_ready - wake up threads to indicate messages have been received |
| 1154 | * @sk: socket |
| 1155 | * @len: the length of messages |
| 1156 | */ |
| 1157 | static void tipc_data_ready(struct sock *sk, int len) |
| 1158 | { |
| 1159 | struct socket_wq *wq; |
| 1160 | |
| 1161 | rcu_read_lock(); |
| 1162 | wq = rcu_dereference(sk->sk_wq); |
| 1163 | if (wq_has_sleeper(wq)) |
| 1164 | wake_up_interruptible_sync_poll(&wq->wait, POLLIN | |
| 1165 | POLLRDNORM | POLLRDBAND); |
| 1166 | rcu_read_unlock(); |
| 1167 | } |
| 1168 | |
| 1169 | /** |
Allan Stephens | 1819b83 | 2008-04-15 00:15:50 -0700 | [diff] [blame] | 1170 | * rx_queue_full - determine if receive queue can accept another message |
| 1171 | * @msg: message to be added to queue |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1172 | * @queue_size: current size of queue |
| 1173 | * @base: nominal maximum size of queue |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1174 | * |
Allan Stephens | 1819b83 | 2008-04-15 00:15:50 -0700 | [diff] [blame] | 1175 | * Returns 1 if queue is unable to accept message, 0 otherwise |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1176 | */ |
Allan Stephens | 1819b83 | 2008-04-15 00:15:50 -0700 | [diff] [blame] | 1177 | static int rx_queue_full(struct tipc_msg *msg, u32 queue_size, u32 base) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1178 | { |
| 1179 | u32 threshold; |
| 1180 | u32 imp = msg_importance(msg); |
| 1181 | |
| 1182 | if (imp == TIPC_LOW_IMPORTANCE) |
| 1183 | threshold = base; |
| 1184 | else if (imp == TIPC_MEDIUM_IMPORTANCE) |
| 1185 | threshold = base * 2; |
| 1186 | else if (imp == TIPC_HIGH_IMPORTANCE) |
| 1187 | threshold = base * 100; |
| 1188 | else |
| 1189 | return 0; |
| 1190 | |
| 1191 | if (msg_connected(msg)) |
| 1192 | threshold *= 4; |
| 1193 | |
Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 1194 | return queue_size >= threshold; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1195 | } |
| 1196 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1197 | /** |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1198 | * filter_rcv - validate incoming message |
| 1199 | * @sk: socket |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1200 | * @buf: message |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1201 | * |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1202 | * Enqueues message on receive queue if acceptable; optionally handles |
| 1203 | * disconnect indication for a connected socket. |
| 1204 | * |
| 1205 | * Called with socket lock already taken; port lock may also be taken. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1206 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1207 | * Returns TIPC error status code (TIPC_OK if message is not to be rejected) |
| 1208 | */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1209 | static u32 filter_rcv(struct sock *sk, struct sk_buff *buf) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1210 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1211 | struct socket *sock = sk->sk_socket; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1212 | struct tipc_msg *msg = buf_msg(buf); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1213 | u32 recv_q_len; |
| 1214 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1215 | /* Reject message if it is wrong sort of message for socket */ |
Allan Stephens | aad5854 | 2012-04-26 18:13:08 -0400 | [diff] [blame] | 1216 | if (msg_type(msg) > TIPC_DIRECT_MSG) |
| 1217 | return TIPC_ERR_NO_PORT; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1218 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1219 | if (sock->state == SS_READY) { |
Allan Stephens | b29f142 | 2010-12-31 18:59:25 +0000 | [diff] [blame] | 1220 | if (msg_connected(msg)) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1221 | return TIPC_ERR_NO_PORT; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1222 | } else { |
Allan Stephens | b29f142 | 2010-12-31 18:59:25 +0000 | [diff] [blame] | 1223 | if (msg_mcast(msg)) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1224 | return TIPC_ERR_NO_PORT; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1225 | if (sock->state == SS_CONNECTED) { |
Allan Stephens | f0712e86 | 2012-04-17 18:42:28 -0400 | [diff] [blame] | 1226 | if (!msg_connected(msg) || |
| 1227 | !tipc_port_peer_msg(tipc_sk_port(sk), msg)) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1228 | return TIPC_ERR_NO_PORT; |
Allan Stephens | b29f142 | 2010-12-31 18:59:25 +0000 | [diff] [blame] | 1229 | } else if (sock->state == SS_CONNECTING) { |
| 1230 | if (!msg_connected(msg) && (msg_errcode(msg) == 0)) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1231 | return TIPC_ERR_NO_PORT; |
Allan Stephens | b29f142 | 2010-12-31 18:59:25 +0000 | [diff] [blame] | 1232 | } else if (sock->state == SS_LISTENING) { |
| 1233 | if (msg_connected(msg) || msg_errcode(msg)) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1234 | return TIPC_ERR_NO_PORT; |
Allan Stephens | b29f142 | 2010-12-31 18:59:25 +0000 | [diff] [blame] | 1235 | } else if (sock->state == SS_DISCONNECTING) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1236 | return TIPC_ERR_NO_PORT; |
Allan Stephens | b29f142 | 2010-12-31 18:59:25 +0000 | [diff] [blame] | 1237 | } else /* (sock->state == SS_UNCONNECTED) */ { |
| 1238 | if (msg_connected(msg) || msg_errcode(msg)) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1239 | return TIPC_ERR_NO_PORT; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1240 | } |
| 1241 | } |
| 1242 | |
| 1243 | /* Reject message if there isn't room to queue it */ |
Allan Stephens | 1819b83 | 2008-04-15 00:15:50 -0700 | [diff] [blame] | 1244 | recv_q_len = (u32)atomic_read(&tipc_queue_size); |
| 1245 | if (unlikely(recv_q_len >= OVERLOAD_LIMIT_BASE)) { |
| 1246 | if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE)) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1247 | return TIPC_ERR_OVERLOAD; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1248 | } |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1249 | recv_q_len = skb_queue_len(&sk->sk_receive_queue); |
Allan Stephens | 1819b83 | 2008-04-15 00:15:50 -0700 | [diff] [blame] | 1250 | if (unlikely(recv_q_len >= (OVERLOAD_LIMIT_BASE / 2))) { |
| 1251 | if (rx_queue_full(msg, recv_q_len, OVERLOAD_LIMIT_BASE / 2)) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1252 | return TIPC_ERR_OVERLOAD; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1253 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1254 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1255 | /* Enqueue message (finally!) */ |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 1256 | TIPC_SKB_CB(buf)->handle = 0; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1257 | atomic_inc(&tipc_queue_size); |
| 1258 | __skb_queue_tail(&sk->sk_receive_queue, buf); |
| 1259 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1260 | /* Initiate connection termination for an incoming 'FIN' */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1261 | if (unlikely(msg_errcode(msg) && (sock->state == SS_CONNECTED))) { |
| 1262 | sock->state = SS_DISCONNECTING; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1263 | tipc_disconnect_port(tipc_sk_port(sk)); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1264 | } |
| 1265 | |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 1266 | sk->sk_data_ready(sk, 0); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1267 | return TIPC_OK; |
| 1268 | } |
| 1269 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1270 | /** |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1271 | * backlog_rcv - handle incoming message from backlog queue |
| 1272 | * @sk: socket |
| 1273 | * @buf: message |
| 1274 | * |
| 1275 | * Caller must hold socket lock, but not port lock. |
| 1276 | * |
| 1277 | * Returns 0 |
| 1278 | */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1279 | static int backlog_rcv(struct sock *sk, struct sk_buff *buf) |
| 1280 | { |
| 1281 | u32 res; |
| 1282 | |
| 1283 | res = filter_rcv(sk, buf); |
| 1284 | if (res) |
| 1285 | tipc_reject_msg(buf, res); |
| 1286 | return 0; |
| 1287 | } |
| 1288 | |
| 1289 | /** |
| 1290 | * dispatch - handle incoming message |
| 1291 | * @tport: TIPC port that received message |
| 1292 | * @buf: message |
| 1293 | * |
| 1294 | * Called with port lock already taken. |
| 1295 | * |
| 1296 | * Returns TIPC error status code (TIPC_OK if message is not to be rejected) |
| 1297 | */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1298 | static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf) |
| 1299 | { |
| 1300 | struct sock *sk = (struct sock *)tport->usr_handle; |
| 1301 | u32 res; |
| 1302 | |
| 1303 | /* |
| 1304 | * Process message if socket is unlocked; otherwise add to backlog queue |
| 1305 | * |
| 1306 | * This code is based on sk_receive_skb(), but must be distinct from it |
| 1307 | * since a TIPC-specific filter/reject mechanism is utilized |
| 1308 | */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1309 | bh_lock_sock(sk); |
| 1310 | if (!sock_owned_by_user(sk)) { |
| 1311 | res = filter_rcv(sk, buf); |
| 1312 | } else { |
Eric Dumazet | f545a38 | 2012-04-22 23:34:26 +0000 | [diff] [blame] | 1313 | if (sk_add_backlog(sk, buf, sk->sk_rcvbuf)) |
Zhu Yi | 53eecb1 | 2010-03-04 18:01:45 +0000 | [diff] [blame] | 1314 | res = TIPC_ERR_OVERLOAD; |
| 1315 | else |
| 1316 | res = TIPC_OK; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1317 | } |
| 1318 | bh_unlock_sock(sk); |
| 1319 | |
| 1320 | return res; |
| 1321 | } |
| 1322 | |
| 1323 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1324 | * wakeupdispatch - wake up port after congestion |
| 1325 | * @tport: port to wakeup |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1326 | * |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1327 | * Called with port lock already taken. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1328 | */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1329 | static void wakeupdispatch(struct tipc_port *tport) |
| 1330 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1331 | struct sock *sk = (struct sock *)tport->usr_handle; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1332 | |
Ying Xue | f288bef | 2012-08-21 11:16:57 +0800 | [diff] [blame] | 1333 | sk->sk_write_space(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | /** |
| 1337 | * connect - establish a connection to another TIPC port |
| 1338 | * @sock: socket structure |
| 1339 | * @dest: socket address for destination port |
| 1340 | * @destlen: size of socket address data structure |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1341 | * @flags: file-related flags associated with socket |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1342 | * |
| 1343 | * Returns 0 on success, errno otherwise |
| 1344 | */ |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1345 | static int connect(struct socket *sock, struct sockaddr *dest, int destlen, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1346 | int flags) |
| 1347 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1348 | struct sock *sk = sock->sk; |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1349 | struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest; |
| 1350 | struct msghdr m = {NULL,}; |
| 1351 | struct sk_buff *buf; |
| 1352 | struct tipc_msg *msg; |
Allan Stephens | a0f40f0 | 2011-05-26 13:44:34 -0400 | [diff] [blame] | 1353 | unsigned int timeout; |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1354 | int res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1355 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1356 | lock_sock(sk); |
| 1357 | |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1358 | /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1359 | if (sock->state == SS_READY) { |
| 1360 | res = -EOPNOTSUPP; |
| 1361 | goto exit; |
| 1362 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1363 | |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1364 | /* For now, TIPC does not support the non-blocking form of connect() */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1365 | if (flags & O_NONBLOCK) { |
Allan Stephens | 35997e3 | 2010-08-17 11:00:05 +0000 | [diff] [blame] | 1366 | res = -EOPNOTSUPP; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1367 | goto exit; |
| 1368 | } |
Allan Stephens | 4934c69 | 2008-04-15 00:16:19 -0700 | [diff] [blame] | 1369 | |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1370 | /* Issue Posix-compliant error code if socket is in the wrong state */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1371 | if (sock->state == SS_LISTENING) { |
| 1372 | res = -EOPNOTSUPP; |
| 1373 | goto exit; |
| 1374 | } |
| 1375 | if (sock->state == SS_CONNECTING) { |
| 1376 | res = -EALREADY; |
| 1377 | goto exit; |
| 1378 | } |
| 1379 | if (sock->state != SS_UNCONNECTED) { |
| 1380 | res = -EISCONN; |
| 1381 | goto exit; |
| 1382 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1383 | |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1384 | /* |
| 1385 | * Reject connection attempt using multicast address |
| 1386 | * |
| 1387 | * Note: send_msg() validates the rest of the address fields, |
| 1388 | * so there's no need to do it here |
| 1389 | */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1390 | if (dst->addrtype == TIPC_ADDR_MCAST) { |
| 1391 | res = -EINVAL; |
| 1392 | goto exit; |
| 1393 | } |
| 1394 | |
| 1395 | /* Reject any messages already in receive queue (very unlikely) */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1396 | reject_rx_queue(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1397 | |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1398 | /* Send a 'SYN-' to destination */ |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1399 | m.msg_name = dest; |
| 1400 | m.msg_namelen = destlen; |
| 1401 | res = send_msg(NULL, sock, &m, 0); |
Allan Stephens | a016892 | 2010-12-31 18:59:35 +0000 | [diff] [blame] | 1402 | if (res < 0) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1403 | goto exit; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1404 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1405 | /* Wait until an 'ACK' or 'RST' arrives, or a timeout occurs */ |
Allan Stephens | 564e83b | 2010-08-17 11:00:15 +0000 | [diff] [blame] | 1406 | timeout = tipc_sk(sk)->conn_timeout; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1407 | release_sock(sk); |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 1408 | res = wait_event_interruptible_timeout(*sk_sleep(sk), |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1409 | (!skb_queue_empty(&sk->sk_receive_queue) || |
| 1410 | (sock->state != SS_CONNECTING)), |
Allan Stephens | a0f40f0 | 2011-05-26 13:44:34 -0400 | [diff] [blame] | 1411 | timeout ? (long)msecs_to_jiffies(timeout) |
| 1412 | : MAX_SCHEDULE_TIMEOUT); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1413 | lock_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1414 | |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1415 | if (res > 0) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1416 | buf = skb_peek(&sk->sk_receive_queue); |
| 1417 | if (buf != NULL) { |
| 1418 | msg = buf_msg(buf); |
| 1419 | res = auto_connect(sock, msg); |
| 1420 | if (!res) { |
| 1421 | if (!msg_data_sz(msg)) |
| 1422 | advance_rx_queue(sk); |
| 1423 | } |
| 1424 | } else { |
Allan Stephens | a016892 | 2010-12-31 18:59:35 +0000 | [diff] [blame] | 1425 | if (sock->state == SS_CONNECTED) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1426 | res = -EISCONN; |
Allan Stephens | a016892 | 2010-12-31 18:59:35 +0000 | [diff] [blame] | 1427 | else |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1428 | res = -ECONNREFUSED; |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1429 | } |
| 1430 | } else { |
| 1431 | if (res == 0) |
| 1432 | res = -ETIMEDOUT; |
| 1433 | else |
| 1434 | ; /* leave "res" unchanged */ |
| 1435 | sock->state = SS_DISCONNECTING; |
| 1436 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1437 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1438 | exit: |
| 1439 | release_sock(sk); |
Allan Stephens | b89741a | 2008-04-15 00:20:37 -0700 | [diff] [blame] | 1440 | return res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1441 | } |
| 1442 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1443 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1444 | * listen - allow socket to listen for incoming connections |
| 1445 | * @sock: socket structure |
| 1446 | * @len: (unused) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1447 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1448 | * Returns 0 on success, errno otherwise |
| 1449 | */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1450 | static int listen(struct socket *sock, int len) |
| 1451 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1452 | struct sock *sk = sock->sk; |
| 1453 | int res; |
| 1454 | |
| 1455 | lock_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1456 | |
Ying Xue | 245f3d3 | 2011-07-06 06:01:13 -0400 | [diff] [blame] | 1457 | if (sock->state != SS_UNCONNECTED) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1458 | res = -EINVAL; |
| 1459 | else { |
| 1460 | sock->state = SS_LISTENING; |
| 1461 | res = 0; |
| 1462 | } |
| 1463 | |
| 1464 | release_sock(sk); |
| 1465 | return res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1466 | } |
| 1467 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1468 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1469 | * accept - wait for connection request |
| 1470 | * @sock: listening socket |
| 1471 | * @newsock: new socket that is to be connected |
| 1472 | * @flags: file-related flags associated with socket |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1473 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1474 | * Returns 0 on success, errno otherwise |
| 1475 | */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1476 | static int accept(struct socket *sock, struct socket *new_sock, int flags) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1477 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1478 | struct sock *sk = sock->sk; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1479 | struct sk_buff *buf; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1480 | int res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1481 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1482 | lock_sock(sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1483 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1484 | if (sock->state != SS_LISTENING) { |
| 1485 | res = -EINVAL; |
| 1486 | goto exit; |
| 1487 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1488 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1489 | while (skb_queue_empty(&sk->sk_receive_queue)) { |
| 1490 | if (flags & O_NONBLOCK) { |
| 1491 | res = -EWOULDBLOCK; |
| 1492 | goto exit; |
| 1493 | } |
| 1494 | release_sock(sk); |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 1495 | res = wait_event_interruptible(*sk_sleep(sk), |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1496 | (!skb_queue_empty(&sk->sk_receive_queue))); |
| 1497 | lock_sock(sk); |
| 1498 | if (res) |
| 1499 | goto exit; |
| 1500 | } |
| 1501 | |
| 1502 | buf = skb_peek(&sk->sk_receive_queue); |
| 1503 | |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 1504 | res = tipc_create(sock_net(sock->sk), new_sock, 0, 0); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1505 | if (!res) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1506 | struct sock *new_sk = new_sock->sk; |
Allan Stephens | 2da5991 | 2008-07-14 22:43:32 -0700 | [diff] [blame] | 1507 | struct tipc_sock *new_tsock = tipc_sk(new_sk); |
| 1508 | struct tipc_port *new_tport = new_tsock->p; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1509 | u32 new_ref = new_tport->ref; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1510 | struct tipc_msg *msg = buf_msg(buf); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1511 | |
| 1512 | lock_sock(new_sk); |
| 1513 | |
| 1514 | /* |
| 1515 | * Reject any stray messages received by new socket |
| 1516 | * before the socket lock was taken (very, very unlikely) |
| 1517 | */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1518 | reject_rx_queue(new_sk); |
| 1519 | |
| 1520 | /* Connect new socket to it's peer */ |
Allan Stephens | 2da5991 | 2008-07-14 22:43:32 -0700 | [diff] [blame] | 1521 | new_tsock->peer_name.ref = msg_origport(msg); |
| 1522 | new_tsock->peer_name.node = msg_orignode(msg); |
| 1523 | tipc_connect2port(new_ref, &new_tsock->peer_name); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1524 | new_sock->state = SS_CONNECTED; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1525 | |
| 1526 | tipc_set_portimportance(new_ref, msg_importance(msg)); |
| 1527 | if (msg_named(msg)) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1528 | new_tport->conn_type = msg_nametype(msg); |
| 1529 | new_tport->conn_instance = msg_nameinst(msg); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1530 | } |
| 1531 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1532 | /* |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1533 | * Respond to 'SYN-' by discarding it & returning 'ACK'-. |
| 1534 | * Respond to 'SYN+' by queuing it on new socket. |
| 1535 | */ |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1536 | if (!msg_data_sz(msg)) { |
| 1537 | struct msghdr m = {NULL,}; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1538 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1539 | advance_rx_queue(sk); |
| 1540 | send_packet(NULL, new_sock, &m, 0); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1541 | } else { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1542 | __skb_dequeue(&sk->sk_receive_queue); |
| 1543 | __skb_queue_head(&new_sk->sk_receive_queue, buf); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1544 | } |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1545 | release_sock(new_sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1546 | } |
| 1547 | exit: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1548 | release_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1549 | return res; |
| 1550 | } |
| 1551 | |
| 1552 | /** |
| 1553 | * shutdown - shutdown socket connection |
| 1554 | * @sock: socket structure |
Allan Stephens | e247a8f | 2008-03-06 15:05:38 -0800 | [diff] [blame] | 1555 | * @how: direction to close (must be SHUT_RDWR) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1556 | * |
| 1557 | * Terminates connection (if necessary), then purges socket's receive queue. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1558 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1559 | * Returns 0 on success, errno otherwise |
| 1560 | */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1561 | static int shutdown(struct socket *sock, int how) |
| 1562 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1563 | struct sock *sk = sock->sk; |
| 1564 | struct tipc_port *tport = tipc_sk_port(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1565 | struct sk_buff *buf; |
| 1566 | int res; |
| 1567 | |
Allan Stephens | e247a8f | 2008-03-06 15:05:38 -0800 | [diff] [blame] | 1568 | if (how != SHUT_RDWR) |
| 1569 | return -EINVAL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1570 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1571 | lock_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1572 | |
| 1573 | switch (sock->state) { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1574 | case SS_CONNECTING: |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1575 | case SS_CONNECTED: |
| 1576 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1577 | restart: |
Paul Gortmaker | 617d3c7 | 2012-04-30 15:29:02 -0400 | [diff] [blame] | 1578 | /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1579 | buf = __skb_dequeue(&sk->sk_receive_queue); |
| 1580 | if (buf) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1581 | atomic_dec(&tipc_queue_size); |
Allan Stephens | 0232fd0 | 2011-02-21 09:45:40 -0500 | [diff] [blame] | 1582 | if (TIPC_SKB_CB(buf)->handle != 0) { |
Allan Stephens | 5f6d912 | 2011-11-04 13:24:29 -0400 | [diff] [blame] | 1583 | kfree_skb(buf); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1584 | goto restart; |
| 1585 | } |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1586 | tipc_disconnect(tport->ref); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1587 | tipc_reject_msg(buf, TIPC_CONN_SHUTDOWN); |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1588 | } else { |
| 1589 | tipc_shutdown(tport->ref); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1590 | } |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1591 | |
| 1592 | sock->state = SS_DISCONNECTING; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1593 | |
| 1594 | /* fall through */ |
| 1595 | |
| 1596 | case SS_DISCONNECTING: |
| 1597 | |
Ying Xue | 7503115 | 2012-10-29 09:38:15 -0400 | [diff] [blame^] | 1598 | /* Discard any unreceived messages */ |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1599 | discard_rx_queue(sk); |
Ying Xue | 7503115 | 2012-10-29 09:38:15 -0400 | [diff] [blame^] | 1600 | |
| 1601 | /* Wake up anyone sleeping in poll */ |
| 1602 | sk->sk_state_change(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1603 | res = 0; |
| 1604 | break; |
| 1605 | |
| 1606 | default: |
| 1607 | res = -ENOTCONN; |
| 1608 | } |
| 1609 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1610 | release_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1611 | return res; |
| 1612 | } |
| 1613 | |
| 1614 | /** |
| 1615 | * setsockopt - set socket option |
| 1616 | * @sock: socket structure |
| 1617 | * @lvl: option level |
| 1618 | * @opt: option identifier |
| 1619 | * @ov: pointer to new option value |
| 1620 | * @ol: length of option value |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1621 | * |
| 1622 | * For stream sockets only, accepts and ignores all IPPROTO_TCP options |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1623 | * (to ease compatibility). |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1624 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1625 | * Returns 0 on success, errno otherwise |
| 1626 | */ |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1627 | static int setsockopt(struct socket *sock, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 1628 | int lvl, int opt, char __user *ov, unsigned int ol) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1629 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1630 | struct sock *sk = sock->sk; |
| 1631 | struct tipc_port *tport = tipc_sk_port(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1632 | u32 value; |
| 1633 | int res; |
| 1634 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1635 | if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) |
| 1636 | return 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1637 | if (lvl != SOL_TIPC) |
| 1638 | return -ENOPROTOOPT; |
| 1639 | if (ol < sizeof(value)) |
| 1640 | return -EINVAL; |
Allan Stephens | 2db9983 | 2010-12-31 18:59:33 +0000 | [diff] [blame] | 1641 | res = get_user(value, (u32 __user *)ov); |
| 1642 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1643 | return res; |
| 1644 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1645 | lock_sock(sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1646 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1647 | switch (opt) { |
| 1648 | case TIPC_IMPORTANCE: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1649 | res = tipc_set_portimportance(tport->ref, value); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1650 | break; |
| 1651 | case TIPC_SRC_DROPPABLE: |
| 1652 | if (sock->type != SOCK_STREAM) |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1653 | res = tipc_set_portunreliable(tport->ref, value); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1654 | else |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1655 | res = -ENOPROTOOPT; |
| 1656 | break; |
| 1657 | case TIPC_DEST_DROPPABLE: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1658 | res = tipc_set_portunreturnable(tport->ref, value); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1659 | break; |
| 1660 | case TIPC_CONN_TIMEOUT: |
Allan Stephens | a0f40f0 | 2011-05-26 13:44:34 -0400 | [diff] [blame] | 1661 | tipc_sk(sk)->conn_timeout = value; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1662 | /* no need to set "res", since already 0 at this point */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1663 | break; |
| 1664 | default: |
| 1665 | res = -EINVAL; |
| 1666 | } |
| 1667 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1668 | release_sock(sk); |
| 1669 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1670 | return res; |
| 1671 | } |
| 1672 | |
| 1673 | /** |
| 1674 | * getsockopt - get socket option |
| 1675 | * @sock: socket structure |
| 1676 | * @lvl: option level |
| 1677 | * @opt: option identifier |
| 1678 | * @ov: receptacle for option value |
| 1679 | * @ol: receptacle for length of option value |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1680 | * |
| 1681 | * For stream sockets only, returns 0 length result for all IPPROTO_TCP options |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1682 | * (to ease compatibility). |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1683 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1684 | * Returns 0 on success, errno otherwise |
| 1685 | */ |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1686 | static int getsockopt(struct socket *sock, |
Al Viro | 28c4dad | 2006-10-10 22:45:57 +0100 | [diff] [blame] | 1687 | int lvl, int opt, char __user *ov, int __user *ol) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1688 | { |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1689 | struct sock *sk = sock->sk; |
| 1690 | struct tipc_port *tport = tipc_sk_port(sk); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1691 | int len; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1692 | u32 value; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1693 | int res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1694 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1695 | if ((lvl == IPPROTO_TCP) && (sock->type == SOCK_STREAM)) |
| 1696 | return put_user(0, ol); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1697 | if (lvl != SOL_TIPC) |
| 1698 | return -ENOPROTOOPT; |
Allan Stephens | 2db9983 | 2010-12-31 18:59:33 +0000 | [diff] [blame] | 1699 | res = get_user(len, ol); |
| 1700 | if (res) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1701 | return res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1702 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1703 | lock_sock(sk); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1704 | |
| 1705 | switch (opt) { |
| 1706 | case TIPC_IMPORTANCE: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1707 | res = tipc_portimportance(tport->ref, &value); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1708 | break; |
| 1709 | case TIPC_SRC_DROPPABLE: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1710 | res = tipc_portunreliable(tport->ref, &value); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1711 | break; |
| 1712 | case TIPC_DEST_DROPPABLE: |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1713 | res = tipc_portunreturnable(tport->ref, &value); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1714 | break; |
| 1715 | case TIPC_CONN_TIMEOUT: |
Allan Stephens | a0f40f0 | 2011-05-26 13:44:34 -0400 | [diff] [blame] | 1716 | value = tipc_sk(sk)->conn_timeout; |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1717 | /* no need to set "res", since already 0 at this point */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1718 | break; |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 1719 | case TIPC_NODE_RECVQ_DEPTH: |
oscar.medina@motorola.com | 6650613 | 2009-06-30 03:25:39 +0000 | [diff] [blame] | 1720 | value = (u32)atomic_read(&tipc_queue_size); |
| 1721 | break; |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 1722 | case TIPC_SOCK_RECVQ_DEPTH: |
oscar.medina@motorola.com | 6650613 | 2009-06-30 03:25:39 +0000 | [diff] [blame] | 1723 | value = skb_queue_len(&sk->sk_receive_queue); |
| 1724 | break; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1725 | default: |
| 1726 | res = -EINVAL; |
| 1727 | } |
| 1728 | |
Allan Stephens | 0c3141e | 2008-04-15 00:22:02 -0700 | [diff] [blame] | 1729 | release_sock(sk); |
| 1730 | |
Paul Gortmaker | 25860c3 | 2010-12-31 18:59:31 +0000 | [diff] [blame] | 1731 | if (res) |
| 1732 | return res; /* "get" failed */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1733 | |
Paul Gortmaker | 25860c3 | 2010-12-31 18:59:31 +0000 | [diff] [blame] | 1734 | if (len < sizeof(value)) |
| 1735 | return -EINVAL; |
| 1736 | |
| 1737 | if (copy_to_user(ov, &value, sizeof(value))) |
| 1738 | return -EFAULT; |
| 1739 | |
| 1740 | return put_user(sizeof(value), ol); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1741 | } |
| 1742 | |
Ben Hutchings | ae86b9e | 2012-07-10 10:55:35 +0000 | [diff] [blame] | 1743 | /* Protocol switches for the various types of TIPC sockets */ |
| 1744 | |
Florian Westphal | bca65ea | 2008-02-07 18:18:01 -0800 | [diff] [blame] | 1745 | static const struct proto_ops msg_ops = { |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 1746 | .owner = THIS_MODULE, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1747 | .family = AF_TIPC, |
| 1748 | .release = release, |
| 1749 | .bind = bind, |
| 1750 | .connect = connect, |
Allan Stephens | 5eee6a6 | 2007-06-10 17:24:55 -0700 | [diff] [blame] | 1751 | .socketpair = sock_no_socketpair, |
Ying Xue | 245f3d3 | 2011-07-06 06:01:13 -0400 | [diff] [blame] | 1752 | .accept = sock_no_accept, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1753 | .getname = get_name, |
| 1754 | .poll = poll, |
Allan Stephens | 5eee6a6 | 2007-06-10 17:24:55 -0700 | [diff] [blame] | 1755 | .ioctl = sock_no_ioctl, |
Ying Xue | 245f3d3 | 2011-07-06 06:01:13 -0400 | [diff] [blame] | 1756 | .listen = sock_no_listen, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1757 | .shutdown = shutdown, |
| 1758 | .setsockopt = setsockopt, |
| 1759 | .getsockopt = getsockopt, |
| 1760 | .sendmsg = send_msg, |
| 1761 | .recvmsg = recv_msg, |
YOSHIFUJI Hideaki | 8238745 | 2007-07-19 10:44:56 +0900 | [diff] [blame] | 1762 | .mmap = sock_no_mmap, |
| 1763 | .sendpage = sock_no_sendpage |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1764 | }; |
| 1765 | |
Florian Westphal | bca65ea | 2008-02-07 18:18:01 -0800 | [diff] [blame] | 1766 | static const struct proto_ops packet_ops = { |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 1767 | .owner = THIS_MODULE, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1768 | .family = AF_TIPC, |
| 1769 | .release = release, |
| 1770 | .bind = bind, |
| 1771 | .connect = connect, |
Allan Stephens | 5eee6a6 | 2007-06-10 17:24:55 -0700 | [diff] [blame] | 1772 | .socketpair = sock_no_socketpair, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1773 | .accept = accept, |
| 1774 | .getname = get_name, |
| 1775 | .poll = poll, |
Allan Stephens | 5eee6a6 | 2007-06-10 17:24:55 -0700 | [diff] [blame] | 1776 | .ioctl = sock_no_ioctl, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1777 | .listen = listen, |
| 1778 | .shutdown = shutdown, |
| 1779 | .setsockopt = setsockopt, |
| 1780 | .getsockopt = getsockopt, |
| 1781 | .sendmsg = send_packet, |
| 1782 | .recvmsg = recv_msg, |
YOSHIFUJI Hideaki | 8238745 | 2007-07-19 10:44:56 +0900 | [diff] [blame] | 1783 | .mmap = sock_no_mmap, |
| 1784 | .sendpage = sock_no_sendpage |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1785 | }; |
| 1786 | |
Florian Westphal | bca65ea | 2008-02-07 18:18:01 -0800 | [diff] [blame] | 1787 | static const struct proto_ops stream_ops = { |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 1788 | .owner = THIS_MODULE, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1789 | .family = AF_TIPC, |
| 1790 | .release = release, |
| 1791 | .bind = bind, |
| 1792 | .connect = connect, |
Allan Stephens | 5eee6a6 | 2007-06-10 17:24:55 -0700 | [diff] [blame] | 1793 | .socketpair = sock_no_socketpair, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1794 | .accept = accept, |
| 1795 | .getname = get_name, |
| 1796 | .poll = poll, |
Allan Stephens | 5eee6a6 | 2007-06-10 17:24:55 -0700 | [diff] [blame] | 1797 | .ioctl = sock_no_ioctl, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1798 | .listen = listen, |
| 1799 | .shutdown = shutdown, |
| 1800 | .setsockopt = setsockopt, |
| 1801 | .getsockopt = getsockopt, |
| 1802 | .sendmsg = send_stream, |
| 1803 | .recvmsg = recv_stream, |
YOSHIFUJI Hideaki | 8238745 | 2007-07-19 10:44:56 +0900 | [diff] [blame] | 1804 | .mmap = sock_no_mmap, |
| 1805 | .sendpage = sock_no_sendpage |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1806 | }; |
| 1807 | |
Florian Westphal | bca65ea | 2008-02-07 18:18:01 -0800 | [diff] [blame] | 1808 | static const struct net_proto_family tipc_family_ops = { |
Allan Stephens | 0e65967 | 2010-12-31 18:59:32 +0000 | [diff] [blame] | 1809 | .owner = THIS_MODULE, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1810 | .family = AF_TIPC, |
| 1811 | .create = tipc_create |
| 1812 | }; |
| 1813 | |
| 1814 | static struct proto tipc_proto = { |
| 1815 | .name = "TIPC", |
| 1816 | .owner = THIS_MODULE, |
| 1817 | .obj_size = sizeof(struct tipc_sock) |
| 1818 | }; |
| 1819 | |
| 1820 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 1821 | * tipc_socket_init - initialize TIPC socket interface |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1822 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1823 | * Returns 0 on success, errno otherwise |
| 1824 | */ |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 1825 | int tipc_socket_init(void) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1826 | { |
| 1827 | int res; |
| 1828 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 1829 | res = proto_register(&tipc_proto, 1); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1830 | if (res) { |
Erik Hugne | 2cf8aa1 | 2012-06-29 00:16:37 -0400 | [diff] [blame] | 1831 | pr_err("Failed to register TIPC protocol type\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1832 | goto out; |
| 1833 | } |
| 1834 | |
| 1835 | res = sock_register(&tipc_family_ops); |
| 1836 | if (res) { |
Erik Hugne | 2cf8aa1 | 2012-06-29 00:16:37 -0400 | [diff] [blame] | 1837 | pr_err("Failed to register TIPC socket type\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1838 | proto_unregister(&tipc_proto); |
| 1839 | goto out; |
| 1840 | } |
| 1841 | |
| 1842 | sockets_enabled = 1; |
| 1843 | out: |
| 1844 | return res; |
| 1845 | } |
| 1846 | |
| 1847 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 1848 | * tipc_socket_stop - stop TIPC socket interface |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1849 | */ |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 1850 | void tipc_socket_stop(void) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1851 | { |
| 1852 | if (!sockets_enabled) |
| 1853 | return; |
| 1854 | |
| 1855 | sockets_enabled = 0; |
| 1856 | sock_unregister(tipc_family_ops.family); |
| 1857 | proto_unregister(&tipc_proto); |
| 1858 | } |