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